api.c 2.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871
  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. typedef struct testVector {
  326. const char* input;
  327. const char* output;
  328. size_t inLen;
  329. size_t outLen;
  330. } testVector;
  331. #if defined(HAVE_PKCS7)
  332. typedef struct {
  333. const byte* content;
  334. word32 contentSz;
  335. int contentOID;
  336. int encryptOID;
  337. int keyWrapOID;
  338. int keyAgreeOID;
  339. byte* cert;
  340. size_t certSz;
  341. byte* privateKey;
  342. word32 privateKeySz;
  343. } pkcs7EnvelopedVector;
  344. #ifndef NO_PKCS7_ENCRYPTED_DATA
  345. typedef struct {
  346. const byte* content;
  347. word32 contentSz;
  348. int contentOID;
  349. int encryptOID;
  350. byte* encryptionKey;
  351. word32 encryptionKeySz;
  352. } pkcs7EncryptedVector;
  353. #endif
  354. #endif /* HAVE_PKCS7 */
  355. /*----------------------------------------------------------------------------*
  356. | Constants
  357. *----------------------------------------------------------------------------*/
  358. /* Test result constants and macros. */
  359. /* Test succeeded. */
  360. #define TEST_SUCCESS (1)
  361. /* Test failed. */
  362. #define TEST_FAIL (0)
  363. /* Test skipped - not run. */
  364. #define TEST_SKIPPED (-7777)
  365. /* Returns the result based on whether check is true.
  366. *
  367. * @param [in] check Condition for success.
  368. * @return When condition is true: TEST_SUCCESS.
  369. * @return When condition is false: TEST_FAIL.
  370. */
  371. #ifdef DEBUG_WOLFSSL_VERBOSE
  372. #define XSTRINGIFY(s) STRINGIFY(s)
  373. #define STRINGIFY(s) #s
  374. #define TEST_RES_CHECK(check) ({ \
  375. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  376. if (_ret == TEST_FAIL) { \
  377. fprintf(stderr, " check \"%s\" at %d ", \
  378. XSTRINGIFY(check), __LINE__); \
  379. } \
  380. _ret; })
  381. #else
  382. #define TEST_RES_CHECK(check) \
  383. ((check) ? TEST_SUCCESS : TEST_FAIL)
  384. #endif /* DEBUG_WOLFSSL_VERBOSE */
  385. #define TEST_STRING "Everyone gets Friday off."
  386. #define TEST_STRING_SZ 25
  387. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  388. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  389. #define TEST_RSA_BITS 1024
  390. #else
  391. #define TEST_RSA_BITS 2048
  392. #endif
  393. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  394. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  395. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  396. static const char* bogusFile =
  397. #ifdef _WIN32
  398. "NUL"
  399. #else
  400. "/dev/null"
  401. #endif
  402. ;
  403. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  404. enum {
  405. TESTING_RSA = 1,
  406. TESTING_ECC = 2
  407. };
  408. #ifdef WOLFSSL_QNX_CAAM
  409. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  410. static int testDevId = WOLFSSL_CAAM_DEVID;
  411. #else
  412. static int testDevId = INVALID_DEVID;
  413. #endif
  414. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  415. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  416. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  417. #define HAVE_IO_TESTS_DEPENDENCIES
  418. #endif
  419. /*----------------------------------------------------------------------------*
  420. | BIO with fixed read/write size
  421. *----------------------------------------------------------------------------*/
  422. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  423. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  424. int len)
  425. {
  426. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  427. len = 0;
  428. }
  429. else {
  430. if (bio->wrSz - bio->wrIdx < len) {
  431. len = bio->wrSz - bio->wrIdx;
  432. }
  433. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  434. bio->wrIdx += len;
  435. }
  436. return len;
  437. }
  438. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  439. {
  440. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  441. len = 0;
  442. }
  443. else {
  444. if (bio->wrSz - bio->rdIdx < len) {
  445. len = bio->wrSz - bio->rdIdx;
  446. }
  447. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  448. bio->rdIdx += len;
  449. }
  450. return len;
  451. }
  452. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  453. {
  454. static WOLFSSL_BIO_METHOD meth;
  455. meth.type = WOLFSSL_BIO_BIO;
  456. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  457. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  458. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  459. return &meth;
  460. }
  461. #endif
  462. /*----------------------------------------------------------------------------*
  463. | Setup
  464. *----------------------------------------------------------------------------*/
  465. static int test_wolfSSL_Init(void)
  466. {
  467. int result;
  468. result = wolfSSL_Init();
  469. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  470. return result;
  471. }
  472. static int test_wolfSSL_Cleanup(void)
  473. {
  474. int result;
  475. result = wolfSSL_Cleanup();
  476. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  477. return result;
  478. }
  479. /* Initialize the wolfCrypt state.
  480. * POST: 0 success.
  481. */
  482. static int test_wolfCrypt_Init(void)
  483. {
  484. int result;
  485. result = wolfCrypt_Init();
  486. result = TEST_RES_CHECK(result == 0);
  487. return result;
  488. } /* END test_wolfCrypt_Init */
  489. static int test_wolfCrypt_Cleanup(void)
  490. {
  491. int result;
  492. result = wolfCrypt_Cleanup();
  493. result = TEST_RES_CHECK(result == 0);
  494. return result;
  495. }
  496. /*----------------------------------------------------------------------------*
  497. | Platform dependent function test
  498. *----------------------------------------------------------------------------*/
  499. static int test_fileAccess(void)
  500. {
  501. int res = TEST_SKIPPED;
  502. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  503. const char *fname[] = {
  504. svrCertFile, svrKeyFile, caCertFile,
  505. eccCertFile, eccKeyFile, eccRsaCertFile,
  506. cliCertFile, cliCertDerFile, cliKeyFile,
  507. dhParamFile,
  508. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  509. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  510. NULL
  511. };
  512. const char derfile[] = "./certs/server-cert.der";
  513. XFILE f;
  514. size_t sz;
  515. byte *buff;
  516. int i;
  517. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  518. for (i=0; fname[i] != NULL ; i++) {
  519. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  520. XFCLOSE(f);
  521. }
  522. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  523. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  524. sz = (size_t) XFTELL(f);
  525. AssertTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  526. AssertTrue(sz == sizeof_server_cert_der_2048);
  527. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  528. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  529. XMEMCMP(server_cert_der_2048, buff, sz);
  530. res = TEST_RES_CHECK(1);
  531. #endif
  532. return res;
  533. }
  534. /*----------------------------------------------------------------------------*
  535. | Method Allocators
  536. *----------------------------------------------------------------------------*/
  537. static int test_wolfSSL_Method_Allocators(void)
  538. {
  539. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  540. do { \
  541. WOLFSSL_METHOD *method; \
  542. condition(method = allocator()); \
  543. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  544. } while(0)
  545. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  546. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  547. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  548. TEST_METHOD_ALLOCATOR(a, AssertNull)
  549. #ifndef NO_OLD_TLS
  550. #ifdef WOLFSSL_ALLOW_SSLV3
  551. #ifndef NO_WOLFSSL_SERVER
  552. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  553. #endif
  554. #ifndef NO_WOLFSSL_CLIENT
  555. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  556. #endif
  557. #endif
  558. #ifdef WOLFSSL_ALLOW_TLSV10
  559. #ifndef NO_WOLFSSL_SERVER
  560. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  561. #endif
  562. #ifndef NO_WOLFSSL_CLIENT
  563. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  564. #endif
  565. #endif
  566. #ifndef NO_WOLFSSL_SERVER
  567. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  568. #endif
  569. #ifndef NO_WOLFSSL_CLIENT
  570. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  571. #endif
  572. #endif /* !NO_OLD_TLS */
  573. #ifndef WOLFSSL_NO_TLS12
  574. #ifndef NO_WOLFSSL_SERVER
  575. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  576. #endif
  577. #ifndef NO_WOLFSSL_CLIENT
  578. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  579. #endif
  580. #endif /* !WOLFSSL_NO_TLS12 */
  581. #ifdef WOLFSSL_TLS13
  582. #ifndef NO_WOLFSSL_SERVER
  583. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  584. #endif
  585. #ifndef NO_WOLFSSL_CLIENT
  586. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  587. #endif
  588. #endif /* WOLFSSL_TLS13 */
  589. #ifndef NO_WOLFSSL_SERVER
  590. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  591. #endif
  592. #ifndef NO_WOLFSSL_CLIENT
  593. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  594. #endif
  595. #ifdef WOLFSSL_DTLS
  596. #ifndef NO_OLD_TLS
  597. #ifndef NO_WOLFSSL_SERVER
  598. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  599. #endif
  600. #ifndef NO_WOLFSSL_CLIENT
  601. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  602. #endif
  603. #endif
  604. #ifndef WOLFSSL_NO_TLS12
  605. #ifndef NO_WOLFSSL_SERVER
  606. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  607. #endif
  608. #ifndef NO_WOLFSSL_CLIENT
  609. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  610. #endif
  611. #endif
  612. #endif /* WOLFSSL_DTLS */
  613. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  614. /* Stubs */
  615. #ifndef NO_WOLFSSL_SERVER
  616. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  617. #endif
  618. #ifndef NO_WOLFSSL_CLIENT
  619. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  620. #endif
  621. #endif
  622. /* Test Either Method (client or server) */
  623. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  624. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  625. #ifndef NO_OLD_TLS
  626. #ifdef WOLFSSL_ALLOW_TLSV10
  627. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  628. #endif
  629. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  630. #endif /* !NO_OLD_TLS */
  631. #ifndef WOLFSSL_NO_TLS12
  632. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  633. #endif /* !WOLFSSL_NO_TLS12 */
  634. #ifdef WOLFSSL_TLS13
  635. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  636. #endif /* WOLFSSL_TLS13 */
  637. #ifdef WOLFSSL_DTLS
  638. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  639. #ifndef NO_OLD_TLS
  640. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  641. #endif /* !NO_OLD_TLS */
  642. #ifndef WOLFSSL_NO_TLS12
  643. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  644. #endif /* !WOLFSSL_NO_TLS12 */
  645. #endif /* WOLFSSL_DTLS */
  646. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  647. return TEST_SUCCESS;
  648. }
  649. /*----------------------------------------------------------------------------*
  650. | Context
  651. *----------------------------------------------------------------------------*/
  652. #ifndef NO_WOLFSSL_SERVER
  653. static int test_wolfSSL_CTX_new(void)
  654. {
  655. WOLFSSL_CTX *ctx;
  656. WOLFSSL_METHOD* method;
  657. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  658. AssertNotNull(method = wolfSSLv23_server_method());
  659. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  660. wolfSSL_CTX_free(ctx);
  661. return TEST_RES_CHECK(1);
  662. }
  663. #endif
  664. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  665. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  666. static int test_for_double_Free(void)
  667. {
  668. WOLFSSL_CTX* ctx;
  669. WOLFSSL* ssl;
  670. int skipTest = 0;
  671. const char* testCertFile;
  672. const char* testKeyFile;
  673. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  674. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  675. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  676. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  677. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  678. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  679. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  680. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  681. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  682. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  683. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  684. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  685. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  686. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  687. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  688. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  689. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  690. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  691. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  692. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  693. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  694. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  695. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  696. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  697. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  698. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  699. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  700. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  701. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  702. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  703. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  704. #ifdef OPENSSL_EXTRA
  705. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  706. "!SRP:!kRSA:!aNULL:!eNULL";
  707. #endif
  708. #ifndef NO_RSA
  709. testCertFile = svrCertFile;
  710. testKeyFile = svrKeyFile;
  711. #elif defined(HAVE_ECC)
  712. testCertFile = eccCertFile;
  713. testKeyFile = eccKeyFile;
  714. #else
  715. skipTest = 1;
  716. #endif
  717. if (skipTest != 1) {
  718. #ifndef NO_WOLFSSL_SERVER
  719. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  720. AssertNotNull(ctx);
  721. #else
  722. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  723. AssertNotNull(ctx);
  724. #endif
  725. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  726. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  727. ssl = wolfSSL_new(ctx);
  728. AssertNotNull(ssl);
  729. /* First test freeing SSL, then CTX */
  730. wolfSSL_free(ssl);
  731. wolfSSL_CTX_free(ctx);
  732. #ifndef NO_WOLFSSL_CLIENT
  733. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  734. AssertNotNull(ctx);
  735. #else
  736. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  737. AssertNotNull(ctx);
  738. #endif
  739. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  740. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  741. ssl = wolfSSL_new(ctx);
  742. AssertNotNull(ssl);
  743. /* Next test freeing CTX then SSL */
  744. wolfSSL_CTX_free(ctx);
  745. wolfSSL_free(ssl);
  746. #ifndef NO_WOLFSSL_SERVER
  747. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  748. AssertNotNull(ctx);
  749. #else
  750. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  751. AssertNotNull(ctx);
  752. #endif
  753. /* Test setting ciphers at ctx level */
  754. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  755. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  756. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  757. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  758. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  759. /* only update TLSv13 suites */
  760. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  761. #endif
  762. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  763. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  764. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  765. /* only update pre-TLSv13 suites */
  766. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  767. #endif
  768. #ifdef OPENSSL_EXTRA
  769. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  770. #endif
  771. AssertNotNull(ssl = wolfSSL_new(ctx));
  772. wolfSSL_CTX_free(ctx);
  773. wolfSSL_free(ssl);
  774. #ifndef NO_WOLFSSL_CLIENT
  775. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  776. AssertNotNull(ctx);
  777. #else
  778. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  779. AssertNotNull(ctx);
  780. #endif
  781. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  782. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  783. ssl = wolfSSL_new(ctx);
  784. AssertNotNull(ssl);
  785. /* test setting ciphers at SSL level */
  786. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  787. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  788. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  789. /* only update TLSv13 suites */
  790. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  791. #endif
  792. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  793. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  794. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  795. /* only update pre-TLSv13 suites */
  796. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  797. #endif
  798. wolfSSL_CTX_free(ctx);
  799. wolfSSL_free(ssl);
  800. }
  801. return TEST_RES_CHECK(1);
  802. }
  803. #endif
  804. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  805. {
  806. int res = TEST_SKIPPED;
  807. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  808. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  809. (!defined(NO_RSA) || defined(HAVE_ECC))
  810. const char* testCertFile;
  811. const char* testKeyFile;
  812. WOLFSSL_CTX* ctx;
  813. WOLFSSL* ssl;
  814. const byte cipherList[] =
  815. {
  816. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  817. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  818. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  819. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  820. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  821. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  822. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  823. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  824. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  825. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  826. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  827. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  828. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  829. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  830. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  831. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  832. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  833. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  834. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  835. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  836. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  837. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  838. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  839. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  840. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  841. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  842. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  843. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  844. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  845. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  846. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  847. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  848. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  849. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  850. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  851. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  852. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  853. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  854. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  855. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  856. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  857. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  858. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  859. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  860. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  861. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  862. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  863. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  864. /* SHA256 */
  865. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  866. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  867. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  868. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  869. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  870. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  871. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  872. /* SHA384 */
  873. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  874. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  875. /* AES-GCM */
  876. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  877. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  878. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  879. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  880. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  881. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  882. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  883. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  884. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  885. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  886. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  887. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  888. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  889. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  890. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  891. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  892. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  893. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  894. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  895. * also, in some of the other AES-CCM suites
  896. * there will be second byte number conflicts
  897. * with non-ECC AES-GCM */
  898. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  899. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  900. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  901. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  902. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  903. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  904. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  905. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  906. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  907. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  908. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  909. /* Camellia */
  910. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  911. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  912. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  913. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  914. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  915. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  916. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  917. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  918. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  919. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  920. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  921. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  922. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  923. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  924. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  925. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  926. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  927. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  928. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  929. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  930. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  931. /* TLS v1.3 cipher suites */
  932. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  933. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  934. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  935. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  936. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  937. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  938. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  939. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  940. };
  941. #ifndef NO_RSA
  942. testCertFile = svrCertFile;
  943. testKeyFile = svrKeyFile;
  944. #elif defined(HAVE_ECC)
  945. testCertFile = eccCertFile;
  946. testKeyFile = eccKeyFile;
  947. #endif
  948. #ifndef NO_WOLFSSL_SERVER
  949. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  950. AssertNotNull(ctx);
  951. #else
  952. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  953. AssertNotNull(ctx);
  954. #endif
  955. AssertTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  956. sizeof(cipherList)));
  957. wolfSSL_CTX_free(ctx);
  958. #ifndef NO_WOLFSSL_SERVER
  959. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  960. AssertNotNull(ctx);
  961. #else
  962. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  963. AssertNotNull(ctx);
  964. #endif
  965. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  966. WOLFSSL_FILETYPE_PEM));
  967. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  968. WOLFSSL_FILETYPE_PEM));
  969. ssl = wolfSSL_new(ctx);
  970. AssertNotNull(ssl);
  971. AssertTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  972. sizeof(cipherList)));
  973. wolfSSL_free(ssl);
  974. wolfSSL_CTX_free(ctx);
  975. res = TEST_RES_CHECK(1);
  976. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  977. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  978. return res;
  979. }
  980. static int test_wolfSSL_CTX_use_certificate_file(void)
  981. {
  982. int res = TEST_SKIPPED;
  983. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  984. WOLFSSL_CTX *ctx;
  985. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  986. /* invalid context */
  987. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  988. WOLFSSL_FILETYPE_PEM));
  989. /* invalid cert file */
  990. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  991. WOLFSSL_FILETYPE_PEM));
  992. /* invalid cert type */
  993. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  994. #ifdef NO_RSA
  995. /* rsa needed */
  996. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  997. #else
  998. /* success */
  999. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1000. #endif
  1001. wolfSSL_CTX_free(ctx);
  1002. res = TEST_RES_CHECK(1);
  1003. #endif
  1004. return res;
  1005. }
  1006. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1007. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1008. {
  1009. int res = TEST_SKIPPED;
  1010. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1011. WOLFSSL_CTX* ctx;
  1012. int ret;
  1013. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1014. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1015. server_cert_der_2048);
  1016. wolfSSL_CTX_free(ctx);
  1017. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1018. #endif
  1019. return res;
  1020. }
  1021. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1022. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1023. * context using buffer.
  1024. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1025. * --enable-testcert flag.
  1026. */
  1027. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1028. {
  1029. int res = TEST_SKIPPED;
  1030. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1031. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1032. WOLFSSL_CTX* ctx;
  1033. int ret;
  1034. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1035. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  1036. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  1037. wolfSSL_CTX_free(ctx);
  1038. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1039. #endif
  1040. return res;
  1041. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  1042. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1043. {
  1044. int res = TEST_SKIPPED;
  1045. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1046. WOLFSSL_CTX *ctx;
  1047. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1048. /* invalid context */
  1049. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1050. WOLFSSL_FILETYPE_PEM));
  1051. /* invalid key file */
  1052. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1053. WOLFSSL_FILETYPE_PEM));
  1054. /* invalid key type */
  1055. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1056. /* success */
  1057. #ifdef NO_RSA
  1058. /* rsa needed */
  1059. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1060. #else
  1061. /* success */
  1062. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1063. #endif
  1064. wolfSSL_CTX_free(ctx);
  1065. res = TEST_RES_CHECK(1);
  1066. #endif
  1067. return res;
  1068. }
  1069. /* test both file and buffer versions along with unloading trusted peer certs */
  1070. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1071. {
  1072. int res = TEST_SKIPPED;
  1073. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1074. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1075. WOLFSSL_CTX *ctx;
  1076. WOLFSSL* ssl;
  1077. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1078. AssertNotNull(ssl = wolfSSL_new(ctx));
  1079. #if !defined(NO_FILESYSTEM)
  1080. /* invalid file */
  1081. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1082. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1083. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1084. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1085. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1086. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1087. /* success */
  1088. AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1089. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1090. /* unload cert */
  1091. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1092. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1093. /* invalid file */
  1094. AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1095. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1096. AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1097. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1098. AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1099. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1100. /* success */
  1101. AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1102. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1103. #ifdef WOLFSSL_LOCAL_X509_STORE
  1104. /* unload cert */
  1105. AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1106. AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1107. #endif
  1108. #endif
  1109. /* Test of loading certs from buffers */
  1110. /* invalid buffer */
  1111. AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1112. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1113. /* success */
  1114. #ifdef USE_CERT_BUFFERS_1024
  1115. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1116. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1117. #endif
  1118. #ifdef USE_CERT_BUFFERS_2048
  1119. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1120. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1121. #endif
  1122. /* unload cert */
  1123. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1124. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1125. wolfSSL_free(ssl);
  1126. wolfSSL_CTX_free(ctx);
  1127. res = TEST_RES_CHECK(1);
  1128. #endif
  1129. return res;
  1130. }
  1131. static int test_wolfSSL_CTX_load_verify_locations(void)
  1132. {
  1133. int res = TEST_SKIPPED;
  1134. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1135. WOLFSSL_CTX *ctx;
  1136. #ifndef NO_RSA
  1137. WOLFSSL_CERT_MANAGER* cm;
  1138. #ifdef PERSIST_CERT_CACHE
  1139. int cacheSz;
  1140. #endif
  1141. #endif
  1142. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1143. const char* load_certs_path = "./certs/external";
  1144. const char* load_no_certs_path = "./examples";
  1145. const char* load_expired_path = "./certs/test/expired";
  1146. #endif
  1147. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1148. /* invalid arguments */
  1149. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  1150. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  1151. /* invalid ca file */
  1152. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1153. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1154. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1155. (defined(WOLFSSL_QT) && \
  1156. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1157. /* invalid path */
  1158. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1159. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1160. #endif
  1161. /* load ca cert */
  1162. #ifdef NO_RSA
  1163. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1164. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1165. #else /* Skip the following test without RSA certs. */
  1166. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1167. #ifdef PERSIST_CERT_CACHE
  1168. /* Get cert cache size */
  1169. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  1170. #endif
  1171. /* Test unloading CA's */
  1172. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1173. #ifdef PERSIST_CERT_CACHE
  1174. /* Verify no certs (result is less than cacheSz) */
  1175. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1176. #endif
  1177. /* load ca cert again */
  1178. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1179. /* Test getting CERT_MANAGER */
  1180. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1181. /* Test unloading CA's using CM */
  1182. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1183. #ifdef PERSIST_CERT_CACHE
  1184. /* Verify no certs (result is less than cacheSz) */
  1185. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1186. #endif
  1187. #endif
  1188. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1189. /* Test loading CA certificates using a path */
  1190. #ifdef NO_RSA
  1191. /* failure here okay since certs in external directory are RSA */
  1192. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1193. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1194. #else
  1195. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1196. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1197. #endif
  1198. /* Test loading path with no files */
  1199. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  1200. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1201. /* Test loading expired CA certificates */
  1202. #ifdef NO_RSA
  1203. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1204. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1205. WOLFSSL_SUCCESS);
  1206. #else
  1207. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1208. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1209. WOLFSSL_SUCCESS);
  1210. #endif
  1211. /* Test loading CA certificates and ignoring all errors */
  1212. #ifdef NO_RSA
  1213. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1214. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1215. #else
  1216. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1217. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1218. #endif
  1219. #endif
  1220. wolfSSL_CTX_free(ctx);
  1221. res = TEST_RES_CHECK(1);
  1222. #endif
  1223. return res;
  1224. }
  1225. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1226. {
  1227. int res = TEST_SKIPPED;
  1228. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1229. (!defined(NO_RSA) || defined(HAVE_ECC))
  1230. WOLFSSL_CTX* ctx;
  1231. byte dirValid = 0;
  1232. int ret = 0;
  1233. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1234. if (ctx == NULL) {
  1235. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1236. ret = -1;
  1237. }
  1238. if (ret == 0) {
  1239. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1240. dirValid = 1;
  1241. #else
  1242. word32 numDirs;
  1243. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1244. if (caDirs == NULL || numDirs == 0) {
  1245. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1246. ret = -1;
  1247. }
  1248. else {
  1249. ReadDirCtx dirCtx;
  1250. word32 i;
  1251. for (i = 0; i < numDirs; ++i) {
  1252. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1253. /* Directory isn't empty. */
  1254. dirValid = 1;
  1255. wc_ReadDirClose(&dirCtx);
  1256. break;
  1257. }
  1258. }
  1259. }
  1260. #endif
  1261. }
  1262. /*
  1263. * If the directory isn't empty, we should be able to load CA
  1264. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1265. * usable.
  1266. */
  1267. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1268. WOLFSSL_SUCCESS) {
  1269. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1270. ret = -1;
  1271. }
  1272. #ifdef OPENSSL_EXTRA
  1273. if (ret == 0 &&
  1274. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1275. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1276. ret = -1;
  1277. }
  1278. #endif /* OPENSSL_EXTRA */
  1279. wolfSSL_CTX_free(ctx);
  1280. res = TEST_RES_CHECK(ret == 0);
  1281. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1282. return res;
  1283. }
  1284. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1285. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  1286. {
  1287. int ret;
  1288. WOLFSSL_CERT_MANAGER* cm;
  1289. cm = wolfSSL_CertManagerNew();
  1290. if (cm == NULL) {
  1291. fprintf(stderr, "test_cm_load_ca failed\n");
  1292. return -1;
  1293. }
  1294. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1295. wolfSSL_CertManagerFree(cm);
  1296. return ret;
  1297. }
  1298. static int test_cm_load_ca_file(const char* ca_cert_file)
  1299. {
  1300. int ret = 0;
  1301. byte* cert_buf = NULL;
  1302. size_t cert_sz = 0;
  1303. #if defined(WOLFSSL_PEM_TO_DER)
  1304. DerBuffer* pDer = NULL;
  1305. #endif
  1306. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1307. if (ret == 0) {
  1308. /* normal test */
  1309. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1310. if (ret == WOLFSSL_SUCCESS) {
  1311. /* test including null terminator in length */
  1312. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1313. if (tmp == NULL) {
  1314. ret = MEMORY_E;
  1315. }
  1316. else {
  1317. cert_buf = tmp;
  1318. cert_buf[cert_sz] = '\0';
  1319. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1320. WOLFSSL_FILETYPE_PEM);
  1321. }
  1322. }
  1323. #if defined(WOLFSSL_PEM_TO_DER)
  1324. if (ret == WOLFSSL_SUCCESS) {
  1325. /* test loading DER */
  1326. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1327. if (ret == 0 && pDer != NULL) {
  1328. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1329. WOLFSSL_FILETYPE_ASN1);
  1330. wc_FreeDer(&pDer);
  1331. }
  1332. }
  1333. #endif
  1334. }
  1335. free(cert_buf);
  1336. return ret;
  1337. }
  1338. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1339. int file_type, word32 flags)
  1340. {
  1341. int ret;
  1342. WOLFSSL_CERT_MANAGER* cm;
  1343. cm = wolfSSL_CertManagerNew();
  1344. if (cm == NULL) {
  1345. fprintf(stderr, "test_cm_load_ca failed\n");
  1346. return -1;
  1347. }
  1348. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1349. 0, flags);
  1350. wolfSSL_CertManagerFree(cm);
  1351. return ret;
  1352. }
  1353. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1354. {
  1355. int ret = 0;
  1356. byte* cert_buf = NULL;
  1357. size_t cert_sz = 0;
  1358. #if defined(WOLFSSL_PEM_TO_DER)
  1359. DerBuffer* pDer = NULL;
  1360. #endif
  1361. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1362. if (ret == 0) {
  1363. /* normal test */
  1364. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1365. WOLFSSL_FILETYPE_PEM, flags);
  1366. if (ret == WOLFSSL_SUCCESS) {
  1367. /* test including null terminator in length */
  1368. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1369. if (tmp == NULL) {
  1370. ret = MEMORY_E;
  1371. }
  1372. else {
  1373. cert_buf = tmp;
  1374. cert_buf[cert_sz] = '\0';
  1375. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1376. WOLFSSL_FILETYPE_PEM, flags);
  1377. }
  1378. }
  1379. #if defined(WOLFSSL_PEM_TO_DER)
  1380. if (ret == WOLFSSL_SUCCESS) {
  1381. /* test loading DER */
  1382. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1383. if (ret == 0 && pDer != NULL) {
  1384. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1385. WOLFSSL_FILETYPE_ASN1, flags);
  1386. wc_FreeDer(&pDer);
  1387. }
  1388. }
  1389. #endif
  1390. }
  1391. free(cert_buf);
  1392. return ret;
  1393. }
  1394. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1395. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1396. {
  1397. int res = TEST_SKIPPED;
  1398. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1399. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1400. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1401. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1402. defined(HAVE_LIGHTY)
  1403. WOLFSSL_CERT_MANAGER* cm = NULL;
  1404. /* Raw OCSP response bytes captured using the following setup:
  1405. * - Run responder with
  1406. * openssl ocsp -port 9999 -ndays 9999
  1407. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1408. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1409. * -rkey certs/ocsp/ocsp-responder-key.pem
  1410. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1411. * - Run client with
  1412. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1413. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1414. * -cert certs/ocsp/server1-cert.pem
  1415. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1416. * - Copy raw response from Wireshark.
  1417. */
  1418. byte response[] = {
  1419. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1420. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1421. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1422. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1423. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1424. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1425. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1426. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1427. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1428. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1429. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1430. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1431. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1432. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1433. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1434. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1435. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1436. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1437. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1438. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1439. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1440. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1441. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1442. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1443. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1444. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1445. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1446. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1447. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1448. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1449. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1450. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1451. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1452. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1453. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1454. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1455. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1456. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1457. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1458. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1459. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1460. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1461. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1462. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1463. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1464. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1465. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1466. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1467. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1468. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1469. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1470. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1471. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1472. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1473. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1474. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1475. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1476. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1477. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1478. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1479. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1480. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1481. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1482. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1483. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1484. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1485. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1486. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1487. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1488. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1489. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1490. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1491. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1492. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1493. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1494. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1495. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1496. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1497. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1498. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1499. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1500. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1501. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1502. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1503. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1504. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1505. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1506. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1507. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1508. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1509. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1510. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1511. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1512. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1513. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1514. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1515. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1516. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1517. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1518. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1519. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1520. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1521. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1522. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1523. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1524. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1525. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1526. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1527. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1528. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1529. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1530. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1531. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1532. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1533. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1534. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1535. 0x59, 0x59, 0xa0, 0x07
  1536. };
  1537. OcspEntry entry[1];
  1538. CertStatus status[1];
  1539. OcspRequest* request;
  1540. byte serial[] = {0x05};
  1541. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1542. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1543. XMEMSET(entry, 0, sizeof(OcspEntry));
  1544. XMEMSET(status, 0, sizeof(CertStatus));
  1545. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1546. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1547. DYNAMIC_TYPE_OCSP_REQUEST);
  1548. AssertNotNull(request->serial);
  1549. request->serialSz = sizeof(serial);
  1550. XMEMCPY(request->serial, serial, sizeof(serial));
  1551. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1552. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1553. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1554. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1555. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1556. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1557. /* Response should be valid. */
  1558. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1559. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1560. /* Flip a byte in the request serial number, response should be invalid
  1561. * now. */
  1562. request->serial[0] ^= request->serial[0];
  1563. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1564. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1565. wolfSSL_OCSP_REQUEST_free(request);
  1566. wolfSSL_CertManagerFree(cm);
  1567. res = TEST_RES_CHECK(1);
  1568. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1569. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1570. #endif /* HAVE_OCSP */
  1571. return res;
  1572. }
  1573. static int test_wolfSSL_CheckOCSPResponse(void)
  1574. {
  1575. int result = TEST_SKIPPED;
  1576. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  1577. const char* responseFile = "./certs/ocsp/test-response.der";
  1578. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  1579. const char* responseNoInternFile = "./certs/ocsp/test-response-nointern.der";
  1580. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  1581. OcspResponse* res = NULL;
  1582. byte data[4096];
  1583. const unsigned char* pt;
  1584. int dataSz;
  1585. XFILE f;
  1586. WOLFSSL_OCSP_BASICRESP* bs;
  1587. WOLFSSL_X509_STORE* st;
  1588. WOLFSSL_X509* issuer;
  1589. f = XFOPEN(responseFile, "rb");
  1590. AssertTrue(f != XBADFILE);
  1591. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1592. AssertIntGT(dataSz, 0);
  1593. XFCLOSE(f);
  1594. pt = data;
  1595. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1596. AssertNotNull(res);
  1597. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1598. AssertNotNull(issuer);
  1599. st = wolfSSL_X509_STORE_new();
  1600. AssertNotNull(st);
  1601. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1602. bs = wolfSSL_OCSP_response_get1_basic(res);
  1603. AssertNotNull(bs);
  1604. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1605. wolfSSL_OCSP_BASICRESP_free(bs);
  1606. wolfSSL_OCSP_RESPONSE_free(res);
  1607. wolfSSL_X509_STORE_free(st);
  1608. wolfSSL_X509_free(issuer);
  1609. /* check loading a response with optional certs */
  1610. f = XFOPEN(responseNoInternFile, "rb");
  1611. AssertTrue(f != XBADFILE);
  1612. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1613. AssertIntGT(dataSz, 0);
  1614. XFCLOSE(f);
  1615. pt = data;
  1616. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1617. AssertNotNull(res);
  1618. wolfSSL_OCSP_RESPONSE_free(res);
  1619. /* check loading a response with multiple certs */
  1620. {
  1621. WOLFSSL_CERT_MANAGER* cm = NULL;
  1622. OcspEntry *entry;
  1623. CertStatus* status;
  1624. OcspRequest* request;
  1625. byte serial1[] = {0x01};
  1626. byte serial[] = {0x02};
  1627. byte issuerHash[] = {
  1628. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  1629. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  1630. 0x37, 0x15, 0x8A, 0x88
  1631. };
  1632. byte issuerKeyHash[] = {
  1633. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  1634. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  1635. 0x7E, 0x72, 0x15, 0x21
  1636. };
  1637. entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  1638. DYNAMIC_TYPE_OPENSSL);
  1639. AssertNotNull(entry);
  1640. status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  1641. DYNAMIC_TYPE_OPENSSL);
  1642. AssertNotNull(status);
  1643. XMEMSET(entry, 0, sizeof(OcspEntry));
  1644. XMEMSET(status, 0, sizeof(CertStatus));
  1645. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1646. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1647. DYNAMIC_TYPE_OCSP_REQUEST);
  1648. AssertNotNull(request->serial);
  1649. request->serialSz = sizeof(serial);
  1650. XMEMCPY(request->serial, serial, sizeof(serial));
  1651. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1652. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1653. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1654. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1655. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  1656. WOLFSSL_SUCCESS);
  1657. f = XFOPEN(responseMultiFile, "rb");
  1658. AssertTrue(f != XBADFILE);
  1659. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1660. AssertIntGT(dataSz, 0);
  1661. XFCLOSE(f);
  1662. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1663. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1664. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1665. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1666. AssertNotNull(entry->status);
  1667. XMEMCPY(request->serial, serial1, sizeof(serial1));
  1668. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1669. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1670. /* store both status's in the entry to check that "next" is not
  1671. * overwritten */
  1672. status->next = entry->status;
  1673. entry->status = status;
  1674. XMEMCPY(request->serial, serial, sizeof(serial));
  1675. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1676. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1677. AssertNotNull(entry->status->next);
  1678. /* compare the status found */
  1679. AssertIntEQ(status->serialSz, entry->status->serialSz);
  1680. AssertIntEQ(XMEMCMP(status->serial, entry->status->serial,
  1681. status->serialSz), 0);
  1682. wolfSSL_OCSP_CERTID_free(entry);
  1683. wolfSSL_OCSP_REQUEST_free(request);
  1684. wolfSSL_CertManagerFree(cm);
  1685. }
  1686. #if defined(WC_RSA_PSS)
  1687. {
  1688. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  1689. /* check loading a response with RSA-PSS signature */
  1690. f = XFOPEN(responsePssFile, "rb");
  1691. AssertTrue(f != XBADFILE);
  1692. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1693. AssertIntGT(dataSz, 0);
  1694. XFCLOSE(f);
  1695. pt = data;
  1696. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1697. AssertNotNull(res);
  1698. /* try to verify the response */
  1699. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1700. AssertNotNull(issuer);
  1701. st = wolfSSL_X509_STORE_new();
  1702. AssertNotNull(st);
  1703. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1704. bs = wolfSSL_OCSP_response_get1_basic(res);
  1705. AssertNotNull(bs);
  1706. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1707. wolfSSL_OCSP_BASICRESP_free(bs);
  1708. wolfSSL_OCSP_RESPONSE_free(res);
  1709. wolfSSL_X509_STORE_free(st);
  1710. wolfSSL_X509_free(issuer);
  1711. }
  1712. #endif
  1713. result = TEST_RES_CHECK(1);
  1714. #endif /* HAVE_OCSP */
  1715. return result;
  1716. }
  1717. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1718. {
  1719. int res = TEST_SKIPPED;
  1720. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1721. const char* ca_cert = "./certs/ca-cert.pem";
  1722. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1723. int ret;
  1724. ret = test_cm_load_ca_file(ca_cert);
  1725. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1726. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1727. #elif defined(NO_RSA)
  1728. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1729. #else
  1730. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1731. #endif
  1732. ret = test_cm_load_ca_file(ca_expired_cert);
  1733. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1734. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1735. res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
  1736. #elif defined(NO_RSA)
  1737. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1738. res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
  1739. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1740. !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  1741. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1742. res = TEST_RES_CHECK(ret == ASN_AFTER_DATE_E);
  1743. #else
  1744. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1745. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1746. #endif
  1747. #endif
  1748. return res;
  1749. }
  1750. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1751. {
  1752. int res = TEST_SKIPPED;
  1753. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1754. const char* ca_cert = "./certs/ca-cert.pem";
  1755. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1756. int ret;
  1757. ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE);
  1758. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1759. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1760. #elif defined(NO_RSA)
  1761. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1762. #else
  1763. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1764. #endif
  1765. ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1766. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY);
  1767. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1768. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1769. res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
  1770. #elif defined(NO_RSA)
  1771. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1772. res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
  1773. #else
  1774. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1775. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1776. #endif
  1777. #endif
  1778. return res;
  1779. }
  1780. static int test_wolfSSL_CertManagerGetCerts(void)
  1781. {
  1782. int res = TEST_SKIPPED;
  1783. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1784. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1785. defined(WOLFSSL_SIGNER_DER_CERT)
  1786. WOLFSSL_CERT_MANAGER* cm = NULL;
  1787. WOLFSSL_STACK* sk = NULL;
  1788. X509* x509 = NULL;
  1789. X509* cert1 = NULL;
  1790. FILE* file1 = NULL;
  1791. #ifdef DEBUG_WOLFSSL_VERBOSE
  1792. WOLFSSL_BIO* bio = NULL;
  1793. #endif
  1794. int i = 0;
  1795. int ret = 0;
  1796. const byte* der;
  1797. int derSz = 0;
  1798. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1799. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1800. fclose(file1);
  1801. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1802. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1803. AssertNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1804. ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz, WOLFSSL_FILETYPE_ASN1);
  1805. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1806. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1807. * and full OpenSSL compatibility */
  1808. AssertIntEQ(ret, ASN_SELF_SIGNED_E);
  1809. #else
  1810. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  1811. #endif
  1812. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1813. "./certs/ca-cert.pem", NULL));
  1814. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1815. for (i = 0; i < sk_X509_num(sk); i++) {
  1816. x509 = sk_X509_value(sk, i);
  1817. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1818. #ifdef DEBUG_WOLFSSL_VERBOSE
  1819. bio = BIO_new(wolfSSL_BIO_s_file());
  1820. if (bio != NULL) {
  1821. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1822. X509_print(bio, x509);
  1823. BIO_free(bio);
  1824. }
  1825. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1826. }
  1827. wolfSSL_X509_free(cert1);
  1828. sk_X509_pop_free(sk, NULL);
  1829. wolfSSL_CertManagerFree(cm);
  1830. res = TEST_RES_CHECK(1);
  1831. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1832. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1833. defined(WOLFSSL_SIGNER_DER_CERT) */
  1834. return res;
  1835. }
  1836. static int test_wolfSSL_CertManagerSetVerify(void)
  1837. {
  1838. int res = TEST_SKIPPED;
  1839. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1840. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1841. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1842. int ret = 0;
  1843. WOLFSSL_CERT_MANAGER* cm;
  1844. int tmp = myVerifyAction;
  1845. const char* ca_cert = "./certs/ca-cert.pem";
  1846. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1847. cm = wolfSSL_CertManagerNew();
  1848. AssertNotNull(cm);
  1849. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1850. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1851. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1852. AssertIntEQ(ret, -1);
  1853. #else
  1854. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1855. #endif
  1856. /* Use the test CB that always accepts certs */
  1857. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1858. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1859. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1860. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1861. {
  1862. const char* verifyCert = "./certs/server-cert.pem";
  1863. /* Use the test CB that always fails certs */
  1864. myVerifyAction = VERIFY_FORCE_FAIL;
  1865. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1866. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1867. }
  1868. #endif
  1869. wolfSSL_CertManagerFree(cm);
  1870. myVerifyAction = tmp;
  1871. res = TEST_RES_CHECK(1);
  1872. #endif
  1873. return res;
  1874. }
  1875. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1876. defined(DEBUG_UNIT_TEST_CERTS)
  1877. /* Used when debugging name constraint tests. Not static to allow use in
  1878. * multiple locations with complex define guards. */
  1879. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1880. {
  1881. BIO* out = BIO_new_file(fileName, "wb");
  1882. if (out != NULL) {
  1883. PEM_write_bio_X509(out, x509);
  1884. BIO_free(out);
  1885. }
  1886. }
  1887. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1888. {
  1889. BIO* out = BIO_new_file(fileName, "wb");
  1890. if (out != NULL) {
  1891. BIO_write(out, der, derSz);
  1892. BIO_free(out);
  1893. }
  1894. }
  1895. #else
  1896. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1897. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1898. #endif
  1899. static int test_wolfSSL_CertManagerNameConstraint(void)
  1900. {
  1901. int res = TEST_SKIPPED;
  1902. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1903. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1904. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1905. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1906. !defined(NO_SHA256)
  1907. WOLFSSL_CERT_MANAGER* cm;
  1908. WOLFSSL_EVP_PKEY *priv;
  1909. WOLFSSL_X509_NAME* name;
  1910. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1911. const char* server_cert = "./certs/test/server-goodcn.pem";
  1912. int i = 0;
  1913. static const byte extNameConsOid[] = {85, 29, 30};
  1914. RsaKey key;
  1915. WC_RNG rng;
  1916. byte *der;
  1917. int derSz;
  1918. word32 idx = 0;
  1919. byte *pt;
  1920. WOLFSSL_X509 *x509, *ca;
  1921. wc_InitRng(&rng);
  1922. /* load in CA private key for signing */
  1923. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1924. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1925. sizeof_server_key_der_2048), 0);
  1926. /* get ca certificate then alter it */
  1927. AssertNotNull(der =
  1928. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1929. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1930. WOLFSSL_FILETYPE_ASN1));
  1931. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1932. XMEMCPY(der, pt, derSz);
  1933. /* find the name constraint extension and alter it */
  1934. pt = der;
  1935. for (i = 0; i < derSz - 3; i++) {
  1936. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1937. pt += 3;
  1938. break;
  1939. }
  1940. pt++;
  1941. }
  1942. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1943. /* go to the length value and set it to 0 */
  1944. while (i < derSz && *pt != 0x81) {
  1945. pt++;
  1946. i++;
  1947. }
  1948. AssertIntNE(i, derSz); /* did not place to alter */
  1949. pt++;
  1950. *pt = 0x00;
  1951. /* resign the altered certificate */
  1952. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1953. FOURK_BUF, &key, NULL, &rng)), 0);
  1954. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1955. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1956. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1957. wolfSSL_CertManagerFree(cm);
  1958. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1959. wolfSSL_X509_free(x509);
  1960. wc_FreeRsaKey(&key);
  1961. wc_FreeRng(&rng);
  1962. /* add email alt name to satisfy constraint */
  1963. pt = (byte*)server_key_der_2048;
  1964. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1965. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1966. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1967. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1968. WOLFSSL_FILETYPE_ASN1));
  1969. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1970. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1971. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1972. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1973. /* Good cert test with proper alt email name */
  1974. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1975. WOLFSSL_FILETYPE_PEM));
  1976. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1977. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1978. AssertNotNull(name = X509_NAME_new());
  1979. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1980. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1981. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1982. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1983. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1984. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1985. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1986. X509_NAME_free(name);
  1987. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1988. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1989. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1990. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1991. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1992. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1993. wolfSSL_X509_free(x509);
  1994. /* Cert with bad alt name list */
  1995. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1996. WOLFSSL_FILETYPE_PEM));
  1997. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1998. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1999. AssertNotNull(name = X509_NAME_new());
  2000. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2001. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2002. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2003. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2004. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2005. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2006. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2007. X509_NAME_free(name);
  2008. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2009. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2010. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2011. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2012. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2013. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2014. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2015. wolfSSL_CertManagerFree(cm);
  2016. wolfSSL_X509_free(x509);
  2017. wolfSSL_X509_free(ca);
  2018. wolfSSL_EVP_PKEY_free(priv);
  2019. res = TEST_RES_CHECK(1);
  2020. #endif
  2021. return res;
  2022. }
  2023. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2024. {
  2025. int res = TEST_SKIPPED;
  2026. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2027. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2028. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2029. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2030. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2031. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2032. const char* server_cert = "./certs/server-cert.pem";
  2033. WOLFSSL_CERT_MANAGER* cm;
  2034. WOLFSSL_X509 *x509, *ca;
  2035. const unsigned char *der;
  2036. const unsigned char *pt;
  2037. WOLFSSL_EVP_PKEY *priv;
  2038. WOLFSSL_X509_NAME* name;
  2039. int derSz;
  2040. /* C=US*/
  2041. char altName[] = {
  2042. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2043. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2044. };
  2045. /* C=ID */
  2046. char altNameFail[] = {
  2047. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2048. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2049. };
  2050. /* C=US ST=California*/
  2051. char altNameExc[] = {
  2052. 0x30, 0x22,
  2053. 0x31, 0x0B,
  2054. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2055. 0x31, 0x13,
  2056. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2057. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2058. };
  2059. /* load in CA private key for signing */
  2060. pt = ca_key_der_2048;
  2061. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2062. sizeof_ca_key_der_2048));
  2063. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2064. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2065. WOLFSSL_FILETYPE_ASN1));
  2066. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2067. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2068. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2069. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2070. WOLFSSL_FILETYPE_PEM));
  2071. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2072. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2073. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2074. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2075. #else
  2076. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2077. #endif
  2078. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2079. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2080. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2081. /* add in matching DIR alt name and resign */
  2082. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2083. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2084. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2085. #else
  2086. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2087. #endif
  2088. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2089. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2090. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2091. wolfSSL_X509_free(x509);
  2092. /* check verify fail */
  2093. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2094. WOLFSSL_FILETYPE_PEM));
  2095. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2096. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2097. /* add in miss matching DIR alt name and resign */
  2098. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2099. ASN_DIR_TYPE);
  2100. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2101. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2102. #else
  2103. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2104. #endif
  2105. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2106. #ifndef WOLFSSL_NO_ASN_STRICT
  2107. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2108. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2109. #else
  2110. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2111. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2112. #endif
  2113. /* check that it still fails if one bad altname and one good altname is in
  2114. * the certificate */
  2115. wolfSSL_X509_free(x509);
  2116. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2117. WOLFSSL_FILETYPE_PEM));
  2118. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2119. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2120. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2121. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2122. ASN_DIR_TYPE);
  2123. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2124. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2125. #else
  2126. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2127. #endif
  2128. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2129. #ifndef WOLFSSL_NO_ASN_STRICT
  2130. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2131. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2132. #else
  2133. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2134. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2135. #endif
  2136. /* check it fails with switching position of bad altname */
  2137. wolfSSL_X509_free(x509);
  2138. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2139. WOLFSSL_FILETYPE_PEM));
  2140. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2141. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2142. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2143. ASN_DIR_TYPE);
  2144. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2145. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2146. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2147. #else
  2148. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2149. #endif
  2150. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2151. #ifndef WOLFSSL_NO_ASN_STRICT
  2152. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2153. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2154. #else
  2155. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2156. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2157. #endif
  2158. wolfSSL_CertManagerFree(cm);
  2159. wolfSSL_X509_free(x509);
  2160. wolfSSL_X509_free(ca);
  2161. /* now test with excluded name constraint */
  2162. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2163. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2164. WOLFSSL_FILETYPE_ASN1));
  2165. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2166. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2167. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2168. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2169. WOLFSSL_FILETYPE_PEM));
  2170. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2171. ASN_DIR_TYPE);
  2172. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2173. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2174. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2175. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2176. #else
  2177. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2178. #endif
  2179. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2180. #ifndef WOLFSSL_NO_ASN_STRICT
  2181. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2182. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2183. #else
  2184. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2185. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2186. #endif
  2187. wolfSSL_CertManagerFree(cm);
  2188. wolfSSL_X509_free(x509);
  2189. wolfSSL_X509_free(ca);
  2190. wolfSSL_EVP_PKEY_free(priv);
  2191. res = TEST_RES_CHECK(1);
  2192. #endif
  2193. return res;
  2194. }
  2195. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2196. {
  2197. int res = TEST_SKIPPED;
  2198. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2199. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2200. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2201. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2202. !defined(NO_SHA256)
  2203. WOLFSSL_CERT_MANAGER* cm;
  2204. WOLFSSL_EVP_PKEY *priv;
  2205. WOLFSSL_X509_NAME* name;
  2206. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2207. const char* server_cert = "./certs/test/server-goodcn.pem";
  2208. byte *der;
  2209. int derSz;
  2210. byte *pt;
  2211. WOLFSSL_X509 *x509, *ca;
  2212. pt = (byte*)server_key_der_2048;
  2213. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2214. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2215. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2216. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2217. WOLFSSL_FILETYPE_ASN1));
  2218. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2219. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2220. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2221. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2222. /* check satisfying .wolfssl.com constraint passes */
  2223. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2224. WOLFSSL_FILETYPE_PEM));
  2225. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2226. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2227. AssertNotNull(name = X509_NAME_new());
  2228. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2229. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2230. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2231. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2232. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2233. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2234. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2235. X509_NAME_free(name);
  2236. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2237. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2238. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2239. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2240. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2241. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2242. wolfSSL_X509_free(x509);
  2243. /* check satisfying .random.com constraint passes */
  2244. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2245. WOLFSSL_FILETYPE_PEM));
  2246. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2247. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2248. AssertNotNull(name = X509_NAME_new());
  2249. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2250. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2251. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2252. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2253. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2254. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2255. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2256. X509_NAME_free(name);
  2257. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2258. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2259. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2260. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2261. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2262. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2263. wolfSSL_X509_free(x509);
  2264. /* check fail case when neither constraint is matched */
  2265. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2266. WOLFSSL_FILETYPE_PEM));
  2267. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2268. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2269. AssertNotNull(name = X509_NAME_new());
  2270. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2271. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2272. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2273. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2274. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2275. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2276. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2277. X509_NAME_free(name);
  2278. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2279. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2280. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2281. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2282. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2283. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2284. wolfSSL_CertManagerFree(cm);
  2285. wolfSSL_X509_free(x509);
  2286. wolfSSL_X509_free(ca);
  2287. wolfSSL_EVP_PKEY_free(priv);
  2288. res = TEST_RES_CHECK(1);
  2289. #endif
  2290. return res;
  2291. }
  2292. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2293. {
  2294. int res = TEST_SKIPPED;
  2295. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2296. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2297. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2298. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2299. !defined(NO_SHA256)
  2300. WOLFSSL_CERT_MANAGER* cm;
  2301. WOLFSSL_EVP_PKEY *priv;
  2302. WOLFSSL_X509_NAME* name;
  2303. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2304. const char* server_cert = "./certs/test/server-goodcn.pem";
  2305. byte *der;
  2306. int derSz;
  2307. byte *pt;
  2308. WOLFSSL_X509 *x509, *ca;
  2309. pt = (byte*)server_key_der_2048;
  2310. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2311. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2312. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2313. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2314. WOLFSSL_FILETYPE_ASN1));
  2315. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2316. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2317. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2318. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2319. /* check satisfying wolfssl.com constraint passes */
  2320. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2321. WOLFSSL_FILETYPE_PEM));
  2322. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2323. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2324. AssertNotNull(name = X509_NAME_new());
  2325. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2326. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2327. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2328. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2329. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2330. X509_NAME_free(name);
  2331. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2332. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2333. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2334. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2335. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2336. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2337. wolfSSL_X509_free(x509);
  2338. /* check satisfying example.com constraint passes */
  2339. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2340. WOLFSSL_FILETYPE_PEM));
  2341. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2342. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2343. AssertNotNull(name = X509_NAME_new());
  2344. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2345. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2346. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2347. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2348. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2349. X509_NAME_free(name);
  2350. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2351. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2352. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2353. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2354. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2355. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2356. wolfSSL_X509_free(x509);
  2357. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2358. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2359. WOLFSSL_FILETYPE_PEM));
  2360. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2361. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2362. AssertNotNull(name = X509_NAME_new());
  2363. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2364. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2365. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2366. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2367. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2368. X509_NAME_free(name);
  2369. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2370. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2371. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2372. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2373. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2374. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2375. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2376. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2377. wolfSSL_X509_free(x509);
  2378. /* check fail when one DNS in the list is bad */
  2379. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2380. WOLFSSL_FILETYPE_PEM));
  2381. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2382. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2383. AssertNotNull(name = X509_NAME_new());
  2384. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2385. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2386. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2387. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2388. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2389. X509_NAME_free(name);
  2390. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2391. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2392. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2393. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2394. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2395. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2396. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2397. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2398. wolfSSL_X509_free(x509);
  2399. /* check fail case when neither constraint is matched */
  2400. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2401. WOLFSSL_FILETYPE_PEM));
  2402. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2403. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2404. AssertNotNull(name = X509_NAME_new());
  2405. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2406. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2407. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2408. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2409. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2410. X509_NAME_free(name);
  2411. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2412. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2413. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2414. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2415. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2416. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2417. wolfSSL_CertManagerFree(cm);
  2418. wolfSSL_X509_free(x509);
  2419. wolfSSL_X509_free(ca);
  2420. wolfSSL_EVP_PKEY_free(priv);
  2421. res = TEST_RES_CHECK(1);
  2422. #endif
  2423. return res;
  2424. }
  2425. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2426. {
  2427. int res = TEST_SKIPPED;
  2428. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2429. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2430. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2431. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2432. !defined(NO_SHA256)
  2433. WOLFSSL_CERT_MANAGER* cm;
  2434. WOLFSSL_EVP_PKEY *priv;
  2435. WOLFSSL_X509_NAME* name;
  2436. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2437. const char* server_cert = "./certs/test/server-goodcn.pem";
  2438. byte *der;
  2439. int derSz;
  2440. byte *pt;
  2441. WOLFSSL_X509 *x509, *ca;
  2442. pt = (byte*)server_key_der_2048;
  2443. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2444. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2445. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2446. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2447. WOLFSSL_FILETYPE_ASN1));
  2448. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2449. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2450. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2451. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2452. /* check satisfying wolfssl.com constraint passes */
  2453. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2454. WOLFSSL_FILETYPE_PEM));
  2455. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2456. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2457. AssertNotNull(name = X509_NAME_new());
  2458. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2459. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2460. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2461. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2462. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2463. X509_NAME_free(name);
  2464. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2465. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2466. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2467. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2468. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2469. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2470. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2471. wolfSSL_X509_free(x509);
  2472. /* fail with DNS check because of common name */
  2473. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2474. WOLFSSL_FILETYPE_PEM));
  2475. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2476. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2477. AssertNotNull(name = X509_NAME_new());
  2478. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2479. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2480. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2481. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2482. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2483. X509_NAME_free(name);
  2484. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2485. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2486. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2487. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2488. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2489. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2490. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2491. wolfSSL_X509_free(x509);
  2492. /* fail on permitted DNS name constraint */
  2493. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2494. WOLFSSL_FILETYPE_PEM));
  2495. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2496. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2497. AssertNotNull(name = X509_NAME_new());
  2498. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2499. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2500. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2501. X509_NAME_free(name);
  2502. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2503. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2504. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2505. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2506. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2507. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2508. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2509. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2510. wolfSSL_X509_free(x509);
  2511. /* fail on permitted email name constraint */
  2512. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2513. WOLFSSL_FILETYPE_PEM));
  2514. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2515. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2516. AssertNotNull(name = X509_NAME_new());
  2517. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2518. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2519. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2520. X509_NAME_free(name);
  2521. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2522. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2523. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2524. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2525. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2526. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2527. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2528. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2529. wolfSSL_X509_free(x509);
  2530. /* success with empty email name */
  2531. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2532. WOLFSSL_FILETYPE_PEM));
  2533. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2534. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2535. AssertNotNull(name = X509_NAME_new());
  2536. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2537. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2538. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2539. X509_NAME_free(name);
  2540. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2541. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2542. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2543. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2544. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2545. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2546. wolfSSL_X509_free(x509);
  2547. wolfSSL_CertManagerFree(cm);
  2548. wolfSSL_X509_free(ca);
  2549. wolfSSL_EVP_PKEY_free(priv);
  2550. res = TEST_RES_CHECK(1);
  2551. #endif
  2552. return res;
  2553. }
  2554. static int test_wolfSSL_FPKI(void)
  2555. {
  2556. int res = TEST_SKIPPED;
  2557. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2558. XFILE f;
  2559. const char* fpkiCert = "./certs/fpki-cert.der";
  2560. DecodedCert cert;
  2561. byte buf[4096];
  2562. byte* uuid;
  2563. byte* fascn;
  2564. word32 fascnSz;
  2565. word32 uuidSz;
  2566. int bytes;
  2567. f = XFOPEN(fpkiCert, "rb");
  2568. AssertTrue((f != XBADFILE));
  2569. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2570. XFCLOSE(f);
  2571. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2572. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2573. AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2574. fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2575. AssertNotNull(fascn);
  2576. AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2577. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2578. AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2579. uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2580. AssertNotNull(uuid);
  2581. AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2582. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2583. wc_FreeDecodedCert(&cert);
  2584. res = TEST_RES_CHECK(1);
  2585. #endif
  2586. return res;
  2587. }
  2588. /* use RID in confuncture with other names to test parsing of unknown other
  2589. * names */
  2590. static int test_wolfSSL_OtherName(void)
  2591. {
  2592. int res = TEST_SKIPPED;
  2593. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2594. XFILE f;
  2595. const char* ridCert = "./certs/rid-cert.der";
  2596. DecodedCert cert;
  2597. byte buf[4096];
  2598. int bytes;
  2599. f = XFOPEN(ridCert, "rb");
  2600. AssertTrue((f != XBADFILE));
  2601. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2602. XFCLOSE(f);
  2603. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2604. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2605. wc_FreeDecodedCert(&cert);
  2606. res = TEST_RES_CHECK(1);
  2607. #endif
  2608. return res;
  2609. }
  2610. static int test_wolfSSL_CertRsaPss(void)
  2611. {
  2612. int res = TEST_SKIPPED;
  2613. /* FIPS v2 and below don't support long salts. */
  2614. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2615. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2616. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2617. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2618. XFILE f;
  2619. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2620. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2621. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2622. RSA_MAX_SIZE >= 3072
  2623. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2624. #endif
  2625. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2626. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2627. #endif
  2628. DecodedCert cert;
  2629. byte buf[4096];
  2630. int bytes;
  2631. WOLFSSL_CERT_MANAGER* cm;
  2632. cm = wolfSSL_CertManagerNew();
  2633. AssertNotNull(cm);
  2634. AssertIntEQ(WOLFSSL_SUCCESS,
  2635. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2636. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2637. AssertIntEQ(WOLFSSL_SUCCESS,
  2638. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2639. #endif
  2640. f = XFOPEN(rsaPssSha256Cert, "rb");
  2641. AssertTrue((f != XBADFILE));
  2642. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2643. XFCLOSE(f);
  2644. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2645. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2646. wc_FreeDecodedCert(&cert);
  2647. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2648. RSA_MAX_SIZE >= 3072
  2649. f = XFOPEN(rsaPssSha384Cert, "rb");
  2650. AssertTrue((f != XBADFILE));
  2651. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2652. XFCLOSE(f);
  2653. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2654. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2655. wc_FreeDecodedCert(&cert);
  2656. #endif
  2657. wolfSSL_CertManagerFree(cm);
  2658. res = TEST_RES_CHECK(1);
  2659. #endif
  2660. return res;
  2661. }
  2662. static int test_wolfSSL_CertManagerCRL(void)
  2663. {
  2664. int res = TEST_SKIPPED;
  2665. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2666. !defined(NO_RSA)
  2667. const char* ca_cert = "./certs/ca-cert.pem";
  2668. const char* crl1 = "./certs/crl/crl.pem";
  2669. const char* crl2 = "./certs/crl/crl2.pem";
  2670. WOLFSSL_CERT_MANAGER* cm = NULL;
  2671. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2672. AssertIntEQ(WOLFSSL_SUCCESS,
  2673. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2674. AssertIntEQ(WOLFSSL_SUCCESS,
  2675. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2676. AssertIntEQ(WOLFSSL_SUCCESS,
  2677. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2678. wolfSSL_CertManagerFreeCRL(cm);
  2679. AssertIntEQ(WOLFSSL_SUCCESS,
  2680. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2681. AssertIntEQ(WOLFSSL_SUCCESS,
  2682. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2683. wolfSSL_CertManagerFree(cm);
  2684. res = TEST_RES_CHECK(1);
  2685. #endif
  2686. return res;
  2687. }
  2688. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2689. {
  2690. int res = TEST_SKIPPED;
  2691. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2692. !defined(NO_WOLFSSL_CLIENT)
  2693. WOLFSSL_CTX* ctx;
  2694. const char* ca_cert = "./certs/ca-cert.pem";
  2695. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2696. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2697. AssertNotNull(ctx);
  2698. /* test good CA */
  2699. AssertTrue(WOLFSSL_SUCCESS ==
  2700. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2701. WOLFSSL_LOAD_FLAG_NONE));
  2702. /* test expired CA */
  2703. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  2704. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2705. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2706. #else
  2707. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2708. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2709. #endif
  2710. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2711. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2712. wolfSSL_CTX_free(ctx);
  2713. res = TEST_RES_CHECK(1);
  2714. #endif
  2715. return res;
  2716. }
  2717. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2718. {
  2719. int res = TEST_SKIPPED;
  2720. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2721. defined(USE_CERT_BUFFERS_2048)
  2722. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2723. WOLFSSL_CTX* ctx;
  2724. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2725. byte ca_expired_cert[TWOK_BUF];
  2726. word32 sizeof_ca_expired_cert;
  2727. XFILE fp;
  2728. #ifndef NO_WOLFSSL_CLIENT
  2729. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2730. #else
  2731. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2732. #endif
  2733. AssertNotNull(ctx);
  2734. /* test good CA */
  2735. AssertTrue(WOLFSSL_SUCCESS ==
  2736. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2737. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2738. WOLFSSL_LOAD_FLAG_NONE));
  2739. /* load expired CA */
  2740. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2741. fp = XFOPEN(ca_expired_cert_file, "rb");
  2742. AssertTrue(fp != XBADFILE);
  2743. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2744. sizeof(ca_expired_cert), fp);
  2745. XFCLOSE(fp);
  2746. /* test expired CA failure */
  2747. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  2748. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2749. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2750. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2751. #else
  2752. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2753. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2754. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2755. #endif
  2756. /* test expired CA success */
  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_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2760. wolfSSL_CTX_free(ctx);
  2761. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2762. res = TEST_RES_CHECK(1);
  2763. #endif
  2764. return res;
  2765. }
  2766. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2767. {
  2768. int res = TEST_SKIPPED;
  2769. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2770. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2771. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2772. WOLFSSL_CTX* ctx;
  2773. #ifndef NO_WOLFSSL_CLIENT
  2774. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2775. #else
  2776. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2777. #endif
  2778. AssertTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  2779. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  2780. WOLFSSL_FILETYPE_ASN1));
  2781. wolfSSL_CTX_free(ctx);
  2782. res = TEST_RES_CHECK(1);
  2783. #endif
  2784. return res;
  2785. }
  2786. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2787. {
  2788. int res = TEST_SKIPPED;
  2789. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2790. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2791. WOLFSSL_CTX* ctx;
  2792. WOLFSSL* ssl;
  2793. const char *certChain[] = {
  2794. "./certs/intermediate/client-int-cert.pem",
  2795. "./certs/intermediate/ca-int2-cert.pem",
  2796. "./certs/intermediate/ca-int-cert.pem",
  2797. "./certs/ca-cert.pem",
  2798. NULL
  2799. };
  2800. const char** cert;
  2801. WOLFSSL_X509* x509;
  2802. WOLF_STACK_OF(X509)* chain = NULL;
  2803. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2804. AssertNotNull(ssl = wolfSSL_new(ctx));
  2805. for (cert = certChain; *cert != NULL; cert++) {
  2806. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2807. AssertNotNull(x509);
  2808. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2809. X509_free(x509);
  2810. }
  2811. for (cert = certChain; *cert != NULL; cert++) {
  2812. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2813. AssertNotNull(x509);
  2814. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2815. X509_free(x509);
  2816. }
  2817. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2818. AssertIntEQ(sk_X509_num(chain), 3);
  2819. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2820. AssertIntEQ(sk_X509_num(chain), 3);
  2821. SSL_free(ssl);
  2822. SSL_CTX_free(ctx);
  2823. res = TEST_RES_CHECK(1);
  2824. #endif
  2825. return res;
  2826. }
  2827. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2828. {
  2829. int res = TEST_SKIPPED;
  2830. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2831. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2832. const char* server_chain_der = "./certs/server-cert-chain.der";
  2833. const char* client_single_pem = "./certs/client-cert.pem";
  2834. WOLFSSL_CTX* ctx;
  2835. int ret = 0;
  2836. (void)server_chain_der;
  2837. (void)client_single_pem;
  2838. (void)ctx;
  2839. #ifndef NO_WOLFSSL_CLIENT
  2840. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2841. AssertNotNull(ctx);
  2842. #else
  2843. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2844. AssertNotNull(ctx);
  2845. #endif
  2846. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2847. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2848. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2849. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2850. wolfSSL_CTX_free(ctx);
  2851. res = TEST_RES_CHECK(ret == 0);
  2852. #endif
  2853. return res;
  2854. }
  2855. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  2856. {
  2857. int res = TEST_SKIPPED;
  2858. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2859. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2860. WOLFSSL_CTX *ctx;
  2861. (void)ctx;
  2862. #ifndef NO_WOLFSSL_CLIENT
  2863. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2864. #else
  2865. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2866. #endif
  2867. /* invalid context */
  2868. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2869. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2870. /* invalid dhParamFile file */
  2871. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2872. NULL, WOLFSSL_FILETYPE_PEM));
  2873. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2874. bogusFile, WOLFSSL_FILETYPE_PEM));
  2875. /* success */
  2876. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2877. WOLFSSL_FILETYPE_PEM));
  2878. wolfSSL_CTX_free(ctx);
  2879. res = TEST_RES_CHECK(1);
  2880. #endif
  2881. return res;
  2882. }
  2883. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2884. {
  2885. int res = TEST_SKIPPED;
  2886. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2887. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2888. WOLFSSL_CTX *ctx;
  2889. (void)ctx;
  2890. #ifndef NO_WOLFSSL_CLIENT
  2891. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2892. #else
  2893. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2894. #endif
  2895. /* invalid context */
  2896. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2897. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2898. /* invalid dhParamFile file */
  2899. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2900. 0, WOLFSSL_FILETYPE_ASN1));
  2901. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2902. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2903. /* success */
  2904. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2905. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2906. wolfSSL_CTX_free(ctx);
  2907. res = TEST_RES_CHECK(1);
  2908. #endif
  2909. return res;
  2910. }
  2911. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2912. {
  2913. int res = TEST_SKIPPED;
  2914. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2915. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2916. WOLFSSL_CTX *ctx;
  2917. (void)ctx;
  2918. #ifndef NO_WOLFSSL_CLIENT
  2919. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2920. AssertNotNull(ctx);
  2921. #else
  2922. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2923. AssertNotNull(ctx);
  2924. #endif
  2925. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2926. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2927. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2928. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2929. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2930. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2931. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2932. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2933. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2934. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2935. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2936. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2937. wolfSSL_CTX_free(ctx);
  2938. res = TEST_RES_CHECK(1);
  2939. #endif
  2940. return res;
  2941. }
  2942. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  2943. {
  2944. int res = TEST_SKIPPED;
  2945. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  2946. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2947. WOLFSSL_CTX* ctx = NULL;
  2948. const char* derCert = "./certs/server-cert.der";
  2949. const char* nullPath = NULL;
  2950. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2951. const char* emptyPath = "";
  2952. /* der load Case 1 ctx NULL */
  2953. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2954. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2955. #ifndef NO_WOLFSSL_CLIENT
  2956. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2957. #else
  2958. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2959. #endif
  2960. /* Case 2 filePath NULL */
  2961. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2962. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2963. /* Case 3 invalid format */
  2964. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2965. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2966. /* Case 4 filePath not valid */
  2967. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2968. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2969. /* Case 5 filePath empty */
  2970. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2971. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2972. #ifndef NO_RSA
  2973. /* Case 6 success case */
  2974. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2975. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2976. #endif
  2977. wolfSSL_CTX_free(ctx);
  2978. res = TEST_RES_CHECK(1);
  2979. #endif
  2980. return res;
  2981. }
  2982. static int test_wolfSSL_CTX_enable_disable(void)
  2983. {
  2984. int res = TEST_SKIPPED;
  2985. #ifndef NO_CERTS
  2986. WOLFSSL_CTX* ctx = NULL;
  2987. #ifdef HAVE_CRL
  2988. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2989. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2990. #endif
  2991. #ifdef HAVE_OCSP
  2992. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2993. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2994. #endif
  2995. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2996. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2997. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2998. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2999. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3000. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3001. #endif
  3002. #ifndef NO_WOLFSSL_CLIENT
  3003. #ifdef HAVE_EXTENDED_MASTER
  3004. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3005. #endif
  3006. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3007. AssertNotNull(ctx);
  3008. #ifdef HAVE_EXTENDED_MASTER
  3009. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3010. #endif
  3011. #elif !defined(NO_WOLFSSL_SERVER)
  3012. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3013. #else
  3014. return TEST_SUCCESS;
  3015. #endif
  3016. #ifdef HAVE_CRL
  3017. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3018. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  3019. #endif
  3020. #ifdef HAVE_OCSP
  3021. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  3022. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  3023. WOLFSSL_SUCCESS);
  3024. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  3025. WOLFSSL_SUCCESS);
  3026. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  3027. WOLFSSL_SUCCESS);
  3028. #endif
  3029. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3030. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3031. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3032. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3033. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3034. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3035. #endif
  3036. wolfSSL_CTX_free(ctx);
  3037. res = TEST_RES_CHECK(1);
  3038. #endif /* NO_CERTS */
  3039. return res;
  3040. }
  3041. static int test_wolfSSL_CTX_ticket_API(void)
  3042. {
  3043. int res = TEST_SKIPPED;
  3044. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3045. WOLFSSL_CTX* ctx = NULL;
  3046. void *userCtx = (void*)"this is my ctx";
  3047. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3048. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3049. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3050. wolfSSL_CTX_free(ctx);
  3051. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3052. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3053. res = TEST_RES_CHECK(1);
  3054. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3055. return res;
  3056. }
  3057. static int test_wolfSSL_set_minmax_proto_version(void)
  3058. {
  3059. int res = TEST_SKIPPED;
  3060. #ifdef OPENSSL_EXTRA
  3061. WOLFSSL_CTX *ctx;
  3062. WOLFSSL *ssl;
  3063. int ret;
  3064. (void)ret;
  3065. (void)ssl;
  3066. #ifndef NO_WOLFSSL_CLIENT
  3067. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3068. AssertNotNull(ssl = wolfSSL_new(ctx));
  3069. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3070. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3071. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3072. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3073. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3074. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3075. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3076. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3077. wolfSSL_free(ssl);
  3078. wolfSSL_CTX_free(ctx);
  3079. #endif
  3080. #ifndef NO_WOLFSSL_SERVER
  3081. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3082. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3083. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3084. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3085. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3086. wolfSSL_CTX_free(ctx);
  3087. #endif
  3088. res = TEST_RES_CHECK(1);
  3089. #endif
  3090. return res;
  3091. }
  3092. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3093. defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  3094. static void test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3095. {
  3096. AssertStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3097. }
  3098. static void test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3099. {
  3100. /* Set TLS 1.2 */
  3101. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3102. WOLFSSL_SUCCESS);
  3103. }
  3104. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3105. * what was set at ctx creation. */
  3106. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3107. {
  3108. callback_functions client_cbs, server_cbs;
  3109. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3110. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3111. client_cbs.method = wolfTLS_client_method;
  3112. server_cbs.method = wolfTLS_server_method;
  3113. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3114. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3115. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3116. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  3117. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  3118. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  3119. return TEST_RES_CHECK(1);
  3120. }
  3121. #else
  3122. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3123. {
  3124. return TEST_SKIPPED;
  3125. }
  3126. #endif
  3127. /*----------------------------------------------------------------------------*
  3128. | SSL
  3129. *----------------------------------------------------------------------------*/
  3130. static int test_server_wolfSSL_new(void)
  3131. {
  3132. int res = TEST_SKIPPED;
  3133. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3134. !defined(NO_WOLFSSL_SERVER)
  3135. WOLFSSL_CTX *ctx;
  3136. WOLFSSL_CTX *ctx_nocert;
  3137. WOLFSSL *ssl;
  3138. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3139. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3140. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  3141. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  3142. /* invalid context */
  3143. AssertNull(ssl = wolfSSL_new(NULL));
  3144. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  3145. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  3146. #endif
  3147. /* success */
  3148. AssertNotNull(ssl = wolfSSL_new(ctx));
  3149. wolfSSL_free(ssl);
  3150. wolfSSL_CTX_free(ctx);
  3151. wolfSSL_CTX_free(ctx_nocert);
  3152. res = TEST_RES_CHECK(1);
  3153. #endif
  3154. return res;
  3155. }
  3156. static int test_client_wolfSSL_new(void)
  3157. {
  3158. int res = TEST_SKIPPED;
  3159. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3160. !defined(NO_WOLFSSL_CLIENT)
  3161. WOLFSSL_CTX *ctx;
  3162. WOLFSSL_CTX *ctx_nocert;
  3163. WOLFSSL *ssl;
  3164. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3165. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3166. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3167. /* invalid context */
  3168. AssertNull(ssl = wolfSSL_new(NULL));
  3169. /* success */
  3170. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  3171. wolfSSL_free(ssl);
  3172. /* success */
  3173. AssertNotNull(ssl = wolfSSL_new(ctx));
  3174. wolfSSL_free(ssl);
  3175. wolfSSL_CTX_free(ctx);
  3176. wolfSSL_CTX_free(ctx_nocert);
  3177. res = TEST_RES_CHECK(1);
  3178. #endif
  3179. return res;
  3180. }
  3181. static int test_wolfSSL_SetTmpDH_file(void)
  3182. {
  3183. int res = TEST_SKIPPED;
  3184. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3185. !defined(NO_WOLFSSL_SERVER)
  3186. WOLFSSL_CTX *ctx;
  3187. WOLFSSL *ssl;
  3188. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3189. #ifndef NO_RSA
  3190. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3191. WOLFSSL_FILETYPE_PEM));
  3192. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3193. WOLFSSL_FILETYPE_PEM));
  3194. #elif defined(HAVE_ECC)
  3195. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3196. WOLFSSL_FILETYPE_PEM));
  3197. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3198. WOLFSSL_FILETYPE_PEM));
  3199. #elif defined(HAVE_ED25519)
  3200. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3201. WOLFSSL_FILETYPE_PEM));
  3202. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3203. WOLFSSL_FILETYPE_PEM));
  3204. #elif defined(HAVE_ED448)
  3205. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3206. WOLFSSL_FILETYPE_PEM));
  3207. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3208. WOLFSSL_FILETYPE_PEM));
  3209. #endif
  3210. AssertNotNull(ssl = wolfSSL_new(ctx));
  3211. /* invalid ssl */
  3212. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3213. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3214. /* invalid dhParamFile file */
  3215. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3216. NULL, WOLFSSL_FILETYPE_PEM));
  3217. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3218. bogusFile, WOLFSSL_FILETYPE_PEM));
  3219. /* success */
  3220. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3221. WOLFSSL_FILETYPE_PEM));
  3222. wolfSSL_free(ssl);
  3223. wolfSSL_CTX_free(ctx);
  3224. res = TEST_RES_CHECK(1);
  3225. #endif
  3226. return res;
  3227. }
  3228. static int test_wolfSSL_SetTmpDH_buffer(void)
  3229. {
  3230. int res = TEST_SKIPPED;
  3231. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3232. WOLFSSL_CTX *ctx;
  3233. WOLFSSL *ssl;
  3234. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3235. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3236. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3237. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3238. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3239. AssertNotNull(ssl = wolfSSL_new(ctx));
  3240. /* invalid ssl */
  3241. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3242. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3243. /* invalid dhParamFile file */
  3244. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3245. 0, WOLFSSL_FILETYPE_ASN1));
  3246. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3247. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3248. /* success */
  3249. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3250. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3251. wolfSSL_free(ssl);
  3252. wolfSSL_CTX_free(ctx);
  3253. res = TEST_RES_CHECK(1);
  3254. #endif
  3255. return res;
  3256. }
  3257. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3258. {
  3259. int res = TEST_SKIPPED;
  3260. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3261. WOLFSSL_CTX *ctx, *ctx2;
  3262. WOLFSSL *ssl, *ssl2;
  3263. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3264. AssertNotNull(ctx);
  3265. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3266. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3267. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3268. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3269. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3270. ssl = wolfSSL_new(ctx);
  3271. AssertNotNull(ssl);
  3272. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3273. AssertNotNull(ctx2);
  3274. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3275. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3276. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3277. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3278. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3279. ssl2 = wolfSSL_new(ctx2);
  3280. AssertNotNull(ssl2);
  3281. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3282. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3283. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3284. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3285. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3286. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3287. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3288. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3289. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3290. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3291. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3292. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3293. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3294. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3295. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3296. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3297. wolfSSL_free(ssl2);
  3298. wolfSSL_CTX_free(ctx2);
  3299. wolfSSL_free(ssl);
  3300. wolfSSL_CTX_free(ctx);
  3301. res = TEST_RES_CHECK(1);
  3302. #endif
  3303. return res;
  3304. }
  3305. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3306. * allowed.
  3307. * POST: return 1 on success.
  3308. */
  3309. static int test_wolfSSL_SetMinVersion(void)
  3310. {
  3311. int res = TEST_SKIPPED;
  3312. #ifndef NO_WOLFSSL_CLIENT
  3313. int failFlag = WOLFSSL_SUCCESS;
  3314. WOLFSSL_CTX* ctx;
  3315. WOLFSSL* ssl;
  3316. int itr;
  3317. #ifndef NO_OLD_TLS
  3318. const int versions[] = {
  3319. #ifdef WOLFSSL_ALLOW_TLSV10
  3320. WOLFSSL_TLSV1,
  3321. #endif
  3322. WOLFSSL_TLSV1_1,
  3323. WOLFSSL_TLSV1_2};
  3324. #elif !defined(WOLFSSL_NO_TLS12)
  3325. const int versions[] = { WOLFSSL_TLSV1_2 };
  3326. #else
  3327. const int versions[] = { WOLFSSL_TLSV1_3 };
  3328. #endif
  3329. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3330. ssl = wolfSSL_new(ctx);
  3331. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3332. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3333. failFlag = WOLFSSL_FAILURE;
  3334. }
  3335. }
  3336. wolfSSL_free(ssl);
  3337. wolfSSL_CTX_free(ctx);
  3338. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3339. #endif
  3340. return res;
  3341. } /* END test_wolfSSL_SetMinVersion */
  3342. #ifdef OPENSSL_EXTRA
  3343. static int test_ED25519(void)
  3344. {
  3345. int res = TEST_SKIPPED;
  3346. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3347. defined(WOLFSSL_KEY_GEN)
  3348. byte priv[ED25519_PRV_KEY_SIZE];
  3349. unsigned int privSz = (unsigned int)sizeof(priv);
  3350. byte pub[ED25519_PUB_KEY_SIZE];
  3351. unsigned int pubSz = (unsigned int)sizeof(pub);
  3352. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3353. const char* msg = TEST_STRING;
  3354. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3355. byte sig[ED25519_SIG_SIZE];
  3356. unsigned int sigSz = (unsigned int)sizeof(sig);
  3357. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3358. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3359. WOLFSSL_SUCCESS);
  3360. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3361. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3362. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3363. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3364. &sigSz), WOLFSSL_SUCCESS);
  3365. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3366. #ifdef HAVE_ED25519_VERIFY
  3367. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3368. sigSz), WOLFSSL_SUCCESS);
  3369. #endif /* HAVE_ED25519_VERIFY */
  3370. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3371. res = TEST_RES_CHECK(1);
  3372. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3373. return res;
  3374. }
  3375. static int test_ED448(void)
  3376. {
  3377. int res = TEST_SKIPPED;
  3378. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3379. defined(WOLFSSL_KEY_GEN)
  3380. byte priv[ED448_PRV_KEY_SIZE];
  3381. unsigned int privSz = (unsigned int)sizeof(priv);
  3382. byte pub[ED448_PUB_KEY_SIZE];
  3383. unsigned int pubSz = (unsigned int)sizeof(pub);
  3384. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3385. const char* msg = TEST_STRING;
  3386. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3387. byte sig[ED448_SIG_SIZE];
  3388. unsigned int sigSz = (unsigned int)sizeof(sig);
  3389. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3390. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3391. WOLFSSL_SUCCESS);
  3392. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3393. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3394. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3395. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3396. &sigSz), WOLFSSL_SUCCESS);
  3397. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3398. #ifdef HAVE_ED448_VERIFY
  3399. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3400. sigSz), WOLFSSL_SUCCESS);
  3401. #endif /* HAVE_ED448_VERIFY */
  3402. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3403. res = TEST_RES_CHECK(1);
  3404. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3405. return res;
  3406. }
  3407. #endif /* OPENSSL_EXTRA */
  3408. #include <wolfssl/openssl/pem.h>
  3409. /*----------------------------------------------------------------------------*
  3410. | EVP
  3411. *----------------------------------------------------------------------------*/
  3412. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3413. {
  3414. int res = TEST_SKIPPED;
  3415. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3416. WOLFSSL_BIO* rbio = NULL;
  3417. WOLFSSL_BIO* wbio = NULL;
  3418. WOLFSSL_EVP_PKEY* pkey = NULL;
  3419. char line[256] = { 0 };
  3420. char line1[256] = { 0 };
  3421. int i;
  3422. /* test error cases */
  3423. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3424. /*
  3425. * test RSA public key print
  3426. * in this test, pass '3' for indent
  3427. */
  3428. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3429. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3430. sizeof_client_keypub_der_1024);
  3431. AssertNotNull(rbio);
  3432. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3433. AssertNotNull(pkey);
  3434. wbio = BIO_new(BIO_s_mem());
  3435. AssertNotNull(wbio);
  3436. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3437. BIO_gets(wbio, line, sizeof(line));
  3438. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3439. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3440. BIO_gets(wbio, line, sizeof(line));
  3441. strcpy(line1, " Modulus:\n");
  3442. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3443. BIO_gets(wbio, line, sizeof(line));
  3444. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  3445. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3446. /* skip to the end of modulus element*/
  3447. for (i = 0; i < 8 ;i++) {
  3448. BIO_gets(wbio, line, sizeof(line));
  3449. }
  3450. BIO_gets(wbio, line, sizeof(line));
  3451. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3452. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3453. /* should reach EOF */
  3454. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3455. EVP_PKEY_free(pkey);
  3456. pkey = NULL;
  3457. BIO_free(rbio);
  3458. BIO_free(wbio);
  3459. rbio = NULL;
  3460. wbio = NULL;
  3461. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3462. /*
  3463. * test DSA public key print
  3464. */
  3465. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3466. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3467. sizeof_dsa_pub_key_der_2048);
  3468. AssertNotNull(rbio);
  3469. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3470. AssertNotNull(pkey);
  3471. wbio = BIO_new(BIO_s_mem());
  3472. AssertNotNull(wbio);
  3473. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3474. BIO_gets(wbio, line, sizeof(line));
  3475. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3476. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3477. BIO_gets(wbio, line, sizeof(line));
  3478. strcpy(line1, "pub:\n");
  3479. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3480. BIO_gets(wbio, line, sizeof(line));
  3481. strcpy(line1,
  3482. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3483. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3484. /* skip to the end of pub element*/
  3485. for (i = 0; i < 17 ;i++) {
  3486. BIO_gets(wbio, line, sizeof(line));
  3487. }
  3488. BIO_gets(wbio, line, sizeof(line));
  3489. strcpy(line1, "P:\n");
  3490. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3491. /* skip to the end of P element*/
  3492. for (i = 0; i < 18 ;i++) {
  3493. BIO_gets(wbio, line, sizeof(line));
  3494. }
  3495. BIO_gets(wbio, line, sizeof(line));
  3496. strcpy(line1, "Q:\n");
  3497. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3498. /* skip to the end of Q element*/
  3499. for (i = 0; i < 3 ;i++) {
  3500. BIO_gets(wbio, line, sizeof(line));
  3501. }
  3502. BIO_gets(wbio, line, sizeof(line));
  3503. strcpy(line1, "G:\n");
  3504. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3505. /* skip to the end of G element*/
  3506. for (i = 0; i < 18 ;i++) {
  3507. BIO_gets(wbio, line, sizeof(line));
  3508. }
  3509. /* should reach EOF */
  3510. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3511. EVP_PKEY_free(pkey);
  3512. pkey = NULL;
  3513. BIO_free(rbio);
  3514. BIO_free(wbio);
  3515. rbio = NULL;
  3516. wbio = NULL;
  3517. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3518. /*
  3519. * test ECC public key print
  3520. */
  3521. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3522. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3523. sizeof_ecc_clikeypub_der_256);
  3524. AssertNotNull(rbio);
  3525. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3526. AssertNotNull(pkey);
  3527. wbio = BIO_new(BIO_s_mem());
  3528. AssertNotNull(wbio);
  3529. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3530. BIO_gets(wbio, line, sizeof(line));
  3531. strcpy(line1, "Public-Key: (256 bit)\n");
  3532. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3533. BIO_gets(wbio, line, sizeof(line));
  3534. strcpy(line1, "pub:\n");
  3535. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3536. BIO_gets(wbio, line, sizeof(line));
  3537. strcpy(line1,
  3538. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3539. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3540. /* skip to the end of pub element*/
  3541. for (i = 0; i < 4 ;i++) {
  3542. BIO_gets(wbio, line, sizeof(line));
  3543. }
  3544. BIO_gets(wbio, line, sizeof(line));
  3545. strcpy(line1, "ASN1 OID: prime256v1\n");
  3546. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3547. BIO_gets(wbio, line, sizeof(line));
  3548. strcpy(line1, "NIST CURVE: P-256\n");
  3549. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3550. /* should reach EOF */
  3551. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3552. EVP_PKEY_free(pkey);
  3553. pkey = NULL;
  3554. BIO_free(rbio);
  3555. BIO_free(wbio);
  3556. rbio = NULL;
  3557. wbio = NULL;
  3558. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3559. /*
  3560. * test DH public key print
  3561. */
  3562. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3563. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3564. sizeof_dh_pub_key_der_2048);
  3565. AssertNotNull(rbio);
  3566. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3567. AssertNotNull(pkey);
  3568. wbio = BIO_new(BIO_s_mem());
  3569. AssertNotNull(wbio);
  3570. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3571. BIO_gets(wbio, line, sizeof(line));
  3572. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3573. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3574. BIO_gets(wbio, line, sizeof(line));
  3575. strcpy(line1, "public-key:\n");
  3576. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3577. BIO_gets(wbio, line, sizeof(line));
  3578. strcpy(line1,
  3579. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3580. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3581. /* skip to the end of public-key element*/
  3582. for (i = 0; i < 17 ;i++) {
  3583. BIO_gets(wbio, line, sizeof(line));
  3584. }
  3585. BIO_gets(wbio, line, sizeof(line));
  3586. strcpy(line1, "prime:\n");
  3587. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3588. BIO_gets(wbio, line, sizeof(line));
  3589. strcpy(line1,
  3590. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3591. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3592. /* skip to the end of prime element*/
  3593. for (i = 0; i < 17 ;i++) {
  3594. BIO_gets(wbio, line, sizeof(line));
  3595. }
  3596. BIO_gets(wbio, line, sizeof(line));
  3597. strcpy(line1, "generator: 2 (0x02)\n");
  3598. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3599. /* should reach EOF */
  3600. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3601. EVP_PKEY_free(pkey);
  3602. pkey = NULL;
  3603. BIO_free(rbio);
  3604. BIO_free(wbio);
  3605. rbio = NULL;
  3606. wbio = NULL;
  3607. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3608. /* to prevent "unused variable" warning */
  3609. (void)pkey;
  3610. (void)wbio;
  3611. (void)rbio;
  3612. (void)line;
  3613. (void)line1;
  3614. (void)i;
  3615. res = TEST_RES_CHECK(1);
  3616. #endif /* OPENSSL_EXTRA */
  3617. return res;
  3618. }
  3619. /* Test functions for base64 encode/decode */
  3620. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3621. {
  3622. int res = TEST_SKIPPED;
  3623. #if defined(OPENSSL_EXTRA) && \
  3624. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3625. EVP_ENCODE_CTX* ctx = NULL;
  3626. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3627. AssertIntEQ( ctx->remaining,0);
  3628. AssertIntEQ( ctx->data[0],0);
  3629. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3630. EVP_ENCODE_CTX_free(ctx);
  3631. res = TEST_RES_CHECK(1);
  3632. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3633. return res;
  3634. }
  3635. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3636. {
  3637. int res = TEST_SKIPPED;
  3638. #if defined(OPENSSL_EXTRA) && \
  3639. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3640. EVP_ENCODE_CTX* ctx = NULL;
  3641. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3642. EVP_ENCODE_CTX_free(ctx);
  3643. res = TEST_RES_CHECK(1);
  3644. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3645. return res;
  3646. }
  3647. static int test_wolfSSL_EVP_EncodeInit(void)
  3648. {
  3649. int res = TEST_SKIPPED;
  3650. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3651. EVP_ENCODE_CTX* ctx = NULL;
  3652. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3653. AssertIntEQ( ctx->remaining,0);
  3654. AssertIntEQ( ctx->data[0],0);
  3655. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3656. /* make ctx dirty */
  3657. ctx->remaining = 10;
  3658. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3659. EVP_EncodeInit(ctx);
  3660. AssertIntEQ( ctx->remaining,0);
  3661. AssertIntEQ( ctx->data[0],0);
  3662. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3663. EVP_ENCODE_CTX_free(ctx);
  3664. res = TEST_RES_CHECK(1);
  3665. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3666. return res;
  3667. }
  3668. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3669. {
  3670. int res = TEST_SKIPPED;
  3671. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3672. int outl;
  3673. int total;
  3674. const unsigned char plain0[] = {"Th"};
  3675. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3676. const unsigned char plain2[] = {"This is additional data."};
  3677. const unsigned char enc0[] = {"VGg=\n"};
  3678. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3679. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3680. const unsigned char enc2[] =
  3681. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3682. unsigned char encOutBuff[300];
  3683. EVP_ENCODE_CTX* ctx = NULL;
  3684. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3685. EVP_EncodeInit(ctx);
  3686. /* illegal parameter test */
  3687. AssertIntEQ(
  3688. EVP_EncodeUpdate(
  3689. NULL, /* pass NULL as ctx */
  3690. encOutBuff,
  3691. &outl,
  3692. plain1,
  3693. sizeof(plain1)-1),
  3694. 0 /* expected result code 0: fail */
  3695. );
  3696. AssertIntEQ(
  3697. EVP_EncodeUpdate(
  3698. ctx,
  3699. NULL, /* pass NULL as out buff */
  3700. &outl,
  3701. plain1,
  3702. sizeof(plain1)-1),
  3703. 0 /* expected result code 0: fail */
  3704. );
  3705. AssertIntEQ(
  3706. EVP_EncodeUpdate(
  3707. ctx,
  3708. encOutBuff,
  3709. NULL, /* pass NULL as outl */
  3710. plain1,
  3711. sizeof(plain1)-1),
  3712. 0 /* expected result code 0: fail */
  3713. );
  3714. AssertIntEQ(
  3715. EVP_EncodeUpdate(
  3716. ctx,
  3717. encOutBuff,
  3718. &outl,
  3719. NULL, /* pass NULL as in */
  3720. sizeof(plain1)-1),
  3721. 0 /* expected result code 0: fail */
  3722. );
  3723. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3724. /* meaningless parameter test */
  3725. AssertIntEQ(
  3726. EVP_EncodeUpdate(
  3727. ctx,
  3728. encOutBuff,
  3729. &outl,
  3730. plain1,
  3731. 0), /* pass zero input */
  3732. 1 /* expected result code 1: success */
  3733. );
  3734. /* very small data encoding test */
  3735. EVP_EncodeInit(ctx);
  3736. AssertIntEQ(
  3737. EVP_EncodeUpdate(
  3738. ctx,
  3739. encOutBuff,
  3740. &outl,
  3741. plain0,
  3742. sizeof(plain0)-1),
  3743. 1 /* expected result code 1: success */
  3744. );
  3745. AssertIntEQ(outl,0);
  3746. EVP_EncodeFinal(
  3747. ctx,
  3748. encOutBuff + outl,
  3749. &outl);
  3750. AssertIntEQ( outl, sizeof(enc0)-1);
  3751. AssertIntEQ(
  3752. XSTRNCMP(
  3753. (const char*)encOutBuff,
  3754. (const char*)enc0,sizeof(enc0) ),
  3755. 0);
  3756. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3757. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3758. sizeof(enc0)-1);
  3759. AssertIntEQ(
  3760. XSTRNCMP(
  3761. (const char*)encOutBuff,
  3762. (const char*)enc0,sizeof(enc0) ),
  3763. 0);
  3764. /* pass small size( < 48bytes ) input, then make sure they are not
  3765. * encoded and just stored in ctx
  3766. */
  3767. EVP_EncodeInit(ctx);
  3768. total = 0;
  3769. outl = 0;
  3770. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3771. AssertIntEQ(
  3772. EVP_EncodeUpdate(
  3773. ctx,
  3774. encOutBuff, /* buffer for output */
  3775. &outl, /* size of output */
  3776. plain1, /* input */
  3777. sizeof(plain1)-1), /* size of input */
  3778. 1); /* expected result code 1:success */
  3779. total += outl;
  3780. AssertIntEQ(outl, 0); /* no output expected */
  3781. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3782. AssertTrue(
  3783. XSTRNCMP((const char*)(ctx->data),
  3784. (const char*)plain1,
  3785. ctx->remaining) ==0 );
  3786. AssertTrue(encOutBuff[0] == 0);
  3787. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3788. * the stored data and the new input together
  3789. */
  3790. AssertIntEQ(
  3791. EVP_EncodeUpdate(
  3792. ctx,
  3793. encOutBuff + outl, /* buffer for output */
  3794. &outl, /* size of output */
  3795. plain2, /* additional input */
  3796. sizeof(plain2) -1), /* size of additional input */
  3797. 1); /* expected result code 1:success */
  3798. total += outl;
  3799. AssertIntNE(outl, 0); /* some output is expected this time*/
  3800. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3801. AssertIntEQ(
  3802. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3803. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3804. EVP_EncodeFinal(
  3805. ctx,
  3806. encOutBuff + outl,
  3807. &outl);
  3808. total += outl;
  3809. AssertIntNE(total,0);
  3810. AssertIntNE(outl,0);
  3811. AssertIntEQ(XSTRNCMP(
  3812. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3813. /* test with illeagal parameters */
  3814. outl = 1;
  3815. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3816. AssertIntEQ(outl, 0);
  3817. outl = 1;
  3818. EVP_EncodeFinal(ctx, NULL, &outl);
  3819. AssertIntEQ(outl, 0);
  3820. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3821. EVP_EncodeFinal(NULL, NULL, NULL);
  3822. EVP_ENCODE_CTX_free(ctx);
  3823. res = TEST_RES_CHECK(1);
  3824. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3825. return res;
  3826. }
  3827. static int test_wolfSSL_EVP_EncodeFinal(void)
  3828. {
  3829. int res = TEST_SKIPPED;
  3830. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3831. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3832. * test_wolfSSL_EVP_EncodeUpdate
  3833. */
  3834. res = TEST_RES_CHECK(1);
  3835. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3836. return res;
  3837. }
  3838. static int test_wolfSSL_EVP_DecodeInit(void)
  3839. {
  3840. int res = TEST_SKIPPED;
  3841. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3842. EVP_ENCODE_CTX* ctx = NULL;
  3843. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3844. AssertIntEQ( ctx->remaining,0);
  3845. AssertIntEQ( ctx->data[0],0);
  3846. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3847. /* make ctx dirty */
  3848. ctx->remaining = 10;
  3849. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3850. EVP_DecodeInit(ctx);
  3851. AssertIntEQ( ctx->remaining,0);
  3852. AssertIntEQ( ctx->data[0],0);
  3853. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3854. EVP_ENCODE_CTX_free(ctx);
  3855. res = TEST_RES_CHECK(1);
  3856. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3857. return res;
  3858. }
  3859. static int test_wolfSSL_EVP_DecodeUpdate(void)
  3860. {
  3861. int res = TEST_SKIPPED;
  3862. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3863. int outl;
  3864. unsigned char decOutBuff[300];
  3865. EVP_ENCODE_CTX* ctx;
  3866. static const unsigned char enc1[] =
  3867. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3868. /* const unsigned char plain1[] =
  3869. {"This is a base64 decoding test."} */
  3870. ctx = EVP_ENCODE_CTX_new();
  3871. EVP_DecodeInit(ctx);
  3872. /* illegal parameter tests */
  3873. /* pass NULL as ctx */
  3874. AssertIntEQ(
  3875. EVP_DecodeUpdate(
  3876. NULL, /* pass NULL as ctx */
  3877. decOutBuff,
  3878. &outl,
  3879. enc1,
  3880. sizeof(enc1)-1),
  3881. -1 /* expected result code -1: fail */
  3882. );
  3883. AssertIntEQ( outl, 0);
  3884. /* pass NULL as output */
  3885. AssertIntEQ(
  3886. EVP_DecodeUpdate(
  3887. ctx,
  3888. NULL, /* pass NULL as out buff */
  3889. &outl,
  3890. enc1,
  3891. sizeof(enc1)-1),
  3892. -1 /* expected result code -1: fail */
  3893. );
  3894. AssertIntEQ( outl, 0);
  3895. /* pass NULL as outl */
  3896. AssertIntEQ(
  3897. EVP_DecodeUpdate(
  3898. ctx,
  3899. decOutBuff,
  3900. NULL, /* pass NULL as outl */
  3901. enc1,
  3902. sizeof(enc1)-1),
  3903. -1 /* expected result code -1: fail */
  3904. );
  3905. /* pass NULL as input */
  3906. AssertIntEQ(
  3907. EVP_DecodeUpdate(
  3908. ctx,
  3909. decOutBuff,
  3910. &outl,
  3911. NULL, /* pass NULL as in */
  3912. sizeof(enc1)-1),
  3913. -1 /* expected result code -1: fail */
  3914. );
  3915. AssertIntEQ( outl, 0);
  3916. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  3917. /* pass zero length input */
  3918. AssertIntEQ(
  3919. EVP_DecodeUpdate(
  3920. ctx,
  3921. decOutBuff,
  3922. &outl,
  3923. enc1,
  3924. 0), /* pass zero as input len */
  3925. 1 /* expected result code 1: success */
  3926. );
  3927. /* decode correct base64 string */
  3928. {
  3929. static const unsigned char enc2[] =
  3930. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3931. static const unsigned char plain2[] =
  3932. {"This is a base64 decoding test."};
  3933. EVP_EncodeInit(ctx);
  3934. AssertIntEQ(
  3935. EVP_DecodeUpdate(
  3936. ctx,
  3937. decOutBuff,
  3938. &outl,
  3939. enc2,
  3940. sizeof(enc2)-1),
  3941. 0 /* expected result code 0: success */
  3942. );
  3943. AssertIntEQ(outl,sizeof(plain2) -1);
  3944. AssertIntEQ(
  3945. EVP_DecodeFinal(
  3946. ctx,
  3947. decOutBuff + outl,
  3948. &outl),
  3949. 1 /* expected result code 1: success */
  3950. );
  3951. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  3952. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3953. sizeof(plain2) -1 ),0);
  3954. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  3955. sizeof(plain2)-1);
  3956. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3957. sizeof(plain2) -1 ),0);
  3958. }
  3959. /* decode correct base64 string which does not have '\n' in its last*/
  3960. {
  3961. static const unsigned char enc3[] =
  3962. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  3963. static const unsigned char plain3[] =
  3964. {"This is a base64 decoding test."}; /* 31 chars */
  3965. EVP_EncodeInit(ctx);
  3966. AssertIntEQ(
  3967. EVP_DecodeUpdate(
  3968. ctx,
  3969. decOutBuff,
  3970. &outl,
  3971. enc3,
  3972. sizeof(enc3)-1),
  3973. 0 /* expected result code 0: success */
  3974. );
  3975. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  3976. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3977. sizeof(plain3) -1 ),0);
  3978. AssertIntEQ(
  3979. EVP_DecodeFinal(
  3980. ctx,
  3981. decOutBuff + outl,
  3982. &outl),
  3983. 1 /* expected result code 1: success */
  3984. );
  3985. AssertIntEQ(outl,0 );
  3986. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  3987. sizeof(plain3)-1);
  3988. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3989. sizeof(plain3) -1 ),0);
  3990. }
  3991. /* decode string which has a padding char ('=') in the illegal position*/
  3992. {
  3993. static const unsigned char enc4[] =
  3994. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  3995. EVP_EncodeInit(ctx);
  3996. AssertIntEQ(
  3997. EVP_DecodeUpdate(
  3998. ctx,
  3999. decOutBuff,
  4000. &outl,
  4001. enc4,
  4002. sizeof(enc4)-1),
  4003. -1 /* expected result code -1: error */
  4004. );
  4005. AssertIntEQ(outl,0);
  4006. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4007. }
  4008. /* small data decode test */
  4009. {
  4010. static const unsigned char enc00[] = {"VG"};
  4011. static const unsigned char enc01[] = {"g=\n"};
  4012. static const unsigned char plain4[] = {"Th"};
  4013. EVP_EncodeInit(ctx);
  4014. AssertIntEQ(
  4015. EVP_DecodeUpdate(
  4016. ctx,
  4017. decOutBuff,
  4018. &outl,
  4019. enc00,
  4020. sizeof(enc00)-1),
  4021. 1 /* expected result code 1: success */
  4022. );
  4023. AssertIntEQ(outl,0);
  4024. AssertIntEQ(
  4025. EVP_DecodeUpdate(
  4026. ctx,
  4027. decOutBuff + outl,
  4028. &outl,
  4029. enc01,
  4030. sizeof(enc01)-1),
  4031. 0 /* expected result code 0: success */
  4032. );
  4033. AssertIntEQ(outl,sizeof(plain4)-1);
  4034. /* test with illegal parameters */
  4035. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4036. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4037. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4038. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4039. EVP_DecodeFinal(
  4040. ctx,
  4041. decOutBuff + outl,
  4042. &outl);
  4043. AssertIntEQ( outl, 0);
  4044. AssertIntEQ(
  4045. XSTRNCMP(
  4046. (const char*)decOutBuff,
  4047. (const char*)plain4,sizeof(plain4)-1 ),
  4048. 0);
  4049. }
  4050. EVP_ENCODE_CTX_free(ctx);
  4051. res = TEST_RES_CHECK(1);
  4052. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4053. return res;
  4054. }
  4055. static int test_wolfSSL_EVP_DecodeFinal(void)
  4056. {
  4057. int res = TEST_SKIPPED;
  4058. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4059. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4060. * test_wolfSSL_EVP_DecodeUpdate
  4061. */
  4062. res = TEST_RES_CHECK(1);
  4063. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4064. return res;
  4065. }
  4066. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4067. */
  4068. #ifdef OPENSSL_EXTRA
  4069. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4070. {
  4071. #ifndef NO_AES
  4072. const WOLFSSL_EVP_CIPHER* c;
  4073. c = wolfSSL_EVP_get_cipherbynid(419);
  4074. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4075. defined(WOLFSSL_AES_128)
  4076. AssertNotNull(c);
  4077. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  4078. #else
  4079. AssertNull(c);
  4080. #endif
  4081. c = wolfSSL_EVP_get_cipherbynid(423);
  4082. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4083. defined(WOLFSSL_AES_192)
  4084. AssertNotNull(c);
  4085. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  4086. #else
  4087. AssertNull(c);
  4088. #endif
  4089. c = wolfSSL_EVP_get_cipherbynid(427);
  4090. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4091. defined(WOLFSSL_AES_256)
  4092. AssertNotNull(c);
  4093. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  4094. #else
  4095. AssertNull(c);
  4096. #endif
  4097. c = wolfSSL_EVP_get_cipherbynid(904);
  4098. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4099. AssertNotNull(c);
  4100. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  4101. #else
  4102. AssertNull(c);
  4103. #endif
  4104. c = wolfSSL_EVP_get_cipherbynid(905);
  4105. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4106. AssertNotNull(c);
  4107. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  4108. #else
  4109. AssertNull(c);
  4110. #endif
  4111. c = wolfSSL_EVP_get_cipherbynid(906);
  4112. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4113. AssertNotNull(c);
  4114. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  4115. #else
  4116. AssertNull(c);
  4117. #endif
  4118. c = wolfSSL_EVP_get_cipherbynid(418);
  4119. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4120. AssertNotNull(c);
  4121. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  4122. #else
  4123. AssertNull(c);
  4124. #endif
  4125. c = wolfSSL_EVP_get_cipherbynid(422);
  4126. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4127. AssertNotNull(c);
  4128. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  4129. #else
  4130. AssertNull(c);
  4131. #endif
  4132. c = wolfSSL_EVP_get_cipherbynid(426);
  4133. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4134. AssertNotNull(c);
  4135. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  4136. #else
  4137. AssertNull(c);
  4138. #endif
  4139. #endif /* !NO_AES */
  4140. #ifndef NO_DES3
  4141. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4142. #ifdef WOLFSSL_DES_ECB
  4143. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4144. #endif
  4145. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4146. #ifdef WOLFSSL_DES_ECB
  4147. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4148. #endif
  4149. #endif /* !NO_DES3 */
  4150. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4151. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4152. #endif
  4153. /* test for nid is out of range */
  4154. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  4155. return TEST_RES_CHECK(1);
  4156. }
  4157. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4158. {
  4159. int res = TEST_SKIPPED;
  4160. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4161. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4162. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4163. const EVP_CIPHER *test;
  4164. byte key[AES_BLOCK_SIZE] = {0};
  4165. byte iv[AES_BLOCK_SIZE] = {0};
  4166. AssertNotNull(ctx);
  4167. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4168. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4169. test = EVP_CIPHER_CTX_cipher(ctx);
  4170. AssertTrue(init == test);
  4171. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4172. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4173. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4174. EVP_CIPHER_CTX_free(ctx);
  4175. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4176. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4177. res = TEST_RES_CHECK(1);
  4178. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4179. return res;
  4180. }
  4181. #endif /* OPENSSL_EXTRA */
  4182. /*----------------------------------------------------------------------------*
  4183. | IO
  4184. *----------------------------------------------------------------------------*/
  4185. /* helper functions */
  4186. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  4187. #ifdef WOLFSSL_SESSION_EXPORT
  4188. #ifdef WOLFSSL_DTLS
  4189. /* set up function for sending session information */
  4190. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  4191. {
  4192. WOLFSSL_CTX* ctx = NULL;
  4193. WOLFSSL* ssl = NULL;
  4194. AssertNotNull(inSsl);
  4195. AssertNotNull(buf);
  4196. AssertIntNE(0, sz);
  4197. /* Set ctx to DTLS 1.2 */
  4198. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  4199. AssertNotNull(ctx);
  4200. ssl = wolfSSL_new(ctx);
  4201. AssertNotNull(ssl);
  4202. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  4203. wolfSSL_free(ssl);
  4204. wolfSSL_CTX_free(ctx);
  4205. (void)userCtx;
  4206. return 0;
  4207. }
  4208. #endif
  4209. /* returns negative value on fail and positive (including 0) on success */
  4210. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  4211. {
  4212. int ret, err, loop_count, count, timeout = 10;
  4213. char msg[] = "I hear you fa shizzle!";
  4214. char input[1024];
  4215. loop_count = ((func_args*)args)->argc;
  4216. #ifdef WOLFSSL_ASYNC_CRYPT
  4217. err = 0; /* Reset error */
  4218. #endif
  4219. do {
  4220. #ifdef WOLFSSL_ASYNC_CRYPT
  4221. if (err == WC_PENDING_E) {
  4222. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4223. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4224. }
  4225. #endif
  4226. ret = wolfSSL_accept(ssl);
  4227. err = wolfSSL_get_error(ssl, 0);
  4228. if (err == WOLFSSL_ERROR_WANT_READ ||
  4229. err == WOLFSSL_ERROR_WANT_WRITE) {
  4230. int select_ret;
  4231. err = WC_PENDING_E;
  4232. select_ret = tcp_select(*sockfd, timeout);
  4233. if (select_ret == TEST_TIMEOUT) {
  4234. return WOLFSSL_FATAL_ERROR;
  4235. }
  4236. }
  4237. } while (err == WC_PENDING_E);
  4238. if (ret != WOLFSSL_SUCCESS) {
  4239. char buff[WOLFSSL_MAX_ERROR_SZ];
  4240. fprintf(stderr, "error = %d, %s\n", err,
  4241. wolfSSL_ERR_error_string(err, buff));
  4242. return ret;
  4243. }
  4244. for (count = 0; count < loop_count; count++) {
  4245. int select_ret;
  4246. select_ret = tcp_select(*sockfd, timeout);
  4247. if (select_ret == TEST_TIMEOUT) {
  4248. ret = WOLFSSL_FATAL_ERROR;
  4249. break;
  4250. }
  4251. do {
  4252. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4253. if (ret > 0) {
  4254. input[ret] = '\0';
  4255. fprintf(stderr, "Client message: %s\n", input);
  4256. }
  4257. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4258. do {
  4259. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  4260. return WOLFSSL_FATAL_ERROR;
  4261. }
  4262. err = wolfSSL_get_error(ssl, ret);
  4263. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4264. }
  4265. return ret;
  4266. }
  4267. #endif /* WOLFSSL_SESSION_EXPORT */
  4268. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4269. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4270. defined(HAVE_AES_CBC)
  4271. typedef struct openssl_key_ctx {
  4272. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4273. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4274. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4275. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4276. } openssl_key_ctx;
  4277. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4278. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4279. static WC_INLINE int OpenSSLTicketInit(void)
  4280. {
  4281. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4282. if (ret != 0) return ret;
  4283. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4284. sizeof(myOpenSSLKey_ctx.name));
  4285. if (ret != 0) return ret;
  4286. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4287. sizeof(myOpenSSLKey_ctx.key));
  4288. if (ret != 0) return ret;
  4289. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4290. sizeof(myOpenSSLKey_ctx.hmacKey));
  4291. if (ret != 0) return ret;
  4292. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4293. sizeof(myOpenSSLKey_ctx.iv));
  4294. if (ret != 0) return ret;
  4295. return 0;
  4296. }
  4297. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4298. byte name[WOLFSSL_TICKET_NAME_SZ],
  4299. byte iv[WOLFSSL_TICKET_IV_SZ],
  4300. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4301. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4302. (void)ssl;
  4303. if (enc) {
  4304. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4305. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4306. }
  4307. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4308. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4309. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4310. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4311. return 0;
  4312. }
  4313. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4314. if (enc)
  4315. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4316. else
  4317. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4318. return 1;
  4319. }
  4320. static WC_INLINE void OpenSSLTicketCleanup(void)
  4321. {
  4322. wc_FreeRng(&myOpenSSLKey_rng);
  4323. }
  4324. #endif
  4325. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4326. #ifdef WC_SHA512_DIGEST_SIZE
  4327. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4328. #else
  4329. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4330. #endif
  4331. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4332. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4333. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4334. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4335. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4336. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  4337. {
  4338. SOCKET_T sockfd = 0;
  4339. SOCKET_T clientfd = 0;
  4340. word16 port;
  4341. callback_functions* cbf;
  4342. WOLFSSL_CTX* ctx = 0;
  4343. WOLFSSL* ssl = 0;
  4344. func_args* opts = (func_args*)args;
  4345. char msg[] = "I hear you fa shizzle!";
  4346. char input[1024];
  4347. int idx;
  4348. int ret, err = 0;
  4349. int sharedCtx = 0;
  4350. int doUdp = 0;
  4351. SOCKADDR_IN_T cliAddr;
  4352. socklen_t cliLen;
  4353. const char* certFile = svrCertFile;
  4354. const char* keyFile = svrKeyFile;
  4355. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4356. size_t msg_len = 0;
  4357. #endif
  4358. #ifdef WOLFSSL_TIRTOS
  4359. fdOpenSession(Task_self());
  4360. #endif
  4361. opts->return_code = TEST_FAIL;
  4362. cbf = opts->callbacks;
  4363. if (cbf != NULL && cbf->ctx) {
  4364. ctx = cbf->ctx;
  4365. sharedCtx = 1;
  4366. }
  4367. else
  4368. {
  4369. WOLFSSL_METHOD* method = NULL;
  4370. if (cbf != NULL && cbf->method != NULL) {
  4371. method = cbf->method();
  4372. }
  4373. else {
  4374. method = wolfSSLv23_server_method();
  4375. }
  4376. ctx = wolfSSL_CTX_new(method);
  4377. }
  4378. if (ctx == NULL) {
  4379. goto done;
  4380. }
  4381. if (cbf == NULL || !cbf->ticNoInit) {
  4382. #if defined(HAVE_SESSION_TICKET) && \
  4383. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4384. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4385. OpenSSLTicketInit();
  4386. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4387. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4388. TicketInit();
  4389. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4390. #endif
  4391. #endif
  4392. }
  4393. #if defined(USE_WINDOWS_API)
  4394. port = opts->signal->port;
  4395. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4396. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4397. /* Let tcp_listen assign port */
  4398. port = 0;
  4399. #else
  4400. /* Use default port */
  4401. port = wolfSSLPort;
  4402. #endif
  4403. if (cbf != NULL)
  4404. doUdp = cbf->doUdp;
  4405. /* do it here to detect failure */
  4406. tcp_accept(
  4407. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4408. if (doUdp) {
  4409. cliLen = sizeof(cliAddr);
  4410. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4411. (struct sockaddr*)&cliAddr, &cliLen);
  4412. AssertIntGT(idx, 0);
  4413. }
  4414. else {
  4415. CloseSocket(sockfd);
  4416. }
  4417. wolfSSL_CTX_set_verify(ctx,
  4418. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4419. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4420. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4421. #endif
  4422. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4423. != WOLFSSL_SUCCESS) {
  4424. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4425. goto done;
  4426. }
  4427. if (cbf != NULL && cbf->certPemFile != NULL)
  4428. certFile = cbf->certPemFile;
  4429. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4430. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  4431. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4432. #else
  4433. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  4434. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4435. #endif
  4436. /*err_sys("can't load server cert chain file, "
  4437. "Please run from wolfSSL home dir");*/
  4438. goto done;
  4439. }
  4440. if (cbf != NULL && cbf->keyPemFile != NULL)
  4441. keyFile = cbf->keyPemFile;
  4442. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4443. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  4444. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4445. #else
  4446. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  4447. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4448. #endif
  4449. /*err_sys("can't load server key file, "
  4450. "Please run from wolfSSL home dir");*/
  4451. goto done;
  4452. }
  4453. #ifdef HAVE_CRL
  4454. if (cbf != NULL && cbf->crlPemFile != NULL) {
  4455. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  4456. goto done;
  4457. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  4458. != WOLFSSL_SUCCESS)
  4459. goto done;
  4460. }
  4461. #endif
  4462. /* call ctx setup callback */
  4463. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4464. cbf->ctx_ready(ctx);
  4465. }
  4466. ssl = wolfSSL_new(ctx);
  4467. if (ssl == NULL) {
  4468. goto done;
  4469. }
  4470. if (doUdp) {
  4471. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4472. if (err != WOLFSSL_SUCCESS)
  4473. goto done;
  4474. }
  4475. #ifdef WOLFSSL_SESSION_EXPORT
  4476. /* only add in more complex nonblocking case with session export tests */
  4477. if (args && opts->argc > 0) {
  4478. /* set as nonblock and time out for waiting on read/write */
  4479. tcp_set_nonblocking(&clientfd);
  4480. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  4481. }
  4482. #endif
  4483. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4484. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  4485. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4486. #else
  4487. if (wolfSSL_use_certificate_file(ssl, certFile,
  4488. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4489. #endif
  4490. /*err_sys("can't load server cert chain file, "
  4491. "Please run from wolfSSL home dir");*/
  4492. goto done;
  4493. }
  4494. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4495. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  4496. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4497. #else
  4498. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  4499. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4500. #endif
  4501. /*err_sys("can't load server key file, "
  4502. "Please run from wolfSSL home dir");*/
  4503. goto done;
  4504. }
  4505. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4506. /*err_sys("SSL_set_fd failed");*/
  4507. goto done;
  4508. }
  4509. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4510. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4511. #elif !defined(NO_DH)
  4512. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4513. #endif
  4514. /* call ssl setup callback */
  4515. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4516. cbf->ssl_ready(ssl);
  4517. }
  4518. #ifdef WOLFSSL_SESSION_EXPORT
  4519. /* only add in more complex nonblocking case with session export tests */
  4520. if (opts->argc > 0) {
  4521. ret = nonblocking_accept_read(args, ssl, &clientfd);
  4522. if (ret >= 0) {
  4523. opts->return_code = TEST_SUCCESS;
  4524. }
  4525. #ifdef WOLFSSL_TIRTOS
  4526. Task_yield();
  4527. #endif
  4528. goto done;
  4529. }
  4530. #endif
  4531. #ifdef WOLFSSL_ASYNC_CRYPT
  4532. err = 0; /* Reset error */
  4533. #endif
  4534. do {
  4535. #ifdef WOLFSSL_ASYNC_CRYPT
  4536. if (err == WC_PENDING_E) {
  4537. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4538. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4539. }
  4540. #endif
  4541. ret = wolfSSL_accept(ssl);
  4542. err = wolfSSL_get_error(ssl, 0);
  4543. } while (err == WC_PENDING_E);
  4544. if (ret != WOLFSSL_SUCCESS) {
  4545. char buff[WOLFSSL_MAX_ERROR_SZ];
  4546. fprintf(stderr, "error = %d, %s\n", err,
  4547. wolfSSL_ERR_error_string(err, buff));
  4548. /*err_sys("SSL_accept failed");*/
  4549. goto done;
  4550. }
  4551. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4552. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4553. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  4554. AssertIntGE(msg_len, 0);
  4555. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4556. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  4557. AssertIntGE(msg_len, 0);
  4558. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4559. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4560. if (idx > 0) {
  4561. input[idx] = '\0';
  4562. fprintf(stderr, "Client message: %s\n", input);
  4563. }
  4564. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4565. /*err_sys("SSL_write failed");*/
  4566. #ifdef WOLFSSL_TIRTOS
  4567. return;
  4568. #else
  4569. return 0;
  4570. #endif
  4571. }
  4572. if (cbf != NULL && cbf->on_result != NULL)
  4573. cbf->on_result(ssl);
  4574. #ifdef WOLFSSL_TIRTOS
  4575. Task_yield();
  4576. #endif
  4577. opts->return_code = TEST_SUCCESS;
  4578. done:
  4579. if (cbf != NULL)
  4580. cbf->last_err = err;
  4581. if (cbf != NULL && cbf->on_cleanup != NULL)
  4582. cbf->on_cleanup(ssl);
  4583. wolfSSL_shutdown(ssl);
  4584. wolfSSL_free(ssl);
  4585. if (!sharedCtx)
  4586. wolfSSL_CTX_free(ctx);
  4587. CloseSocket(clientfd);
  4588. #ifdef WOLFSSL_TIRTOS
  4589. fdCloseSession(Task_self());
  4590. #endif
  4591. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4592. && defined(HAVE_THREAD_LS)
  4593. wc_ecc_fp_free(); /* free per thread cache */
  4594. #endif
  4595. if (cbf == NULL || !cbf->ticNoInit) {
  4596. #if defined(HAVE_SESSION_TICKET) && \
  4597. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4598. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4599. OpenSSLTicketCleanup();
  4600. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4601. TicketCleanup();
  4602. #endif
  4603. #endif
  4604. }
  4605. #ifndef WOLFSSL_TIRTOS
  4606. return 0;
  4607. #endif
  4608. }
  4609. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4610. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4611. {
  4612. SOCKET_T sockfd = 0;
  4613. SOCKET_T clientfd = 0;
  4614. word16 port;
  4615. callback_functions* cbf;
  4616. WOLFSSL_CTX* ctx = 0;
  4617. WOLFSSL* ssl = 0;
  4618. char msg[] = "I hear you fa shizzle!";
  4619. char input[1024];
  4620. int idx;
  4621. int ret, err = 0;
  4622. int sharedCtx = 0;
  4623. int loop_count = ((func_args*)args)->argc;
  4624. int count = 0;
  4625. #ifdef WOLFSSL_TIRTOS
  4626. fdOpenSession(Task_self());
  4627. #endif
  4628. ((func_args*)args)->return_code = TEST_FAIL;
  4629. cbf = ((func_args*)args)->callbacks;
  4630. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4631. if (cbf != NULL && cbf->ctx) {
  4632. ctx = cbf->ctx;
  4633. sharedCtx = 1;
  4634. }
  4635. else
  4636. #endif
  4637. {
  4638. WOLFSSL_METHOD* method = NULL;
  4639. if (cbf != NULL && cbf->method != NULL) {
  4640. method = cbf->method();
  4641. }
  4642. else {
  4643. method = wolfSSLv23_server_method();
  4644. }
  4645. ctx = wolfSSL_CTX_new(method);
  4646. }
  4647. #if defined(USE_WINDOWS_API)
  4648. port = ((func_args*)args)->signal->port;
  4649. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4650. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4651. /* Let tcp_listen assign port */
  4652. port = 0;
  4653. #else
  4654. /* Use default port */
  4655. port = wolfSSLPort;
  4656. #endif
  4657. wolfSSL_CTX_set_verify(ctx,
  4658. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4659. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4660. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4661. #endif
  4662. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4663. != WOLFSSL_SUCCESS) {
  4664. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4665. goto done;
  4666. }
  4667. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4668. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4669. /*err_sys("can't load server cert chain file, "
  4670. "Please run from wolfSSL home dir");*/
  4671. goto done;
  4672. }
  4673. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4674. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4675. /*err_sys("can't load server key file, "
  4676. "Please run from wolfSSL home dir");*/
  4677. goto done;
  4678. }
  4679. /* call ctx setup callback */
  4680. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4681. cbf->ctx_ready(ctx);
  4682. }
  4683. while (count != loop_count) {
  4684. ssl = wolfSSL_new(ctx);
  4685. if (ssl == NULL) {
  4686. goto done;
  4687. }
  4688. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4689. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4690. /*err_sys("can't load server cert chain file, "
  4691. "Please run from wolfSSL home dir");*/
  4692. goto done;
  4693. }
  4694. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4695. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4696. /*err_sys("can't load server key file, "
  4697. "Please run from wolfSSL home dir");*/
  4698. goto done;
  4699. }
  4700. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4701. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4702. #elif !defined(NO_DH)
  4703. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4704. #endif
  4705. /* call ssl setup callback */
  4706. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4707. cbf->ssl_ready(ssl);
  4708. }
  4709. /* do it here to detect failure */
  4710. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4711. CloseSocket(sockfd);
  4712. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4713. /*err_sys("SSL_set_fd failed");*/
  4714. goto done;
  4715. }
  4716. #ifdef WOLFSSL_ASYNC_CRYPT
  4717. err = 0; /* Reset error */
  4718. #endif
  4719. do {
  4720. #ifdef WOLFSSL_ASYNC_CRYPT
  4721. if (err == WC_PENDING_E) {
  4722. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4723. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4724. }
  4725. #endif
  4726. ret = wolfSSL_accept(ssl);
  4727. err = wolfSSL_get_error(ssl, 0);
  4728. } while (err == WC_PENDING_E);
  4729. if (ret != WOLFSSL_SUCCESS) {
  4730. char buff[WOLFSSL_MAX_ERROR_SZ];
  4731. fprintf(stderr, "error = %d, %s\n", err,
  4732. wolfSSL_ERR_error_string(err, buff));
  4733. /*err_sys("SSL_accept failed");*/
  4734. goto done;
  4735. }
  4736. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4737. if (idx > 0) {
  4738. input[idx] = '\0';
  4739. fprintf(stderr, "Client message: %s\n", input);
  4740. }
  4741. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4742. /*err_sys("SSL_write failed");*/
  4743. #ifdef WOLFSSL_TIRTOS
  4744. return;
  4745. #else
  4746. return 0;
  4747. #endif
  4748. }
  4749. /* free ssl for this connection */
  4750. wolfSSL_shutdown(ssl);
  4751. wolfSSL_free(ssl); ssl = NULL;
  4752. CloseSocket(clientfd);
  4753. count++;
  4754. }
  4755. #ifdef WOLFSSL_TIRTOS
  4756. Task_yield();
  4757. #endif
  4758. ((func_args*)args)->return_code = TEST_SUCCESS;
  4759. done:
  4760. if (ssl != NULL) {
  4761. wolfSSL_shutdown(ssl);
  4762. wolfSSL_free(ssl);
  4763. }
  4764. if (!sharedCtx)
  4765. wolfSSL_CTX_free(ctx);
  4766. CloseSocket(clientfd);
  4767. #ifdef WOLFSSL_TIRTOS
  4768. fdCloseSession(Task_self());
  4769. #endif
  4770. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4771. && defined(HAVE_THREAD_LS)
  4772. wc_ecc_fp_free(); /* free per thread cache */
  4773. #endif
  4774. #ifndef WOLFSSL_TIRTOS
  4775. return 0;
  4776. #endif
  4777. }
  4778. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4779. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4780. static int test_client_nofail(void* args, cbType cb)
  4781. {
  4782. #if !defined(NO_WOLFSSL_CLIENT)
  4783. SOCKET_T sockfd = 0;
  4784. callback_functions* cbf;
  4785. WOLFSSL_CTX* ctx = 0;
  4786. WOLFSSL* ssl = 0;
  4787. WOLFSSL_CIPHER* cipher;
  4788. char msg[64] = "hello wolfssl!";
  4789. char reply[1024];
  4790. int input;
  4791. int msgSz = (int)XSTRLEN(msg);
  4792. int ret, err = 0;
  4793. int cipherSuite;
  4794. int sharedCtx = 0;
  4795. int doUdp = 0;
  4796. const char* cipherName1, *cipherName2;
  4797. #ifdef WOLFSSL_TIRTOS
  4798. fdOpenSession(Task_self());
  4799. #endif
  4800. ((func_args*)args)->return_code = TEST_FAIL;
  4801. cbf = ((func_args*)args)->callbacks;
  4802. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4803. if (cbf != NULL && cbf->ctx) {
  4804. ctx = cbf->ctx;
  4805. sharedCtx = cbf->isSharedCtx;
  4806. }
  4807. else
  4808. #endif
  4809. {
  4810. WOLFSSL_METHOD* method = NULL;
  4811. if (cbf != NULL && cbf->method != NULL) {
  4812. method = cbf->method();
  4813. }
  4814. else {
  4815. method = wolfSSLv23_client_method();
  4816. }
  4817. ctx = wolfSSL_CTX_new(method);
  4818. }
  4819. if (cbf != NULL)
  4820. doUdp = cbf->doUdp;
  4821. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4822. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4823. #endif
  4824. /* Do connect here so server detects failures */
  4825. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4826. doUdp, 0, NULL);
  4827. /* Connect the socket so that we don't have to set the peer later on */
  4828. if (doUdp)
  4829. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  4830. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4831. {
  4832. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4833. goto done;
  4834. }
  4835. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4836. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4837. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4838. #else
  4839. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4840. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4841. #endif
  4842. /*err_sys("can't load client cert file, "
  4843. "Please run from wolfSSL home dir");*/
  4844. goto done;
  4845. }
  4846. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4847. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4848. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4849. #else
  4850. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4851. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4852. #endif
  4853. /*err_sys("can't load client key file, "
  4854. "Please run from wolfSSL home dir");*/
  4855. goto done;
  4856. }
  4857. #ifdef HAVE_CRL
  4858. if (cbf != NULL && cbf->crlPemFile != NULL) {
  4859. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  4860. goto done;
  4861. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  4862. != WOLFSSL_SUCCESS)
  4863. goto done;
  4864. }
  4865. #endif
  4866. /* call ctx setup callback */
  4867. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4868. cbf->ctx_ready(ctx);
  4869. }
  4870. ssl = wolfSSL_new(ctx);
  4871. if (ssl == NULL) {
  4872. goto done;
  4873. }
  4874. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4875. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4876. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4877. #else
  4878. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4879. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4880. #endif
  4881. /*err_sys("can't load client cert file, "
  4882. "Please run from wolfSSL home dir");*/
  4883. goto done;
  4884. }
  4885. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4886. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4887. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4888. #else
  4889. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4890. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4891. #endif
  4892. /*err_sys("can't load client key file, "
  4893. "Please run from wolfSSL home dir");*/
  4894. goto done;
  4895. }
  4896. if (!doUdp) {
  4897. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4898. /*err_sys("SSL_set_fd failed");*/
  4899. goto done;
  4900. }
  4901. }
  4902. else {
  4903. #ifdef WOLFSSL_DTLS
  4904. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4905. /*err_sys("SSL_set_fd failed");*/
  4906. goto done;
  4907. }
  4908. #else
  4909. goto done;
  4910. #endif
  4911. }
  4912. /* call ssl setup callback */
  4913. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4914. cbf->ssl_ready(ssl);
  4915. }
  4916. #ifdef WOLFSSL_ASYNC_CRYPT
  4917. err = 0; /* Reset error */
  4918. #endif
  4919. do {
  4920. #ifdef WOLFSSL_ASYNC_CRYPT
  4921. if (err == WC_PENDING_E) {
  4922. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4923. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4924. }
  4925. #endif
  4926. ret = wolfSSL_connect(ssl);
  4927. err = wolfSSL_get_error(ssl, 0);
  4928. } while (err == WC_PENDING_E);
  4929. if (ret != WOLFSSL_SUCCESS) {
  4930. char buff[WOLFSSL_MAX_ERROR_SZ];
  4931. fprintf(stderr, "error = %d, %s\n", err,
  4932. wolfSSL_ERR_error_string(err, buff));
  4933. /*err_sys("SSL_connect failed");*/
  4934. goto done;
  4935. }
  4936. /* test the various get cipher methods */
  4937. /* Internal cipher suite names */
  4938. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  4939. cipherName1 = wolfSSL_get_cipher_name(ssl);
  4940. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  4941. (cipherSuite >> 8), cipherSuite & 0xFF);
  4942. AssertStrEQ(cipherName1, cipherName2);
  4943. /* IANA Cipher Suites Names */
  4944. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  4945. then it's the internal cipher suite name */
  4946. cipher = wolfSSL_get_current_cipher(ssl);
  4947. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  4948. cipherName2 = wolfSSL_get_cipher(ssl);
  4949. AssertStrEQ(cipherName1, cipherName2);
  4950. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  4951. !defined(WOLFSSL_QT)
  4952. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  4953. (cipherSuite >> 8), cipherSuite & 0xFF);
  4954. AssertStrEQ(cipherName1, cipherName2);
  4955. #endif
  4956. if (cb != NULL)
  4957. (cb)(ctx, ssl);
  4958. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4959. /*err_sys("SSL_write failed");*/
  4960. goto done;
  4961. }
  4962. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4963. if (input > 0) {
  4964. reply[input] = '\0';
  4965. fprintf(stderr, "Server response: %s\n", reply);
  4966. }
  4967. if (cbf != NULL && cbf->on_result != NULL)
  4968. cbf->on_result(ssl);
  4969. ((func_args*)args)->return_code = TEST_SUCCESS;
  4970. done:
  4971. if (cbf != NULL)
  4972. cbf->last_err = err;
  4973. if (cbf != NULL && cbf->on_cleanup != NULL)
  4974. cbf->on_cleanup(ssl);
  4975. wolfSSL_free(ssl);
  4976. if (!sharedCtx)
  4977. wolfSSL_CTX_free(ctx);
  4978. CloseSocket(sockfd);
  4979. #ifdef WOLFSSL_TIRTOS
  4980. fdCloseSession(Task_self());
  4981. #endif
  4982. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4983. && defined(HAVE_THREAD_LS)
  4984. wc_ecc_fp_free(); /* free per thread cache */
  4985. #endif
  4986. #else
  4987. (void)args;
  4988. (void)cb;
  4989. #endif /* !NO_WOLFSSL_CLIENT */
  4990. return 0;
  4991. }
  4992. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  4993. callback_functions* server_cb)
  4994. {
  4995. func_args client_args;
  4996. func_args server_args;
  4997. tcp_ready ready;
  4998. THREAD_TYPE serverThread;
  4999. XMEMSET(&client_args, 0, sizeof(func_args));
  5000. XMEMSET(&server_args, 0, sizeof(func_args));
  5001. #ifdef WOLFSSL_TIRTOS
  5002. fdOpenSession(Task_self());
  5003. #endif
  5004. StartTCP();
  5005. InitTcpReady(&ready);
  5006. #if defined(USE_WINDOWS_API)
  5007. /* use RNG to get random port if using windows */
  5008. ready.port = GetRandomPort();
  5009. #endif
  5010. server_args.signal = &ready;
  5011. server_args.callbacks = server_cb;
  5012. client_args.signal = &ready;
  5013. client_args.callbacks = client_cb;
  5014. start_thread(test_server_nofail, &server_args, &serverThread);
  5015. wait_tcp_ready(&server_args);
  5016. test_client_nofail(&client_args, NULL);
  5017. join_thread(serverThread);
  5018. client_cb->return_code = client_args.return_code;
  5019. server_cb->return_code = server_args.return_code;
  5020. FreeTcpReady(&ready);
  5021. #ifdef WOLFSSL_TIRTOS
  5022. fdOpenSession(Task_self());
  5023. #endif
  5024. }
  5025. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5026. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
  5027. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  5028. {
  5029. SOCKET_T sockfd = 0;
  5030. callback_functions* cbf;
  5031. WOLFSSL_CTX* ctx = 0;
  5032. WOLFSSL* ssl = 0;
  5033. WOLFSSL_SESSION* session = NULL;
  5034. char msg[64] = "hello wolfssl!";
  5035. char reply[1024];
  5036. int input;
  5037. int msgSz = (int)XSTRLEN(msg);
  5038. int ret, err = 0;
  5039. int sharedCtx = 0;
  5040. #ifdef WOLFSSL_TIRTOS
  5041. fdOpenSession(Task_self());
  5042. #endif
  5043. ((func_args*)args)->return_code = TEST_FAIL;
  5044. cbf = ((func_args*)args)->callbacks;
  5045. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5046. if (cbf != NULL && cbf->ctx) {
  5047. ctx = cbf->ctx;
  5048. sharedCtx = 1;
  5049. }
  5050. else
  5051. #endif
  5052. {
  5053. WOLFSSL_METHOD* method = NULL;
  5054. if (cbf != NULL && cbf->method != NULL) {
  5055. method = cbf->method();
  5056. }
  5057. else {
  5058. method = wolfSSLv23_client_method();
  5059. }
  5060. ctx = wolfSSL_CTX_new(method);
  5061. }
  5062. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5063. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5064. #endif
  5065. /* Do connect here so server detects failures */
  5066. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5067. 0, 0, NULL);
  5068. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5069. {
  5070. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5071. goto done;
  5072. }
  5073. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5074. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5075. /*err_sys("can't load client cert file, "
  5076. "Please run from wolfSSL home dir");*/
  5077. goto done;
  5078. }
  5079. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5080. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5081. /*err_sys("can't load client key file, "
  5082. "Please run from wolfSSL home dir");*/
  5083. goto done;
  5084. }
  5085. /* call ctx setup callback */
  5086. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5087. cbf->ctx_ready(ctx);
  5088. }
  5089. ssl = wolfSSL_new(ctx);
  5090. if (ssl == NULL) {
  5091. goto done;
  5092. }
  5093. /* keep handshakre resources for re-using WOLFSSL obj */
  5094. wolfSSL_KeepArrays(ssl);
  5095. if (wolfSSL_KeepHandshakeResources(ssl)) {
  5096. /* err_sys("SSL_KeepHandshakeResources failed"); */
  5097. goto done;
  5098. }
  5099. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5100. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5101. /*err_sys("can't load client cert file, "
  5102. "Please run from wolfSSL home dir");*/
  5103. goto done;
  5104. }
  5105. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5106. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5107. /*err_sys("can't load client key file, "
  5108. "Please run from wolfSSL home dir");*/
  5109. goto done;
  5110. }
  5111. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5112. /*err_sys("SSL_set_fd failed");*/
  5113. goto done;
  5114. }
  5115. /* call ssl setup callback */
  5116. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5117. cbf->ssl_ready(ssl);
  5118. }
  5119. #ifdef WOLFSSL_ASYNC_CRYPT
  5120. err = 0; /* Reset error */
  5121. #endif
  5122. do {
  5123. #ifdef WOLFSSL_ASYNC_CRYPT
  5124. if (err == WC_PENDING_E) {
  5125. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5126. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5127. }
  5128. #endif
  5129. ret = wolfSSL_connect(ssl);
  5130. err = wolfSSL_get_error(ssl, 0);
  5131. } while (err == WC_PENDING_E);
  5132. if (ret != WOLFSSL_SUCCESS) {
  5133. char buff[WOLFSSL_MAX_ERROR_SZ];
  5134. fprintf(stderr, "error = %d, %s\n", err,
  5135. wolfSSL_ERR_error_string(err, buff));
  5136. /*err_sys("SSL_connect failed");*/
  5137. goto done;
  5138. }
  5139. /* Build first session */
  5140. if (cb != NULL)
  5141. ((cbType)cb)(ctx, ssl);
  5142. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5143. /*err_sys("SSL_write failed");*/
  5144. goto done;
  5145. }
  5146. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5147. if (input > 0) {
  5148. reply[input] = '\0';
  5149. fprintf(stderr, "Server response: %s\n", reply);
  5150. }
  5151. /* Session Resumption by re-using WOLFSSL object */
  5152. wolfSSL_set_quiet_shutdown(ssl, 1);
  5153. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  5154. /* err_sys ("SSL shutdown failed"); */
  5155. goto done;
  5156. }
  5157. session = wolfSSL_get1_session(ssl);
  5158. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  5159. /* err_sys ("SSL_clear failed"); */
  5160. goto done;
  5161. }
  5162. wolfSSL_set_session(ssl, session);
  5163. wolfSSL_SESSION_free(session);
  5164. session = NULL;
  5165. /* close socket once */
  5166. CloseSocket(sockfd);
  5167. sockfd = 0;
  5168. /* wait until server ready */
  5169. wait_tcp_ready((func_args*)server_args);
  5170. fprintf(stderr, "session resumption\n");
  5171. /* Do re-connect */
  5172. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5173. 0, 0, NULL);
  5174. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5175. /*err_sys("SSL_set_fd failed");*/
  5176. goto done;
  5177. }
  5178. #ifdef WOLFSSL_ASYNC_CRYPT
  5179. err = 0; /* Reset error */
  5180. #endif
  5181. do {
  5182. #ifdef WOLFSSL_ASYNC_CRYPT
  5183. if (err == WC_PENDING_E) {
  5184. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5185. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5186. }
  5187. #endif
  5188. ret = wolfSSL_connect(ssl);
  5189. err = wolfSSL_get_error(ssl, 0);
  5190. } while (err == WC_PENDING_E);
  5191. if (ret != WOLFSSL_SUCCESS) {
  5192. char buff[WOLFSSL_MAX_ERROR_SZ];
  5193. fprintf(stderr, "error = %d, %s\n", err,
  5194. wolfSSL_ERR_error_string(err, buff));
  5195. /*err_sys("SSL_connect failed");*/
  5196. goto done;
  5197. }
  5198. /* Build first session */
  5199. if (cb != NULL)
  5200. ((cbType)cb)(ctx, ssl);
  5201. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5202. /*err_sys("SSL_write failed");*/
  5203. goto done;
  5204. }
  5205. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5206. if (input > 0) {
  5207. reply[input] = '\0';
  5208. fprintf(stderr, "Server response: %s\n", reply);
  5209. }
  5210. ((func_args*)args)->return_code = TEST_SUCCESS;
  5211. done:
  5212. wolfSSL_free(ssl);
  5213. if (!sharedCtx)
  5214. wolfSSL_CTX_free(ctx);
  5215. CloseSocket(sockfd);
  5216. #ifdef WOLFSSL_TIRTOS
  5217. fdCloseSession(Task_self());
  5218. #endif
  5219. return;
  5220. }
  5221. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  5222. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  5223. static int test_client_verifyDepth(void* args)
  5224. {
  5225. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5226. SOCKET_T sockfd = 0;
  5227. callback_functions* cbf;
  5228. WOLFSSL_CTX* ctx = 0;
  5229. WOLFSSL* ssl = 0;
  5230. char msg[64] = "hello wolfssl!";
  5231. char reply[1024];
  5232. int input;
  5233. int msgSz = (int)XSTRLEN(msg);
  5234. int ret, err = 0;
  5235. int verify_depth = ((func_args*)args)->argc;
  5236. ((func_args*)args)->return_code = TEST_FAIL;
  5237. cbf = ((func_args*)args)->callbacks;
  5238. {
  5239. WOLFSSL_METHOD* method = NULL;
  5240. if (cbf != NULL && cbf->method != NULL) {
  5241. method = cbf->method();
  5242. }
  5243. else {
  5244. method = wolfSSLv23_client_method();
  5245. }
  5246. ctx = wolfSSL_CTX_new(method);
  5247. }
  5248. /* Do connect here so server detects failures */
  5249. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5250. 0, 0, NULL);
  5251. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  5252. != WOLFSSL_SUCCESS)
  5253. {
  5254. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5255. goto done;
  5256. }
  5257. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5258. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5259. /*err_sys("can't load client cert file, "
  5260. "Please run from wolfSSL home dir");*/
  5261. goto done;
  5262. }
  5263. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5264. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5265. /*err_sys("can't load client key file, "
  5266. "Please run from wolfSSL home dir");*/
  5267. goto done;
  5268. }
  5269. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  5270. /* set verify depth */
  5271. if (verify_depth == 0) {
  5272. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  5273. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5274. }
  5275. else if (verify_depth == -1) {
  5276. myVerifyAction = VERIFY_USE_PREVERFIY;
  5277. SSL_CTX_set_verify_depth(ctx, 0);
  5278. }
  5279. else if (verify_depth > 0) {
  5280. myVerifyAction = VERIFY_USE_PREVERFIY;
  5281. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5282. }
  5283. ssl = wolfSSL_new(ctx);
  5284. if (ssl == NULL) {
  5285. goto done;
  5286. }
  5287. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5288. /*err_sys("SSL_set_fd failed");*/
  5289. goto done;
  5290. }
  5291. #ifdef WOLFSSL_ASYNC_CRYPT
  5292. err = 0; /* Reset error */
  5293. #endif
  5294. do {
  5295. #ifdef WOLFSSL_ASYNC_CRYPT
  5296. if (err == WC_PENDING_E) {
  5297. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5298. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5299. }
  5300. #endif
  5301. ret = wolfSSL_connect(ssl);
  5302. err = wolfSSL_get_error(ssl, 0);
  5303. } while (err == WC_PENDING_E);
  5304. if (ret != WOLFSSL_SUCCESS) {
  5305. char buff[WOLFSSL_MAX_ERROR_SZ];
  5306. fprintf(stderr, "error = %d, %s\n", err,
  5307. wolfSSL_ERR_error_string(err, buff));
  5308. goto done;
  5309. }
  5310. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5311. goto done;
  5312. }
  5313. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5314. if (input > 0) {
  5315. reply[input] = '\0';
  5316. fprintf(stderr, "Server response: %s\n", reply);
  5317. }
  5318. ((func_args*)args)->return_code = TEST_SUCCESS;
  5319. done:
  5320. wolfSSL_free(ssl);
  5321. wolfSSL_CTX_free(ctx);
  5322. CloseSocket(sockfd);
  5323. #else
  5324. (void)args;
  5325. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  5326. return 0;
  5327. }
  5328. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5329. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  5330. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  5331. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  5332. #define HAVE_ALPN_PROTOS_SUPPORT
  5333. #endif
  5334. /* Generic TLS client / server with callbacks for API unit tests
  5335. * Used by SNI / ALPN / crypto callback helper functions */
  5336. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5337. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  5338. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  5339. #define ENABLE_TLS_CALLBACK_TEST
  5340. #endif
  5341. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  5342. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  5343. /* TLS server for API unit testing - generic */
  5344. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  5345. {
  5346. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5347. WOLFSSL_CTX* ctx = NULL;
  5348. WOLFSSL* ssl = NULL;
  5349. SOCKET_T sfd = 0;
  5350. SOCKET_T cfd = 0;
  5351. word16 port;
  5352. char msg[] = "I hear you fa shizzle!";
  5353. int len = (int) XSTRLEN(msg);
  5354. char input[1024];
  5355. int idx;
  5356. int ret, err = 0;
  5357. ((func_args*)args)->return_code = TEST_FAIL;
  5358. #ifdef WOLFSSL_STATIC_MEMORY
  5359. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5360. callbacks->memSz > 0) {
  5361. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5362. callbacks->mem, callbacks->memSz, 0, 1);
  5363. if (ret != WOLFSSL_SUCCESS) {
  5364. fprintf(stderr, "CTX static new failed %d\n", ret);
  5365. return 0;
  5366. }
  5367. }
  5368. #else
  5369. if (ctx == NULL) {
  5370. ctx = wolfSSL_CTX_new(callbacks->method());
  5371. }
  5372. if (ctx == NULL) {
  5373. fprintf(stderr, "CTX new failed\n");
  5374. return 0;
  5375. }
  5376. #endif
  5377. /* set defaults */
  5378. if (callbacks->caPemFile == NULL)
  5379. callbacks->caPemFile = cliCertFile;
  5380. if (callbacks->certPemFile == NULL)
  5381. callbacks->certPemFile = svrCertFile;
  5382. if (callbacks->keyPemFile == NULL)
  5383. callbacks->keyPemFile = svrKeyFile;
  5384. #ifdef WOLFSSL_TIRTOS
  5385. fdOpenSession(Task_self());
  5386. #endif
  5387. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5388. #if defined(USE_WINDOWS_API)
  5389. port = ((func_args*)args)->signal->port;
  5390. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5391. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5392. /* Let tcp_listen assign port */
  5393. port = 0;
  5394. #else
  5395. /* Use default port */
  5396. port = wolfSSLPort;
  5397. #endif
  5398. wolfSSL_CTX_set_verify(ctx,
  5399. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5400. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5401. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5402. #endif
  5403. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  5404. if (callbacks->method == wolfDTLSv1_2_server_method) {
  5405. AssertIntEQ(WOLFSSL_SUCCESS,
  5406. wolfSSL_CTX_dtls_set_export(ctx, test_export));
  5407. }
  5408. #endif
  5409. AssertIntEQ(WOLFSSL_SUCCESS,
  5410. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5411. AssertIntEQ(WOLFSSL_SUCCESS,
  5412. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5413. WOLFSSL_FILETYPE_PEM));
  5414. AssertIntEQ(WOLFSSL_SUCCESS,
  5415. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5416. WOLFSSL_FILETYPE_PEM));
  5417. #ifdef HAVE_CRL
  5418. if (callbacks->crlPemFile != NULL) {
  5419. AssertIntEQ(WOLFSSL_SUCCESS,
  5420. wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  5421. WOLFSSL_FILETYPE_PEM));
  5422. }
  5423. #endif
  5424. if (callbacks->ctx_ready)
  5425. callbacks->ctx_ready(ctx);
  5426. ssl = wolfSSL_new(ctx);
  5427. if (ssl == NULL) {
  5428. fprintf(stderr, "SSL new failed\n");
  5429. wolfSSL_CTX_free(ctx);
  5430. return 0;
  5431. }
  5432. if (wolfSSL_dtls(ssl)) {
  5433. SOCKADDR_IN_T cliAddr;
  5434. socklen_t cliLen;
  5435. cliLen = sizeof(cliAddr);
  5436. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  5437. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  5438. (struct sockaddr*)&cliAddr, &cliLen);
  5439. AssertIntGT(idx, 0);
  5440. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5441. }
  5442. else {
  5443. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5444. CloseSocket(sfd);
  5445. }
  5446. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  5447. if (callbacks->loadToSSL) {
  5448. wolfSSL_SetDevId(ssl, callbacks->devId);
  5449. AssertIntEQ(WOLFSSL_SUCCESS,
  5450. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5451. WOLFSSL_FILETYPE_PEM));
  5452. AssertIntEQ(WOLFSSL_SUCCESS,
  5453. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5454. WOLFSSL_FILETYPE_PEM));
  5455. }
  5456. #ifdef NO_PSK
  5457. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5458. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5459. #elif !defined(NO_DH)
  5460. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5461. #endif
  5462. #endif
  5463. if (callbacks->ssl_ready)
  5464. callbacks->ssl_ready(ssl);
  5465. #ifdef WOLFSSL_ASYNC_CRYPT
  5466. err = 0; /* Reset error */
  5467. #endif
  5468. do {
  5469. #ifdef WOLFSSL_ASYNC_CRYPT
  5470. if (err == WC_PENDING_E) {
  5471. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5472. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5473. }
  5474. #endif
  5475. ret = wolfSSL_accept(ssl);
  5476. err = wolfSSL_get_error(ssl, ret);
  5477. } while (err == WC_PENDING_E);
  5478. if (ret != WOLFSSL_SUCCESS) {
  5479. char buff[WOLFSSL_MAX_ERROR_SZ];
  5480. fprintf(stderr, "accept error = %d, %s\n", err,
  5481. wolfSSL_ERR_error_string(err, buff));
  5482. /*err_sys("SSL_accept failed");*/
  5483. }
  5484. else {
  5485. #ifdef WOLFSSL_ASYNC_CRYPT
  5486. err = 0; /* Reset error */
  5487. #endif
  5488. do {
  5489. #ifdef WOLFSSL_ASYNC_CRYPT
  5490. if (err == WC_PENDING_E) {
  5491. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5492. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5493. }
  5494. #endif
  5495. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5496. err = wolfSSL_get_error(ssl, idx);
  5497. } while (err == WC_PENDING_E);
  5498. if (idx > 0) {
  5499. input[idx] = 0;
  5500. fprintf(stderr, "Client message: %s\n", input);
  5501. }
  5502. #ifdef WOLFSSL_ASYNC_CRYPT
  5503. err = 0; /* Reset error */
  5504. #endif
  5505. do {
  5506. #ifdef WOLFSSL_ASYNC_CRYPT
  5507. if (err == WC_PENDING_E) {
  5508. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5509. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5510. }
  5511. #endif
  5512. ret = wolfSSL_write(ssl, msg, len);
  5513. err = wolfSSL_get_error(ssl, ret);
  5514. } while (err == WC_PENDING_E);
  5515. AssertIntEQ(len, ret);
  5516. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  5517. defined(WOLFSSL_DTLS)
  5518. if (wolfSSL_dtls(ssl)) {
  5519. byte* import;
  5520. word32 sz;
  5521. wolfSSL_dtls_export(ssl, NULL, &sz);
  5522. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5523. AssertNotNull(import);
  5524. idx = wolfSSL_dtls_export(ssl, import, &sz);
  5525. AssertIntGE(idx, 0);
  5526. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  5527. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5528. }
  5529. #endif
  5530. #ifdef WOLFSSL_TIRTOS
  5531. Task_yield();
  5532. #endif
  5533. ((func_args*)args)->return_code = TEST_SUCCESS;
  5534. }
  5535. if (callbacks->on_result)
  5536. callbacks->on_result(ssl);
  5537. wolfSSL_shutdown(ssl);
  5538. wolfSSL_free(ssl);
  5539. wolfSSL_CTX_free(ctx);
  5540. CloseSocket(cfd);
  5541. #ifdef WOLFSSL_TIRTOS
  5542. fdCloseSession(Task_self());
  5543. #endif
  5544. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5545. && defined(HAVE_THREAD_LS)
  5546. wc_ecc_fp_free(); /* free per thread cache */
  5547. #endif
  5548. #ifndef WOLFSSL_TIRTOS
  5549. return 0;
  5550. #endif
  5551. }
  5552. /* TLS Client for API unit testing - generic */
  5553. static void run_wolfssl_client(void* args)
  5554. {
  5555. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5556. WOLFSSL_CTX* ctx = NULL;
  5557. WOLFSSL* ssl = NULL;
  5558. SOCKET_T sfd = 0;
  5559. char msg[] = "hello wolfssl server!";
  5560. int len = (int) XSTRLEN(msg);
  5561. char input[1024];
  5562. int ret, err = 0;
  5563. ((func_args*)args)->return_code = TEST_FAIL;
  5564. /* set defaults */
  5565. if (callbacks->caPemFile == NULL)
  5566. callbacks->caPemFile = caCertFile;
  5567. if (callbacks->certPemFile == NULL)
  5568. callbacks->certPemFile = cliCertFile;
  5569. if (callbacks->keyPemFile == NULL)
  5570. callbacks->keyPemFile = cliKeyFile;
  5571. #ifdef WOLFSSL_STATIC_MEMORY
  5572. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5573. callbacks->memSz > 0) {
  5574. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5575. callbacks->mem, callbacks->memSz, 0, 1);
  5576. if (ret != WOLFSSL_SUCCESS) {
  5577. fprintf(stderr, "CTX static new failed %d\n", ret);
  5578. return;
  5579. }
  5580. }
  5581. #else
  5582. if (ctx == NULL) {
  5583. ctx = wolfSSL_CTX_new(callbacks->method());
  5584. }
  5585. if (ctx == NULL) {
  5586. fprintf(stderr, "CTX new failed\n");
  5587. return;
  5588. }
  5589. #endif
  5590. #ifdef WOLFSSL_TIRTOS
  5591. fdOpenSession(Task_self());
  5592. #endif
  5593. if (!callbacks->loadToSSL) {
  5594. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5595. }
  5596. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5597. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5598. #endif
  5599. AssertIntEQ(WOLFSSL_SUCCESS,
  5600. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5601. if (!callbacks->loadToSSL) {
  5602. AssertIntEQ(WOLFSSL_SUCCESS,
  5603. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5604. WOLFSSL_FILETYPE_PEM));
  5605. AssertIntEQ(WOLFSSL_SUCCESS,
  5606. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5607. WOLFSSL_FILETYPE_PEM));
  5608. }
  5609. #ifdef HAVE_CRL
  5610. if (callbacks->crlPemFile != NULL) {
  5611. AssertIntEQ(WOLFSSL_SUCCESS,
  5612. wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  5613. WOLFSSL_FILETYPE_PEM));
  5614. }
  5615. #endif
  5616. if (callbacks->ctx_ready)
  5617. callbacks->ctx_ready(ctx);
  5618. ssl = wolfSSL_new(ctx);
  5619. if (wolfSSL_dtls(ssl)) {
  5620. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5621. 1, 0, ssl);
  5622. }
  5623. else {
  5624. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5625. 0, 0, ssl);
  5626. }
  5627. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  5628. if (callbacks->loadToSSL) {
  5629. wolfSSL_SetDevId(ssl, callbacks->devId);
  5630. AssertIntEQ(WOLFSSL_SUCCESS,
  5631. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5632. WOLFSSL_FILETYPE_PEM));
  5633. AssertIntEQ(WOLFSSL_SUCCESS,
  5634. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5635. WOLFSSL_FILETYPE_PEM));
  5636. }
  5637. if (callbacks->ssl_ready)
  5638. callbacks->ssl_ready(ssl);
  5639. #ifdef WOLFSSL_ASYNC_CRYPT
  5640. err = 0; /* Reset error */
  5641. #endif
  5642. do {
  5643. #ifdef WOLFSSL_ASYNC_CRYPT
  5644. if (err == WC_PENDING_E) {
  5645. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5646. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5647. }
  5648. #endif
  5649. ret = wolfSSL_connect(ssl);
  5650. err = wolfSSL_get_error(ssl, ret);
  5651. } while (err == WC_PENDING_E);
  5652. if (ret != WOLFSSL_SUCCESS) {
  5653. char buff[WOLFSSL_MAX_ERROR_SZ];
  5654. fprintf(stderr, "error = %d, %s\n", err,
  5655. wolfSSL_ERR_error_string(err, buff));
  5656. /*err_sys("SSL_connect failed");*/
  5657. }
  5658. else {
  5659. #ifdef WOLFSSL_ASYNC_CRYPT
  5660. err = 0; /* Reset error */
  5661. #endif
  5662. do {
  5663. #ifdef WOLFSSL_ASYNC_CRYPT
  5664. if (err == WC_PENDING_E) {
  5665. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5666. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5667. }
  5668. #endif
  5669. ret = wolfSSL_write(ssl, msg, len);
  5670. err = wolfSSL_get_error(ssl, ret);
  5671. } while (err == WC_PENDING_E);
  5672. AssertIntEQ(len, ret);
  5673. #ifdef WOLFSSL_ASYNC_CRYPT
  5674. err = 0; /* Reset error */
  5675. #endif
  5676. do {
  5677. #ifdef WOLFSSL_ASYNC_CRYPT
  5678. if (err == WC_PENDING_E) {
  5679. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5680. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5681. }
  5682. #endif
  5683. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5684. err = wolfSSL_get_error(ssl, ret);
  5685. } while (err == WC_PENDING_E);
  5686. if (ret > 0) {
  5687. input[ret] = '\0'; /* null term */
  5688. fprintf(stderr, "Server response: %s\n", input);
  5689. }
  5690. ((func_args*)args)->return_code = TEST_SUCCESS;
  5691. }
  5692. if (callbacks->on_result)
  5693. callbacks->on_result(ssl);
  5694. wolfSSL_free(ssl);
  5695. wolfSSL_CTX_free(ctx);
  5696. CloseSocket(sfd);
  5697. #ifdef WOLFSSL_TIRTOS
  5698. fdCloseSession(Task_self());
  5699. #endif
  5700. }
  5701. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5702. static int test_wolfSSL_read_write(void)
  5703. {
  5704. /* The unit testing for read and write shall happen simultaneously, since
  5705. * one can't do anything with one without the other. (Except for a failure
  5706. * test case.) This function will call all the others that will set up,
  5707. * execute, and report their test findings.
  5708. *
  5709. * Set up the success case first. This function will become the template
  5710. * for the other tests. This should eventually be renamed
  5711. *
  5712. * The success case isn't interesting, how can this fail?
  5713. * - Do not give the client context a CA certificate. The connect should
  5714. * fail. Do not need server for this?
  5715. * - Using NULL for the ssl object on server. Do not need client for this.
  5716. * - Using NULL for the ssl object on client. Do not need server for this.
  5717. * - Good ssl objects for client and server. Client write() without server
  5718. * read().
  5719. * - Good ssl objects for client and server. Server write() without client
  5720. * read().
  5721. * - Forgetting the password callback?
  5722. */
  5723. tcp_ready ready;
  5724. func_args client_args;
  5725. func_args server_args;
  5726. THREAD_TYPE serverThread;
  5727. XMEMSET(&client_args, 0, sizeof(func_args));
  5728. XMEMSET(&server_args, 0, sizeof(func_args));
  5729. #ifdef WOLFSSL_TIRTOS
  5730. fdOpenSession(Task_self());
  5731. #endif
  5732. StartTCP();
  5733. InitTcpReady(&ready);
  5734. #if defined(USE_WINDOWS_API)
  5735. /* use RNG to get random port if using windows */
  5736. ready.port = GetRandomPort();
  5737. #endif
  5738. server_args.signal = &ready;
  5739. client_args.signal = &ready;
  5740. start_thread(test_server_nofail, &server_args, &serverThread);
  5741. wait_tcp_ready(&server_args);
  5742. test_client_nofail(&client_args, NULL);
  5743. join_thread(serverThread);
  5744. AssertTrue(client_args.return_code);
  5745. AssertTrue(server_args.return_code);
  5746. FreeTcpReady(&ready);
  5747. #ifdef WOLFSSL_TIRTOS
  5748. fdOpenSession(Task_self());
  5749. #endif
  5750. return TEST_RES_CHECK(1);
  5751. }
  5752. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  5753. {
  5754. int res = TEST_SKIPPED;
  5755. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5756. !defined(WOLFSSL_TLS13)
  5757. /* The unit test for session resumption by re-using WOLFSSL object.
  5758. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5759. * for second connection.
  5760. */
  5761. tcp_ready ready;
  5762. func_args client_args;
  5763. func_args server_args;
  5764. THREAD_TYPE serverThread;
  5765. XMEMSET(&client_args, 0, sizeof(func_args));
  5766. XMEMSET(&server_args, 0, sizeof(func_args));
  5767. #ifdef WOLFSSL_TIRTOS
  5768. fdOpenSession(Task_self());
  5769. #endif
  5770. StartTCP();
  5771. InitTcpReady(&ready);
  5772. #if defined(USE_WINDOWS_API)
  5773. /* use RNG to get random port if using windows */
  5774. ready.port = GetRandomPort();
  5775. #endif
  5776. server_args.signal = &ready;
  5777. client_args.signal = &ready;
  5778. /* the var is used for loop number */
  5779. server_args.argc = 2;
  5780. start_thread(test_server_loop, &server_args, &serverThread);
  5781. wait_tcp_ready(&server_args);
  5782. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5783. join_thread(serverThread);
  5784. AssertTrue(client_args.return_code);
  5785. AssertTrue(server_args.return_code);
  5786. FreeTcpReady(&ready);
  5787. #ifdef WOLFSSL_TIRTOS
  5788. fdOpenSession(Task_self());
  5789. #endif
  5790. res = TEST_RES_CHECK(1);
  5791. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5792. return res;
  5793. }
  5794. static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5795. {
  5796. int res = TEST_SKIPPED;
  5797. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5798. /* This unit test is to check set verify Depth */
  5799. tcp_ready ready;
  5800. func_args client_args;
  5801. func_args server_args;
  5802. THREAD_TYPE serverThread;
  5803. callback_functions client_cbf;
  5804. XMEMSET(&client_args, 0, sizeof(func_args));
  5805. XMEMSET(&server_args, 0, sizeof(func_args));
  5806. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5807. #ifdef WOLFSSL_TLS13
  5808. client_cbf.method = wolfTLSv1_3_client_method;
  5809. #endif /* WOLFSSL_TLS13 */
  5810. client_args.callbacks = &client_cbf;
  5811. StartTCP();
  5812. InitTcpReady(&ready);
  5813. #if defined(USE_WINDOWS_API)
  5814. /* use RNG to get random port if using windows */
  5815. ready.port = GetRandomPort();
  5816. #endif
  5817. server_args.signal = &ready;
  5818. client_args.signal = &ready;
  5819. /* the var is used for loop number */
  5820. server_args.argc = 1;
  5821. /* test case 1 verify depth is equal to peer chain */
  5822. {
  5823. start_thread(test_server_nofail, &server_args, &serverThread);
  5824. wait_tcp_ready(&server_args);
  5825. /* the var is used for verify depth */
  5826. client_args.argc = 2;
  5827. test_client_verifyDepth(&client_args);
  5828. join_thread(serverThread);
  5829. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5830. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5831. }
  5832. /* test case 2
  5833. * verify depth is zero, number of peer's chain is 2.
  5834. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5835. * callback.
  5836. */
  5837. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5838. {
  5839. start_thread(test_server_nofail, &server_args, &serverThread);
  5840. wait_tcp_ready(&server_args);
  5841. client_args.argc = 0;
  5842. test_client_verifyDepth(&client_args);
  5843. join_thread(serverThread);
  5844. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5845. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5846. }
  5847. /* test case 3
  5848. * verify depth is zero, number of peer's chain is 2
  5849. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5850. * therefore, handshake becomes failure.
  5851. */
  5852. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5853. {
  5854. start_thread(test_server_nofail, &server_args, &serverThread);
  5855. wait_tcp_ready(&server_args);
  5856. client_args.argc = -1;
  5857. test_client_verifyDepth(&client_args);
  5858. join_thread(serverThread);
  5859. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5860. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5861. }
  5862. FreeTcpReady(&ready);
  5863. res = TEST_RES_CHECK(1);
  5864. #else
  5865. (void)test_client_verifyDepth;
  5866. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5867. return res;
  5868. }
  5869. static int test_wolfSSL_CTX_set_cipher_list(void)
  5870. {
  5871. int res = TEST_SKIPPED;
  5872. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5873. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  5874. && !defined(NO_SHA256) && defined(HAVE_ECC)
  5875. WOLFSSL_CTX* ctx;
  5876. WOLFSSL_CTX* ctxClient;
  5877. WOLFSSL* sslClient;
  5878. tcp_ready ready;
  5879. func_args client_args;
  5880. func_args server_args;
  5881. callback_functions client_cb;
  5882. callback_functions server_cb;
  5883. THREAD_TYPE serverThread;
  5884. XMEMSET(&client_args, 0, sizeof(func_args));
  5885. XMEMSET(&server_args, 0, sizeof(func_args));
  5886. StartTCP();
  5887. InitTcpReady(&ready);
  5888. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5889. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5890. AssertNotNull((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())));
  5891. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  5892. AssertIntEQ(WOLFSSL_SUCCESS,
  5893. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5894. AssertIntEQ(WOLFSSL_SUCCESS,
  5895. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5896. AssertIntEQ(WOLFSSL_SUCCESS,
  5897. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5898. AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  5899. AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE-RSA-AES128-SHA256"));
  5900. client_cb.ctx = ctxClient;
  5901. server_cb.ctx = ctx;
  5902. /* we are responsible for free'ing WOLFSSL_CTX */
  5903. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  5904. server_args.signal = &ready;
  5905. server_args.callbacks = &server_cb;
  5906. client_args.signal = &ready;
  5907. client_args.callbacks = &client_cb;
  5908. client_args.return_code = TEST_FAIL;
  5909. start_thread(test_server_nofail, &server_args, &serverThread);
  5910. wait_tcp_ready(&server_args);
  5911. test_client_nofail(&client_args, NULL);
  5912. join_thread(serverThread);
  5913. wolfSSL_CTX_free(client_cb.ctx);
  5914. wolfSSL_CTX_free(server_cb.ctx);
  5915. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5916. FreeTcpReady(&ready);
  5917. /* check with cipher string that has '+' */
  5918. AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  5919. AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  5920. AssertNotNull((sslClient = wolfSSL_new(ctxClient)));
  5921. /* check for the existance of an ECDHE ECDSA cipher suite */
  5922. {
  5923. int i = 0;
  5924. int found = 0;
  5925. const char* suite;
  5926. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk;
  5927. WOLFSSL_CIPHER* current;
  5928. AssertNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  5929. do {
  5930. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  5931. if (current) {
  5932. suite = wolfSSL_CIPHER_get_name(current);
  5933. if (suite && XSTRSTR(suite, "ECDSA")) {
  5934. found = 1;
  5935. break;
  5936. }
  5937. }
  5938. } while (current);
  5939. AssertIntEQ(found, 1);
  5940. }
  5941. wolfSSL_free(sslClient);
  5942. wolfSSL_CTX_free(ctxClient);
  5943. res = TEST_RES_CHECK(1);
  5944. #endif
  5945. return res;
  5946. }
  5947. static int test_client_get_finished(void* args, cbType cb)
  5948. {
  5949. #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
  5950. SOCKET_T sockfd = 0;
  5951. callback_functions* cbf;
  5952. WOLFSSL_CTX* ctx = 0;
  5953. WOLFSSL* ssl = 0;
  5954. char msg[64] = "hello wolfssl!";
  5955. char reply[1024];
  5956. int msgSz = (int)XSTRLEN(msg);
  5957. int ret, err = 0;
  5958. WOLFSSL_METHOD* method = NULL;
  5959. size_t msg_len = 0;
  5960. (void) args;
  5961. (void) cb;
  5962. ((func_args*)args)->return_code = TEST_FAIL;
  5963. cbf = ((func_args*)args)->callbacks;
  5964. if (cbf != NULL && cbf->method != NULL) {
  5965. method = cbf->method();
  5966. }
  5967. else {
  5968. method = wolfSSLv23_client_method();
  5969. }
  5970. ctx = wolfSSL_CTX_new(method);
  5971. /* Do connect here so server detects failures */
  5972. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5973. 0, 0, NULL);
  5974. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5975. {
  5976. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5977. goto done;
  5978. }
  5979. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5980. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5981. goto done;
  5982. }
  5983. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5984. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5985. goto done;
  5986. }
  5987. /* call ctx setup callback */
  5988. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5989. cbf->ctx_ready(ctx);
  5990. }
  5991. ssl = wolfSSL_new(ctx);
  5992. if (ssl == NULL) {
  5993. goto done;
  5994. }
  5995. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5996. goto done;
  5997. }
  5998. /* call ssl setup callback */
  5999. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6000. cbf->ssl_ready(ssl);
  6001. }
  6002. #ifdef WOLFSSL_ASYNC_CRYPT
  6003. err = 0; /* Reset error */
  6004. #endif
  6005. do {
  6006. #ifdef WOLFSSL_ASYNC_CRYPT
  6007. if (err == WC_PENDING_E) {
  6008. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6009. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6010. }
  6011. #endif
  6012. ret = wolfSSL_connect(ssl);
  6013. err = wolfSSL_get_error(ssl, 0);
  6014. } while (err == WC_PENDING_E);
  6015. if (ret != WOLFSSL_SUCCESS) {
  6016. char buff[WOLFSSL_MAX_ERROR_SZ];
  6017. fprintf(stderr, "error = %d, %s\n", err,
  6018. wolfSSL_ERR_error_string(err, buff));
  6019. goto done;
  6020. }
  6021. /* get_finished test */
  6022. /* 1. get own sent message */
  6023. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  6024. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  6025. AssertIntGE(msg_len, 0);
  6026. /* 2. get peer message */
  6027. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  6028. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  6029. AssertIntGE(msg_len, 0);
  6030. if (cb != NULL)
  6031. (cb)(ctx, ssl);
  6032. #ifdef WOLFSSL_ASYNC_CRYPT
  6033. err = 0; /* Reset error */
  6034. #endif
  6035. do {
  6036. #ifdef WOLFSSL_ASYNC_CRYPT
  6037. if (err == WC_PENDING_E) {
  6038. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6039. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6040. }
  6041. #endif
  6042. ret = wolfSSL_write(ssl, msg, msgSz);
  6043. err = wolfSSL_get_error(ssl, 0);
  6044. } while (err == WC_PENDING_E);
  6045. if (ret != msgSz) {
  6046. /*err_sys("SSL_write failed");*/
  6047. goto done;
  6048. }
  6049. #ifdef WOLFSSL_ASYNC_CRYPT
  6050. err = 0; /* Reset error */
  6051. #endif
  6052. do {
  6053. #ifdef WOLFSSL_ASYNC_CRYPT
  6054. if (err == WC_PENDING_E) {
  6055. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6056. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6057. }
  6058. #endif
  6059. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6060. err = wolfSSL_get_error(ssl, 0);
  6061. } while (err == WC_PENDING_E);
  6062. if (ret > 0) {
  6063. reply[ret] = '\0';
  6064. fprintf(stderr, "Server response: %s\n", reply);
  6065. }
  6066. ((func_args*)args)->return_code = TEST_SUCCESS;
  6067. done:
  6068. wolfSSL_free(ssl);
  6069. wolfSSL_CTX_free(ctx);
  6070. CloseSocket(sockfd);
  6071. #else
  6072. (void)args;
  6073. (void)cb;
  6074. #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
  6075. return 0;
  6076. }
  6077. static int test_wolfSSL_get_finished(void)
  6078. {
  6079. int res = TEST_SKIPPED;
  6080. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6081. tcp_ready ready;
  6082. func_args client_args;
  6083. func_args server_args;
  6084. THREAD_TYPE serverThread;
  6085. XMEMSET(&client_args, 0, sizeof(func_args));
  6086. XMEMSET(&server_args, 0, sizeof(func_args));
  6087. StartTCP();
  6088. InitTcpReady(&ready);
  6089. #if defined(USE_WINDOWS_API)
  6090. /* use RNG to get random port if using windows */
  6091. ready.port = GetRandomPort();
  6092. #endif
  6093. server_args.signal = &ready;
  6094. client_args.signal = &ready;
  6095. start_thread(test_server_nofail, &server_args, &serverThread);
  6096. wait_tcp_ready(&server_args);
  6097. test_client_get_finished(&client_args, NULL);
  6098. join_thread(serverThread);
  6099. AssertTrue(client_args.return_code);
  6100. AssertTrue(server_args.return_code);
  6101. /* test received msg vs sent msg */
  6102. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6103. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6104. FreeTcpReady(&ready);
  6105. res = TEST_RES_CHECK(1);
  6106. #else
  6107. (void)test_client_get_finished;
  6108. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  6109. return res;
  6110. }
  6111. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6112. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6113. !defined(NO_SESSION_CACHE)
  6114. /* Sessions to restore/store */
  6115. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6116. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6117. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6118. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6119. {
  6120. /* Don't store sessions. Lookup is still enabled. */
  6121. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6122. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6123. #ifdef OPENSSL_EXTRA
  6124. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6125. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6126. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6127. #endif
  6128. /* Require both peers to provide certs */
  6129. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6130. }
  6131. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6132. {
  6133. WOLFSSL_SESSION** sess;
  6134. if (wolfSSL_is_server(ssl))
  6135. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6136. else
  6137. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6138. if (*sess == NULL) {
  6139. #ifdef NO_SESSION_CACHE_REF
  6140. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6141. #else
  6142. /* Test for backwards compatibility */
  6143. if (wolfSSL_is_server(ssl)) {
  6144. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6145. }
  6146. else {
  6147. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6148. }
  6149. #endif
  6150. /* Now save the session in the internal store to make it available
  6151. * for lookup. For TLS 1.3, we can't save the session without
  6152. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6153. * session from cache. */
  6154. if (wolfSSL_is_server(ssl)
  6155. #ifndef WOLFSSL_TICKET_HAVE_ID
  6156. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6157. #endif
  6158. )
  6159. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6160. *sess), WOLFSSL_SUCCESS);
  6161. }
  6162. else {
  6163. /* If we have a session retrieved then remaining connections should be
  6164. * resuming on that session */
  6165. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6166. }
  6167. /* Save CTX to be able to decrypt tickets */
  6168. if (wolfSSL_is_server(ssl) &&
  6169. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6170. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6171. = wolfSSL_get_SSL_CTX(ssl));
  6172. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6173. WOLFSSL_SUCCESS);
  6174. }
  6175. #ifdef SESSION_CERTS
  6176. #ifndef WOLFSSL_TICKET_HAVE_ID
  6177. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6178. wolfSSL_session_reused(ssl))
  6179. #endif
  6180. {
  6181. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6182. * for all connections. TLS 1.3 only has tickets so if we don't
  6183. * include the session id in the ticket then the certificates
  6184. * will not be available on resumption. */
  6185. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6186. AssertNotNull(peer);
  6187. wolfSSL_X509_free(peer);
  6188. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6189. #ifdef OPENSSL_EXTRA
  6190. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  6191. #endif
  6192. }
  6193. #endif /* SESSION_CERTS */
  6194. }
  6195. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  6196. {
  6197. /* Set the session to reuse for the client */
  6198. AssertIntEQ(wolfSSL_set_session(ssl,
  6199. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  6200. }
  6201. #endif
  6202. static int test_wolfSSL_CTX_add_session(void)
  6203. {
  6204. int res = TEST_SKIPPED;
  6205. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6206. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6207. !defined(NO_SESSION_CACHE)
  6208. tcp_ready ready;
  6209. func_args client_args;
  6210. func_args server_args;
  6211. THREAD_TYPE serverThread;
  6212. callback_functions client_cb;
  6213. callback_functions server_cb;
  6214. method_provider methods[][2] = {
  6215. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6216. !defined(NO_DES3))
  6217. /* Without AES there are almost no ciphersuites available. This leads
  6218. * to no ciphersuites being available and an error. */
  6219. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  6220. #endif
  6221. #ifndef WOLFSSL_NO_TLS12
  6222. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  6223. #endif
  6224. /* Needs the default ticket callback since it is tied to the
  6225. * connection context and this makes it easy to carry over the ticket
  6226. * crypto context between connections */
  6227. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6228. defined(HAVE_SESSION_TICKET)
  6229. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  6230. #endif
  6231. };
  6232. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  6233. size_t i, j;
  6234. for (i = 0; i < methodsLen; i++) {
  6235. /* First run creates a connection while the second+ run will attempt
  6236. * to resume the connection. The trick is that the internal cache
  6237. * is turned off. wolfSSL_CTX_add_session should put the session in
  6238. * the cache anyway. */
  6239. test_wolfSSL_CTX_add_session_client_sess = NULL;
  6240. test_wolfSSL_CTX_add_session_server_sess = NULL;
  6241. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  6242. #ifdef NO_SESSION_CACHE_REF
  6243. for (j = 0; j < 5; j++) {
  6244. #else
  6245. /* The session may be overwritten in this case. Do only one resumption
  6246. * to stop this test from failing intermittently. */
  6247. for (j = 0; j < 2; j++) {
  6248. #endif
  6249. #ifdef WOLFSSL_TIRTOS
  6250. fdOpenSession(Task_self());
  6251. #endif
  6252. StartTCP();
  6253. InitTcpReady(&ready);
  6254. XMEMSET(&client_args, 0, sizeof(func_args));
  6255. XMEMSET(&server_args, 0, sizeof(func_args));
  6256. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6257. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6258. client_cb.method = methods[i][0];
  6259. server_cb.method = methods[i][1];
  6260. server_args.signal = &ready;
  6261. server_args.callbacks = &server_cb;
  6262. client_args.signal = &ready;
  6263. client_args.callbacks = &client_cb;
  6264. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  6265. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  6266. server_cb.isSharedCtx = 1;
  6267. }
  6268. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6269. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6270. if (j != 0)
  6271. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  6272. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6273. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6274. server_cb.ticNoInit = 1; /* Use default builtin */
  6275. start_thread(test_server_nofail, &server_args, &serverThread);
  6276. wait_tcp_ready(&server_args);
  6277. test_client_nofail(&client_args, NULL);
  6278. join_thread(serverThread);
  6279. AssertTrue(client_args.return_code);
  6280. AssertTrue(server_args.return_code);
  6281. FreeTcpReady(&ready);
  6282. }
  6283. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  6284. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  6285. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  6286. }
  6287. res = TEST_RES_CHECK(1);
  6288. #endif
  6289. return res;
  6290. }
  6291. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6292. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6293. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  6294. defined(SESSION_CERTS) && defined(HAVE_SESSION_TICKET) && \
  6295. !defined(TITAN_SESSION_CACHE) && \
  6296. !defined(HUGE_SESSION_CACHE) && \
  6297. !defined(BIG_SESSION_CACHE) && \
  6298. !defined(MEDIUM_SESSION_CACHE)
  6299. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  6300. /* Sessions to restore/store */
  6301. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  6302. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  6303. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  6304. static int twcase_new_session_called = 0;
  6305. static int twcase_remove_session_called = 0;
  6306. static int twcase_get_session_called = 0;
  6307. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  6308. #define SESSION_CACHE_SIZE 33
  6309. typedef struct {
  6310. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  6311. WOLFSSL_SESSION* value;
  6312. } hashTable_entry;
  6313. typedef struct {
  6314. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  6315. size_t capacity; /* size of entries */
  6316. size_t length; /* number of items in the hash table */
  6317. wolfSSL_Mutex htLock; /* lock */
  6318. }hashTable;
  6319. static hashTable server_sessionCache;
  6320. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  6321. {
  6322. int i;
  6323. (void)ssl;
  6324. /*
  6325. * This example uses a hash table.
  6326. * Steps you should take for a non-demo code:
  6327. * - acquire a lock for the file named according to the session id
  6328. * - open the file
  6329. * - encrypt and write the SSL_SESSION object to the file
  6330. * - release the lock
  6331. *
  6332. * Return:
  6333. * 0: The callback does not wish to hold a reference of the sess
  6334. * 1: The callback wants to hold a reference of the sess. The callback is
  6335. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  6336. */
  6337. if (sess == NULL)
  6338. return 0;
  6339. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  6340. return 0;
  6341. }
  6342. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6343. if (server_sessionCache.entries[i].value == NULL) {
  6344. if (sess->haveAltSessionID == 1)
  6345. server_sessionCache.entries[i].key = sess->altSessionID;
  6346. else
  6347. server_sessionCache.entries[i].key = sess->sessionID;
  6348. server_sessionCache.entries[i].value = sess;
  6349. server_sessionCache.length++;
  6350. break;
  6351. }
  6352. }
  6353. ++twcase_new_session_called;
  6354. wc_UnLockMutex(&server_sessionCache.htLock);
  6355. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  6356. twcase_new_session_called);
  6357. return 1;
  6358. }
  6359. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  6360. {
  6361. int i;
  6362. (void)ctx;
  6363. (void)sess;
  6364. if (sess == NULL)
  6365. return;
  6366. /*
  6367. * This example uses a hash table.
  6368. * Steps you should take for a non-demo code:
  6369. * - acquire a lock for the file named according to the session id
  6370. * - remove the file
  6371. * - release the lock
  6372. */
  6373. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  6374. return;
  6375. }
  6376. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6377. if (server_sessionCache.entries[i].key != NULL &&
  6378. XMEMCMP(server_sessionCache.entries[i].key,
  6379. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  6380. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  6381. server_sessionCache.entries[i].value = NULL;
  6382. server_sessionCache.entries[i].key = NULL;
  6383. server_sessionCache.length--;
  6384. break;
  6385. }
  6386. }
  6387. ++twcase_remove_session_called;
  6388. wc_UnLockMutex(&server_sessionCache.htLock);
  6389. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  6390. twcase_remove_session_called);
  6391. }
  6392. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  6393. const unsigned char *id, int len, int *ref)
  6394. {
  6395. int i;
  6396. (void)ssl;
  6397. (void)id;
  6398. (void)len;
  6399. /*
  6400. * This example uses a hash table.
  6401. * Steps you should take for a non-demo code:
  6402. * - acquire a lock for the file named according to the session id in the
  6403. * 2nd arg
  6404. * - read and decrypt contents of file and create a new SSL_SESSION
  6405. * - object release the lock
  6406. * - return the new session object
  6407. */
  6408. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  6409. ++twcase_get_session_called);
  6410. /* This callback want to retain a copy of the object. If we want wolfSSL to
  6411. * be responsible for the pointer then set to 0. */
  6412. *ref = 1;
  6413. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6414. if (server_sessionCache.entries[i].key != NULL &&
  6415. XMEMCMP(server_sessionCache.entries[i].key, id,
  6416. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  6417. return server_sessionCache.entries[i].value;
  6418. }
  6419. }
  6420. return NULL;
  6421. }
  6422. static void twcase_get_sessionCb_cleanup(void)
  6423. {
  6424. int i;
  6425. int cnt = 0;
  6426. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  6427. * for freeing sessions */
  6428. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6429. if (server_sessionCache.entries[i].value != NULL) {
  6430. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  6431. cnt++;
  6432. }
  6433. }
  6434. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  6435. cnt);
  6436. }
  6437. static void twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  6438. {
  6439. /* off - Disable internal cache */
  6440. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6441. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6442. #ifdef OPENSSL_EXTRA
  6443. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6444. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6445. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6446. #endif
  6447. /* off - Donot setup external cache */
  6448. /* Require both peers to provide certs */
  6449. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6450. }
  6451. static void twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  6452. {
  6453. /* on - internal cache is on by default*/
  6454. /* off - Donot setup external cache */
  6455. /* Require both peers to provide certs */
  6456. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6457. }
  6458. static void twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  6459. {
  6460. /* off - Disable internal cache */
  6461. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6462. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6463. #ifdef OPENSSL_EXTRA
  6464. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6465. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6466. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6467. #endif
  6468. /* on - Enable external cache */
  6469. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6470. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6471. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6472. /* Require both peers to provide certs */
  6473. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6474. }
  6475. static void twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  6476. {
  6477. /* on - internal cache is on by default */
  6478. /* on - Enable external cache */
  6479. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6480. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6481. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6482. /* Require both peers to provide certs */
  6483. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6484. }
  6485. static void twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  6486. {
  6487. /* on - internal cache is on by default */
  6488. /* on - Enable external cache */
  6489. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6490. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6491. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6492. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  6493. /* Require both peers to provide certs */
  6494. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6495. }
  6496. static void twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  6497. {
  6498. WOLFSSL_SESSION** sess;
  6499. if (wolfSSL_is_server(ssl))
  6500. sess = &twcase_server_first_session_ptr;
  6501. else
  6502. return;
  6503. if (*sess == NULL) {
  6504. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6505. /* Now save the session in the internal store to make it available
  6506. * for lookup. For TLS 1.3, we can't save the session without
  6507. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6508. * session from cache. */
  6509. if (wolfSSL_is_server(ssl)
  6510. #ifndef WOLFSSL_TICKET_HAVE_ID
  6511. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6512. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  6513. #endif
  6514. ) {
  6515. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6516. *sess), WOLFSSL_SUCCESS);
  6517. }
  6518. }
  6519. /* Save CTX to be able to decrypt tickets */
  6520. if (twcase_server_current_ctx_ptr == NULL) {
  6521. AssertNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  6522. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6523. WOLFSSL_SUCCESS);
  6524. }
  6525. #ifdef SESSION_CERTS
  6526. #ifndef WOLFSSL_TICKET_HAVE_ID
  6527. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6528. wolfSSL_session_reused(ssl))
  6529. #endif
  6530. {
  6531. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6532. * for all connections. TLS 1.3 only has tickets so if we don't
  6533. * include the session id in the ticket then the certificates
  6534. * will not be available on resumption. */
  6535. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6536. AssertNotNull(peer);
  6537. wolfSSL_X509_free(peer);
  6538. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6539. }
  6540. #endif
  6541. }
  6542. static void twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  6543. {
  6544. WOLFSSL_SESSION** sess;
  6545. sess = &twcase_client_first_session_ptr;
  6546. if (*sess == NULL) {
  6547. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6548. }
  6549. else {
  6550. /* If we have a session retrieved then remaining connections should be
  6551. * resuming on that session */
  6552. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6553. }
  6554. #ifdef SESSION_CERTS
  6555. #ifndef WOLFSSL_TICKET_HAVE_ID
  6556. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6557. wolfSSL_session_reused(ssl))
  6558. #endif
  6559. {
  6560. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6561. AssertNotNull(peer);
  6562. wolfSSL_X509_free(peer);
  6563. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6564. #ifdef OPENSSL_EXTRA
  6565. AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
  6566. #endif
  6567. }
  6568. #endif
  6569. }
  6570. static void twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  6571. {
  6572. /* Set the session to reuse for the client */
  6573. AssertNotNull(ssl);
  6574. AssertNotNull(twcase_client_first_session_ptr);
  6575. AssertIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  6576. WOLFSSL_SUCCESS);
  6577. }
  6578. #endif
  6579. static int test_wolfSSL_CTX_add_session_ext(void)
  6580. {
  6581. int res = TEST_SKIPPED;
  6582. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6583. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6584. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  6585. defined(SESSION_CERTS) && defined(HAVE_SESSION_TICKET) && \
  6586. !defined(TITAN_SESSION_CACHE) && \
  6587. !defined(HUGE_SESSION_CACHE) && \
  6588. !defined(BIG_SESSION_CACHE) && \
  6589. !defined(MEDIUM_SESSION_CACHE)
  6590. /* Test the default 33 sessions */
  6591. struct test_params {
  6592. method_provider client_meth;
  6593. method_provider server_meth;
  6594. const char* tls_version;
  6595. } params[] = {
  6596. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6597. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  6598. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  6599. #ifdef WOLFSSL_DTLS13
  6600. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  6601. #endif
  6602. #endif
  6603. #ifndef WOLFSSL_NO_TLS12
  6604. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  6605. #ifdef WOLFSSL_DTLS
  6606. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  6607. #endif
  6608. #endif
  6609. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6610. !defined(NO_DES3))
  6611. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  6612. #ifdef WOLFSSL_DTLS
  6613. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  6614. #endif
  6615. #endif
  6616. };
  6617. const int paramsLen = sizeof(params)/sizeof(*params);
  6618. int i, j;
  6619. /* Clear cache before starting */
  6620. wolfSSL_CTX_flush_sessions(NULL, -1);
  6621. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  6622. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  6623. return BAD_MUTEX_E;
  6624. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  6625. for (i = 0; i < paramsLen; i++) {
  6626. fprintf(stderr, "\tBegin %s\n", params[i].tls_version);
  6627. for (j = 0; j < 5; j++) {
  6628. int tls13 = XSTRSTR(params[i].tls_version, "TLSv1_3") != NULL;
  6629. int dtls = XSTRSTR(params[i].tls_version, "DTLS") != NULL;
  6630. callback_functions client_cb;
  6631. callback_functions server_cb;
  6632. (void)dtls;
  6633. /* Test five cache configurations */
  6634. twcase_client_first_session_ptr = NULL;
  6635. twcase_server_first_session_ptr = NULL;
  6636. twcase_server_current_ctx_ptr = NULL;
  6637. twcase_new_session_called = 0;
  6638. twcase_remove_session_called = 0;
  6639. twcase_get_session_called = 0;
  6640. /* connection 1 - first connection */
  6641. fprintf(stderr, "\tconnect: %s: j=%d, methodsLen=%d\n",
  6642. params[i].tls_version, j, paramsLen);
  6643. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6644. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6645. client_cb.method = params[i].client_meth;
  6646. server_cb.method = params[i].server_meth;
  6647. if (dtls)
  6648. client_cb.doUdp = server_cb.doUdp = 1;
  6649. /* Setup internal and external cache */
  6650. switch (j) {
  6651. case 0:
  6652. /* SSL_OP_NO_TICKET stateful ticket case */
  6653. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  6654. break;
  6655. case 1:
  6656. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  6657. break;
  6658. case 2:
  6659. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  6660. break;
  6661. case 3:
  6662. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  6663. break;
  6664. case 4:
  6665. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  6666. break;
  6667. }
  6668. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  6669. /* Add session to internal cache and save SSL session for testing */
  6670. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  6671. /* Save client SSL session for testing */
  6672. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  6673. server_cb.ticNoInit = 1; /* Use default builtin */
  6674. /* Don't free/release ctx */
  6675. server_cb.ctx = twcase_server_current_ctx_ptr;
  6676. server_cb.isSharedCtx = 1;
  6677. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  6678. AssertTrue(client_cb.return_code);
  6679. AssertTrue(server_cb.return_code);
  6680. AssertIntEQ(twcase_get_session_called, 0);
  6681. switch (j) {
  6682. case 0:
  6683. case 1:
  6684. case 2:
  6685. /* cache cannot be searched with out a connection */
  6686. /* Add a new session */
  6687. AssertIntEQ(twcase_new_session_called, 1);
  6688. /* In twcase_server_sess_ctx_pre_shutdown
  6689. * wolfSSL_CTX_add_session which evicts the existing session
  6690. * in cache and adds it back in */
  6691. AssertIntLE(twcase_remove_session_called, 1);
  6692. break;
  6693. case 3:
  6694. case 4:
  6695. /* no external cache */
  6696. AssertIntEQ(twcase_new_session_called, 0);
  6697. AssertIntEQ(twcase_remove_session_called, 0);
  6698. break;
  6699. }
  6700. /* connection 2 - session resume */
  6701. fprintf(stderr, "\tresume: %s: j=%d, methodsLen=%d\n",
  6702. params[i].tls_version, j, paramsLen);
  6703. twcase_new_session_called = 0;
  6704. twcase_remove_session_called = 0;
  6705. twcase_get_session_called = 0;
  6706. server_cb.on_result = 0;
  6707. client_cb.on_result = 0;
  6708. server_cb.ticNoInit = 1; /* Use default builtin */
  6709. server_cb.ctx = twcase_server_current_ctx_ptr;
  6710. /* try session resumption */
  6711. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  6712. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  6713. AssertTrue(client_cb.return_code);
  6714. AssertTrue(server_cb.return_code);
  6715. /* Clear cache before checking */
  6716. wolfSSL_CTX_flush_sessions(NULL, -1);
  6717. switch (j) {
  6718. case 0:
  6719. if (tls13) {
  6720. /* (D)TLSv1.3 stateful case */
  6721. /* cache hit */
  6722. /* DTLS accesses cache once for stateless parsing and
  6723. * once for stateful parsing */
  6724. AssertIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  6725. /* (D)TLSv1.3 creates a new ticket,
  6726. * updates both internal and external cache */
  6727. AssertIntEQ(twcase_new_session_called, 1);
  6728. AssertIntEQ(twcase_remove_session_called, 1);
  6729. }
  6730. else {
  6731. /* non (D)TLSv1.3 case, no update */
  6732. /* DTLS accesses cache once for stateless parsing and
  6733. * once for stateful parsing */
  6734. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  6735. AssertIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  6736. #else
  6737. AssertIntEQ(twcase_get_session_called, 1);
  6738. #endif
  6739. AssertIntEQ(twcase_new_session_called, 0);
  6740. /* Called on session added in
  6741. * twcase_server_sess_ctx_pre_shutdown */
  6742. AssertIntEQ(twcase_remove_session_called, 1);
  6743. }
  6744. break;
  6745. case 1:
  6746. if (tls13) {
  6747. /* (D)TLSv1.3 case */
  6748. /* cache hit */
  6749. AssertIntEQ(twcase_get_session_called, 1);
  6750. /* (D)TLSv1.3 creates a new ticket,
  6751. * updates both internal and external cache */
  6752. AssertIntEQ(twcase_new_session_called, 1);
  6753. /* Called on session added in
  6754. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  6755. AssertIntEQ(twcase_remove_session_called, 1);
  6756. }
  6757. else {
  6758. /* non (D)TLSv1.3 case */
  6759. /* cache hit */
  6760. /* DTLS accesses cache once for stateless parsing and
  6761. * once for stateful parsing */
  6762. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  6763. AssertIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  6764. #else
  6765. AssertIntEQ(twcase_get_session_called, 1);
  6766. #endif
  6767. AssertIntEQ(twcase_new_session_called, 0);
  6768. /* Called on session added in
  6769. * twcase_server_sess_ctx_pre_shutdown */
  6770. AssertIntEQ(twcase_remove_session_called, 1);
  6771. }
  6772. break;
  6773. case 2:
  6774. if (tls13) {
  6775. /* (D)TLSv1.3 case */
  6776. /* cache hit */
  6777. AssertIntEQ(twcase_get_session_called, 1);
  6778. /* (D)TLSv1.3 creates a new ticket,
  6779. * updates both internal and external cache */
  6780. AssertIntEQ(twcase_new_session_called, 1);
  6781. /* Called on session added in
  6782. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  6783. AssertIntEQ(twcase_remove_session_called, 1);
  6784. }
  6785. else {
  6786. /* non (D)TLSv1.3 case */
  6787. /* cache hit */
  6788. /* DTLS accesses cache once for stateless parsing and
  6789. * once for stateful parsing */
  6790. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  6791. AssertIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  6792. #else
  6793. AssertIntEQ(twcase_get_session_called, 1);
  6794. #endif
  6795. AssertIntEQ(twcase_new_session_called, 0);
  6796. /* Called on session added in
  6797. * twcase_server_sess_ctx_pre_shutdown */
  6798. AssertIntEQ(twcase_remove_session_called, 1);
  6799. }
  6800. break;
  6801. case 3:
  6802. case 4:
  6803. /* no external cache */
  6804. AssertIntEQ(twcase_get_session_called, 0);
  6805. AssertIntEQ(twcase_new_session_called, 0);
  6806. AssertIntEQ(twcase_remove_session_called, 0);
  6807. break;
  6808. }
  6809. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  6810. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  6811. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  6812. }
  6813. twcase_get_sessionCb_cleanup();
  6814. XMEMSET(&server_sessionCache.entries, 0,
  6815. sizeof(server_sessionCache.entries));
  6816. fprintf(stderr, "\tEnd %s\n", params[i].tls_version);
  6817. }
  6818. wc_FreeMutex(&server_sessionCache.htLock);
  6819. res = TEST_RES_CHECK(1);
  6820. #endif
  6821. return res;
  6822. }
  6823. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6824. /* canned export of a session using older version 3 */
  6825. static unsigned char version_3[] = {
  6826. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  6827. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  6828. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6829. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  6830. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6831. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6832. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  6833. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  6834. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  6835. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6836. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6837. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  6838. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6839. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  6840. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  6841. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  6842. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  6843. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6844. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  6845. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  6846. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6847. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6848. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6849. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6850. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x20, 0x05,
  6858. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  6859. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  6860. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  6861. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  6862. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  6863. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  6864. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  6865. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  6866. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  6867. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6868. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  6869. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  6870. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6871. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  6872. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  6873. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  6874. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  6875. 0x2E, 0x31, 0xED, 0x4F
  6876. };
  6877. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  6878. static int test_wolfSSL_dtls_export(void)
  6879. {
  6880. int res = TEST_SKIPPED;
  6881. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6882. tcp_ready ready;
  6883. func_args client_args;
  6884. func_args server_args;
  6885. THREAD_TYPE serverThread;
  6886. callback_functions server_cbf;
  6887. callback_functions client_cbf;
  6888. #ifdef WOLFSSL_TIRTOS
  6889. fdOpenSession(Task_self());
  6890. #endif
  6891. InitTcpReady(&ready);
  6892. #if defined(USE_WINDOWS_API)
  6893. /* use RNG to get random port if using windows */
  6894. ready.port = GetRandomPort();
  6895. #endif
  6896. /* set using dtls */
  6897. XMEMSET(&client_args, 0, sizeof(func_args));
  6898. XMEMSET(&server_args, 0, sizeof(func_args));
  6899. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6900. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6901. server_cbf.method = wolfDTLSv1_2_server_method;
  6902. client_cbf.method = wolfDTLSv1_2_client_method;
  6903. server_args.callbacks = &server_cbf;
  6904. client_args.callbacks = &client_cbf;
  6905. server_args.signal = &ready;
  6906. client_args.signal = &ready;
  6907. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6908. wait_tcp_ready(&server_args);
  6909. run_wolfssl_client(&client_args);
  6910. join_thread(serverThread);
  6911. AssertTrue(client_args.return_code);
  6912. AssertTrue(server_args.return_code);
  6913. FreeTcpReady(&ready);
  6914. #ifdef WOLFSSL_TIRTOS
  6915. fdOpenSession(Task_self());
  6916. #endif
  6917. {
  6918. SOCKET_T sockfd = 0;
  6919. WOLFSSL_CTX* ctx;
  6920. WOLFSSL* ssl;
  6921. char msg[64] = "hello wolfssl!";
  6922. char reply[1024];
  6923. int msgSz = (int)XSTRLEN(msg);
  6924. byte *session, *window;
  6925. unsigned int sessionSz, windowSz;
  6926. #ifndef TEST_IPV6
  6927. struct sockaddr_in peerAddr;
  6928. #else
  6929. struct sockaddr_in6 peerAddr;
  6930. #endif /* TEST_IPV6 */
  6931. int i;
  6932. /* Set ctx to DTLS 1.2 */
  6933. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  6934. AssertNotNull(ssl = wolfSSL_new(ctx));
  6935. /* test importing version 3 */
  6936. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6937. /* test importing bad length and bad version */
  6938. version_3[2] += 1;
  6939. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6940. version_3[2] -= 1; version_3[1] = 0XA0;
  6941. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6942. wolfSSL_free(ssl);
  6943. wolfSSL_CTX_free(ctx);
  6944. /* check storing client state after connection and storing window only */
  6945. #ifdef WOLFSSL_TIRTOS
  6946. fdOpenSession(Task_self());
  6947. #endif
  6948. InitTcpReady(&ready);
  6949. #if defined(USE_WINDOWS_API)
  6950. /* use RNG to get random port if using windows */
  6951. ready.port = GetRandomPort();
  6952. #endif
  6953. /* set using dtls */
  6954. XMEMSET(&server_args, 0, sizeof(func_args));
  6955. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6956. server_cbf.method = wolfDTLSv1_2_server_method;
  6957. server_cbf.doUdp = 1;
  6958. server_args.callbacks = &server_cbf;
  6959. server_args.argc = 3; /* set loop_count to 3 */
  6960. server_args.signal = &ready;
  6961. start_thread(test_server_nofail, &server_args, &serverThread);
  6962. wait_tcp_ready(&server_args);
  6963. /* create and connect with client */
  6964. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  6965. AssertIntEQ(WOLFSSL_SUCCESS,
  6966. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  6967. AssertIntEQ(WOLFSSL_SUCCESS,
  6968. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  6969. AssertIntEQ(WOLFSSL_SUCCESS,
  6970. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  6971. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  6972. AssertNotNull(ssl = wolfSSL_new(ctx));
  6973. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6974. /* store server information connected too */
  6975. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  6976. #ifndef TEST_IPV6
  6977. peerAddr.sin_family = AF_INET;
  6978. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  6979. peerAddr.sin_port = XHTONS(server_args.signal->port);
  6980. #else
  6981. peerAddr.sin6_family = AF_INET6;
  6982. AssertIntEQ(
  6983. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  6984. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  6985. #endif
  6986. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6987. WOLFSSL_SUCCESS);
  6988. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  6989. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  6990. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6991. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  6992. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6993. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6994. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  6995. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6996. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6997. wolfSSL_free(ssl);
  6998. for (i = 1; i < server_args.argc; i++) {
  6999. /* restore state */
  7000. AssertNotNull(ssl = wolfSSL_new(ctx));
  7001. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  7002. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  7003. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  7004. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  7005. WOLFSSL_SUCCESS);
  7006. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7007. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  7008. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7009. wolfSSL_free(ssl);
  7010. }
  7011. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7012. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7013. wolfSSL_CTX_free(ctx);
  7014. fprintf(stderr, "done and waiting for server\n");
  7015. join_thread(serverThread);
  7016. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  7017. FreeTcpReady(&ready);
  7018. #ifdef WOLFSSL_TIRTOS
  7019. fdOpenSession(Task_self());
  7020. #endif
  7021. }
  7022. res = TEST_RES_CHECK(1);
  7023. #endif
  7024. return res;
  7025. }
  7026. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  7027. #ifdef WOLFSSL_TLS13
  7028. static const byte canned_client_tls13_session[] = {
  7029. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  7030. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7031. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  7032. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7033. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  7034. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7035. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7036. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  7037. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7038. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7039. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7040. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  7041. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7042. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7043. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7044. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7045. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7046. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7047. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7048. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7049. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7050. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7051. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7052. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7053. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7054. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7055. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7056. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  7057. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7058. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  7059. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  7060. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  7061. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  7062. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  7063. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7064. 0x00, 0x03
  7065. };
  7066. static const byte canned_server_tls13_session[] = {
  7067. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  7068. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7069. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  7070. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7071. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7072. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7073. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7074. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  7075. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7076. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7077. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7078. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  7079. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7080. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7081. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7082. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7083. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7084. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7085. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7086. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7087. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7088. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7089. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7090. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7091. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7092. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7093. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7094. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  7095. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7096. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  7097. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  7098. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  7099. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  7100. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  7101. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7102. 0x00, 0x04
  7103. };
  7104. #endif /* WOLFSSL_TLS13 */
  7105. static const byte canned_client_session[] = {
  7106. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  7107. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  7108. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  7109. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7110. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  7111. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  7113. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  7114. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  7115. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7116. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7117. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  7118. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  7119. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  7120. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  7121. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  7122. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  7123. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  7124. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  7125. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  7126. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  7127. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  7128. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  7129. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  7130. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  7131. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  7132. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  7133. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  7134. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  7135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7139. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  7140. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  7141. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  7142. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  7143. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  7144. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  7145. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7146. 0x00, 0x03
  7147. };
  7148. static const byte canned_server_session[] = {
  7149. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  7150. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  7151. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7152. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7153. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7155. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  7156. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  7157. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  7158. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7159. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7160. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  7161. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  7162. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  7163. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  7164. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  7165. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  7166. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  7167. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  7168. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  7169. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  7170. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  7171. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  7172. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  7173. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  7174. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  7175. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  7176. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  7177. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  7178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7179. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7180. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7181. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7182. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  7183. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  7184. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  7185. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  7186. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  7187. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  7188. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7189. 0x00, 0x04
  7190. };
  7191. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  7192. {
  7193. SOCKET_T sockfd = 0;
  7194. SOCKET_T clientfd = 0;
  7195. word16 port;
  7196. callback_functions* cbf;
  7197. WOLFSSL_CTX* ctx = 0;
  7198. WOLFSSL* ssl = 0;
  7199. char msg[] = "I hear you fa shizzle!";
  7200. char input[1024];
  7201. int idx;
  7202. #ifdef WOLFSSL_TIRTOS
  7203. fdOpenSession(Task_self());
  7204. #endif
  7205. ((func_args*)args)->return_code = TEST_FAIL;
  7206. cbf = ((func_args*)args)->callbacks;
  7207. {
  7208. WOLFSSL_METHOD* method = NULL;
  7209. if (cbf != NULL && cbf->method != NULL) {
  7210. method = cbf->method();
  7211. }
  7212. else {
  7213. method = wolfTLSv1_2_server_method();
  7214. }
  7215. ctx = wolfSSL_CTX_new(method);
  7216. }
  7217. if (ctx == NULL) {
  7218. goto done;
  7219. }
  7220. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  7221. #if defined(USE_WINDOWS_API)
  7222. port = ((func_args*)args)->signal->port;
  7223. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7224. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7225. /* Let tcp_listen assign port */
  7226. port = 0;
  7227. #else
  7228. /* Use default port */
  7229. port = wolfSSLPort;
  7230. #endif
  7231. /* do it here to detect failure */
  7232. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7233. CloseSocket(sockfd);
  7234. /* call ctx setup callback */
  7235. if (cbf != NULL && cbf->ctx_ready != NULL) {
  7236. cbf->ctx_ready(ctx);
  7237. }
  7238. ssl = wolfSSL_new(ctx);
  7239. if (ssl == NULL) {
  7240. goto done;
  7241. }
  7242. wolfSSL_set_fd(ssl, clientfd);
  7243. /* call ssl setup callback */
  7244. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7245. cbf->ssl_ready(ssl);
  7246. }
  7247. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7248. if (idx > 0) {
  7249. input[idx] = '\0';
  7250. fprintf(stderr, "Client message export/import: %s\n", input);
  7251. }
  7252. else {
  7253. fprintf(stderr, "ret = %d error = %d\n", idx,
  7254. wolfSSL_get_error(ssl, idx));
  7255. goto done;
  7256. }
  7257. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  7258. /*err_sys("SSL_write failed");*/
  7259. #ifdef WOLFSSL_TIRTOS
  7260. return;
  7261. #else
  7262. return 0;
  7263. #endif
  7264. }
  7265. #ifdef WOLFSSL_TIRTOS
  7266. Task_yield();
  7267. #endif
  7268. ((func_args*)args)->return_code = TEST_SUCCESS;
  7269. done:
  7270. wolfSSL_shutdown(ssl);
  7271. wolfSSL_free(ssl);
  7272. wolfSSL_CTX_free(ctx);
  7273. CloseSocket(clientfd);
  7274. #ifdef WOLFSSL_TIRTOS
  7275. fdCloseSession(Task_self());
  7276. #endif
  7277. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7278. && defined(HAVE_THREAD_LS)
  7279. wc_ecc_fp_free(); /* free per thread cache */
  7280. #endif
  7281. #if defined(HAVE_SESSION_TICKET) && \
  7282. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  7283. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  7284. OpenSSLTicketCleanup();
  7285. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  7286. TicketCleanup();
  7287. #endif
  7288. #endif
  7289. #ifndef WOLFSSL_TIRTOS
  7290. return 0;
  7291. #endif
  7292. }
  7293. static void load_tls12_canned_server(WOLFSSL* ssl)
  7294. {
  7295. int clientfd = wolfSSL_get_fd(ssl);
  7296. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  7297. sizeof(canned_server_session)), sizeof(canned_server_session));
  7298. wolfSSL_set_fd(ssl, clientfd);
  7299. }
  7300. #ifdef WOLFSSL_TLS13
  7301. static void load_tls13_canned_server(WOLFSSL* ssl)
  7302. {
  7303. int clientfd = wolfSSL_get_fd(ssl);
  7304. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  7305. sizeof(canned_server_tls13_session)),
  7306. sizeof(canned_server_tls13_session));
  7307. wolfSSL_set_fd(ssl, clientfd);
  7308. }
  7309. #endif
  7310. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  7311. static int test_wolfSSL_tls_export_run(int v)
  7312. {
  7313. SOCKET_T sockfd = 0;
  7314. WOLFSSL_CTX* ctx = 0;
  7315. WOLFSSL* ssl = 0;
  7316. char msg[64] = "hello wolfssl!";
  7317. char reply[1024];
  7318. word32 replySz;
  7319. int msgSz = (int)XSTRLEN(msg);
  7320. const byte* clientSession = NULL;
  7321. int clientSessionSz = 0;
  7322. tcp_ready ready;
  7323. func_args server_args;
  7324. THREAD_TYPE serverThread;
  7325. callback_functions server_cbf;
  7326. #ifdef WOLFSSL_TIRTOS
  7327. fdOpenSession(Task_self());
  7328. #endif
  7329. InitTcpReady(&ready);
  7330. #if defined(USE_WINDOWS_API)
  7331. /* use RNG to get random port if using windows */
  7332. ready.port = GetRandomPort();
  7333. #endif
  7334. XMEMSET(&server_args, 0, sizeof(func_args));
  7335. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7336. switch (v) {
  7337. case WOLFSSL_TLSV1_2:
  7338. server_cbf.method = wolfTLSv1_2_server_method;
  7339. server_cbf.ssl_ready = load_tls12_canned_server;
  7340. /* setup the client side */
  7341. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  7342. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  7343. clientSession = canned_client_session;
  7344. clientSessionSz = sizeof(canned_client_session);
  7345. break;
  7346. #ifdef WOLFSSL_TLS13
  7347. case WOLFSSL_TLSV1_3:
  7348. server_cbf.method = wolfTLSv1_3_server_method;
  7349. server_cbf.ssl_ready = load_tls13_canned_server;
  7350. /* setup the client side */
  7351. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  7352. clientSession = canned_client_tls13_session;
  7353. clientSessionSz = sizeof(canned_client_tls13_session);
  7354. break;
  7355. #endif
  7356. }
  7357. server_args.callbacks = &server_cbf;
  7358. server_args.signal = &ready;
  7359. start_thread(tls_export_server, &server_args, &serverThread);
  7360. wait_tcp_ready(&server_args);
  7361. #ifdef WOLFSSL_TIRTOS
  7362. fdOpenSession(Task_self());
  7363. #endif
  7364. AssertNotNull(ssl = wolfSSL_new(ctx));
  7365. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  7366. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  7367. clientSessionSz);
  7368. replySz = sizeof(reply);
  7369. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  7370. #if !defined(NO_PSK) && defined(HAVE_ANON)
  7371. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  7372. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  7373. #endif
  7374. wolfSSL_set_fd(ssl, sockfd);
  7375. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7376. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  7377. wolfSSL_free(ssl);
  7378. wolfSSL_CTX_free(ctx);
  7379. CloseSocket(sockfd);
  7380. #ifdef WOLFSSL_TIRTOS
  7381. fdCloseSession(Task_self());
  7382. #endif
  7383. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7384. && defined(HAVE_THREAD_LS)
  7385. wc_ecc_fp_free(); /* free per thread cache */
  7386. #endif
  7387. join_thread(serverThread);
  7388. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  7389. FreeTcpReady(&ready);
  7390. #ifdef WOLFSSL_TIRTOS
  7391. fdOpenSession(Task_self());
  7392. #endif
  7393. return TEST_RES_CHECK(1);
  7394. }
  7395. #endif
  7396. static int test_wolfSSL_tls_export(void)
  7397. {
  7398. int res = TEST_SKIPPED;
  7399. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  7400. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  7401. #ifdef WOLFSSL_TLS13
  7402. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  7403. #endif
  7404. res = TEST_RES_CHECK(1);
  7405. #endif
  7406. return res;
  7407. }
  7408. /*----------------------------------------------------------------------------*
  7409. | TLS extensions tests
  7410. *----------------------------------------------------------------------------*/
  7411. #ifdef ENABLE_TLS_CALLBACK_TEST
  7412. /* Connection test runner - generic */
  7413. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  7414. callback_functions* server_callbacks)
  7415. {
  7416. tcp_ready ready;
  7417. func_args client_args;
  7418. func_args server_args;
  7419. THREAD_TYPE serverThread;
  7420. XMEMSET(&client_args, 0, sizeof(func_args));
  7421. XMEMSET(&server_args, 0, sizeof(func_args));
  7422. StartTCP();
  7423. client_args.callbacks = client_callbacks;
  7424. server_args.callbacks = server_callbacks;
  7425. #ifdef WOLFSSL_TIRTOS
  7426. fdOpenSession(Task_self());
  7427. #endif
  7428. /* RUN Server side */
  7429. InitTcpReady(&ready);
  7430. #if defined(USE_WINDOWS_API)
  7431. /* use RNG to get random port if using windows */
  7432. ready.port = GetRandomPort();
  7433. #endif
  7434. server_args.signal = &ready;
  7435. client_args.signal = &ready;
  7436. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7437. wait_tcp_ready(&server_args);
  7438. /* RUN Client side */
  7439. run_wolfssl_client(&client_args);
  7440. join_thread(serverThread);
  7441. FreeTcpReady(&ready);
  7442. #ifdef WOLFSSL_TIRTOS
  7443. fdCloseSession(Task_self());
  7444. #endif
  7445. client_callbacks->return_code = client_args.return_code;
  7446. server_callbacks->return_code = server_args.return_code;
  7447. }
  7448. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7449. #ifdef HAVE_SNI
  7450. static int test_wolfSSL_UseSNI_params(void)
  7451. {
  7452. int res = TEST_SKIPPED;
  7453. #if !defined(NO_WOLFSSL_CLIENT)
  7454. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7455. WOLFSSL *ssl = wolfSSL_new(ctx);
  7456. AssertNotNull(ctx);
  7457. AssertNotNull(ssl);
  7458. /* invalid [ctx|ssl] */
  7459. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  7460. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  7461. /* invalid type */
  7462. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  7463. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  7464. /* invalid data */
  7465. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  7466. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  7467. /* success case */
  7468. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  7469. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  7470. wolfSSL_free(ssl);
  7471. wolfSSL_CTX_free(ctx);
  7472. res = TEST_RES_CHECK(1);
  7473. #endif /* !NO_WOLFSSL_CLIENT */
  7474. return res;
  7475. }
  7476. /* BEGIN of connection tests callbacks */
  7477. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7478. {
  7479. AssertIntEQ(WOLFSSL_SUCCESS,
  7480. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  7481. }
  7482. static void use_SNI_at_ssl(WOLFSSL* ssl)
  7483. {
  7484. AssertIntEQ(WOLFSSL_SUCCESS,
  7485. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  7486. }
  7487. static void different_SNI_at_ssl(WOLFSSL* ssl)
  7488. {
  7489. AssertIntEQ(WOLFSSL_SUCCESS,
  7490. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  7491. }
  7492. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  7493. {
  7494. use_SNI_at_ssl(ssl);
  7495. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7496. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  7497. }
  7498. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  7499. {
  7500. use_SNI_at_ssl(ssl);
  7501. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7502. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  7503. }
  7504. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7505. {
  7506. use_SNI_at_ctx(ctx);
  7507. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  7508. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7509. }
  7510. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  7511. {
  7512. use_SNI_at_ssl(ssl);
  7513. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7514. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7515. }
  7516. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7517. {
  7518. use_SNI_at_ctx(ctx);
  7519. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  7520. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7521. }
  7522. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  7523. {
  7524. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  7525. }
  7526. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  7527. {
  7528. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  7529. }
  7530. static void verify_SNI_no_matching(WOLFSSL* ssl)
  7531. {
  7532. byte type = WOLFSSL_SNI_HOST_NAME;
  7533. void* request = (void*) &type; /* to be overwritten */
  7534. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  7535. AssertNotNull(request);
  7536. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7537. AssertNull(request);
  7538. }
  7539. static void verify_SNI_real_matching(WOLFSSL* ssl)
  7540. {
  7541. byte type = WOLFSSL_SNI_HOST_NAME;
  7542. void* request = NULL;
  7543. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  7544. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7545. AssertNotNull(request);
  7546. AssertStrEQ("www.wolfssl.com", (char*)request);
  7547. }
  7548. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  7549. {
  7550. byte type = WOLFSSL_SNI_HOST_NAME;
  7551. void* request = NULL;
  7552. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  7553. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7554. AssertNotNull(request);
  7555. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  7556. }
  7557. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7558. {
  7559. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  7560. }
  7561. /* END of connection tests callbacks */
  7562. static int test_wolfSSL_UseSNI_connection(void)
  7563. {
  7564. int res = TEST_SKIPPED;
  7565. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7566. callback_functions client_cb;
  7567. callback_functions server_cb;
  7568. size_t i;
  7569. struct {
  7570. method_provider client_meth;
  7571. method_provider server_meth;
  7572. } methods[] = {
  7573. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  7574. {wolfSSLv23_client_method, wolfSSLv23_server_method},
  7575. #endif
  7576. #ifndef WOLFSSL_NO_TLS12
  7577. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
  7578. #endif
  7579. #ifdef WOLFSSL_TLS13
  7580. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
  7581. #endif
  7582. };
  7583. for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
  7584. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7585. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7586. client_cb.method = methods[i].client_meth;
  7587. server_cb.method = methods[i].server_meth;
  7588. client_cb.devId = testDevId;
  7589. server_cb.devId = testDevId;
  7590. /* success case at ctx */
  7591. printf("success case at ctx\n");
  7592. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7593. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7594. test_wolfSSL_client_server(&client_cb, &server_cb);
  7595. /* success case at ssl */
  7596. printf("success case at ssl\n");
  7597. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  7598. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  7599. test_wolfSSL_client_server(&client_cb, &server_cb);
  7600. /* default mismatch behavior */
  7601. printf("default mismatch behavior\n");
  7602. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7603. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  7604. test_wolfSSL_client_server(&client_cb, &server_cb);
  7605. /* continue on mismatch */
  7606. printf("continue on mismatch\n");
  7607. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7608. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  7609. test_wolfSSL_client_server(&client_cb, &server_cb);
  7610. /* fake answer on mismatch */
  7611. printf("fake answer 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_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  7614. test_wolfSSL_client_server(&client_cb, &server_cb);
  7615. /* sni abort - success */
  7616. printf("sni abort - success\n");
  7617. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7618. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7619. test_wolfSSL_client_server(&client_cb, &server_cb);
  7620. /* sni abort - abort when absent (ctx) */
  7621. printf("sni abort - abort when absent (ctx)\n");
  7622. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7623. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7624. test_wolfSSL_client_server(&client_cb, &server_cb);
  7625. /* sni abort - abort when absent (ssl) */
  7626. printf("sni abort - abort when absent (ssl)\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 = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7629. test_wolfSSL_client_server(&client_cb, &server_cb);
  7630. /* sni abort - success when overwritten */
  7631. printf("sni abort - success when overwritten\n");
  7632. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7633. 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;
  7634. test_wolfSSL_client_server(&client_cb, &server_cb);
  7635. /* sni abort - success when allowing mismatches */
  7636. printf("sni abort - success when allowing mismatches\n");
  7637. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7638. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  7639. test_wolfSSL_client_server(&client_cb, &server_cb);
  7640. }
  7641. res = TEST_RES_CHECK(1);
  7642. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7643. return res;
  7644. }
  7645. static int test_wolfSSL_SNI_GetFromBuffer(void)
  7646. {
  7647. byte buff[] = { /* www.paypal.com */
  7648. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  7649. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  7650. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  7651. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7652. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7653. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  7654. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  7655. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  7656. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7657. };
  7658. byte buff2[] = { /* api.textmate.org */
  7659. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  7660. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  7661. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  7662. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  7663. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  7664. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  7665. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  7666. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  7667. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  7668. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  7669. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  7670. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  7671. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  7672. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  7673. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  7674. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  7675. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  7676. };
  7677. byte buff3[] = { /* no sni extension */
  7678. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  7679. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  7680. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  7681. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7682. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7683. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  7684. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7685. };
  7686. byte buff4[] = { /* last extension has zero size */
  7687. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  7688. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7689. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7690. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7691. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  7692. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  7693. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  7694. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  7695. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  7696. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  7697. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  7698. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  7699. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  7700. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  7701. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  7702. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  7703. 0x12, 0x00, 0x00
  7704. };
  7705. byte buff5[] = { /* SSL v2.0 client hello */
  7706. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  7707. /* dummy bytes bellow, just to pass size check */
  7708. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7709. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7710. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7711. };
  7712. byte result[32] = {0};
  7713. word32 length = 32;
  7714. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  7715. 0, result, &length));
  7716. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  7717. 0, result, &length));
  7718. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7719. 1, result, &length));
  7720. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7721. 0, result, &length));
  7722. buff[0] = 0x16;
  7723. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7724. 0, result, &length));
  7725. buff[1] = 0x03;
  7726. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  7727. sizeof(buff), 0, result, &length));
  7728. buff[2] = 0x03;
  7729. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  7730. sizeof(buff), 0, result, &length));
  7731. buff[4] = 0x64;
  7732. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7733. 0, result, &length));
  7734. result[length] = 0;
  7735. AssertStrEQ("www.paypal.com", (const char*) result);
  7736. length = 32;
  7737. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7738. 0, result, &length));
  7739. result[length] = 0;
  7740. AssertStrEQ("api.textmate.org", (const char*) result);
  7741. /* SSL v2.0 tests */
  7742. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  7743. sizeof(buff5), 0, result, &length));
  7744. buff5[2] = 0x02;
  7745. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7746. sizeof(buff5), 0, result, &length));
  7747. buff5[2] = 0x01; buff5[6] = 0x08;
  7748. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7749. sizeof(buff5), 0, result, &length));
  7750. buff5[6] = 0x09; buff5[8] = 0x01;
  7751. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7752. sizeof(buff5), 0, result, &length));
  7753. return TEST_RES_CHECK(1);
  7754. }
  7755. #endif /* HAVE_SNI */
  7756. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  7757. static int test_wolfSSL_UseTrustedCA(void)
  7758. {
  7759. int res = TEST_SKIPPED;
  7760. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  7761. && !defined(NO_RSA)
  7762. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7763. WOLFSSL_CTX *ctx;
  7764. WOLFSSL *ssl;
  7765. byte id[20];
  7766. #ifndef NO_WOLFSSL_SERVER
  7767. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  7768. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7769. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7770. #else
  7771. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  7772. #endif
  7773. AssertNotNull((ssl = wolfSSL_new(ctx)));
  7774. XMEMSET(id, 0, sizeof(id));
  7775. /* error cases */
  7776. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  7777. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7778. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  7779. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7780. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  7781. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7782. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  7783. #ifdef NO_SHA
  7784. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7785. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7786. #endif
  7787. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7788. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  7789. /* success cases */
  7790. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7791. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  7792. #ifndef NO_SHA
  7793. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7794. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7795. #endif
  7796. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7797. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  7798. wolfSSL_free(ssl);
  7799. wolfSSL_CTX_free(ctx);
  7800. res = TEST_RES_CHECK(1);
  7801. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7802. #endif /* HAVE_TRUSTED_CA */
  7803. return res;
  7804. }
  7805. static int test_wolfSSL_UseMaxFragment(void)
  7806. {
  7807. int res = TEST_SKIPPED;
  7808. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  7809. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7810. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7811. #ifndef NO_WOLFSSL_SERVER
  7812. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7813. #else
  7814. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7815. #endif
  7816. WOLFSSL *ssl;
  7817. #ifdef OPENSSL_EXTRA
  7818. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  7819. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  7820. #else
  7821. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  7822. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  7823. #endif
  7824. #ifndef NO_WOLFSSL_SERVER
  7825. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7826. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7827. #endif
  7828. AssertNotNull(ctx);
  7829. ssl = wolfSSL_new(ctx);
  7830. AssertNotNull(ssl);
  7831. #ifdef OPENSSL_EXTRA
  7832. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  7833. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  7834. #else
  7835. UseMaxFragment = wolfSSL_UseMaxFragment;
  7836. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  7837. #endif
  7838. /* error cases */
  7839. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  7840. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  7841. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  7842. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  7843. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  7844. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  7845. /* success case */
  7846. #ifdef OPENSSL_EXTRA
  7847. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7848. #else
  7849. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7850. #endif
  7851. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  7852. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  7853. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  7854. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  7855. #ifdef OPENSSL_EXTRA
  7856. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7857. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7858. #else
  7859. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7860. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7861. #endif
  7862. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  7863. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  7864. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  7865. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  7866. #ifdef OPENSSL_EXTRA
  7867. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7868. #else
  7869. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7870. #endif
  7871. wolfSSL_free(ssl);
  7872. wolfSSL_CTX_free(ctx);
  7873. res = TEST_RES_CHECK(1);
  7874. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7875. #endif
  7876. return res;
  7877. }
  7878. static int test_wolfSSL_UseTruncatedHMAC(void)
  7879. {
  7880. int res = TEST_SKIPPED;
  7881. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  7882. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7883. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7884. #ifndef NO_WOLFSSL_SERVER
  7885. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7886. #else
  7887. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7888. #endif
  7889. WOLFSSL *ssl;
  7890. AssertNotNull(ctx);
  7891. #ifndef NO_WOLFSSL_SERVER
  7892. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7893. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7894. #endif
  7895. ssl = wolfSSL_new(ctx);
  7896. AssertNotNull(ssl);
  7897. /* error cases */
  7898. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  7899. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  7900. /* success case */
  7901. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  7902. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  7903. wolfSSL_free(ssl);
  7904. wolfSSL_CTX_free(ctx);
  7905. res = TEST_RES_CHECK(1);
  7906. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7907. #endif
  7908. return res;
  7909. }
  7910. static int test_wolfSSL_UseSupportedCurve(void)
  7911. {
  7912. int res = TEST_SKIPPED;
  7913. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  7914. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7915. WOLFSSL *ssl = wolfSSL_new(ctx);
  7916. AssertNotNull(ctx);
  7917. AssertNotNull(ssl);
  7918. /* error cases */
  7919. AssertIntNE(WOLFSSL_SUCCESS,
  7920. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7921. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  7922. AssertIntNE(WOLFSSL_SUCCESS,
  7923. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7924. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  7925. /* success case */
  7926. AssertIntEQ(WOLFSSL_SUCCESS,
  7927. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  7928. AssertIntEQ(WOLFSSL_SUCCESS,
  7929. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  7930. wolfSSL_free(ssl);
  7931. wolfSSL_CTX_free(ctx);
  7932. res = TEST_RES_CHECK(1);
  7933. #endif
  7934. return res;
  7935. }
  7936. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  7937. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7938. {
  7939. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  7940. }
  7941. static void use_ALPN_all(WOLFSSL* ssl)
  7942. {
  7943. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7944. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7945. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7946. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7947. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7948. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7949. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7950. }
  7951. static void use_ALPN_all_continue(WOLFSSL* ssl)
  7952. {
  7953. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7954. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7955. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7956. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7957. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7958. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7959. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7960. }
  7961. static void use_ALPN_one(WOLFSSL* ssl)
  7962. {
  7963. /* spdy/2 */
  7964. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7965. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7966. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7967. }
  7968. static void use_ALPN_unknown(WOLFSSL* ssl)
  7969. {
  7970. /* http/2.0 */
  7971. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7972. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7973. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7974. }
  7975. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  7976. {
  7977. /* http/2.0 */
  7978. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7979. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7980. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7981. }
  7982. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  7983. {
  7984. /* spdy/3 */
  7985. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7986. char *proto = NULL;
  7987. word16 protoSz = 0;
  7988. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7989. /* check value */
  7990. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  7991. if (proto) {
  7992. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  7993. }
  7994. }
  7995. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  7996. {
  7997. char *proto = NULL;
  7998. word16 protoSz = 0;
  7999. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  8000. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8001. /* check value */
  8002. AssertIntEQ(1, (0 == protoSz));
  8003. AssertIntEQ(1, (NULL == proto));
  8004. }
  8005. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  8006. {
  8007. /* http/1.1 */
  8008. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8009. char *proto;
  8010. word16 protoSz = 0;
  8011. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8012. /* check value */
  8013. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8014. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8015. }
  8016. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  8017. {
  8018. /* spdy/2 */
  8019. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8020. char *proto;
  8021. word16 protoSz = 0;
  8022. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8023. /* check value */
  8024. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8025. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8026. }
  8027. static void verify_ALPN_client_list(WOLFSSL* ssl)
  8028. {
  8029. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8030. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8031. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8032. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8033. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8034. char *clist = NULL;
  8035. word16 clistSz = 0;
  8036. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  8037. &clistSz));
  8038. /* check value */
  8039. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  8040. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  8041. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  8042. }
  8043. static int test_wolfSSL_UseALPN_connection(void)
  8044. {
  8045. int res = TEST_SKIPPED;
  8046. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8047. callback_functions client_cb;
  8048. callback_functions server_cb;
  8049. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8050. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8051. client_cb.method = wolfSSLv23_client_method;
  8052. server_cb.method = wolfSSLv23_server_method;
  8053. client_cb.devId = testDevId;
  8054. server_cb.devId = testDevId;
  8055. /* success case same list */
  8056. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8057. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  8058. test_wolfSSL_client_server(&client_cb, &server_cb);
  8059. /* success case only one for server */
  8060. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8061. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  8062. test_wolfSSL_client_server(&client_cb, &server_cb);
  8063. /* success case only one for client */
  8064. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  8065. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  8066. test_wolfSSL_client_server(&client_cb, &server_cb);
  8067. /* success case none for client */
  8068. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8069. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  8070. test_wolfSSL_client_server(&client_cb, &server_cb);
  8071. /* success case mismatch behavior but option 'continue' set */
  8072. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  8073. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  8074. test_wolfSSL_client_server(&client_cb, &server_cb);
  8075. /* success case read protocol send by client */
  8076. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8077. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  8078. test_wolfSSL_client_server(&client_cb, &server_cb);
  8079. /* mismatch behavior with same list
  8080. * the first and only this one must be taken */
  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_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  8083. test_wolfSSL_client_server(&client_cb, &server_cb);
  8084. /* default mismatch behavior */
  8085. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8086. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  8087. test_wolfSSL_client_server(&client_cb, &server_cb);
  8088. res = TEST_RES_CHECK(1);
  8089. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8090. return res;
  8091. }
  8092. static int test_wolfSSL_UseALPN_params(void)
  8093. {
  8094. int res = TEST_SKIPPED;
  8095. #ifndef NO_WOLFSSL_CLIENT
  8096. /* "http/1.1" */
  8097. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8098. /* "spdy/1" */
  8099. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  8100. /* "spdy/2" */
  8101. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8102. /* "spdy/3" */
  8103. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8104. char buff[256];
  8105. word32 idx;
  8106. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8107. WOLFSSL *ssl = wolfSSL_new(ctx);
  8108. AssertNotNull(ctx);
  8109. AssertNotNull(ssl);
  8110. /* error cases */
  8111. AssertIntNE(WOLFSSL_SUCCESS,
  8112. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  8113. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8114. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  8115. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8116. /* success case */
  8117. /* http1 only */
  8118. AssertIntEQ(WOLFSSL_SUCCESS,
  8119. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  8120. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8121. /* http1, spdy1 */
  8122. XMEMCPY(buff, http1, sizeof(http1));
  8123. idx = sizeof(http1);
  8124. buff[idx++] = ',';
  8125. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8126. idx += sizeof(spdy1);
  8127. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8128. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8129. /* http1, spdy2, spdy1 */
  8130. XMEMCPY(buff, http1, sizeof(http1));
  8131. idx = sizeof(http1);
  8132. buff[idx++] = ',';
  8133. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  8134. idx += sizeof(spdy2);
  8135. buff[idx++] = ',';
  8136. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8137. idx += sizeof(spdy1);
  8138. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8139. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8140. /* spdy3, http1, spdy2, spdy1 */
  8141. XMEMCPY(buff, spdy3, sizeof(spdy3));
  8142. idx = sizeof(spdy3);
  8143. buff[idx++] = ',';
  8144. XMEMCPY(buff+idx, http1, sizeof(http1));
  8145. idx += sizeof(http1);
  8146. buff[idx++] = ',';
  8147. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  8148. idx += sizeof(spdy2);
  8149. buff[idx++] = ',';
  8150. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8151. idx += sizeof(spdy1);
  8152. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8153. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8154. wolfSSL_free(ssl);
  8155. wolfSSL_CTX_free(ctx);
  8156. res = TEST_RES_CHECK(1);
  8157. #endif
  8158. return res;
  8159. }
  8160. #endif /* HAVE_ALPN */
  8161. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  8162. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  8163. {
  8164. unsigned char p[] = {
  8165. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  8166. 6, 's', 'p', 'd', 'y', '/', '2',
  8167. 6, 's', 'p', 'd', 'y', '/', '1',
  8168. };
  8169. unsigned char p_len = sizeof(p);
  8170. int ret;
  8171. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  8172. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  8173. AssertIntEQ(ret, 0);
  8174. #else
  8175. AssertIntEQ(ret, SSL_SUCCESS);
  8176. #endif
  8177. }
  8178. static void set_alpn_protos(SSL* ssl)
  8179. {
  8180. unsigned char p[] = {
  8181. 6, 's', 'p', 'd', 'y', '/', '3',
  8182. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  8183. 6, 's', 'p', 'd', 'y', '/', '2',
  8184. 6, 's', 'p', 'd', 'y', '/', '1',
  8185. };
  8186. unsigned char p_len = sizeof(p);
  8187. int ret;
  8188. ret = SSL_set_alpn_protos(ssl, p, p_len);
  8189. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  8190. AssertIntEQ(ret, 0);
  8191. #else
  8192. AssertIntEQ(ret, SSL_SUCCESS);
  8193. #endif
  8194. }
  8195. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  8196. {
  8197. /* "spdy/3" */
  8198. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8199. const unsigned char *proto;
  8200. unsigned int protoSz = 0;
  8201. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  8202. /* check value */
  8203. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8204. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8205. }
  8206. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  8207. {
  8208. /* "http/1.1" */
  8209. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8210. const unsigned char *proto;
  8211. unsigned int protoSz = 0;
  8212. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  8213. /* check value */
  8214. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8215. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8216. }
  8217. static int test_wolfSSL_set_alpn_protos(void)
  8218. {
  8219. int res = TEST_SKIPPED;
  8220. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8221. callback_functions client_cb;
  8222. callback_functions server_cb;
  8223. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8224. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8225. client_cb.method = wolfSSLv23_client_method;
  8226. server_cb.method = wolfSSLv23_server_method;
  8227. client_cb.devId = testDevId;
  8228. server_cb.devId = testDevId;
  8229. /* use CTX_alpn_protos */
  8230. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8231. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  8232. test_wolfSSL_client_server(&client_cb, &server_cb);
  8233. /* use set_alpn_protos */
  8234. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  8235. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  8236. test_wolfSSL_client_server(&client_cb, &server_cb);
  8237. res = TEST_RES_CHECK(1);
  8238. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8239. return res;
  8240. }
  8241. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  8242. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  8243. {
  8244. int res = TEST_SKIPPED;
  8245. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  8246. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8247. WOLFSSL *ssl = wolfSSL_new(ctx);
  8248. AssertNotNull(ctx);
  8249. AssertNotNull(ssl);
  8250. /* error cases */
  8251. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  8252. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  8253. /* success cases */
  8254. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  8255. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  8256. wolfSSL_free(ssl);
  8257. wolfSSL_CTX_free(ctx);
  8258. res = TEST_RES_CHECK(1);
  8259. #endif
  8260. return res;
  8261. }
  8262. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  8263. {
  8264. int res = TEST_SKIPPED;
  8265. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  8266. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8267. WOLFSSL *ssl = wolfSSL_new(ctx);
  8268. AssertNotNull(ctx);
  8269. AssertNotNull(ssl);
  8270. /* error cases */
  8271. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  8272. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  8273. /* success cases */
  8274. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  8275. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  8276. wolfSSL_free(ssl);
  8277. wolfSSL_CTX_free(ctx);
  8278. res = TEST_RES_CHECK(1);
  8279. #endif
  8280. return res;
  8281. }
  8282. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  8283. static int test_wolfSSL_SCR_Reconnect(void)
  8284. {
  8285. int res = TEST_SKIPPED;
  8286. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  8287. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  8288. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256)
  8289. struct test_memio_ctx test_ctx;
  8290. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  8291. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  8292. byte data;
  8293. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  8294. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  8295. test_ctx.s_ciphers =
  8296. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  8297. AssertIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  8298. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  8299. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  8300. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  8301. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  8302. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  8303. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8304. /* WOLFSSL_FATAL_ERROR since it will block */
  8305. AssertIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  8306. AssertIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  8307. WOLFSSL_ERROR_WANT_READ);
  8308. AssertIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  8309. AssertIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  8310. WOLFSSL_ERROR_WANT_READ);
  8311. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8312. wolfSSL_free(ssl_c);
  8313. ssl_c = NULL;
  8314. wolfSSL_free(ssl_s);
  8315. ssl_s = NULL;
  8316. wolfSSL_CTX_free(ctx_c);
  8317. ctx_c = NULL;
  8318. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  8319. AssertIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  8320. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  8321. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8322. wolfSSL_free(ssl_s);
  8323. wolfSSL_free(ssl_c);
  8324. wolfSSL_CTX_free(ctx_s);
  8325. wolfSSL_CTX_free(ctx_c);
  8326. res = TEST_RES_CHECK(1);
  8327. #endif
  8328. return res;
  8329. }
  8330. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  8331. (!defined(NO_RSA) || defined(HAVE_ECC))
  8332. /* Called when writing. */
  8333. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  8334. {
  8335. (void)ssl;
  8336. (void)buf;
  8337. (void)sz;
  8338. (void)ctx;
  8339. /* Force error return from wolfSSL_accept_TLSv13(). */
  8340. return WANT_WRITE;
  8341. }
  8342. /* Called when reading. */
  8343. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  8344. {
  8345. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  8346. int len = (int)msg->length;
  8347. (void)ssl;
  8348. (void)sz;
  8349. /* Pass back as much of message as will fit in buffer. */
  8350. if (len > sz)
  8351. len = sz;
  8352. XMEMCPY(buf, msg->buffer, len);
  8353. /* Move over returned data. */
  8354. msg->buffer += len;
  8355. msg->length -= len;
  8356. /* Amount actually copied. */
  8357. return len;
  8358. }
  8359. #endif
  8360. /* Test the detection of duplicate known TLS extensions.
  8361. * Specifically in a ClientHello.
  8362. */
  8363. static int test_tls_ext_duplicate(void)
  8364. {
  8365. int res = TEST_SKIPPED;
  8366. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  8367. !defined(NO_FILESYSTEM)
  8368. const unsigned char clientHelloDupTlsExt[] = {
  8369. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  8370. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  8371. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  8372. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  8373. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  8374. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  8375. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  8376. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  8377. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8378. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  8379. 0x00, 0x9e, 0x01, 0x00,
  8380. /* Extensions - duplicate signature algorithms. */
  8381. 0x00, 0x19, 0x00, 0x0d,
  8382. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  8383. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  8384. /* Supported Versions extension for TLS 1.3. */
  8385. 0x00, 0x2b,
  8386. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  8387. };
  8388. WOLFSSL_BUFFER_INFO msg;
  8389. const char* testCertFile;
  8390. const char* testKeyFile;
  8391. WOLFSSL_CTX *ctx;
  8392. WOLFSSL *ssl;
  8393. #ifndef NO_RSA
  8394. testCertFile = svrCertFile;
  8395. testKeyFile = svrKeyFile;
  8396. #elif defined(HAVE_ECC)
  8397. testCertFile = eccCertFile;
  8398. testKeyFile = eccKeyFile;
  8399. #endif
  8400. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8401. AssertNotNull(ctx);
  8402. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  8403. WOLFSSL_FILETYPE_PEM));
  8404. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  8405. WOLFSSL_FILETYPE_PEM));
  8406. /* Read from 'msg'. */
  8407. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  8408. /* No where to send to - dummy sender. */
  8409. wolfSSL_SetIOSend(ctx, DummySend);
  8410. ssl = wolfSSL_new(ctx);
  8411. AssertNotNull(ssl);
  8412. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  8413. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  8414. wolfSSL_SetIOReadCtx(ssl, &msg);
  8415. AssertIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  8416. AssertIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  8417. wolfSSL_free(ssl);
  8418. wolfSSL_CTX_free(ctx);
  8419. res = TEST_RES_CHECK(1);
  8420. #endif
  8421. return res;
  8422. }
  8423. /*----------------------------------------------------------------------------*
  8424. | X509 Tests
  8425. *----------------------------------------------------------------------------*/
  8426. static int test_wolfSSL_X509_NAME_get_entry(void)
  8427. {
  8428. int res = TEST_SKIPPED;
  8429. #if !defined(NO_CERTS) && !defined(NO_RSA)
  8430. #if defined(OPENSSL_ALL) || \
  8431. (defined(OPENSSL_EXTRA) && \
  8432. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  8433. /* use openssl like name to test mapping */
  8434. X509_NAME_ENTRY* ne;
  8435. X509_NAME* name;
  8436. X509* x509;
  8437. #ifndef NO_FILESYSTEM
  8438. ASN1_STRING* asn;
  8439. char* subCN = NULL;
  8440. #endif
  8441. int idx;
  8442. ASN1_OBJECT *object = NULL;
  8443. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  8444. defined(WOLFSSL_NGINX)
  8445. #ifndef NO_BIO
  8446. BIO* bio;
  8447. #endif
  8448. #endif
  8449. #ifndef NO_FILESYSTEM
  8450. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8451. WOLFSSL_FILETYPE_PEM);
  8452. AssertNotNull(x509);
  8453. name = X509_get_subject_name(x509);
  8454. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  8455. AssertIntGE(idx, 0);
  8456. ne = X509_NAME_get_entry(name, idx);
  8457. AssertNotNull(ne);
  8458. asn = X509_NAME_ENTRY_get_data(ne);
  8459. AssertNotNull(asn);
  8460. subCN = (char*)ASN1_STRING_data(asn);
  8461. AssertNotNull(subCN);
  8462. wolfSSL_FreeX509(x509);
  8463. #endif
  8464. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8465. WOLFSSL_FILETYPE_PEM);
  8466. AssertNotNull(x509);
  8467. name = X509_get_subject_name(x509);
  8468. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  8469. AssertIntGE(idx, 0);
  8470. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  8471. defined(WOLFSSL_NGINX)
  8472. #ifndef NO_BIO
  8473. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  8474. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  8475. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  8476. AssertIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  8477. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  8478. BIO_free(bio);
  8479. #endif
  8480. #endif
  8481. ne = X509_NAME_get_entry(name, idx);
  8482. AssertNotNull(ne);
  8483. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  8484. wolfSSL_FreeX509(x509);
  8485. res = TEST_RES_CHECK(1);
  8486. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  8487. #endif /* !NO_CERTS && !NO_RSA */
  8488. return res;
  8489. }
  8490. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  8491. static int test_wolfSSL_PKCS12(void)
  8492. {
  8493. int res = TEST_SKIPPED;
  8494. /* .p12 file is encrypted with DES3 */
  8495. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  8496. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  8497. * Password Key
  8498. */
  8499. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  8500. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  8501. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  8502. byte buf[6000];
  8503. char file[] = "./certs/test-servercert.p12";
  8504. char order[] = "./certs/ecc-rsa-server.p12";
  8505. #ifdef WC_RC2
  8506. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  8507. #endif
  8508. char pass[] = "a password";
  8509. const char goodPsw[] = "wolfSSL test";
  8510. const char badPsw[] = "bad";
  8511. #ifdef HAVE_ECC
  8512. WOLFSSL_X509_NAME* subject;
  8513. WOLFSSL_X509 *x509;
  8514. #endif
  8515. XFILE f;
  8516. int bytes, ret, goodPswLen, badPswLen;
  8517. WOLFSSL_BIO *bio;
  8518. WOLFSSL_EVP_PKEY *pkey;
  8519. WC_PKCS12 *pkcs12;
  8520. WC_PKCS12 *pkcs12_2;
  8521. WOLFSSL_X509 *cert;
  8522. WOLFSSL_X509 *tmp;
  8523. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  8524. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8525. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8526. WOLFSSL_CTX *ctx;
  8527. WOLFSSL *ssl;
  8528. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  8529. #endif
  8530. f = XFOPEN(file, "rb");
  8531. AssertTrue((f != XBADFILE));
  8532. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8533. XFCLOSE(f);
  8534. goodPswLen = (int)XSTRLEN(goodPsw);
  8535. badPswLen = (int)XSTRLEN(badPsw);
  8536. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  8537. AssertNotNull(bio);
  8538. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  8539. d2i_PKCS12_bio(bio, &pkcs12);
  8540. AssertNotNull(pkcs12);
  8541. BIO_free(bio);
  8542. /* check verify MAC directly */
  8543. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  8544. AssertIntEQ(ret, 1);
  8545. /* check verify MAC fail case directly */
  8546. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  8547. AssertIntEQ(ret, 0);
  8548. /* check verify MAC fail case */
  8549. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8550. AssertIntEQ(ret, 0);
  8551. AssertNull(pkey);
  8552. AssertNull(cert);
  8553. /* check parse with no extra certs kept */
  8554. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8555. AssertIntEQ(ret, 1);
  8556. AssertNotNull(pkey);
  8557. AssertNotNull(cert);
  8558. wolfSSL_EVP_PKEY_free(pkey);
  8559. wolfSSL_X509_free(cert);
  8560. /* check parse with extra certs kept */
  8561. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8562. AssertIntEQ(ret, 1);
  8563. AssertNotNull(pkey);
  8564. AssertNotNull(cert);
  8565. AssertNotNull(ca);
  8566. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8567. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8568. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  8569. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8570. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8571. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8572. #else
  8573. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8574. #endif
  8575. /* Copy stack structure */
  8576. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  8577. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  8578. /* CTX now owns the tmp_ca stack structure */
  8579. tmp_ca = NULL;
  8580. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  8581. AssertNotNull(tmp_ca);
  8582. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  8583. /* Check that the main cert is also set */
  8584. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  8585. AssertNotNull(ssl = SSL_new(ctx));
  8586. AssertNotNull(SSL_get_certificate(ssl));
  8587. SSL_free(ssl);
  8588. SSL_CTX_free(ctx);
  8589. #endif
  8590. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8591. /* should be 2 other certs on stack */
  8592. tmp = sk_X509_pop(ca);
  8593. AssertNotNull(tmp);
  8594. X509_free(tmp);
  8595. tmp = sk_X509_pop(ca);
  8596. AssertNotNull(tmp);
  8597. X509_free(tmp);
  8598. AssertNull(sk_X509_pop(ca));
  8599. EVP_PKEY_free(pkey);
  8600. X509_free(cert);
  8601. sk_X509_pop_free(ca, X509_free);
  8602. /* check PKCS12_create */
  8603. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  8604. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  8605. SSL_SUCCESS);
  8606. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8607. -1, -1, 100, -1, 0)));
  8608. EVP_PKEY_free(pkey);
  8609. X509_free(cert);
  8610. sk_X509_pop_free(ca, NULL);
  8611. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8612. SSL_SUCCESS);
  8613. PKCS12_free(pkcs12_2);
  8614. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8615. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8616. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8617. 2000, 1, 0)));
  8618. EVP_PKEY_free(pkey);
  8619. X509_free(cert);
  8620. sk_X509_pop_free(ca, NULL);
  8621. /* convert to DER then back and parse */
  8622. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  8623. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  8624. PKCS12_free(pkcs12_2);
  8625. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  8626. BIO_free(bio);
  8627. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8628. SSL_SUCCESS);
  8629. /* should be 2 other certs on stack */
  8630. tmp = sk_X509_pop(ca);
  8631. AssertNotNull(tmp);
  8632. X509_free(tmp);
  8633. tmp = sk_X509_pop(ca);
  8634. AssertNotNull(tmp);
  8635. X509_free(tmp);
  8636. AssertNull(sk_X509_pop(ca));
  8637. #ifndef NO_RC4
  8638. PKCS12_free(pkcs12_2);
  8639. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  8640. NID_pbe_WithSHA1And128BitRC4,
  8641. NID_pbe_WithSHA1And128BitRC4,
  8642. 2000, 1, 0)));
  8643. EVP_PKEY_free(pkey);
  8644. X509_free(cert);
  8645. sk_X509_pop_free(ca, NULL);
  8646. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8647. SSL_SUCCESS);
  8648. #endif /* NO_RC4 */
  8649. EVP_PKEY_free(pkey);
  8650. X509_free(cert);
  8651. PKCS12_free(pkcs12);
  8652. PKCS12_free(pkcs12_2);
  8653. sk_X509_pop_free(ca, NULL);
  8654. #ifdef HAVE_ECC
  8655. /* test order of parsing */
  8656. f = XFOPEN(order, "rb");
  8657. AssertTrue(f != XBADFILE);
  8658. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8659. XFCLOSE(f);
  8660. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8661. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8662. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  8663. WOLFSSL_SUCCESS);
  8664. /* check use of pkey after parse */
  8665. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8666. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8667. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8668. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8669. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8670. #else
  8671. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8672. #endif
  8673. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  8674. SSL_CTX_free(ctx);
  8675. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8676. #endif
  8677. AssertNotNull(pkey);
  8678. AssertNotNull(cert);
  8679. AssertNotNull(ca);
  8680. /* compare subject lines of certificates */
  8681. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8682. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  8683. SSL_FILETYPE_PEM));
  8684. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8685. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8686. X509_free(x509);
  8687. /* test expected fail case */
  8688. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8689. SSL_FILETYPE_PEM));
  8690. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8691. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8692. X509_free(x509);
  8693. X509_free(cert);
  8694. /* get subject line from ca stack */
  8695. AssertNotNull(cert = sk_X509_pop(ca));
  8696. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8697. /* compare subject from certificate in ca to expected */
  8698. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8699. SSL_FILETYPE_PEM));
  8700. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8701. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8702. EVP_PKEY_free(pkey);
  8703. X509_free(x509);
  8704. X509_free(cert);
  8705. BIO_free(bio);
  8706. PKCS12_free(pkcs12);
  8707. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  8708. /* test order of parsing */
  8709. f = XFOPEN(file, "rb");
  8710. AssertTrue(f != XBADFILE);
  8711. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8712. XFCLOSE(f);
  8713. /* check verify MAC fail case */
  8714. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8715. AssertIntEQ(ret, 0);
  8716. AssertNull(pkey);
  8717. AssertNull(cert);
  8718. /* check parse with no extra certs kept */
  8719. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8720. AssertIntEQ(ret, 1);
  8721. AssertNotNull(pkey);
  8722. AssertNotNull(cert);
  8723. wolfSSL_EVP_PKEY_free(pkey);
  8724. wolfSSL_X509_free(cert);
  8725. /* check parse with extra certs kept */
  8726. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8727. AssertIntEQ(ret, 1);
  8728. AssertNotNull(pkey);
  8729. AssertNotNull(cert);
  8730. AssertNotNull(ca);
  8731. wolfSSL_EVP_PKEY_free(pkey);
  8732. wolfSSL_X509_free(cert);
  8733. sk_X509_pop_free(ca, NULL);
  8734. PKCS12_free(pkcs12);
  8735. #endif /* HAVE_ECC */
  8736. #ifdef WC_RC2
  8737. /* test PKCS#12 with RC2 encryption */
  8738. f = XFOPEN(rc2p12, "rb");
  8739. AssertTrue(f != XBADFILE);
  8740. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8741. XFCLOSE(f);
  8742. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8743. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8744. /* check verify MAC fail case */
  8745. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8746. AssertIntEQ(ret, 0);
  8747. AssertNull(pkey);
  8748. AssertNull(cert);
  8749. /* check parse iwth not extra certs kept */
  8750. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8751. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8752. AssertNotNull(pkey);
  8753. AssertNotNull(cert);
  8754. wolfSSL_EVP_PKEY_free(pkey);
  8755. wolfSSL_X509_free(cert);
  8756. /* check parse with extra certs kept */
  8757. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8758. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8759. AssertNotNull(pkey);
  8760. AssertNotNull(cert);
  8761. AssertNotNull(ca);
  8762. wolfSSL_EVP_PKEY_free(pkey);
  8763. wolfSSL_X509_free(cert);
  8764. sk_X509_pop_free(ca, NULL);
  8765. BIO_free(bio);
  8766. PKCS12_free(pkcs12);
  8767. #endif /* WC_RC2 */
  8768. /* Test i2d_PKCS12_bio */
  8769. f = XFOPEN(file, "rb");
  8770. AssertTrue((f != XBADFILE));
  8771. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8772. XFCLOSE(f);
  8773. bio = BIO_new(BIO_s_mem());
  8774. AssertNotNull(bio);
  8775. ret = i2d_PKCS12_bio(bio, pkcs12);
  8776. AssertIntEQ(ret, 1);
  8777. ret = i2d_PKCS12_bio(NULL, pkcs12);
  8778. AssertIntEQ(ret, 0);
  8779. ret = i2d_PKCS12_bio(bio, NULL);
  8780. AssertIntEQ(ret, 0);
  8781. PKCS12_free(pkcs12);
  8782. BIO_free(bio);
  8783. (void)order;
  8784. res = TEST_RES_CHECK(1);
  8785. #endif /* OPENSSL_EXTRA */
  8786. #endif /* HAVE_FIPS */
  8787. return res;
  8788. }
  8789. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  8790. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  8791. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  8792. #define TEST_PKCS8_ENC
  8793. #endif
  8794. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8795. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8796. /* used to keep track if FailTestCallback was called */
  8797. static int failTestCallbackCalled = 0;
  8798. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  8799. {
  8800. (void)passwd;
  8801. (void)sz;
  8802. (void)rw;
  8803. (void)userdata;
  8804. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  8805. failTestCallbackCalled = 1;
  8806. return -1;
  8807. }
  8808. #endif
  8809. static int test_wolfSSL_no_password_cb(void)
  8810. {
  8811. int res = TEST_SKIPPED;
  8812. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8813. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8814. WOLFSSL_CTX* ctx;
  8815. byte buff[FOURK_BUF];
  8816. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8817. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8818. XFILE f;
  8819. int bytes;
  8820. #ifndef NO_WOLFSSL_CLIENT
  8821. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  8822. #else
  8823. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  8824. #endif
  8825. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  8826. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  8827. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8828. XFCLOSE(f);
  8829. AssertIntLE(bytes, sizeof(buff));
  8830. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8831. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8832. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  8833. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8834. XFCLOSE(f);
  8835. AssertIntLE(bytes, sizeof(buff));
  8836. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8837. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8838. wolfSSL_CTX_free(ctx);
  8839. if (failTestCallbackCalled != 0) {
  8840. Fail(("Password callback should not be called by default"),
  8841. ("Password callback was called without attempting "
  8842. "to first decipher private key without password."));
  8843. }
  8844. res = TEST_RES_CHECK(1);
  8845. #endif
  8846. return res;
  8847. }
  8848. #ifdef TEST_PKCS8_ENC
  8849. /* for PKCS8 test case */
  8850. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  8851. {
  8852. int flag = 0;
  8853. (void)rw;
  8854. if (userdata != NULL) {
  8855. flag = *((int*)userdata); /* user set data */
  8856. }
  8857. switch (flag) {
  8858. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  8859. * can be anything the user wishes to be passed to the callback
  8860. * associated with the WOLFSSL_CTX */
  8861. XSTRNCPY(passwd, "yassl123", sz);
  8862. return 8;
  8863. default:
  8864. return BAD_FUNC_ARG;
  8865. }
  8866. }
  8867. #endif /* TEST_PKCS8_ENC */
  8868. /* Testing functions dealing with PKCS8 */
  8869. static int test_wolfSSL_PKCS8(void)
  8870. {
  8871. int res = TEST_SKIPPED;
  8872. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  8873. !defined(WOLFCRYPT_ONLY)
  8874. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8875. byte buff[FOURK_BUF];
  8876. byte der[FOURK_BUF];
  8877. #ifndef NO_RSA
  8878. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  8879. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  8880. #endif
  8881. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8882. #ifdef HAVE_ECC
  8883. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8884. #endif
  8885. XFILE f;
  8886. int bytes;
  8887. WOLFSSL_CTX* ctx;
  8888. #if defined(HAVE_ECC) && !defined(NO_CODING)
  8889. int ret;
  8890. ecc_key key;
  8891. word32 x = 0;
  8892. #endif
  8893. #ifdef TEST_PKCS8_ENC
  8894. #if !defined(NO_RSA) && !defined(NO_SHA)
  8895. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  8896. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  8897. #endif
  8898. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8899. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  8900. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  8901. #endif
  8902. int flag;
  8903. #endif
  8904. (void)der;
  8905. #ifndef NO_WOLFSSL_CLIENT
  8906. #ifndef WOLFSSL_NO_TLS12
  8907. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8908. #else
  8909. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8910. #endif
  8911. #else
  8912. #ifndef WOLFSSL_NO_TLS12
  8913. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  8914. #else
  8915. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  8916. #endif
  8917. #endif
  8918. #ifdef TEST_PKCS8_ENC
  8919. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  8920. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  8921. flag = 1; /* used by password callback as return code */
  8922. #if !defined(NO_RSA) && !defined(NO_SHA)
  8923. /* test loading PEM PKCS8 encrypted file */
  8924. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  8925. AssertTrue((f != XBADFILE));
  8926. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8927. XFCLOSE(f);
  8928. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8929. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8930. /* this next case should fail because of password callback return code */
  8931. flag = 0; /* used by password callback as return code */
  8932. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8933. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8934. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8935. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8936. "yassl123"), 0);
  8937. /* test that error value is returned with a bad password */
  8938. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8939. "bad"), 0);
  8940. /* test loading PEM PKCS8 encrypted file */
  8941. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  8942. AssertTrue((f != XBADFILE));
  8943. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8944. XFCLOSE(f);
  8945. flag = 1; /* used by password callback as return code */
  8946. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8947. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8948. /* this next case should fail because of password callback return code */
  8949. flag = 0; /* used by password callback as return code */
  8950. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8951. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8952. #endif /* !NO_RSA && !NO_SHA */
  8953. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8954. /* test loading PEM PKCS8 encrypted ECC Key file */
  8955. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  8956. AssertTrue((f != XBADFILE));
  8957. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8958. XFCLOSE(f);
  8959. flag = 1; /* used by password callback as return code */
  8960. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8961. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8962. /* this next case should fail because of password callback return code */
  8963. flag = 0; /* used by password callback as return code */
  8964. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8965. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8966. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8967. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8968. "yassl123"), 0);
  8969. /* test that error value is returned with a bad password */
  8970. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8971. "bad"), 0);
  8972. /* test loading DER PKCS8 encrypted ECC Key file */
  8973. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  8974. AssertTrue((f != XBADFILE));
  8975. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8976. XFCLOSE(f);
  8977. flag = 1; /* used by password callback as return code */
  8978. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8979. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8980. /* this next case should fail because of password callback return code */
  8981. flag = 0; /* used by password callback as return code */
  8982. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8983. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8984. /* leave flag as "okay" */
  8985. flag = 1;
  8986. #endif /* HAVE_ECC && !NO_SHA */
  8987. #endif /* TEST_PKCS8_ENC */
  8988. #ifndef NO_RSA
  8989. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  8990. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  8991. AssertTrue((f != XBADFILE));
  8992. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8993. XFCLOSE(f);
  8994. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8995. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8996. /* test loading PEM PKCS8 private key file (not encrypted) */
  8997. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  8998. AssertTrue((f != XBADFILE));
  8999. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  9000. XFCLOSE(f);
  9001. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9002. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9003. #endif /* !NO_RSA */
  9004. /* Test PKCS8 PEM ECC key no crypt */
  9005. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  9006. AssertTrue((f != XBADFILE));
  9007. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  9008. XFCLOSE(f);
  9009. #ifdef HAVE_ECC
  9010. /* Test PKCS8 PEM ECC key no crypt */
  9011. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9012. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9013. #ifndef NO_CODING
  9014. /* decrypt PKCS8 PEM to key in DER format */
  9015. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  9016. (word32)sizeof(der), NULL)), 0);
  9017. ret = wc_ecc_init(&key);
  9018. if (ret == 0) {
  9019. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  9020. wc_ecc_free(&key);
  9021. }
  9022. AssertIntEQ(ret, 0);
  9023. #endif
  9024. /* Test PKCS8 DER ECC key no crypt */
  9025. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  9026. AssertTrue((f != XBADFILE));
  9027. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  9028. XFCLOSE(f);
  9029. /* Test using a PKCS8 ECC PEM */
  9030. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9031. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9032. #else
  9033. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  9034. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  9035. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  9036. #endif /* HAVE_ECC */
  9037. wolfSSL_CTX_free(ctx);
  9038. res = TEST_RES_CHECK(1);
  9039. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9040. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  9041. return res;
  9042. }
  9043. static int test_wolfSSL_PKCS8_ED25519(void)
  9044. {
  9045. int res = TEST_SKIPPED;
  9046. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  9047. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  9048. defined(HAVE_ED25519_KEY_IMPORT)
  9049. const byte encPrivKey[] = \
  9050. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  9051. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  9052. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  9053. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  9054. "2L9W4v7swXkV+X+a1ww=\n"
  9055. "-----END ENCRYPTED PRIVATE KEY-----\n";
  9056. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  9057. byte der[FOURK_BUF];
  9058. WOLFSSL_CTX* ctx;
  9059. int bytes;
  9060. XMEMSET(der, 0, sizeof(der));
  9061. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  9062. (word32)sizeof(der), password)), 0);
  9063. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9064. #ifndef NO_WOLFSSL_SERVER
  9065. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9066. #else
  9067. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9068. #endif
  9069. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  9070. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9071. wolfSSL_CTX_free(ctx);
  9072. res = TEST_RES_CHECK(1);
  9073. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9074. #endif
  9075. return res;
  9076. }
  9077. static int test_wolfSSL_PKCS8_ED448(void)
  9078. {
  9079. int res = TEST_SKIPPED;
  9080. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  9081. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  9082. defined(HAVE_ED448_KEY_IMPORT)
  9083. const byte encPrivKey[] = \
  9084. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  9085. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  9086. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  9087. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  9088. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  9089. "-----END ENCRYPTED PRIVATE KEY-----\n";
  9090. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  9091. byte der[FOURK_BUF];
  9092. WOLFSSL_CTX* ctx;
  9093. int bytes;
  9094. XMEMSET(der, 0, sizeof(der));
  9095. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  9096. (word32)sizeof(der), password)), 0);
  9097. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9098. #ifndef NO_WOLFSSL_SERVER
  9099. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9100. #else
  9101. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9102. #endif
  9103. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  9104. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9105. wolfSSL_CTX_free(ctx);
  9106. res = TEST_RES_CHECK(1);
  9107. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9108. #endif
  9109. return res;
  9110. }
  9111. /* Testing functions dealing with PKCS5 */
  9112. static int test_wolfSSL_PKCS5(void)
  9113. {
  9114. int res = TEST_SKIPPED;
  9115. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  9116. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  9117. const char* passwd = "myfipsPa$$W0rd";
  9118. #else
  9119. const char *passwd = "pass1234";
  9120. #endif
  9121. const unsigned char *salt = (unsigned char *)"salt1234";
  9122. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  9123. DYNAMIC_TYPE_TMP_BUFFER);
  9124. int ret = 0;
  9125. AssertNotNull(out);
  9126. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  9127. (int)XSTRLEN((const char *) salt), 10,
  9128. WC_SHA_DIGEST_SIZE,out);
  9129. AssertIntEQ(ret, SSL_SUCCESS);
  9130. #ifdef WOLFSSL_SHA512
  9131. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  9132. (int)XSTRLEN((const char *) salt), 10,
  9133. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  9134. AssertIntEQ(ret, SSL_SUCCESS);
  9135. #endif
  9136. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9137. res = TEST_RES_CHECK(1);
  9138. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  9139. return res;
  9140. }
  9141. /* test parsing URI from certificate */
  9142. static int test_wolfSSL_URI(void)
  9143. {
  9144. int res = TEST_SKIPPED;
  9145. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9146. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  9147. defined(OPENSSL_EXTRA))
  9148. WOLFSSL_X509* x509;
  9149. const char uri[] = "./certs/client-uri-cert.pem";
  9150. const char urn[] = "./certs/client-absolute-urn.pem";
  9151. const char badUri[] = "./certs/client-relative-uri.pem";
  9152. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  9153. AssertNotNull(x509);
  9154. wolfSSL_FreeX509(x509);
  9155. x509 = wolfSSL_X509_load_certificate_file(urn, WOLFSSL_FILETYPE_PEM);
  9156. AssertNotNull(x509);
  9157. wolfSSL_FreeX509(x509);
  9158. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  9159. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  9160. && !defined(WOLFSSL_FPKI)
  9161. AssertNull(x509);
  9162. #else
  9163. AssertNotNull(x509);
  9164. wolfSSL_FreeX509(x509);
  9165. #endif
  9166. res = TEST_RES_CHECK(1);
  9167. #endif
  9168. return res;
  9169. }
  9170. static int test_wolfSSL_TBS(void)
  9171. {
  9172. int res = TEST_SKIPPED;
  9173. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9174. && defined(OPENSSL_EXTRA)
  9175. WOLFSSL_X509* x509;
  9176. const unsigned char* tbs;
  9177. int tbsSz;
  9178. AssertNotNull(x509 =
  9179. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  9180. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  9181. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  9182. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  9183. AssertIntEQ(tbsSz, 1003);
  9184. wolfSSL_FreeX509(x509);
  9185. res = TEST_RES_CHECK(1);
  9186. #endif
  9187. return res;
  9188. }
  9189. static int test_wolfSSL_X509_verify(void)
  9190. {
  9191. int res = TEST_SKIPPED;
  9192. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9193. && defined(OPENSSL_EXTRA)
  9194. WOLFSSL_X509* ca;
  9195. WOLFSSL_X509* serv;
  9196. WOLFSSL_EVP_PKEY* pkey;
  9197. unsigned char buf[2048];
  9198. const unsigned char* pt = NULL;
  9199. int bufSz;
  9200. AssertNotNull(ca =
  9201. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  9202. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  9203. WOLFSSL_SUCCESS);
  9204. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  9205. WOLFSSL_SUCCESS);
  9206. AssertIntEQ(bufSz, 294);
  9207. bufSz = 2048;
  9208. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  9209. WOLFSSL_SUCCESS);
  9210. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  9211. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  9212. AssertNotNull(serv =
  9213. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  9214. /* success case */
  9215. pt = buf;
  9216. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  9217. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  9218. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  9219. wolfSSL_EVP_PKEY_free(pkey);
  9220. /* fail case */
  9221. bufSz = 2048;
  9222. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  9223. WOLFSSL_SUCCESS);
  9224. pt = buf;
  9225. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  9226. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  9227. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  9228. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  9229. wolfSSL_EVP_PKEY_free(pkey);
  9230. wolfSSL_FreeX509(ca);
  9231. wolfSSL_FreeX509(serv);
  9232. res = TEST_RES_CHECK(1);
  9233. #endif
  9234. return res;
  9235. }
  9236. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  9237. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  9238. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  9239. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) \
  9240. && !defined(NO_ASN_TIME)
  9241. /* create certificate with version 2 */
  9242. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  9243. {
  9244. WOLFSSL_X509 *x509, *x509v2;
  9245. WOLFSSL_EVP_PKEY *priv, *pub;
  9246. unsigned char *der = NULL, *key = NULL, *pt;
  9247. char *header, *name;
  9248. int derSz;
  9249. long keySz;
  9250. XFILE fp;
  9251. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  9252. time_t t;
  9253. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9254. WOLFSSL_FILETYPE_PEM));
  9255. fp = XFOPEN(cliKeyFile, "rb");
  9256. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  9257. WOLFSSL_SUCCESS);
  9258. XFCLOSE(fp);
  9259. pt = key;
  9260. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  9261. (const unsigned char**)&pt, keySz));
  9262. /* create the version 2 certificate */
  9263. AssertNotNull(x509v2 = X509_new());
  9264. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  9265. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  9266. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  9267. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  9268. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  9269. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  9270. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  9271. t = time(NULL);
  9272. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  9273. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  9274. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  9275. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  9276. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  9277. derSz = wolfSSL_i2d_X509(x509v2, &der);
  9278. AssertIntGT(derSz, 0);
  9279. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  9280. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9281. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  9282. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9283. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9284. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9285. wolfSSL_X509_free(x509);
  9286. wolfSSL_X509_free(x509v2);
  9287. wolfSSL_EVP_PKEY_free(priv);
  9288. wolfSSL_EVP_PKEY_free(pub);
  9289. wolfSSL_ASN1_TIME_free(notBefore);
  9290. wolfSSL_ASN1_TIME_free(notAfter);
  9291. }
  9292. /* override certificate version error */
  9293. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  9294. {
  9295. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  9296. AssertIntEQ(store->error, ASN_VERSION_E);
  9297. #else
  9298. AssertIntEQ(store->error, 0);
  9299. #endif
  9300. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  9301. (void)preverify;
  9302. return 1;
  9303. }
  9304. /* set verify callback that will override bad certificate version */
  9305. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  9306. {
  9307. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  9308. }
  9309. #endif
  9310. static int test_wolfSSL_X509_TLS_version(void)
  9311. {
  9312. int res = TEST_SKIPPED;
  9313. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  9314. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  9315. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  9316. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) \
  9317. && !defined(NO_ASN_TIME)
  9318. tcp_ready ready;
  9319. func_args server_args;
  9320. func_args client_args;
  9321. THREAD_TYPE serverThread;
  9322. callback_functions func_cb_client;
  9323. callback_functions func_cb_server;
  9324. /* test server rejects a client certificate that is not version 3 */
  9325. #ifdef WOLFSSL_TIRTOS
  9326. fdOpenSession(Task_self());
  9327. #endif
  9328. XMEMSET(&server_args, 0, sizeof(func_args));
  9329. XMEMSET(&client_args, 0, sizeof(func_args));
  9330. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  9331. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  9332. StartTCP();
  9333. InitTcpReady(&ready);
  9334. #if defined(USE_WINDOWS_API)
  9335. /* use RNG to get random port if using windows */
  9336. ready.port = GetRandomPort();
  9337. #endif
  9338. server_args.signal = &ready;
  9339. client_args.signal = &ready;
  9340. server_args.return_code = TEST_FAIL;
  9341. client_args.return_code = TEST_FAIL;
  9342. func_cb_client.ctx_ready = &test_set_x509_badversion;
  9343. #ifndef WOLFSSL_NO_TLS12
  9344. func_cb_client.method = wolfTLSv1_2_client_method;
  9345. #else
  9346. func_cb_client.method = wolfTLSv1_3_client_method;
  9347. #endif
  9348. client_args.callbacks = &func_cb_client;
  9349. #ifndef WOLFSSL_NO_TLS12
  9350. func_cb_server.method = wolfTLSv1_2_server_method;
  9351. #else
  9352. func_cb_server.method = wolfTLSv1_3_server_method;
  9353. #endif
  9354. server_args.callbacks = &func_cb_server;
  9355. start_thread(test_server_nofail, &server_args, &serverThread);
  9356. wait_tcp_ready(&server_args);
  9357. test_client_nofail(&client_args, NULL);
  9358. join_thread(serverThread);
  9359. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  9360. AssertIntEQ(client_args.return_code, TEST_FAIL);
  9361. AssertIntEQ(server_args.return_code, TEST_FAIL);
  9362. #else
  9363. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  9364. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  9365. #endif
  9366. FreeTcpReady(&ready);
  9367. #ifdef WOLFSSL_TIRTOS
  9368. fdCloseSession(Task_self());
  9369. #endif
  9370. /* Now re run but override the bad X509 version */
  9371. #ifdef WOLFSSL_TIRTOS
  9372. fdOpenSession(Task_self());
  9373. #endif
  9374. XMEMSET(&server_args, 0, sizeof(func_args));
  9375. XMEMSET(&client_args, 0, sizeof(func_args));
  9376. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  9377. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  9378. StartTCP();
  9379. InitTcpReady(&ready);
  9380. #if defined(USE_WINDOWS_API)
  9381. /* use RNG to get random port if using windows */
  9382. ready.port = GetRandomPort();
  9383. #endif
  9384. server_args.signal = &ready;
  9385. client_args.signal = &ready;
  9386. server_args.return_code = TEST_FAIL;
  9387. client_args.return_code = TEST_FAIL;
  9388. func_cb_client.ctx_ready = &test_set_x509_badversion;
  9389. func_cb_server.ctx_ready = &test_set_override_x509;
  9390. #ifndef WOLFSSL_NO_TLS12
  9391. func_cb_client.method = wolfTLSv1_2_client_method;
  9392. #else
  9393. func_cb_client.method = wolfTLSv1_3_client_method;
  9394. #endif
  9395. client_args.callbacks = &func_cb_client;
  9396. #ifndef WOLFSSL_NO_TLS12
  9397. func_cb_server.method = wolfTLSv1_2_server_method;
  9398. #else
  9399. func_cb_server.method = wolfTLSv1_3_server_method;
  9400. #endif
  9401. server_args.callbacks = &func_cb_server;
  9402. start_thread(test_server_nofail, &server_args, &serverThread);
  9403. wait_tcp_ready(&server_args);
  9404. test_client_nofail(&client_args, NULL);
  9405. join_thread(serverThread);
  9406. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  9407. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  9408. FreeTcpReady(&ready);
  9409. #ifdef WOLFSSL_TIRTOS
  9410. fdCloseSession(Task_self());
  9411. #endif
  9412. res = TEST_RES_CHECK(1);
  9413. #endif
  9414. return res;
  9415. }
  9416. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  9417. * version allowed.
  9418. * POST: 1 on success.
  9419. */
  9420. static int test_wolfSSL_CTX_SetMinVersion(void)
  9421. {
  9422. int res = TEST_SKIPPED;
  9423. #ifndef NO_WOLFSSL_CLIENT
  9424. int failFlag = WOLFSSL_SUCCESS;
  9425. WOLFSSL_CTX* ctx;
  9426. int itr;
  9427. #ifndef NO_OLD_TLS
  9428. const int versions[] = {
  9429. #ifdef WOLFSSL_ALLOW_TLSV10
  9430. WOLFSSL_TLSV1,
  9431. #endif
  9432. WOLFSSL_TLSV1_1,
  9433. WOLFSSL_TLSV1_2 };
  9434. #elif !defined(WOLFSSL_NO_TLS12)
  9435. const int versions[] = { WOLFSSL_TLSV1_2 };
  9436. #elif defined(WOLFSSL_TLS13)
  9437. const int versions[] = { WOLFSSL_TLSV1_3 };
  9438. #else
  9439. const int versions[0];
  9440. #endif
  9441. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9442. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  9443. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  9444. != WOLFSSL_SUCCESS) {
  9445. failFlag = WOLFSSL_FAILURE;
  9446. }
  9447. }
  9448. wolfSSL_CTX_free(ctx);
  9449. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  9450. #endif
  9451. return res;
  9452. } /* END test_wolfSSL_CTX_SetMinVersion */
  9453. /*----------------------------------------------------------------------------*
  9454. | OCSP Stapling
  9455. *----------------------------------------------------------------------------*/
  9456. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  9457. * need to contact the CA, lowering the cost of cert revocation checking.
  9458. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  9459. * POST: 1 returned for success.
  9460. */
  9461. static int test_wolfSSL_UseOCSPStapling(void)
  9462. {
  9463. int res = TEST_SKIPPED;
  9464. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  9465. !defined(NO_WOLFSSL_CLIENT)
  9466. int ret;
  9467. WOLFSSL_CTX* ctx;
  9468. WOLFSSL* ssl;
  9469. #ifndef NO_WOLFSSL_CLIENT
  9470. #ifndef WOLFSSL_NO_TLS12
  9471. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  9472. #else
  9473. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  9474. #endif
  9475. #else
  9476. #ifndef WOLFSSL_NO_TLS12
  9477. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  9478. #else
  9479. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  9480. #endif
  9481. #endif
  9482. ssl = wolfSSL_new(ctx);
  9483. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  9484. WOLFSSL_CSR2_OCSP_USE_NONCE);
  9485. wolfSSL_free(ssl);
  9486. wolfSSL_CTX_free(ctx);
  9487. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  9488. #endif
  9489. return res;
  9490. } /*END test_wolfSSL_UseOCSPStapling */
  9491. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  9492. * stapling eliminates the need to contact the CA and lowers cert revocation
  9493. * check.
  9494. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  9495. */
  9496. static int test_wolfSSL_UseOCSPStaplingV2(void)
  9497. {
  9498. int res = TEST_SKIPPED;
  9499. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  9500. !defined(NO_WOLFSSL_CLIENT)
  9501. int ret;
  9502. WOLFSSL_CTX* ctx;
  9503. WOLFSSL* ssl;
  9504. #ifndef NO_WOLFSSL_CLIENT
  9505. #ifndef WOLFSSL_NO_TLS12
  9506. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  9507. #else
  9508. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  9509. #endif
  9510. #else
  9511. #ifndef WOLFSSL_NO_TLS12
  9512. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  9513. #else
  9514. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  9515. #endif
  9516. #endif
  9517. ssl = wolfSSL_new(ctx);
  9518. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  9519. WOLFSSL_CSR2_OCSP_USE_NONCE );
  9520. wolfSSL_free(ssl);
  9521. wolfSSL_CTX_free(ctx);
  9522. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  9523. #endif
  9524. return res;
  9525. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  9526. /*----------------------------------------------------------------------------*
  9527. | Multicast Tests
  9528. *----------------------------------------------------------------------------*/
  9529. static int test_wolfSSL_mcast(void)
  9530. {
  9531. int res = TEST_SKIPPED;
  9532. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  9533. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  9534. WOLFSSL_CTX* ctx;
  9535. WOLFSSL* ssl;
  9536. int result;
  9537. byte preMasterSecret[512];
  9538. byte clientRandom[32];
  9539. byte serverRandom[32];
  9540. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  9541. byte buf[256];
  9542. word16 newId;
  9543. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  9544. AssertNotNull(ctx);
  9545. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  9546. AssertIntEQ(result, WOLFSSL_SUCCESS);
  9547. ssl = wolfSSL_new(ctx);
  9548. AssertNotNull(ssl);
  9549. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  9550. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  9551. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  9552. result = wolfSSL_set_secret(ssl, 23,
  9553. preMasterSecret, sizeof(preMasterSecret),
  9554. clientRandom, serverRandom, suite);
  9555. AssertIntEQ(result, WOLFSSL_SUCCESS);
  9556. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  9557. AssertIntLE(result, 0);
  9558. AssertIntLE(newId, 100);
  9559. wolfSSL_free(ssl);
  9560. wolfSSL_CTX_free(ctx);
  9561. res = TEST_RES_CHECK(1);
  9562. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  9563. * WOLFSSL_SNIFFER) */
  9564. return res;
  9565. }
  9566. /*----------------------------------------------------------------------------*
  9567. | Wolfcrypt
  9568. *----------------------------------------------------------------------------*/
  9569. /*
  9570. * Unit test for the wc_InitBlake2b()
  9571. */
  9572. static int test_wc_InitBlake2b(void)
  9573. {
  9574. int res = TEST_SKIPPED;
  9575. #ifdef HAVE_BLAKE2
  9576. Blake2b blake;
  9577. int ret = 0;
  9578. /* Test good arg. */
  9579. ret = wc_InitBlake2b(&blake, 64);
  9580. if (ret != 0) {
  9581. ret = WOLFSSL_FATAL_ERROR;
  9582. }
  9583. /* Test bad arg. */
  9584. if (!ret) {
  9585. ret = wc_InitBlake2b(NULL, 64);
  9586. if (ret == 0) {
  9587. ret = WOLFSSL_FATAL_ERROR;
  9588. }
  9589. else {
  9590. ret = 0;
  9591. }
  9592. }
  9593. if (!ret) {
  9594. ret = wc_InitBlake2b(NULL, 128);
  9595. if (ret == 0) {
  9596. ret = WOLFSSL_FATAL_ERROR;
  9597. }
  9598. else {
  9599. ret = 0;
  9600. }
  9601. }
  9602. if (!ret) {
  9603. ret = wc_InitBlake2b(&blake, 128);
  9604. if (ret == 0) {
  9605. ret = WOLFSSL_FATAL_ERROR;
  9606. }
  9607. else {
  9608. ret = 0;
  9609. }
  9610. }
  9611. if (!ret) {
  9612. ret = wc_InitBlake2b(NULL, 0);
  9613. if (ret == 0) {
  9614. ret = WOLFSSL_FATAL_ERROR;
  9615. }
  9616. else {
  9617. ret = 0;
  9618. }
  9619. }
  9620. if (!ret) {
  9621. ret = wc_InitBlake2b(&blake, 0);
  9622. if (ret == 0) {
  9623. ret = WOLFSSL_FATAL_ERROR;
  9624. }
  9625. else {
  9626. ret = 0;
  9627. }
  9628. }
  9629. res = TEST_RES_CHECK(ret == 0);
  9630. #endif
  9631. return res;
  9632. } /*END test_wc_InitBlake2b*/
  9633. /*
  9634. * Unit test for the wc_InitBlake2b_WithKey()
  9635. */
  9636. static int test_wc_InitBlake2b_WithKey(void)
  9637. {
  9638. int res = TEST_SKIPPED;
  9639. #ifdef HAVE_BLAKE2
  9640. Blake2b blake;
  9641. word32 digestSz = BLAKE2B_KEYBYTES;
  9642. byte key[BLAKE2B_KEYBYTES];
  9643. word32 keylen = BLAKE2B_KEYBYTES;
  9644. int ret = 0;
  9645. XMEMSET(key, 0, sizeof(key));
  9646. /* Test good arg. */
  9647. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  9648. if (ret != 0) {
  9649. ret = WOLFSSL_FATAL_ERROR;
  9650. }
  9651. /* Test bad args. */
  9652. if (ret == 0) {
  9653. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  9654. if (ret == BAD_FUNC_ARG) {
  9655. ret = 0;
  9656. }
  9657. }
  9658. if (ret == 0) {
  9659. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  9660. if (ret == BAD_FUNC_ARG) {
  9661. ret = 0;
  9662. }
  9663. }
  9664. if (ret == 0) {
  9665. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  9666. }
  9667. res = TEST_RES_CHECK(ret == 0);
  9668. #endif
  9669. return res;
  9670. } /*END wc_InitBlake2b_WithKey*/
  9671. /*
  9672. * Unit test for the wc_InitBlake2s_WithKey()
  9673. */
  9674. static int test_wc_InitBlake2s_WithKey(void)
  9675. {
  9676. int res = TEST_SKIPPED;
  9677. #ifdef HAVE_BLAKE2S
  9678. Blake2s blake;
  9679. word32 digestSz = BLAKE2S_KEYBYTES;
  9680. byte *key = (byte*)"01234567890123456789012345678901";
  9681. word32 keylen = BLAKE2S_KEYBYTES;
  9682. int ret = 0;
  9683. /* Test good arg. */
  9684. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  9685. if (ret != 0) {
  9686. ret = WOLFSSL_FATAL_ERROR;
  9687. }
  9688. /* Test bad args. */
  9689. if (ret == 0) {
  9690. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  9691. if (ret == BAD_FUNC_ARG) {
  9692. ret = 0;
  9693. }
  9694. }
  9695. if (ret == 0) {
  9696. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  9697. if (ret == BAD_FUNC_ARG) {
  9698. ret = 0;
  9699. }
  9700. }
  9701. if (ret == 0) {
  9702. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  9703. }
  9704. res = TEST_RES_CHECK(ret == 0);
  9705. #endif
  9706. return res;
  9707. } /*END wc_InitBlake2s_WithKey*/
  9708. /*
  9709. * Unit test for the wc_InitMd5()
  9710. */
  9711. static int test_wc_InitMd5(void)
  9712. {
  9713. int res = TEST_SKIPPED;
  9714. #ifndef NO_MD5
  9715. wc_Md5 md5;
  9716. int ret;
  9717. int flag = 0;
  9718. /* Test good arg. */
  9719. ret = wc_InitMd5(&md5);
  9720. if (ret != 0) {
  9721. flag = WOLFSSL_FATAL_ERROR;
  9722. }
  9723. /* Test bad arg. */
  9724. if (!flag) {
  9725. ret = wc_InitMd5(NULL);
  9726. if (ret != BAD_FUNC_ARG) {
  9727. flag = WOLFSSL_FATAL_ERROR;
  9728. }
  9729. }
  9730. wc_Md5Free(&md5);
  9731. res = TEST_RES_CHECK(flag == 0);
  9732. #endif
  9733. return res;
  9734. } /* END test_wc_InitMd5 */
  9735. /*
  9736. * Testing wc_UpdateMd5()
  9737. */
  9738. static int test_wc_Md5Update(void)
  9739. {
  9740. int res = TEST_SKIPPED;
  9741. #ifndef NO_MD5
  9742. wc_Md5 md5;
  9743. byte hash[WC_MD5_DIGEST_SIZE];
  9744. testVector a, b, c;
  9745. int ret;
  9746. int flag = 0;
  9747. ret = wc_InitMd5(&md5);
  9748. if (ret != 0) {
  9749. flag = ret;
  9750. }
  9751. /* Input */
  9752. if (!flag) {
  9753. a.input = "a";
  9754. a.inLen = XSTRLEN(a.input);
  9755. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  9756. if (ret != 0) {
  9757. flag = ret;
  9758. }
  9759. }
  9760. if (!flag) {
  9761. ret = wc_Md5Final(&md5, hash);
  9762. if (ret != 0) {
  9763. flag = ret;
  9764. }
  9765. }
  9766. /* Update input. */
  9767. if (!flag) {
  9768. a.input = "abc";
  9769. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  9770. "\x72";
  9771. a.inLen = XSTRLEN(a.input);
  9772. a.outLen = XSTRLEN(a.output);
  9773. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  9774. if (ret != 0) {
  9775. flag = ret;
  9776. }
  9777. }
  9778. if (!flag) {
  9779. ret = wc_Md5Final(&md5, hash);
  9780. if (ret != 0) {
  9781. flag = ret;
  9782. }
  9783. }
  9784. if (!flag) {
  9785. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  9786. flag = WOLFSSL_FATAL_ERROR;
  9787. }
  9788. }
  9789. /*Pass in bad values. */
  9790. if (!flag) {
  9791. b.input = NULL;
  9792. b.inLen = 0;
  9793. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  9794. if (ret != 0) {
  9795. flag = ret;
  9796. }
  9797. }
  9798. if (!flag) {
  9799. c.input = NULL;
  9800. c.inLen = WC_MD5_DIGEST_SIZE;
  9801. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  9802. if (ret != BAD_FUNC_ARG) {
  9803. flag = WOLFSSL_FATAL_ERROR;
  9804. }
  9805. }
  9806. if (!flag) {
  9807. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  9808. if (ret != BAD_FUNC_ARG) {
  9809. flag = WOLFSSL_FATAL_ERROR;
  9810. }
  9811. }
  9812. wc_Md5Free(&md5);
  9813. res = TEST_RES_CHECK(flag == 0);
  9814. #endif
  9815. return res;
  9816. } /* END test_wc_Md5Update() */
  9817. /*
  9818. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  9819. */
  9820. static int test_wc_Md5Final(void)
  9821. {
  9822. int res = TEST_SKIPPED;
  9823. #ifndef NO_MD5
  9824. /* Instantiate */
  9825. wc_Md5 md5;
  9826. byte* hash_test[3];
  9827. byte hash1[WC_MD5_DIGEST_SIZE];
  9828. byte hash2[2*WC_MD5_DIGEST_SIZE];
  9829. byte hash3[5*WC_MD5_DIGEST_SIZE];
  9830. int times, i, ret;
  9831. int flag = 0;
  9832. /* Initialize */
  9833. ret = wc_InitMd5(&md5);
  9834. if (ret != 0) {
  9835. flag = ret;
  9836. }
  9837. if (!flag) {
  9838. hash_test[0] = hash1;
  9839. hash_test[1] = hash2;
  9840. hash_test[2] = hash3;
  9841. }
  9842. times = sizeof(hash_test)/sizeof(byte*);
  9843. for (i = 0; i < times; i++) {
  9844. if (!flag) {
  9845. ret = wc_Md5Final(&md5, hash_test[i]);
  9846. if (ret != 0) {
  9847. flag = WOLFSSL_FATAL_ERROR;
  9848. }
  9849. }
  9850. }
  9851. /* Test bad args. */
  9852. if (!flag) {
  9853. ret = wc_Md5Final(NULL, NULL);
  9854. if (ret != BAD_FUNC_ARG) {
  9855. flag = WOLFSSL_FATAL_ERROR;
  9856. }
  9857. }
  9858. if (!flag) {
  9859. ret = wc_Md5Final(NULL, hash1);
  9860. if (ret != BAD_FUNC_ARG) {
  9861. flag = WOLFSSL_FATAL_ERROR;
  9862. }
  9863. }
  9864. if (!flag) {
  9865. ret = wc_Md5Final(&md5, NULL);
  9866. if (ret != BAD_FUNC_ARG) {
  9867. flag = WOLFSSL_FATAL_ERROR;
  9868. }
  9869. }
  9870. wc_Md5Free(&md5);
  9871. res = TEST_RES_CHECK(flag == 0);
  9872. #endif
  9873. return res;
  9874. }
  9875. /*
  9876. * Unit test for the wc_InitSha()
  9877. */
  9878. static int test_wc_InitSha(void)
  9879. {
  9880. int res = TEST_SKIPPED;
  9881. #ifndef NO_SHA
  9882. wc_Sha sha;
  9883. int ret;
  9884. int flag = 0;
  9885. /* Test good arg. */
  9886. ret = wc_InitSha(&sha);
  9887. if (ret != 0) {
  9888. flag = WOLFSSL_FATAL_ERROR;
  9889. }
  9890. /* Test bad arg. */
  9891. if (!flag) {
  9892. ret = wc_InitSha(NULL);
  9893. if (ret != BAD_FUNC_ARG) {
  9894. flag = WOLFSSL_FATAL_ERROR;
  9895. }
  9896. }
  9897. wc_ShaFree(&sha);
  9898. res = TEST_RES_CHECK(flag == 0);
  9899. #endif
  9900. return res;
  9901. } /* END test_wc_InitSha */
  9902. /*
  9903. * Tesing wc_ShaUpdate()
  9904. */
  9905. static int test_wc_ShaUpdate(void)
  9906. {
  9907. int res = TEST_SKIPPED;
  9908. #ifndef NO_SHA
  9909. wc_Sha sha;
  9910. byte hash[WC_SHA_DIGEST_SIZE];
  9911. testVector a, b, c;
  9912. int flag = 0;
  9913. int ret;
  9914. ret = wc_InitSha(&sha);
  9915. if (ret != 0) {
  9916. flag = ret;
  9917. }
  9918. /* Input. */
  9919. if (!flag) {
  9920. a.input = "a";
  9921. a.inLen = XSTRLEN(a.input);
  9922. ret = wc_ShaUpdate(&sha, NULL, 0);
  9923. if (ret != 0) {
  9924. flag = ret;
  9925. }
  9926. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  9927. if (ret != 0) {
  9928. flag = ret;
  9929. }
  9930. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9931. if (ret != 0) {
  9932. flag = ret;
  9933. }
  9934. }
  9935. if (!flag) {
  9936. ret = wc_ShaFinal(&sha, hash);
  9937. if (ret != 0) {
  9938. flag = ret;
  9939. }
  9940. }
  9941. /* Update input. */
  9942. if (!flag) {
  9943. a.input = "abc";
  9944. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  9945. "\x6C\x9C\xD0\xD8\x9D";
  9946. a.inLen = XSTRLEN(a.input);
  9947. a.outLen = XSTRLEN(a.output);
  9948. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9949. if (ret != 0) {
  9950. flag = ret;
  9951. }
  9952. }
  9953. if (!flag) {
  9954. ret = wc_ShaFinal(&sha, hash);
  9955. if (ret !=0) {
  9956. flag = ret;
  9957. }
  9958. }
  9959. if (!flag) {
  9960. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  9961. flag = WOLFSSL_FATAL_ERROR;
  9962. }
  9963. }
  9964. /* Try passing in bad values. */
  9965. if (!flag) {
  9966. b.input = NULL;
  9967. b.inLen = 0;
  9968. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  9969. if (ret != 0) {
  9970. flag = ret;
  9971. }
  9972. }
  9973. if (!flag) {
  9974. c.input = NULL;
  9975. c.inLen = WC_SHA_DIGEST_SIZE;
  9976. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  9977. if (ret != BAD_FUNC_ARG) {
  9978. flag = WOLFSSL_FATAL_ERROR;
  9979. }
  9980. }
  9981. if (!flag) {
  9982. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  9983. if (ret != BAD_FUNC_ARG) {
  9984. flag = WOLFSSL_FATAL_ERROR;
  9985. }
  9986. }
  9987. wc_ShaFree(&sha);
  9988. res = TEST_RES_CHECK(flag == 0);
  9989. #endif
  9990. return res;
  9991. } /* END test_wc_ShaUpdate() */
  9992. /*
  9993. * Unit test on wc_ShaFinal
  9994. */
  9995. static int test_wc_ShaFinal(void)
  9996. {
  9997. int res = TEST_SKIPPED;
  9998. #ifndef NO_SHA
  9999. wc_Sha sha;
  10000. byte* hash_test[3];
  10001. byte hash1[WC_SHA_DIGEST_SIZE];
  10002. byte hash2[2*WC_SHA_DIGEST_SIZE];
  10003. byte hash3[5*WC_SHA_DIGEST_SIZE];
  10004. int times, i, ret;
  10005. int flag = 0;
  10006. /*Initialize*/
  10007. ret = wc_InitSha(&sha);
  10008. if (ret) {
  10009. flag = ret;
  10010. }
  10011. if (!flag) {
  10012. hash_test[0] = hash1;
  10013. hash_test[1] = hash2;
  10014. hash_test[2] = hash3;
  10015. }
  10016. times = sizeof(hash_test)/sizeof(byte*);
  10017. for (i = 0; i < times; i++) {
  10018. if (!flag) {
  10019. ret = wc_ShaFinal(&sha, hash_test[i]);
  10020. if (ret != 0) {
  10021. flag = WOLFSSL_FATAL_ERROR;
  10022. }
  10023. }
  10024. }
  10025. /* Test bad args. */
  10026. if (!flag) {
  10027. ret = wc_ShaFinal(NULL, NULL);
  10028. if (ret != BAD_FUNC_ARG) {
  10029. flag = WOLFSSL_FATAL_ERROR;
  10030. }
  10031. }
  10032. if (!flag) {
  10033. ret = wc_ShaFinal(NULL, hash1);
  10034. if (ret != BAD_FUNC_ARG) {
  10035. flag = WOLFSSL_FATAL_ERROR;
  10036. }
  10037. }
  10038. if (!flag) {
  10039. ret = wc_ShaFinal(&sha, NULL);
  10040. if (ret != BAD_FUNC_ARG) {
  10041. flag = WOLFSSL_FATAL_ERROR;
  10042. }
  10043. }
  10044. wc_ShaFree(&sha);
  10045. res = TEST_RES_CHECK(flag == 0);
  10046. #endif
  10047. return res;
  10048. } /* END test_wc_ShaFinal */
  10049. /*
  10050. * Unit test for wc_InitSha256()
  10051. */
  10052. static int test_wc_InitSha256(void)
  10053. {
  10054. int res = TEST_SKIPPED;
  10055. #ifndef NO_SHA256
  10056. wc_Sha256 sha256;
  10057. int ret;
  10058. int flag = 0;
  10059. /* Test good arg. */
  10060. ret = wc_InitSha256(&sha256);
  10061. if (ret != 0) {
  10062. flag = WOLFSSL_FATAL_ERROR;
  10063. }
  10064. /* Test bad arg. */
  10065. if (!flag) {
  10066. ret = wc_InitSha256(NULL);
  10067. if (ret != BAD_FUNC_ARG) {
  10068. flag = WOLFSSL_FATAL_ERROR;
  10069. }
  10070. }
  10071. wc_Sha256Free(&sha256);
  10072. res = TEST_RES_CHECK(flag == 0);
  10073. #endif
  10074. return res;
  10075. } /* END test_wc_InitSha256 */
  10076. /*
  10077. * Unit test for wc_Sha256Update()
  10078. */
  10079. static int test_wc_Sha256Update(void)
  10080. {
  10081. int res = TEST_SKIPPED;
  10082. #ifndef NO_SHA256
  10083. wc_Sha256 sha256;
  10084. byte hash[WC_SHA256_DIGEST_SIZE];
  10085. testVector a, b, c;
  10086. int ret;
  10087. int flag = 0;
  10088. ret = wc_InitSha256(&sha256);
  10089. if (ret != 0) {
  10090. flag = ret;
  10091. }
  10092. /* Input. */
  10093. if (!flag) {
  10094. a.input = "a";
  10095. a.inLen = XSTRLEN(a.input);
  10096. ret = wc_Sha256Update(&sha256, NULL, 0);
  10097. if (ret != 0) {
  10098. flag = ret;
  10099. }
  10100. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  10101. if (ret != 0) {
  10102. flag = ret;
  10103. }
  10104. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  10105. if (ret != 0) {
  10106. flag = ret;
  10107. }
  10108. }
  10109. if (!flag) {
  10110. ret = wc_Sha256Final(&sha256, hash);
  10111. if (ret != 0) {
  10112. flag = ret;
  10113. }
  10114. }
  10115. /* Update input. */
  10116. if (!flag) {
  10117. a.input = "abc";
  10118. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  10119. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  10120. "\x15\xAD";
  10121. a.inLen = XSTRLEN(a.input);
  10122. a.outLen = XSTRLEN(a.output);
  10123. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  10124. if (ret != 0) {
  10125. flag = ret;
  10126. }
  10127. }
  10128. if (!flag) {
  10129. ret = wc_Sha256Final(&sha256, hash);
  10130. if (ret != 0) {
  10131. flag = ret;
  10132. }
  10133. }
  10134. if (!flag) {
  10135. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  10136. flag = WOLFSSL_FATAL_ERROR;
  10137. }
  10138. }
  10139. /* Try passing in bad values */
  10140. if (!flag) {
  10141. b.input = NULL;
  10142. b.inLen = 0;
  10143. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  10144. if (ret != 0) {
  10145. flag = ret;
  10146. }
  10147. }
  10148. if (!flag) {
  10149. c.input = NULL;
  10150. c.inLen = WC_SHA256_DIGEST_SIZE;
  10151. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  10152. if (ret != BAD_FUNC_ARG) {
  10153. flag = WOLFSSL_FATAL_ERROR;
  10154. }
  10155. }
  10156. if (!flag) {
  10157. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  10158. if (ret != BAD_FUNC_ARG) {
  10159. flag = WOLFSSL_FATAL_ERROR;
  10160. }
  10161. }
  10162. wc_Sha256Free(&sha256);
  10163. res = TEST_RES_CHECK(flag == 0);
  10164. #endif
  10165. return res;
  10166. } /* END test_wc_Sha256Update */
  10167. /*
  10168. * Unit test function for wc_Sha256Final()
  10169. */
  10170. static int test_wc_Sha256Final(void)
  10171. {
  10172. int res = TEST_SKIPPED;
  10173. #ifndef NO_SHA256
  10174. wc_Sha256 sha256;
  10175. byte* hash_test[3];
  10176. byte hash1[WC_SHA256_DIGEST_SIZE];
  10177. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10178. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10179. int times, i, ret;
  10180. int flag = 0;
  10181. /* Initialize */
  10182. ret = wc_InitSha256(&sha256);
  10183. if (ret != 0) {
  10184. flag = ret;
  10185. }
  10186. if (!flag) {
  10187. hash_test[0] = hash1;
  10188. hash_test[1] = hash2;
  10189. hash_test[2] = hash3;
  10190. }
  10191. times = sizeof(hash_test) / sizeof(byte*);
  10192. for (i = 0; i < times; i++) {
  10193. if (!flag) {
  10194. ret = wc_Sha256Final(&sha256, hash_test[i]);
  10195. if (ret != 0) {
  10196. flag = WOLFSSL_FATAL_ERROR;
  10197. }
  10198. }
  10199. }
  10200. /* Test bad args. */
  10201. if (!flag ) {
  10202. ret = wc_Sha256Final(NULL, NULL);
  10203. if (ret != BAD_FUNC_ARG) {
  10204. flag = WOLFSSL_FATAL_ERROR;
  10205. }
  10206. }
  10207. if (!flag) {
  10208. ret = wc_Sha256Final(NULL, hash1);
  10209. if (ret != BAD_FUNC_ARG) {
  10210. flag = WOLFSSL_FATAL_ERROR;
  10211. }
  10212. }
  10213. if (!flag) {
  10214. ret = wc_Sha256Final(&sha256, NULL);
  10215. if (ret != BAD_FUNC_ARG) {
  10216. flag = WOLFSSL_FATAL_ERROR;
  10217. }
  10218. }
  10219. wc_Sha256Free(&sha256);
  10220. res = TEST_RES_CHECK(flag == 0);
  10221. #endif
  10222. return res;
  10223. } /* END test_wc_Sha256Final */
  10224. /*
  10225. * Unit test function for wc_Sha256FinalRaw()
  10226. */
  10227. static int test_wc_Sha256FinalRaw(void)
  10228. {
  10229. int res = TEST_SKIPPED;
  10230. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  10231. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  10232. !defined(WOLFSSL_NO_HASH_RAW)
  10233. wc_Sha256 sha256;
  10234. byte* hash_test[3];
  10235. byte hash1[WC_SHA256_DIGEST_SIZE];
  10236. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10237. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10238. int times, i, ret;
  10239. int flag = 0;
  10240. /* Initialize */
  10241. ret = wc_InitSha256(&sha256);
  10242. if (ret != 0) {
  10243. flag = ret;
  10244. }
  10245. if (!flag) {
  10246. hash_test[0] = hash1;
  10247. hash_test[1] = hash2;
  10248. hash_test[2] = hash3;
  10249. }
  10250. times = sizeof(hash_test) / sizeof(byte*);
  10251. for (i = 0; i < times; i++) {
  10252. if (!flag) {
  10253. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  10254. if (ret != 0) {
  10255. flag = WOLFSSL_FATAL_ERROR;
  10256. }
  10257. }
  10258. }
  10259. /* Test bad args. */
  10260. if (!flag) {
  10261. ret = wc_Sha256FinalRaw(NULL, NULL);
  10262. if (ret != BAD_FUNC_ARG) {
  10263. flag = WOLFSSL_FATAL_ERROR;
  10264. }
  10265. }
  10266. if (!flag) {
  10267. ret = wc_Sha256FinalRaw(NULL, hash1);
  10268. if (ret != BAD_FUNC_ARG) {
  10269. flag = WOLFSSL_FATAL_ERROR;
  10270. }
  10271. }
  10272. if (!flag) {
  10273. ret = wc_Sha256FinalRaw(&sha256, NULL);
  10274. if (ret != BAD_FUNC_ARG) {
  10275. flag = WOLFSSL_FATAL_ERROR;
  10276. }
  10277. }
  10278. wc_Sha256Free(&sha256);
  10279. res = TEST_RES_CHECK(flag == 0);
  10280. #endif
  10281. return res;
  10282. } /* END test_wc_Sha256FinalRaw */
  10283. /*
  10284. * Unit test function for wc_Sha256GetFlags()
  10285. */
  10286. static int test_wc_Sha256GetFlags(void)
  10287. {
  10288. int res = TEST_SKIPPED;
  10289. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  10290. wc_Sha256 sha256;
  10291. word32 flags = 0;
  10292. int flag = 0;
  10293. /* Initialize */
  10294. flag = wc_InitSha256(&sha256);
  10295. if (flag == 0) {
  10296. flag = wc_Sha256GetFlags(&sha256, &flags);
  10297. }
  10298. if (flag == 0) {
  10299. if (flags & WC_HASH_FLAG_ISCOPY) {
  10300. flag = 0;
  10301. }
  10302. }
  10303. wc_Sha256Free(&sha256);
  10304. res = TEST_RES_CHECK(flag == 0);
  10305. #endif
  10306. return res;
  10307. } /* END test_wc_Sha256GetFlags */
  10308. /*
  10309. * Unit test function for wc_Sha256Free()
  10310. */
  10311. static int test_wc_Sha256Free(void)
  10312. {
  10313. int res = TEST_SKIPPED;
  10314. #ifndef NO_SHA256
  10315. wc_Sha256Free(NULL);
  10316. res = TEST_RES_CHECK(1);
  10317. #endif
  10318. return res;
  10319. } /* END test_wc_Sha256Free */
  10320. /*
  10321. * Unit test function for wc_Sha256GetHash()
  10322. */
  10323. static int test_wc_Sha256GetHash(void)
  10324. {
  10325. int res = TEST_SKIPPED;
  10326. #ifndef NO_SHA256
  10327. wc_Sha256 sha256;
  10328. byte hash1[WC_SHA256_DIGEST_SIZE];
  10329. int flag = 0;
  10330. /* Initialize */
  10331. flag = wc_InitSha256(&sha256);
  10332. if (flag == 0) {
  10333. flag = wc_Sha256GetHash(&sha256, hash1);
  10334. }
  10335. /*test bad arguments*/
  10336. if (flag == 0) {
  10337. flag = wc_Sha256GetHash(NULL, NULL);
  10338. if (flag == BAD_FUNC_ARG) {
  10339. flag = 0;
  10340. }
  10341. }
  10342. if (flag == 0) {
  10343. flag = wc_Sha256GetHash(NULL, hash1);
  10344. if (flag == BAD_FUNC_ARG) {
  10345. flag = 0;
  10346. }
  10347. }
  10348. if (flag == 0) {
  10349. flag = wc_Sha256GetHash(&sha256, NULL);
  10350. if (flag == BAD_FUNC_ARG) {
  10351. flag = 0;
  10352. }
  10353. }
  10354. wc_Sha256Free(&sha256);
  10355. res = TEST_RES_CHECK(flag == 0);
  10356. #endif
  10357. return res;
  10358. } /* END test_wc_Sha256GetHash */
  10359. /*
  10360. * Unit test function for wc_Sha256Copy()
  10361. */
  10362. static int test_wc_Sha256Copy(void)
  10363. {
  10364. int res = TEST_SKIPPED;
  10365. #ifndef NO_SHA256
  10366. wc_Sha256 sha256;
  10367. wc_Sha256 temp;
  10368. int flag = 0;
  10369. /* Initialize */
  10370. flag = wc_InitSha256(&sha256);
  10371. if (flag == 0) {
  10372. flag = wc_InitSha256(&temp);
  10373. }
  10374. if (flag == 0) {
  10375. flag = wc_Sha256Copy(&sha256, &temp);
  10376. }
  10377. /*test bad arguments*/
  10378. if (flag == 0) {
  10379. flag = wc_Sha256Copy(NULL, NULL);
  10380. if (flag == BAD_FUNC_ARG) {
  10381. flag = 0;
  10382. }
  10383. }
  10384. if (flag == 0) {
  10385. flag = wc_Sha256Copy(NULL, &temp);
  10386. if (flag == BAD_FUNC_ARG) {
  10387. flag = 0;
  10388. }
  10389. }
  10390. if (flag == 0) {
  10391. flag = wc_Sha256Copy(&sha256, NULL);
  10392. if (flag == BAD_FUNC_ARG) {
  10393. flag = 0;
  10394. }
  10395. }
  10396. wc_Sha256Free(&sha256);
  10397. wc_Sha256Free(&temp);
  10398. res = TEST_RES_CHECK(flag == 0);
  10399. #endif
  10400. return res;
  10401. } /* END test_wc_Sha256Copy */
  10402. /*
  10403. * Testing wc_InitSha512()
  10404. */
  10405. static int test_wc_InitSha512(void)
  10406. {
  10407. int res = TEST_SKIPPED;
  10408. #ifdef WOLFSSL_SHA512
  10409. wc_Sha512 sha512;
  10410. int ret;
  10411. int flag = 0;
  10412. /* Test good arg. */
  10413. ret = wc_InitSha512(&sha512);
  10414. if (ret != 0) {
  10415. flag = WOLFSSL_FATAL_ERROR;
  10416. }
  10417. /* Test bad arg. */
  10418. if (!flag) {
  10419. ret = wc_InitSha512(NULL);
  10420. if (ret != BAD_FUNC_ARG) {
  10421. flag = WOLFSSL_FATAL_ERROR;
  10422. }
  10423. }
  10424. wc_Sha512Free(&sha512);
  10425. res = TEST_RES_CHECK(flag == 0);
  10426. #endif
  10427. return res;
  10428. } /* END test_wc_InitSha512 */
  10429. /*
  10430. * wc_Sha512Update() test.
  10431. */
  10432. static int test_wc_Sha512Update(void)
  10433. {
  10434. int res = TEST_SKIPPED;
  10435. #ifdef WOLFSSL_SHA512
  10436. wc_Sha512 sha512;
  10437. byte hash[WC_SHA512_DIGEST_SIZE];
  10438. testVector a, b, c;
  10439. int ret;
  10440. int flag = 0;
  10441. ret = wc_InitSha512(&sha512);
  10442. if (ret != 0) {
  10443. flag = ret;
  10444. }
  10445. /* Input. */
  10446. if (!flag) {
  10447. a.input = "a";
  10448. a.inLen = XSTRLEN(a.input);
  10449. ret = wc_Sha512Update(&sha512, NULL, 0);
  10450. if (ret != 0) {
  10451. flag = ret;
  10452. }
  10453. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  10454. if (ret != 0) {
  10455. flag = ret;
  10456. }
  10457. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10458. if (ret != 0) {
  10459. flag = ret;
  10460. }
  10461. ret = wc_Sha512Final(&sha512, hash);
  10462. if (ret != 0) {
  10463. flag = ret;
  10464. }
  10465. }
  10466. /* Update input. */
  10467. if (!flag) {
  10468. a.input = "abc";
  10469. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  10470. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  10471. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  10472. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  10473. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  10474. a.inLen = XSTRLEN(a.input);
  10475. a.outLen = XSTRLEN(a.output);
  10476. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10477. if (ret != 0) {
  10478. flag = ret;
  10479. }
  10480. }
  10481. if (!flag) {
  10482. ret = wc_Sha512Final(&sha512, hash);
  10483. if (ret != 0) {
  10484. flag = ret;
  10485. }
  10486. }
  10487. if (!flag) {
  10488. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  10489. flag = WOLFSSL_FATAL_ERROR;
  10490. }
  10491. }
  10492. /* Try passing in bad values */
  10493. if (!flag) {
  10494. b.input = NULL;
  10495. b.inLen = 0;
  10496. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  10497. if (ret != 0) {
  10498. flag = ret;
  10499. }
  10500. }
  10501. if (!flag) {
  10502. c.input = NULL;
  10503. c.inLen = WC_SHA512_DIGEST_SIZE;
  10504. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10505. if (ret != BAD_FUNC_ARG) {
  10506. flag = WOLFSSL_FATAL_ERROR;
  10507. }
  10508. }
  10509. if (!flag) {
  10510. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  10511. if (ret != BAD_FUNC_ARG) {
  10512. flag = WOLFSSL_FATAL_ERROR;
  10513. }
  10514. }
  10515. wc_Sha512Free(&sha512);
  10516. res = TEST_RES_CHECK(flag == 0);
  10517. #endif
  10518. return res;
  10519. } /* END test_wc_Sha512Update */
  10520. #ifdef WOLFSSL_SHA512
  10521. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10522. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  10523. /* Perfoms test for
  10524. * - wc_Sha512Final/wc_Sha512FinalRaw
  10525. * - wc_Sha512_224Final/wc_Sha512_224Final
  10526. * - wc_Sha512_256Final/wc_Sha512_256Final
  10527. * parameter:
  10528. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  10529. * WC_HASH_TYPE_SHA512_256
  10530. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  10531. *return 0 on success
  10532. */
  10533. static int test_Sha512_Family_Final(int type, int isRaw)
  10534. {
  10535. wc_Sha512 sha512;
  10536. byte* hash_test[3];
  10537. byte hash1[WC_SHA512_DIGEST_SIZE];
  10538. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10539. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10540. int times, i, ret;
  10541. int(*initFp)(wc_Sha512*);
  10542. int(*finalFp)(wc_Sha512*, byte*);
  10543. void(*freeFp)(wc_Sha512*);
  10544. if (type == WC_HASH_TYPE_SHA512) {
  10545. initFp = wc_InitSha512;
  10546. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10547. !defined(WOLFSSL_NO_HASH_RAW)
  10548. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  10549. #else
  10550. finalFp = (isRaw)? NULL : wc_Sha512Final;
  10551. #endif
  10552. freeFp = wc_Sha512Free;
  10553. }
  10554. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10555. #if !defined(WOLFSSL_NOSHA512_224)
  10556. else if (type == WC_HASH_TYPE_SHA512_224) {
  10557. initFp = wc_InitSha512_224;
  10558. #if !defined(WOLFSSL_NO_HASH_RAW)
  10559. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  10560. #else
  10561. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  10562. #endif
  10563. freeFp = wc_Sha512_224Free;
  10564. }
  10565. #endif
  10566. #if !defined(WOLFSSL_NOSHA512_256)
  10567. else if (type == WC_HASH_TYPE_SHA512_256) {
  10568. initFp = wc_InitSha512_256;
  10569. #if !defined(WOLFSSL_NO_HASH_RAW)
  10570. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  10571. #else
  10572. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  10573. #endif
  10574. freeFp = wc_Sha512_256Free;
  10575. }
  10576. #endif
  10577. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10578. else
  10579. return TEST_FAIL;
  10580. /* Initialize */
  10581. ret = initFp(&sha512);
  10582. if (!ret) {
  10583. hash_test[0] = hash1;
  10584. hash_test[1] = hash2;
  10585. hash_test[2] = hash3;
  10586. }
  10587. times = sizeof(hash_test) / sizeof(byte *);
  10588. /* Good test args. */
  10589. for (i = 0; i < times && ret == 0; i++) {
  10590. ret = finalFp(&sha512, hash_test[i]);
  10591. }
  10592. /* Test bad args. */
  10593. if (!ret) {
  10594. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  10595. ret = WOLFSSL_FATAL_ERROR;
  10596. }
  10597. }
  10598. if (!ret) {
  10599. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  10600. ret = WOLFSSL_FATAL_ERROR;
  10601. }
  10602. }
  10603. if (!ret) {
  10604. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  10605. ret = WOLFSSL_FATAL_ERROR;
  10606. }
  10607. }
  10608. freeFp(&sha512);
  10609. return ret;
  10610. }
  10611. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10612. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10613. #endif /* WOLFSSL_SHA512 */
  10614. /*
  10615. * Unit test function for wc_Sha512Final()
  10616. */
  10617. static int test_wc_Sha512Final(void)
  10618. {
  10619. int res = TEST_SKIPPED;
  10620. #ifdef WOLFSSL_SHA512
  10621. wc_Sha512 sha512;
  10622. byte* hash_test[3];
  10623. byte hash1[WC_SHA512_DIGEST_SIZE];
  10624. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10625. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10626. int times, i, ret;
  10627. int flag = 0;
  10628. /* Initialize */
  10629. ret = wc_InitSha512(&sha512);
  10630. if (ret != 0) {
  10631. flag = ret;
  10632. }
  10633. if (!flag) {
  10634. hash_test[0] = hash1;
  10635. hash_test[1] = hash2;
  10636. hash_test[2] = hash3;
  10637. }
  10638. times = sizeof(hash_test) / sizeof(byte *);
  10639. for (i = 0; i < times; i++) {
  10640. if (!flag) {
  10641. ret = wc_Sha512Final(&sha512, hash_test[i]);
  10642. if (ret != 0) {
  10643. flag = WOLFSSL_FATAL_ERROR;
  10644. }
  10645. }
  10646. }
  10647. /* Test bad args. */
  10648. if (!flag) {
  10649. ret = wc_Sha512Final(NULL, NULL);
  10650. if (ret != BAD_FUNC_ARG) {
  10651. flag = WOLFSSL_FATAL_ERROR;
  10652. }
  10653. }
  10654. if (!flag) {
  10655. ret = wc_Sha512Final(NULL, hash1);
  10656. if (ret != BAD_FUNC_ARG) {
  10657. flag = WOLFSSL_FATAL_ERROR;
  10658. }
  10659. }
  10660. if (!flag) {
  10661. ret = wc_Sha512Final(&sha512, NULL);
  10662. if (ret != BAD_FUNC_ARG) {
  10663. flag = WOLFSSL_FATAL_ERROR;
  10664. }
  10665. }
  10666. wc_Sha512Free(&sha512);
  10667. res = TEST_RES_CHECK(flag == 0);
  10668. #endif
  10669. return res;
  10670. } /* END test_wc_Sha512Final */
  10671. /*
  10672. * Unit test function for wc_Sha512GetFlags()
  10673. */
  10674. static int test_wc_Sha512GetFlags(void)
  10675. {
  10676. int res = TEST_SKIPPED;
  10677. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  10678. wc_Sha512 sha512;
  10679. word32 flags = 0;
  10680. int flag = 0;
  10681. /* Initialize */
  10682. flag = wc_InitSha512(&sha512);
  10683. if (flag == 0) {
  10684. flag = wc_Sha512GetFlags(&sha512, &flags);
  10685. }
  10686. if (flag == 0) {
  10687. if (flags & WC_HASH_FLAG_ISCOPY) {
  10688. flag = 0;
  10689. }
  10690. }
  10691. wc_Sha512Free(&sha512);
  10692. res = TEST_RES_CHECK(flag == 0);
  10693. #endif
  10694. return res;
  10695. } /* END test_wc_Sha512GetFlags */
  10696. /*
  10697. * Unit test function for wc_Sha512FinalRaw()
  10698. */
  10699. static int test_wc_Sha512FinalRaw(void)
  10700. {
  10701. int res = TEST_SKIPPED;
  10702. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10703. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10704. !defined(WOLFSSL_NO_HASH_RAW)
  10705. wc_Sha512 sha512;
  10706. byte* hash_test[3];
  10707. byte hash1[WC_SHA512_DIGEST_SIZE];
  10708. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10709. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10710. int times, i, ret;
  10711. int flag = 0;
  10712. /* Initialize */
  10713. ret = wc_InitSha512(&sha512);
  10714. if (ret != 0) {
  10715. flag = ret;
  10716. }
  10717. if (!flag) {
  10718. hash_test[0] = hash1;
  10719. hash_test[1] = hash2;
  10720. hash_test[2] = hash3;
  10721. }
  10722. times = sizeof(hash_test) / sizeof(byte*);
  10723. /* Good test args. */
  10724. for (i = 0; i < times; i++) {
  10725. if (!flag) {
  10726. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  10727. if (ret != 0) {
  10728. flag = WOLFSSL_FATAL_ERROR;
  10729. }
  10730. }
  10731. }
  10732. /* Test bad args. */
  10733. if (!flag ) {
  10734. ret = wc_Sha512FinalRaw(NULL, NULL);
  10735. if (ret != BAD_FUNC_ARG) {
  10736. flag = WOLFSSL_FATAL_ERROR;
  10737. }
  10738. }
  10739. if (!flag) {
  10740. ret = wc_Sha512FinalRaw(NULL, hash1);
  10741. if (ret != BAD_FUNC_ARG) {
  10742. flag = WOLFSSL_FATAL_ERROR;
  10743. }
  10744. }
  10745. if (!flag) {
  10746. ret = wc_Sha512FinalRaw(&sha512, NULL);
  10747. if (ret != BAD_FUNC_ARG) {
  10748. flag = WOLFSSL_FATAL_ERROR;
  10749. }
  10750. }
  10751. wc_Sha512Free(&sha512);
  10752. res = TEST_RES_CHECK(flag == 0);
  10753. #endif
  10754. return res;
  10755. } /* END test_wc_Sha512FinalRaw */
  10756. /*
  10757. * Unit test function for wc_Sha512Free()
  10758. */
  10759. static int test_wc_Sha512Free(void)
  10760. {
  10761. int res = TEST_SKIPPED;
  10762. #ifdef WOLFSSL_SHA512
  10763. wc_Sha512Free(NULL);
  10764. res = TEST_RES_CHECK(1);
  10765. #endif
  10766. return res;
  10767. } /* END test_wc_Sha512Free */
  10768. #ifdef WOLFSSL_SHA512
  10769. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10770. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  10771. static int test_Sha512_Family_GetHash(int type )
  10772. {
  10773. int flag = 0;
  10774. int(*initFp)(wc_Sha512*);
  10775. int(*ghashFp)(wc_Sha512*, byte*);
  10776. wc_Sha512 sha512;
  10777. byte hash1[WC_SHA512_DIGEST_SIZE];
  10778. if (type == WC_HASH_TYPE_SHA512) {
  10779. initFp = wc_InitSha512;
  10780. ghashFp = wc_Sha512GetHash;
  10781. }
  10782. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10783. #if !defined(WOLFSSL_NOSHA512_224)
  10784. else if (type == WC_HASH_TYPE_SHA512_224) {
  10785. initFp = wc_InitSha512_224;
  10786. ghashFp = wc_Sha512_224GetHash;
  10787. }
  10788. #endif
  10789. #if !defined(WOLFSSL_NOSHA512_256)
  10790. else if (type == WC_HASH_TYPE_SHA512_256) {
  10791. initFp = wc_InitSha512_256;
  10792. ghashFp = wc_Sha512_256GetHash;
  10793. }
  10794. #endif
  10795. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10796. else {
  10797. initFp = NULL;
  10798. ghashFp = NULL;
  10799. }
  10800. if (initFp == NULL || ghashFp == NULL)
  10801. return TEST_FAIL;
  10802. if (!flag) {
  10803. flag = initFp(&sha512);
  10804. }
  10805. if (!flag) {
  10806. flag = ghashFp(&sha512, hash1);
  10807. }
  10808. /*test bad arguments*/
  10809. if (!flag) {
  10810. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  10811. flag = WOLFSSL_FATAL_ERROR;
  10812. }
  10813. if (!flag) {
  10814. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  10815. flag = WOLFSSL_FATAL_ERROR;
  10816. }
  10817. if (!flag) {
  10818. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  10819. flag = WOLFSSL_FATAL_ERROR;
  10820. }
  10821. wc_Sha512Free(&sha512);
  10822. return flag;
  10823. }
  10824. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10825. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10826. #endif /* WOLFSSL_SHA512 */
  10827. /*
  10828. * Unit test function for wc_Sha512GetHash()
  10829. */
  10830. static int test_wc_Sha512GetHash(void)
  10831. {
  10832. int res = TEST_SKIPPED;
  10833. #ifdef WOLFSSL_SHA512
  10834. wc_Sha512 sha512;
  10835. byte hash1[WC_SHA512_DIGEST_SIZE];
  10836. int flag = 0;
  10837. /* Initialize */
  10838. flag = wc_InitSha512(&sha512);
  10839. if (flag == 0) {
  10840. flag = wc_Sha512GetHash(&sha512, hash1);
  10841. }
  10842. /*test bad arguments*/
  10843. if (flag == 0) {
  10844. flag = wc_Sha512GetHash(NULL, NULL);
  10845. if (flag == BAD_FUNC_ARG) {
  10846. flag = 0;
  10847. }
  10848. }
  10849. if (flag == 0) {
  10850. flag = wc_Sha512GetHash(NULL, hash1);
  10851. if (flag == BAD_FUNC_ARG) {
  10852. flag = 0;
  10853. }
  10854. }
  10855. if (flag == 0) {
  10856. flag = wc_Sha512GetHash(&sha512, NULL);
  10857. if (flag == BAD_FUNC_ARG) {
  10858. flag = 0;
  10859. }
  10860. }
  10861. wc_Sha512Free(&sha512);
  10862. res = TEST_RES_CHECK(flag == 0);
  10863. #endif
  10864. return res;
  10865. } /* END test_wc_Sha512GetHash */
  10866. /*
  10867. * Unit test function for wc_Sha512Copy()
  10868. */
  10869. static int test_wc_Sha512Copy(void)
  10870. {
  10871. int res = TEST_SKIPPED;
  10872. #ifdef WOLFSSL_SHA512
  10873. wc_Sha512 sha512;
  10874. wc_Sha512 temp;
  10875. int flag;
  10876. /* Initialize */
  10877. flag = wc_InitSha512(&sha512);
  10878. if (flag == 0) {
  10879. flag = wc_InitSha512(&temp);
  10880. }
  10881. if (flag == 0) {
  10882. flag = wc_Sha512Copy(&sha512, &temp);
  10883. }
  10884. /*test bad arguments*/
  10885. if (flag == 0) {
  10886. flag = wc_Sha512Copy(NULL, NULL);
  10887. if (flag == BAD_FUNC_ARG) {
  10888. flag = 0;
  10889. }
  10890. }
  10891. if (flag == 0) {
  10892. flag = wc_Sha512Copy(NULL, &temp);
  10893. if (flag == BAD_FUNC_ARG) {
  10894. flag = 0;
  10895. }
  10896. }
  10897. if (flag == 0) {
  10898. flag = wc_Sha512Copy(&sha512, NULL);
  10899. if (flag == BAD_FUNC_ARG) {
  10900. flag = 0;
  10901. }
  10902. }
  10903. wc_Sha512Free(&sha512);
  10904. wc_Sha512Free(&temp);
  10905. res = TEST_RES_CHECK(flag == 0);
  10906. #endif
  10907. return res;
  10908. } /* END test_wc_Sha512Copy */
  10909. static int test_wc_InitSha512_224(void)
  10910. {
  10911. int res = TEST_SKIPPED;
  10912. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10913. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10914. wc_Sha512 sha512;
  10915. int ret;
  10916. int flag = 0;
  10917. /* Test good arg. */
  10918. ret = wc_InitSha512_224(&sha512);
  10919. if (ret != 0) {
  10920. flag = WOLFSSL_FATAL_ERROR;
  10921. }
  10922. /* Test bad arg. */
  10923. if (!flag) {
  10924. ret = wc_InitSha512_224(NULL);
  10925. if (ret != BAD_FUNC_ARG) {
  10926. flag = WOLFSSL_FATAL_ERROR;
  10927. }
  10928. }
  10929. wc_Sha512_224Free(&sha512);
  10930. res = TEST_RES_CHECK(flag == 0);
  10931. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10932. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10933. return res;
  10934. }
  10935. static int test_wc_Sha512_224Update(void)
  10936. {
  10937. int res = TEST_SKIPPED;
  10938. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10939. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10940. wc_Sha512 sha512;
  10941. byte hash[WC_SHA512_DIGEST_SIZE];
  10942. testVector a, c;
  10943. int ret;
  10944. int flag = 0;
  10945. ret = wc_InitSha512_224(&sha512);
  10946. if (ret != 0) {
  10947. flag = ret;
  10948. }
  10949. /* Input. */
  10950. if (!flag) {
  10951. a.input = "a";
  10952. a.inLen = XSTRLEN(a.input);
  10953. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  10954. if (ret != 0) {
  10955. flag = ret;
  10956. }
  10957. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  10958. if (ret != 0) {
  10959. flag = ret;
  10960. }
  10961. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10962. if (ret != 0) {
  10963. flag = ret;
  10964. }
  10965. ret = wc_Sha512_224Final(&sha512, hash);
  10966. if (ret != 0) {
  10967. flag = ret;
  10968. }
  10969. }
  10970. /* Update input. */
  10971. if (!flag) {
  10972. a.input = "abc";
  10973. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  10974. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  10975. a.inLen = XSTRLEN(a.input);
  10976. a.outLen = XSTRLEN(a.output);
  10977. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10978. if (ret != 0) {
  10979. flag = ret;
  10980. }
  10981. }
  10982. if (!flag) {
  10983. ret = wc_Sha512_224Final(&sha512, hash);
  10984. if (ret != 0) {
  10985. flag = ret;
  10986. }
  10987. }
  10988. if (!flag) {
  10989. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  10990. flag = WOLFSSL_FATAL_ERROR;
  10991. }
  10992. }
  10993. if (!flag) {
  10994. c.input = NULL;
  10995. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  10996. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10997. if (ret != BAD_FUNC_ARG) {
  10998. flag = WOLFSSL_FATAL_ERROR;
  10999. }
  11000. }
  11001. if (!flag) {
  11002. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11003. if (ret != BAD_FUNC_ARG) {
  11004. flag = WOLFSSL_FATAL_ERROR;
  11005. }
  11006. }
  11007. wc_Sha512_224Free(&sha512);
  11008. res = TEST_RES_CHECK(flag == 0);
  11009. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11010. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11011. return res;
  11012. }
  11013. static int test_wc_Sha512_224Final(void)
  11014. {
  11015. int res = TEST_SKIPPED;
  11016. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11017. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11018. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  11019. res = TEST_RES_CHECK(ret == 0);
  11020. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11021. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11022. return res;
  11023. }
  11024. static int test_wc_Sha512_224GetFlags(void)
  11025. {
  11026. int res = TEST_SKIPPED;
  11027. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11028. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11029. wc_Sha512 sha512, copy;
  11030. word32 flags = 0;
  11031. int flag = 0;
  11032. /* Initialize */
  11033. flag = wc_InitSha512_224(&sha512);
  11034. if (!flag) {
  11035. flag = wc_InitSha512_224(&copy);
  11036. }
  11037. if (!flag) {
  11038. flag = wc_Sha512_224Copy(&sha512, &copy);
  11039. }
  11040. if (!flag) {
  11041. flag = wc_Sha512_224GetFlags(&copy, &flags);
  11042. }
  11043. if (!flag) {
  11044. if (flags & WC_HASH_FLAG_ISCOPY)
  11045. flag = 0;
  11046. else
  11047. flag = WOLFSSL_FATAL_ERROR;
  11048. }
  11049. wc_Sha512_224Free(&copy);
  11050. wc_Sha512_224Free(&sha512);
  11051. res = TEST_RES_CHECK(flag == 0);
  11052. #endif
  11053. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11054. return res;
  11055. }
  11056. static int test_wc_Sha512_224FinalRaw(void)
  11057. {
  11058. int res = TEST_SKIPPED;
  11059. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11060. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11061. !defined(WOLFSSL_NO_HASH_RAW)
  11062. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  11063. res = TEST_RES_CHECK(ret == 0);
  11064. #endif
  11065. return res;
  11066. }
  11067. static int test_wc_Sha512_224Free(void)
  11068. {
  11069. int res = TEST_SKIPPED;
  11070. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11071. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11072. wc_Sha512_224Free(NULL);
  11073. res = TEST_RES_CHECK(1);
  11074. #endif
  11075. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11076. return res;
  11077. }
  11078. static int test_wc_Sha512_224GetHash(void)
  11079. {
  11080. int res = TEST_SKIPPED;
  11081. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11082. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11083. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  11084. res = TEST_RES_CHECK(ret == 0);
  11085. #endif
  11086. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11087. return res;
  11088. }
  11089. static int test_wc_Sha512_224Copy(void)
  11090. {
  11091. int res = TEST_SKIPPED;
  11092. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11093. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11094. wc_Sha512 sha512;
  11095. wc_Sha512 temp;
  11096. int flag = 0;
  11097. /* Initialize */
  11098. flag = wc_InitSha512_224(&sha512);
  11099. if (flag == 0) {
  11100. flag = wc_InitSha512_224(&temp);
  11101. }
  11102. if (flag == 0) {
  11103. flag = wc_Sha512_224Copy(&sha512, &temp);
  11104. }
  11105. /*test bad arguments*/
  11106. if (flag == 0) {
  11107. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  11108. flag = WOLFSSL_FATAL_ERROR;
  11109. }
  11110. if (flag == 0) {
  11111. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  11112. flag = WOLFSSL_FATAL_ERROR;
  11113. }
  11114. if (flag == 0) {
  11115. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  11116. flag = WOLFSSL_FATAL_ERROR;
  11117. }
  11118. wc_Sha512_224Free(&sha512);
  11119. wc_Sha512_224Free(&temp);
  11120. res = TEST_RES_CHECK(flag == 0);
  11121. #endif
  11122. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11123. return res;
  11124. }
  11125. static int test_wc_InitSha512_256(void)
  11126. {
  11127. int res = TEST_SKIPPED;
  11128. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11129. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11130. wc_Sha512 sha512;
  11131. int ret;
  11132. int flag = 0;
  11133. /* Test good arg. */
  11134. ret = wc_InitSha512_256(&sha512);
  11135. if (ret != 0) {
  11136. flag = WOLFSSL_FATAL_ERROR;
  11137. }
  11138. /* Test bad arg. */
  11139. if (!flag) {
  11140. ret = wc_InitSha512_256(NULL);
  11141. if (ret != BAD_FUNC_ARG) {
  11142. flag = WOLFSSL_FATAL_ERROR;
  11143. }
  11144. }
  11145. wc_Sha512_256Free(&sha512);
  11146. res = TEST_RES_CHECK(flag == 0);
  11147. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11148. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11149. return res;
  11150. }
  11151. static int test_wc_Sha512_256Update(void)
  11152. {
  11153. int res = TEST_SKIPPED;
  11154. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11155. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11156. wc_Sha512 sha512;
  11157. byte hash[WC_SHA512_DIGEST_SIZE];
  11158. testVector a, c;
  11159. int ret;
  11160. int flag = 0;
  11161. ret = wc_InitSha512_256(&sha512);
  11162. if (ret != 0) {
  11163. flag = ret;
  11164. }
  11165. /* Input. */
  11166. if (!flag) {
  11167. a.input = "a";
  11168. a.inLen = XSTRLEN(a.input);
  11169. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  11170. if (ret != 0) {
  11171. flag = ret;
  11172. }
  11173. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  11174. if (ret != 0) {
  11175. flag = ret;
  11176. }
  11177. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  11178. if (ret != 0) {
  11179. flag = ret;
  11180. }
  11181. ret = wc_Sha512_256Final(&sha512, hash);
  11182. if (ret != 0) {
  11183. flag = ret;
  11184. }
  11185. }
  11186. /* Update input. */
  11187. if (!flag) {
  11188. a.input = "abc";
  11189. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11190. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11191. "\x07\xe7\xaf\x23";
  11192. a.inLen = XSTRLEN(a.input);
  11193. a.outLen = XSTRLEN(a.output);
  11194. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  11195. if (ret != 0) {
  11196. flag = ret;
  11197. }
  11198. }
  11199. if (!flag) {
  11200. ret = wc_Sha512_256Final(&sha512, hash);
  11201. if (ret != 0) {
  11202. flag = ret;
  11203. }
  11204. }
  11205. if (!flag) {
  11206. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  11207. flag = WOLFSSL_FATAL_ERROR;
  11208. }
  11209. }
  11210. if (!flag) {
  11211. c.input = NULL;
  11212. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11213. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  11214. if (ret != BAD_FUNC_ARG) {
  11215. flag = WOLFSSL_FATAL_ERROR;
  11216. }
  11217. }
  11218. if (!flag) {
  11219. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  11220. if (ret != BAD_FUNC_ARG) {
  11221. flag = WOLFSSL_FATAL_ERROR;
  11222. }
  11223. }
  11224. wc_Sha512_256Free(&sha512);
  11225. res = TEST_RES_CHECK(flag == 0);
  11226. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11227. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11228. return res;
  11229. }
  11230. static int test_wc_Sha512_256Final(void)
  11231. {
  11232. int res = TEST_SKIPPED;
  11233. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11234. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11235. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  11236. res = TEST_RES_CHECK(ret == 0);
  11237. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11238. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11239. return res;
  11240. }
  11241. static int test_wc_Sha512_256GetFlags(void)
  11242. {
  11243. int res = TEST_SKIPPED;
  11244. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11245. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11246. wc_Sha512 sha512, copy;
  11247. word32 flags = 0;
  11248. int flag = 0;
  11249. /* Initialize */
  11250. flag = wc_InitSha512_256(&sha512);
  11251. if (!flag ) {
  11252. flag = wc_InitSha512_256(&copy);
  11253. }
  11254. if (!flag ) {
  11255. flag = wc_Sha512_256Copy(&sha512, &copy);
  11256. }
  11257. if (!flag ) {
  11258. flag = wc_Sha512_256GetFlags(&copy, &flags);
  11259. }
  11260. if (!flag) {
  11261. if (flags & WC_HASH_FLAG_ISCOPY)
  11262. flag = 0;
  11263. else
  11264. flag = WOLFSSL_FATAL_ERROR;
  11265. }
  11266. wc_Sha512_256Free(&sha512);
  11267. res = TEST_RES_CHECK(flag == 0);
  11268. #endif
  11269. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11270. return res;
  11271. }
  11272. static int test_wc_Sha512_256FinalRaw(void)
  11273. {
  11274. int res = TEST_SKIPPED;
  11275. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11276. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11277. !defined(WOLFSSL_NO_HASH_RAW)
  11278. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  11279. res = TEST_RES_CHECK(ret == 0);
  11280. #endif
  11281. return res;
  11282. }
  11283. static int test_wc_Sha512_256Free(void)
  11284. {
  11285. int res = TEST_SKIPPED;
  11286. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11287. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11288. wc_Sha512_256Free(NULL);
  11289. res = TEST_RES_CHECK(1);
  11290. #endif
  11291. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11292. return res;
  11293. }
  11294. static int test_wc_Sha512_256GetHash(void)
  11295. {
  11296. int res = TEST_SKIPPED;
  11297. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11298. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11299. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  11300. res = TEST_RES_CHECK(ret == 0);
  11301. #endif
  11302. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11303. return res;
  11304. }
  11305. static int test_wc_Sha512_256Copy(void)
  11306. {
  11307. int res = TEST_SKIPPED;
  11308. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11309. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11310. wc_Sha512 sha512;
  11311. wc_Sha512 temp;
  11312. int flag = 0;
  11313. /* Initialize */
  11314. flag = wc_InitSha512_256(&sha512);
  11315. if (flag == 0) {
  11316. flag = wc_InitSha512_256(&temp);
  11317. }
  11318. if (flag == 0) {
  11319. flag = wc_Sha512_256Copy(&sha512, &temp);
  11320. }
  11321. /*test bad arguments*/
  11322. if (flag == 0) {
  11323. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  11324. flag = WOLFSSL_FATAL_ERROR;
  11325. }
  11326. if (flag == 0) {
  11327. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  11328. flag = WOLFSSL_FATAL_ERROR;
  11329. }
  11330. if (flag == 0) {
  11331. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  11332. flag = WOLFSSL_FATAL_ERROR;
  11333. }
  11334. wc_Sha512_256Free(&sha512);
  11335. wc_Sha512_256Free(&temp);
  11336. res = TEST_RES_CHECK(flag == 0);
  11337. #endif
  11338. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11339. return res;
  11340. }
  11341. /*
  11342. * Testing wc_InitSha384()
  11343. */
  11344. static int test_wc_InitSha384(void)
  11345. {
  11346. int res = TEST_SKIPPED;
  11347. #ifdef WOLFSSL_SHA384
  11348. wc_Sha384 sha384;
  11349. int ret;
  11350. int flag = 0;
  11351. /* Test good arg. */
  11352. ret = wc_InitSha384(&sha384);
  11353. if (ret != 0) {
  11354. flag = WOLFSSL_FATAL_ERROR;
  11355. }
  11356. /* Test bad arg. */
  11357. if (!flag) {
  11358. ret = wc_InitSha384(NULL);
  11359. if (ret != BAD_FUNC_ARG) {
  11360. flag = WOLFSSL_FATAL_ERROR;
  11361. }
  11362. }
  11363. wc_Sha384Free(&sha384);
  11364. res = TEST_RES_CHECK(flag == 0);
  11365. #endif
  11366. return res;
  11367. } /* END test_wc_InitSha384 */
  11368. /*
  11369. * test wc_Sha384Update()
  11370. */
  11371. static int test_wc_Sha384Update(void)
  11372. {
  11373. int res = TEST_SKIPPED;
  11374. #ifdef WOLFSSL_SHA384
  11375. wc_Sha384 sha384;
  11376. byte hash[WC_SHA384_DIGEST_SIZE];
  11377. testVector a, b, c;
  11378. int ret;
  11379. int flag = 0;
  11380. ret = wc_InitSha384(&sha384);
  11381. if (ret != 0) {
  11382. flag = ret;
  11383. }
  11384. /* Input */
  11385. if (!flag) {
  11386. a.input = "a";
  11387. a.inLen = XSTRLEN(a.input);
  11388. ret = wc_Sha384Update(&sha384, NULL, 0);
  11389. if (ret != 0) {
  11390. flag = ret;
  11391. }
  11392. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  11393. if (ret != 0) {
  11394. flag = ret;
  11395. }
  11396. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  11397. if (ret != 0) {
  11398. flag = ret;
  11399. }
  11400. }
  11401. if (!flag) {
  11402. ret = wc_Sha384Final(&sha384, hash);
  11403. if (ret != 0) {
  11404. flag = ret;
  11405. }
  11406. }
  11407. /* Update input. */
  11408. if (!flag) {
  11409. a.input = "abc";
  11410. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11411. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11412. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11413. "\xc8\x25\xa7";
  11414. a.inLen = XSTRLEN(a.input);
  11415. a.outLen = XSTRLEN(a.output);
  11416. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  11417. if (ret != 0) {
  11418. flag = ret;
  11419. }
  11420. }
  11421. if (!flag) {
  11422. ret = wc_Sha384Final(&sha384, hash);
  11423. if (ret != 0) {
  11424. flag = ret;
  11425. }
  11426. }
  11427. if (!flag) {
  11428. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  11429. flag = WOLFSSL_FATAL_ERROR;
  11430. }
  11431. }
  11432. /* Pass in bad values. */
  11433. if (!flag) {
  11434. b.input = NULL;
  11435. b.inLen = 0;
  11436. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  11437. if (ret != 0) {
  11438. flag = ret;
  11439. }
  11440. }
  11441. if (!flag) {
  11442. c.input = NULL;
  11443. c.inLen = WC_SHA384_DIGEST_SIZE;
  11444. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  11445. if (ret != BAD_FUNC_ARG) {
  11446. flag = WOLFSSL_FATAL_ERROR;
  11447. }
  11448. }
  11449. if (!flag) {
  11450. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  11451. if (ret != BAD_FUNC_ARG) {
  11452. flag = WOLFSSL_FATAL_ERROR;
  11453. }
  11454. }
  11455. wc_Sha384Free(&sha384);
  11456. res = TEST_RES_CHECK(flag == 0);
  11457. #endif
  11458. return res;
  11459. } /* END test_wc_Sha384Update */
  11460. /*
  11461. * Unit test function for wc_Sha384Final();
  11462. */
  11463. static int test_wc_Sha384Final(void)
  11464. {
  11465. int res = TEST_SKIPPED;
  11466. #ifdef WOLFSSL_SHA384
  11467. wc_Sha384 sha384;
  11468. byte* hash_test[3];
  11469. byte hash1[WC_SHA384_DIGEST_SIZE];
  11470. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11471. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11472. int times, i, ret;
  11473. int flag = 0;
  11474. /* Initialize */
  11475. ret = wc_InitSha384(&sha384);
  11476. if (ret) {
  11477. flag = ret;
  11478. }
  11479. if (!flag) {
  11480. hash_test[0] = hash1;
  11481. hash_test[1] = hash2;
  11482. hash_test[2] = hash3;
  11483. }
  11484. times = sizeof(hash_test) / sizeof(byte*);
  11485. /* Good test args. */
  11486. for (i = 0; i < times; i++) {
  11487. if (!flag) {
  11488. ret = wc_Sha384Final(&sha384, hash_test[i]);
  11489. if (ret != 0) {
  11490. flag = WOLFSSL_FATAL_ERROR;
  11491. }
  11492. }
  11493. }
  11494. /* Test bad args. */
  11495. if (!flag) {
  11496. ret = wc_Sha384Final(NULL, NULL);
  11497. if (ret != BAD_FUNC_ARG) {
  11498. flag = WOLFSSL_FATAL_ERROR;
  11499. }
  11500. }
  11501. if (!flag) {
  11502. ret = wc_Sha384Final(NULL, hash1);
  11503. if (ret != BAD_FUNC_ARG) {
  11504. flag = WOLFSSL_FATAL_ERROR;
  11505. }
  11506. }
  11507. if (!flag) {
  11508. ret = wc_Sha384Final(&sha384, NULL);
  11509. if (ret != BAD_FUNC_ARG) {
  11510. flag = WOLFSSL_FATAL_ERROR;
  11511. }
  11512. }
  11513. wc_Sha384Free(&sha384);
  11514. res = TEST_RES_CHECK(flag == 0);
  11515. #endif
  11516. return res;
  11517. } /* END test_wc_Sha384Final */
  11518. /*
  11519. * Unit test function for wc_Sha384GetFlags()
  11520. */
  11521. static int test_wc_Sha384GetFlags(void)
  11522. {
  11523. int res = TEST_SKIPPED;
  11524. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  11525. wc_Sha384 sha384;
  11526. word32 flags = 0;
  11527. int flag = 0;
  11528. /* Initialize */
  11529. flag = wc_InitSha384(&sha384);
  11530. if (flag == 0) {
  11531. flag = wc_Sha384GetFlags(&sha384, &flags);
  11532. }
  11533. if (flag == 0) {
  11534. if (flags & WC_HASH_FLAG_ISCOPY) {
  11535. flag = 0;
  11536. }
  11537. }
  11538. wc_Sha384Free(&sha384);
  11539. res = TEST_RES_CHECK(flag == 0);
  11540. #endif
  11541. return res;
  11542. } /* END test_wc_Sha384GetFlags */
  11543. /*
  11544. * Unit test function for wc_Sha384FinalRaw()
  11545. */
  11546. static int test_wc_Sha384FinalRaw(void)
  11547. {
  11548. int res = TEST_SKIPPED;
  11549. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11550. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11551. !defined(WOLFSSL_NO_HASH_RAW)
  11552. wc_Sha384 sha384;
  11553. byte* hash_test[3];
  11554. byte hash1[WC_SHA384_DIGEST_SIZE];
  11555. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11556. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11557. int times, i, ret;
  11558. int flag = 0;
  11559. /* Initialize */
  11560. ret = wc_InitSha384(&sha384);
  11561. if (ret != 0) {
  11562. flag = ret;
  11563. }
  11564. if (!flag) {
  11565. hash_test[0] = hash1;
  11566. hash_test[1] = hash2;
  11567. hash_test[2] = hash3;
  11568. }
  11569. times = sizeof(hash_test) / sizeof(byte*);
  11570. /* Good test args. */
  11571. for (i = 0; i < times; i++) {
  11572. if (!flag) {
  11573. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  11574. if (ret != 0) {
  11575. flag = WOLFSSL_FATAL_ERROR;
  11576. }
  11577. }
  11578. }
  11579. /* Test bad args. */
  11580. if (!flag ) {
  11581. ret = wc_Sha384FinalRaw(NULL, NULL);
  11582. if (ret != BAD_FUNC_ARG) {
  11583. flag = WOLFSSL_FATAL_ERROR;
  11584. }
  11585. }
  11586. if (!flag) {
  11587. ret = wc_Sha384FinalRaw(NULL, hash1);
  11588. if (ret != BAD_FUNC_ARG) {
  11589. flag = WOLFSSL_FATAL_ERROR;
  11590. }
  11591. }
  11592. if (!flag) {
  11593. ret = wc_Sha384FinalRaw(&sha384, NULL);
  11594. if (ret != BAD_FUNC_ARG) {
  11595. flag = WOLFSSL_FATAL_ERROR;
  11596. }
  11597. }
  11598. wc_Sha384Free(&sha384);
  11599. res = TEST_RES_CHECK(flag == 0);
  11600. #endif
  11601. return res;
  11602. } /* END test_wc_Sha384FinalRaw */
  11603. /*
  11604. * Unit test function for wc_Sha384Free()
  11605. */
  11606. static int test_wc_Sha384Free(void)
  11607. {
  11608. int res = TEST_SKIPPED;
  11609. #ifdef WOLFSSL_SHA384
  11610. wc_Sha384Free(NULL);
  11611. res = TEST_RES_CHECK(1);
  11612. #endif
  11613. return res;
  11614. } /* END test_wc_Sha384Free */
  11615. /*
  11616. * Unit test function for wc_Sha384GetHash()
  11617. */
  11618. static int test_wc_Sha384GetHash(void)
  11619. {
  11620. int res = TEST_SKIPPED;
  11621. #ifdef WOLFSSL_SHA384
  11622. wc_Sha384 sha384;
  11623. byte hash1[WC_SHA384_DIGEST_SIZE];
  11624. int flag = 0;
  11625. /* Initialize */
  11626. flag = wc_InitSha384(&sha384);
  11627. if (flag == 0) {
  11628. flag = wc_Sha384GetHash(&sha384, hash1);
  11629. }
  11630. /*test bad arguments*/
  11631. if (flag == 0) {
  11632. flag = wc_Sha384GetHash(NULL, NULL);
  11633. if (flag == BAD_FUNC_ARG) {
  11634. flag = 0;
  11635. }
  11636. }
  11637. if (flag == 0) {
  11638. flag = wc_Sha384GetHash(NULL, hash1);
  11639. if (flag == BAD_FUNC_ARG) {
  11640. flag = 0;
  11641. }
  11642. }
  11643. if (flag == 0) {
  11644. flag = wc_Sha384GetHash(&sha384, NULL);
  11645. if (flag == BAD_FUNC_ARG) {
  11646. flag = 0;
  11647. }
  11648. }
  11649. wc_Sha384Free(&sha384);
  11650. res = TEST_RES_CHECK(flag == 0);
  11651. #endif
  11652. return res;
  11653. } /* END test_wc_Sha384GetHash */
  11654. /*
  11655. * Unit test function for wc_Sha384Copy()
  11656. */
  11657. static int test_wc_Sha384Copy(void)
  11658. {
  11659. int res = TEST_SKIPPED;
  11660. #ifdef WOLFSSL_SHA384
  11661. wc_Sha384 sha384;
  11662. wc_Sha384 temp;
  11663. int flag = 0;
  11664. /* Initialize */
  11665. flag = wc_InitSha384(&sha384);
  11666. if (flag == 0) {
  11667. flag = wc_InitSha384(&temp);
  11668. }
  11669. if (flag == 0) {
  11670. flag = wc_Sha384Copy(&sha384, &temp);
  11671. }
  11672. /*test bad arguments*/
  11673. if (flag == 0) {
  11674. flag = wc_Sha384Copy(NULL, NULL);
  11675. if (flag == BAD_FUNC_ARG) {
  11676. flag = 0;
  11677. }
  11678. }
  11679. if (flag == 0) {
  11680. flag = wc_Sha384Copy(NULL, &temp);
  11681. if (flag == BAD_FUNC_ARG) {
  11682. flag = 0;
  11683. }
  11684. }
  11685. if (flag == 0) {
  11686. flag = wc_Sha384Copy(&sha384, NULL);
  11687. if (flag == BAD_FUNC_ARG) {
  11688. flag = 0;
  11689. }
  11690. }
  11691. wc_Sha384Free(&sha384);
  11692. wc_Sha384Free(&temp);
  11693. res = TEST_RES_CHECK(flag == 0);
  11694. #endif
  11695. return res;
  11696. } /* END test_wc_Sha384Copy */
  11697. /*
  11698. * Testing wc_InitSha224();
  11699. */
  11700. static int test_wc_InitSha224(void)
  11701. {
  11702. int res = TEST_SKIPPED;
  11703. #ifdef WOLFSSL_SHA224
  11704. wc_Sha224 sha224;
  11705. int ret;
  11706. int flag = 0;
  11707. /* Test good arg. */
  11708. ret = wc_InitSha224(&sha224);
  11709. if (ret != 0) {
  11710. flag = WOLFSSL_FATAL_ERROR;
  11711. }
  11712. /* Test bad arg. */
  11713. if (!flag) {
  11714. ret = wc_InitSha224(NULL);
  11715. if (ret != BAD_FUNC_ARG) {
  11716. flag = WOLFSSL_FATAL_ERROR;
  11717. }
  11718. }
  11719. wc_Sha224Free(&sha224);
  11720. res = TEST_RES_CHECK(flag == 0);
  11721. #endif
  11722. return res;
  11723. } /* END test_wc_InitSha224 */
  11724. /*
  11725. * Unit test on wc_Sha224Update
  11726. */
  11727. static int test_wc_Sha224Update(void)
  11728. {
  11729. int res = TEST_SKIPPED;
  11730. #ifdef WOLFSSL_SHA224
  11731. wc_Sha224 sha224;
  11732. byte hash[WC_SHA224_DIGEST_SIZE];
  11733. testVector a, b, c;
  11734. int ret;
  11735. int flag = 0;
  11736. ret = wc_InitSha224(&sha224);
  11737. if (ret != 0) {
  11738. flag = ret;
  11739. }
  11740. /* Input. */
  11741. if (!flag) {
  11742. a.input = "a";
  11743. a.inLen = XSTRLEN(a.input);
  11744. ret = wc_Sha224Update(&sha224, NULL, 0);
  11745. if (ret != 0) {
  11746. flag = ret;
  11747. }
  11748. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  11749. if (ret != 0) {
  11750. flag = ret;
  11751. }
  11752. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11753. if (ret != 0) {
  11754. flag = ret;
  11755. }
  11756. }
  11757. if (!flag) {
  11758. ret = wc_Sha224Final(&sha224, hash);
  11759. if (ret != 0) {
  11760. flag = ret;
  11761. }
  11762. }
  11763. /* Update input. */
  11764. if (!flag) {
  11765. a.input = "abc";
  11766. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11767. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11768. a.inLen = XSTRLEN(a.input);
  11769. a.outLen = XSTRLEN(a.output);
  11770. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11771. if (ret != 0) {
  11772. flag = ret;
  11773. }
  11774. }
  11775. if (!flag) {
  11776. ret = wc_Sha224Final(&sha224, hash);
  11777. if (ret != 0) {
  11778. flag = ret;
  11779. }
  11780. }
  11781. if (!flag) {
  11782. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  11783. flag = WOLFSSL_FATAL_ERROR;
  11784. }
  11785. }
  11786. /* Pass in bad values. */
  11787. if (!flag) {
  11788. b.input = NULL;
  11789. b.inLen = 0;
  11790. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  11791. if (ret != 0) {
  11792. flag = ret;
  11793. }
  11794. }
  11795. if (!flag) {
  11796. c.input = NULL;
  11797. c.inLen = WC_SHA224_DIGEST_SIZE;
  11798. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  11799. if (ret != BAD_FUNC_ARG) {
  11800. flag = WOLFSSL_FATAL_ERROR;
  11801. }
  11802. }
  11803. if (!flag) {
  11804. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11805. if (ret != BAD_FUNC_ARG) {
  11806. flag = WOLFSSL_FATAL_ERROR;
  11807. }
  11808. }
  11809. wc_Sha224Free(&sha224);
  11810. res = TEST_RES_CHECK(flag == 0);
  11811. #endif
  11812. return res;
  11813. } /* END test_wc_Sha224Update */
  11814. /*
  11815. * Unit test for wc_Sha224Final();
  11816. */
  11817. static int test_wc_Sha224Final(void)
  11818. {
  11819. int res = TEST_SKIPPED;
  11820. #ifdef WOLFSSL_SHA224
  11821. wc_Sha224 sha224;
  11822. byte* hash_test[3];
  11823. byte hash1[WC_SHA224_DIGEST_SIZE];
  11824. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11825. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11826. int times, i, ret;
  11827. int flag = 0;
  11828. /* Initialize */
  11829. ret = wc_InitSha224(&sha224);
  11830. if (ret) {
  11831. flag = ret;
  11832. }
  11833. if (!flag) {
  11834. hash_test[0] = hash1;
  11835. hash_test[1] = hash2;
  11836. hash_test[2] = hash3;
  11837. }
  11838. times = sizeof(hash_test) / sizeof(byte*);
  11839. /* Good test args. */
  11840. /* Testing oversized buffers. */
  11841. for (i = 0; i < times; i++) {
  11842. if (!flag) {
  11843. ret = wc_Sha224Final(&sha224, hash_test[i]);
  11844. if (ret != 0) {
  11845. flag = WOLFSSL_FATAL_ERROR;
  11846. }
  11847. }
  11848. }
  11849. /* Test bad args. */
  11850. if (!flag) {
  11851. ret = wc_Sha224Final(NULL, NULL);
  11852. if (ret != BAD_FUNC_ARG) {
  11853. flag = WOLFSSL_FATAL_ERROR;
  11854. }
  11855. }
  11856. if (!flag) {
  11857. ret = wc_Sha224Final(NULL, hash1);
  11858. if (ret != BAD_FUNC_ARG) {
  11859. flag = WOLFSSL_FATAL_ERROR;
  11860. }
  11861. }
  11862. if (!flag) {
  11863. ret = wc_Sha224Final(&sha224, NULL);
  11864. if (ret != BAD_FUNC_ARG) {
  11865. flag = WOLFSSL_FATAL_ERROR;
  11866. }
  11867. }
  11868. wc_Sha224Free(&sha224);
  11869. res = TEST_RES_CHECK(flag == 0);
  11870. #endif
  11871. return res;
  11872. } /* END test_wc_Sha224Final */
  11873. /*
  11874. * Unit test function for wc_Sha224SetFlags()
  11875. */
  11876. static int test_wc_Sha224SetFlags(void)
  11877. {
  11878. int res = TEST_SKIPPED;
  11879. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11880. wc_Sha224 sha224;
  11881. word32 flags = 0;
  11882. int flag = 0;
  11883. /* Initialize */
  11884. flag = wc_InitSha224(&sha224);
  11885. if (flag == 0) {
  11886. flag = wc_Sha224SetFlags(&sha224, flags);
  11887. }
  11888. if (flag == 0) {
  11889. if (flags & WC_HASH_FLAG_ISCOPY) {
  11890. flag = 0;
  11891. }
  11892. }
  11893. wc_Sha224Free(&sha224);
  11894. res = TEST_RES_CHECK(flag == 0);
  11895. #endif
  11896. return res;
  11897. } /* END test_wc_Sha224SetFlags */
  11898. /*
  11899. * Unit test function for wc_Sha224GetFlags()
  11900. */
  11901. static int test_wc_Sha224GetFlags(void)
  11902. {
  11903. int res = TEST_SKIPPED;
  11904. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11905. wc_Sha224 sha224;
  11906. word32 flags = 0;
  11907. int flag = 0;
  11908. /* Initialize */
  11909. flag = wc_InitSha224(&sha224);
  11910. if (flag == 0) {
  11911. flag = wc_Sha224GetFlags(&sha224, &flags);
  11912. }
  11913. if (flag == 0) {
  11914. if (flags & WC_HASH_FLAG_ISCOPY) {
  11915. flag = 0;
  11916. }
  11917. }
  11918. wc_Sha224Free(&sha224);
  11919. res = TEST_RES_CHECK(flag == 0);
  11920. #endif
  11921. return res;
  11922. } /* END test_wc_Sha224GetFlags */
  11923. /*
  11924. * Unit test function for wc_Sha224Free()
  11925. */
  11926. static int test_wc_Sha224Free(void)
  11927. {
  11928. int res = TEST_SKIPPED;
  11929. #ifdef WOLFSSL_SHA224
  11930. wc_Sha224Free(NULL);
  11931. res = TEST_RES_CHECK(1);
  11932. #endif
  11933. return res;
  11934. } /* END test_wc_Sha224Free */
  11935. /*
  11936. * Unit test function for wc_Sha224GetHash()
  11937. */
  11938. static int test_wc_Sha224GetHash(void)
  11939. {
  11940. int res = TEST_SKIPPED;
  11941. #ifdef WOLFSSL_SHA224
  11942. wc_Sha224 sha224;
  11943. byte hash1[WC_SHA224_DIGEST_SIZE];
  11944. int flag = 0;
  11945. /* Initialize */
  11946. flag = wc_InitSha224(&sha224);
  11947. if (flag == 0) {
  11948. flag = wc_Sha224GetHash(&sha224, hash1);
  11949. }
  11950. /*test bad arguments*/
  11951. if (flag == 0) {
  11952. flag = wc_Sha224GetHash(NULL, NULL);
  11953. if (flag == BAD_FUNC_ARG) {
  11954. flag = 0;
  11955. }
  11956. }
  11957. if (flag == 0) {
  11958. flag = wc_Sha224GetHash(NULL, hash1);
  11959. if (flag == BAD_FUNC_ARG) {
  11960. flag = 0;
  11961. }
  11962. }
  11963. if (flag == 0) {
  11964. flag = wc_Sha224GetHash(&sha224, NULL);
  11965. if (flag == BAD_FUNC_ARG) {
  11966. flag = 0;
  11967. }
  11968. }
  11969. wc_Sha224Free(&sha224);
  11970. res = TEST_RES_CHECK(flag == 0);
  11971. #endif
  11972. return res;
  11973. } /* END test_wc_Sha224GetHash */
  11974. /*
  11975. * Unit test function for wc_Sha224Copy()
  11976. */
  11977. static int test_wc_Sha224Copy(void)
  11978. {
  11979. int res = TEST_SKIPPED;
  11980. #ifdef WOLFSSL_SHA224
  11981. wc_Sha224 sha224;
  11982. wc_Sha224 temp;
  11983. int flag = 0;
  11984. /* Initialize */
  11985. flag = wc_InitSha224(&sha224);
  11986. if (flag == 0) {
  11987. flag = wc_InitSha224(&temp);
  11988. }
  11989. if (flag == 0) {
  11990. flag = wc_Sha224Copy(&sha224, &temp);
  11991. }
  11992. /*test bad arguments*/
  11993. if (flag == 0) {
  11994. flag = wc_Sha224Copy(NULL, NULL);
  11995. if (flag == BAD_FUNC_ARG) {
  11996. flag = 0;
  11997. }
  11998. }
  11999. if (flag == 0) {
  12000. flag = wc_Sha224Copy(NULL, &temp);
  12001. if (flag == BAD_FUNC_ARG) {
  12002. flag = 0;
  12003. }
  12004. }
  12005. if (flag == 0) {
  12006. flag = wc_Sha224Copy(&sha224, NULL);
  12007. if (flag == BAD_FUNC_ARG) {
  12008. flag = 0;
  12009. }
  12010. }
  12011. wc_Sha224Free(&sha224);
  12012. wc_Sha224Free(&temp);
  12013. res = TEST_RES_CHECK(flag == 0);
  12014. #endif
  12015. return res;
  12016. } /* END test_wc_Sha224Copy */
  12017. /*
  12018. * Testing wc_InitRipeMd()
  12019. */
  12020. static int test_wc_InitRipeMd(void)
  12021. {
  12022. int res = TEST_SKIPPED;
  12023. #ifdef WOLFSSL_RIPEMD
  12024. RipeMd ripemd;
  12025. int ret;
  12026. int flag = 0;
  12027. /* Test good arg. */
  12028. ret = wc_InitRipeMd(&ripemd);
  12029. if (ret != 0) {
  12030. flag = WOLFSSL_FATAL_ERROR;
  12031. }
  12032. /* Test bad arg. */
  12033. if (!flag) {
  12034. ret = wc_InitRipeMd(NULL);
  12035. if (ret != BAD_FUNC_ARG) {
  12036. flag = WOLFSSL_FATAL_ERROR;
  12037. }
  12038. }
  12039. res = TEST_RES_CHECK(flag == 0);
  12040. #endif
  12041. return res;
  12042. } /* END test_wc_InitRipeMd */
  12043. /*
  12044. * Testing wc_RipeMdUpdate()
  12045. */
  12046. static int test_wc_RipeMdUpdate(void)
  12047. {
  12048. int res = TEST_SKIPPED;
  12049. #ifdef WOLFSSL_RIPEMD
  12050. RipeMd ripemd;
  12051. byte hash[RIPEMD_DIGEST_SIZE];
  12052. testVector a, b, c;
  12053. int ret;
  12054. int flag = 0;
  12055. ret = wc_InitRipeMd(&ripemd);
  12056. if (ret != 0) {
  12057. flag = ret;
  12058. }
  12059. /* Input */
  12060. if (!flag) {
  12061. a.input = "a";
  12062. a.inLen = XSTRLEN(a.input);
  12063. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  12064. if (ret != 0) {
  12065. flag = ret;
  12066. }
  12067. }
  12068. if (!flag) {
  12069. ret = wc_RipeMdFinal(&ripemd, hash);
  12070. if (ret != 0) {
  12071. flag = ret;
  12072. }
  12073. }
  12074. /* Update input. */
  12075. if (!flag) {
  12076. a.input = "abc";
  12077. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12078. "\xb0\x87\xf1\x5a\x0b\xfc";
  12079. a.inLen = XSTRLEN(a.input);
  12080. a.outLen = XSTRLEN(a.output);
  12081. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  12082. if (ret != 0) {
  12083. flag = ret;
  12084. }
  12085. }
  12086. if (!flag) {
  12087. ret = wc_RipeMdFinal(&ripemd, hash);
  12088. if (ret != 0) {
  12089. flag = ret;
  12090. }
  12091. }
  12092. if (!flag) {
  12093. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  12094. flag = WOLFSSL_FATAL_ERROR;
  12095. }
  12096. }
  12097. /* Pass in bad values. */
  12098. if (!flag) {
  12099. b.input = NULL;
  12100. b.inLen = 0;
  12101. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  12102. if (ret != 0) {
  12103. flag = ret;
  12104. }
  12105. }
  12106. if (!flag) {
  12107. c.input = NULL;
  12108. c.inLen = RIPEMD_DIGEST_SIZE;
  12109. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  12110. if (ret != BAD_FUNC_ARG) {
  12111. flag = WOLFSSL_FATAL_ERROR;
  12112. }
  12113. }
  12114. if (!flag) {
  12115. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12116. if (ret != BAD_FUNC_ARG) {
  12117. flag = WOLFSSL_FATAL_ERROR;
  12118. }
  12119. }
  12120. res = TEST_RES_CHECK(flag == 0);
  12121. #endif
  12122. return res;
  12123. } /* END test_wc_RipeMdUdpate */
  12124. /*
  12125. * Unit test function for wc_RipeMdFinal()
  12126. */
  12127. static int test_wc_RipeMdFinal(void)
  12128. {
  12129. int res = TEST_SKIPPED;
  12130. #ifdef WOLFSSL_RIPEMD
  12131. RipeMd ripemd;
  12132. byte* hash_test[3];
  12133. byte hash1[RIPEMD_DIGEST_SIZE];
  12134. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12135. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12136. int times, i, ret;
  12137. int flag = 0;
  12138. /* Initialize */
  12139. ret = wc_InitRipeMd(&ripemd);
  12140. if (ret != 0) {
  12141. flag = ret;
  12142. }
  12143. if (!flag) {
  12144. hash_test[0] = hash1;
  12145. hash_test[1] = hash2;
  12146. hash_test[2] = hash3;
  12147. }
  12148. times = sizeof(hash_test) / sizeof(byte*);
  12149. /* Testing oversized buffers. */
  12150. for (i = 0; i < times; i++) {
  12151. if (!flag) {
  12152. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  12153. if (ret != 0) {
  12154. flag = WOLFSSL_FATAL_ERROR;
  12155. }
  12156. }
  12157. }
  12158. /* Test bad args. */
  12159. if (!flag) {
  12160. ret = wc_RipeMdFinal(NULL, NULL);
  12161. if (ret != BAD_FUNC_ARG) {
  12162. flag = WOLFSSL_FATAL_ERROR;
  12163. }
  12164. }
  12165. if (!flag) {
  12166. ret = wc_RipeMdFinal(NULL, hash1);
  12167. if (ret != BAD_FUNC_ARG) {
  12168. flag = WOLFSSL_FATAL_ERROR;
  12169. }
  12170. }
  12171. if (!flag) {
  12172. ret = wc_RipeMdFinal(&ripemd, NULL);
  12173. if (ret != BAD_FUNC_ARG) {
  12174. flag = WOLFSSL_FATAL_ERROR;
  12175. }
  12176. }
  12177. res = TEST_RES_CHECK(flag == 0);
  12178. #endif
  12179. return res;
  12180. } /* END test_wc_RipeMdFinal */
  12181. /*
  12182. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12183. * wc_InitSha3_512
  12184. */
  12185. static int test_wc_InitSha3(void)
  12186. {
  12187. int res = TEST_SKIPPED;
  12188. #if defined(WOLFSSL_SHA3)
  12189. wc_Sha3 sha3;
  12190. int ret = 0;
  12191. (void)sha3;
  12192. #if !defined(WOLFSSL_NOSHA3_224)
  12193. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12194. /* Test bad args. */
  12195. if (ret == 0) {
  12196. ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
  12197. if (ret == BAD_FUNC_ARG) {
  12198. ret = 0;
  12199. }
  12200. else if (ret == 0) {
  12201. ret = WOLFSSL_FATAL_ERROR;
  12202. }
  12203. }
  12204. wc_Sha3_224_Free(&sha3);
  12205. #endif /* NOSHA3_224 */
  12206. #if !defined(WOLFSSL_NOSHA3_256)
  12207. if (ret == 0) {
  12208. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12209. /* Test bad args. */
  12210. if (ret == 0) {
  12211. ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
  12212. if (ret == BAD_FUNC_ARG) {
  12213. ret = 0;
  12214. }
  12215. else if (ret == 0) {
  12216. ret = WOLFSSL_FATAL_ERROR;
  12217. }
  12218. }
  12219. wc_Sha3_256_Free(&sha3);
  12220. } /* END sha3_256 */
  12221. #endif /* NOSHA3_256 */
  12222. #if !defined(WOLFSSL_NOSHA3_384)
  12223. if (ret == 0) {
  12224. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12225. /* Test bad args. */
  12226. if (ret == 0) {
  12227. ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
  12228. if (ret == BAD_FUNC_ARG) {
  12229. ret = 0;
  12230. }
  12231. else if (ret == 0) {
  12232. ret = WOLFSSL_FATAL_ERROR;
  12233. }
  12234. }
  12235. wc_Sha3_384_Free(&sha3);
  12236. } /* END sha3_384 */
  12237. #endif /* NOSHA3_384 */
  12238. #if !defined(WOLFSSL_NOSHA3_512)
  12239. if (ret == 0) {
  12240. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12241. /* Test bad args. */
  12242. if (ret == 0) {
  12243. ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
  12244. if (ret == BAD_FUNC_ARG) {
  12245. ret = 0;
  12246. }
  12247. else if (ret == 0) {
  12248. ret = WOLFSSL_FATAL_ERROR;
  12249. }
  12250. }
  12251. wc_Sha3_512_Free(&sha3);
  12252. } /* END sha3_512 */
  12253. #endif /* NOSHA3_512 */
  12254. res = TEST_RES_CHECK(ret == 0);
  12255. #endif
  12256. return res;
  12257. } /* END test_wc_InitSha3 */
  12258. /*
  12259. * Testing wc_Sha3_Update()
  12260. */
  12261. static int testing_wc_Sha3_Update(void)
  12262. {
  12263. int res = TEST_SKIPPED;
  12264. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12265. !defined(WOLFSSL_AFALG_XILINX)
  12266. wc_Sha3 sha3;
  12267. byte msg[] = "Everybody's working for the weekend.";
  12268. byte msg2[] = "Everybody gets Friday off.";
  12269. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12270. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12271. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12272. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12273. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12274. word32 msglen = sizeof(msg) - 1;
  12275. word32 msg2len = sizeof(msg2);
  12276. word32 msgCmplen = sizeof(msgCmp);
  12277. int ret = 0;
  12278. #if !defined(WOLFSSL_NOSHA3_224)
  12279. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12280. if (ret != 0) {
  12281. return TEST_FAIL;
  12282. }
  12283. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  12284. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12285. ret = WOLFSSL_FATAL_ERROR;
  12286. }
  12287. if (ret == 0) {
  12288. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  12289. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12290. ret = WOLFSSL_FATAL_ERROR;
  12291. }
  12292. }
  12293. /* Pass bad args. */
  12294. if (ret == 0) {
  12295. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  12296. if (ret == BAD_FUNC_ARG) {
  12297. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  12298. }
  12299. if (ret == BAD_FUNC_ARG) {
  12300. wc_Sha3_224_Free(&sha3);
  12301. if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
  12302. return TEST_FAIL;
  12303. }
  12304. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  12305. if (ret == 0) {
  12306. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  12307. }
  12308. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12309. ret = WOLFSSL_FATAL_ERROR;
  12310. }
  12311. }
  12312. }
  12313. wc_Sha3_224_Free(&sha3);
  12314. #endif /* SHA3_224 */
  12315. #if !defined(WOLFSSL_NOSHA3_256)
  12316. if (ret == 0) {
  12317. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12318. if (ret != 0) {
  12319. return TEST_FAIL;
  12320. }
  12321. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  12322. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12323. ret = WOLFSSL_FATAL_ERROR;
  12324. }
  12325. if (ret == 0) {
  12326. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  12327. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12328. ret = WOLFSSL_FATAL_ERROR;
  12329. }
  12330. }
  12331. /* Pass bad args. */
  12332. if (ret == 0) {
  12333. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  12334. if (ret == BAD_FUNC_ARG) {
  12335. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  12336. }
  12337. if (ret == BAD_FUNC_ARG) {
  12338. wc_Sha3_256_Free(&sha3);
  12339. if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
  12340. return TEST_FAIL;
  12341. }
  12342. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  12343. if (ret == 0) {
  12344. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  12345. }
  12346. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12347. ret = WOLFSSL_FATAL_ERROR;
  12348. }
  12349. }
  12350. }
  12351. wc_Sha3_256_Free(&sha3);
  12352. }
  12353. #endif /* SHA3_256 */
  12354. #if !defined(WOLFSSL_NOSHA3_384)
  12355. if (ret == 0) {
  12356. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12357. if (ret != 0) {
  12358. return TEST_FAIL;
  12359. }
  12360. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  12361. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12362. ret = WOLFSSL_FATAL_ERROR;
  12363. }
  12364. if (ret == 0) {
  12365. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  12366. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12367. ret = WOLFSSL_FATAL_ERROR;
  12368. }
  12369. }
  12370. /* Pass bad args. */
  12371. if (ret == 0) {
  12372. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  12373. if (ret == BAD_FUNC_ARG) {
  12374. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  12375. }
  12376. if (ret == BAD_FUNC_ARG) {
  12377. wc_Sha3_384_Free(&sha3);
  12378. if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
  12379. return TEST_FAIL;
  12380. }
  12381. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  12382. if (ret == 0) {
  12383. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  12384. }
  12385. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12386. ret = WOLFSSL_FATAL_ERROR;
  12387. }
  12388. }
  12389. }
  12390. wc_Sha3_384_Free(&sha3);
  12391. }
  12392. #endif /* SHA3_384 */
  12393. #if !defined(WOLFSSL_NOSHA3_512)
  12394. if (ret == 0) {
  12395. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12396. if (ret != 0) {
  12397. return TEST_FAIL;
  12398. }
  12399. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  12400. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12401. ret = WOLFSSL_FATAL_ERROR;
  12402. }
  12403. if (ret == 0) {
  12404. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  12405. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12406. ret = WOLFSSL_FATAL_ERROR;
  12407. }
  12408. }
  12409. /* Pass bad args. */
  12410. if (ret == 0) {
  12411. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  12412. if (ret == BAD_FUNC_ARG) {
  12413. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  12414. }
  12415. if (ret == BAD_FUNC_ARG) {
  12416. wc_Sha3_512_Free(&sha3);
  12417. if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
  12418. return TEST_FAIL;
  12419. }
  12420. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  12421. if (ret == 0) {
  12422. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  12423. }
  12424. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12425. ret = WOLFSSL_FATAL_ERROR;
  12426. }
  12427. }
  12428. }
  12429. wc_Sha3_512_Free(&sha3);
  12430. }
  12431. #endif /* SHA3_512 */
  12432. res = TEST_RES_CHECK(ret == 0);
  12433. #endif /* WOLFSSL_SHA3 */
  12434. return res;
  12435. } /* END testing_wc_Sha3_Update */
  12436. /*
  12437. * Testing wc_Sha3_224_Final()
  12438. */
  12439. static int test_wc_Sha3_224_Final(void)
  12440. {
  12441. int res = TEST_SKIPPED;
  12442. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12443. wc_Sha3 sha3;
  12444. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12445. "nopnopq";
  12446. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12447. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12448. "\x64\xea\xd0\xfc\xce\x33";
  12449. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12450. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12451. int ret = 0;
  12452. /* Init stack variables. */
  12453. XMEMSET(hash, 0, sizeof(hash));
  12454. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12455. if (ret != 0) {
  12456. return TEST_FAIL;
  12457. }
  12458. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12459. if (ret == 0) {
  12460. ret = wc_Sha3_224_Final(&sha3, hash);
  12461. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  12462. ret = WOLFSSL_FATAL_ERROR;
  12463. }
  12464. }
  12465. /* Test bad args. */
  12466. if (ret == 0) {
  12467. ret = wc_Sha3_224_Final(NULL, hash);
  12468. if (ret == 0) {
  12469. ret = wc_Sha3_224_Final(&sha3, NULL);
  12470. }
  12471. if (ret == BAD_FUNC_ARG) {
  12472. ret = 0;
  12473. }
  12474. else if (ret == 0) {
  12475. ret = WOLFSSL_FATAL_ERROR;
  12476. }
  12477. }
  12478. wc_Sha3_224_Free(&sha3);
  12479. if (ret == 0) {
  12480. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12481. if (ret != 0) {
  12482. return TEST_FAIL;
  12483. }
  12484. /* Init stack variables. */
  12485. XMEMSET(hash, 0, sizeof(hash));
  12486. XMEMSET(hashRet, 0, sizeof(hashRet));
  12487. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12488. if (ret == 0) {
  12489. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  12490. }
  12491. if (ret == 0) {
  12492. ret = wc_Sha3_224_Final(&sha3, hash);
  12493. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  12494. ret = WOLFSSL_FATAL_ERROR;
  12495. }
  12496. }
  12497. if (ret == 0) {
  12498. /* Test bad args. */
  12499. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  12500. if (ret == BAD_FUNC_ARG) {
  12501. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  12502. }
  12503. if (ret == BAD_FUNC_ARG) {
  12504. ret = 0;
  12505. }
  12506. else if (ret == 0) {
  12507. ret = WOLFSSL_FATAL_ERROR;
  12508. }
  12509. }
  12510. }
  12511. wc_Sha3_224_Free(&sha3);
  12512. res = TEST_RES_CHECK(ret == 0);
  12513. #endif
  12514. return res;
  12515. } /* END test_wc_Sha3_224_Final */
  12516. /*
  12517. * Testing wc_Sha3_256_Final()
  12518. */
  12519. static int test_wc_Sha3_256_Final(void)
  12520. {
  12521. int res = TEST_SKIPPED;
  12522. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12523. wc_Sha3 sha3;
  12524. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12525. "nopnopq";
  12526. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12527. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12528. "\xdd\x97\x49\x6d\x33\x76";
  12529. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12530. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12531. int ret = 0;
  12532. /* Init stack variables. */
  12533. XMEMSET(hash, 0, sizeof(hash));
  12534. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12535. if (ret != 0) {
  12536. return TEST_FAIL;
  12537. }
  12538. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12539. if (ret == 0) {
  12540. ret = wc_Sha3_256_Final(&sha3, hash);
  12541. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  12542. ret = WOLFSSL_FATAL_ERROR;
  12543. }
  12544. }
  12545. /* Test bad args. */
  12546. if (ret == 0) {
  12547. ret = wc_Sha3_256_Final(NULL, hash);
  12548. if (ret == 0) {
  12549. ret = wc_Sha3_256_Final(&sha3, NULL);
  12550. }
  12551. if (ret == BAD_FUNC_ARG) {
  12552. ret = 0;
  12553. }
  12554. else if (ret == 0) {
  12555. ret = WOLFSSL_FATAL_ERROR;
  12556. }
  12557. }
  12558. wc_Sha3_256_Free(&sha3);
  12559. if (ret == 0) {
  12560. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12561. if (ret != 0) {
  12562. return TEST_FAIL;
  12563. }
  12564. /* Init stack variables. */
  12565. XMEMSET(hash, 0, sizeof(hash));
  12566. XMEMSET(hashRet, 0, sizeof(hashRet));
  12567. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12568. if (ret == 0) {
  12569. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  12570. }
  12571. if (ret == 0) {
  12572. ret = wc_Sha3_256_Final(&sha3, hash);
  12573. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  12574. ret = WOLFSSL_FATAL_ERROR;
  12575. }
  12576. }
  12577. if (ret == 0) {
  12578. /* Test bad args. */
  12579. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  12580. if (ret == BAD_FUNC_ARG) {
  12581. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  12582. }
  12583. if (ret == BAD_FUNC_ARG) {
  12584. ret = 0;
  12585. }
  12586. else if (ret == 0) {
  12587. ret = WOLFSSL_FATAL_ERROR;
  12588. }
  12589. }
  12590. }
  12591. wc_Sha3_256_Free(&sha3);
  12592. res = TEST_RES_CHECK(ret == 0);
  12593. #endif
  12594. return res;
  12595. } /* END test_wc_Sha3_256_Final */
  12596. /*
  12597. * Testing wc_Sha3_384_Final()
  12598. */
  12599. static int test_wc_Sha3_384_Final(void)
  12600. {
  12601. int res = TEST_SKIPPED;
  12602. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12603. wc_Sha3 sha3;
  12604. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12605. "nopnopq";
  12606. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12607. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12608. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12609. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12610. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12611. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12612. int ret = 0;
  12613. /* Init stack variables. */
  12614. XMEMSET(hash, 0, sizeof(hash));
  12615. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12616. if (ret != 0) {
  12617. return TEST_FAIL;
  12618. }
  12619. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12620. if (ret == 0) {
  12621. ret = wc_Sha3_384_Final(&sha3, hash);
  12622. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12623. ret = WOLFSSL_FATAL_ERROR;
  12624. }
  12625. }
  12626. /* Test bad args. */
  12627. if (ret == 0) {
  12628. ret = wc_Sha3_384_Final(NULL, hash);
  12629. if (ret == 0) {
  12630. ret = wc_Sha3_384_Final(&sha3, NULL);
  12631. }
  12632. if (ret == BAD_FUNC_ARG) {
  12633. ret = 0;
  12634. }
  12635. else if (ret == 0) {
  12636. ret = WOLFSSL_FATAL_ERROR;
  12637. }
  12638. }
  12639. wc_Sha3_384_Free(&sha3);
  12640. if (ret == 0) {
  12641. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12642. if (ret != 0) {
  12643. return TEST_FAIL;
  12644. }
  12645. /* Init stack variables. */
  12646. XMEMSET(hash, 0, sizeof(hash));
  12647. XMEMSET(hashRet, 0, sizeof(hashRet));
  12648. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12649. if (ret == 0) {
  12650. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  12651. }
  12652. if (ret == 0) {
  12653. ret = wc_Sha3_384_Final(&sha3, hash);
  12654. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12655. ret = WOLFSSL_FATAL_ERROR;
  12656. }
  12657. }
  12658. if (ret == 0) {
  12659. /* Test bad args. */
  12660. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  12661. if (ret == BAD_FUNC_ARG) {
  12662. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  12663. }
  12664. if (ret == BAD_FUNC_ARG) {
  12665. ret = 0;
  12666. }
  12667. else if (ret == 0) {
  12668. ret = WOLFSSL_FATAL_ERROR;
  12669. }
  12670. }
  12671. }
  12672. wc_Sha3_384_Free(&sha3);
  12673. res = TEST_RES_CHECK(ret == 0);
  12674. #endif
  12675. return res;
  12676. } /* END test_wc_Sha3_384_Final */
  12677. /*
  12678. * Testing wc_Sha3_512_Final()
  12679. */
  12680. static int test_wc_Sha3_512_Final(void)
  12681. {
  12682. int res = TEST_SKIPPED;
  12683. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12684. !defined(WOLFSSL_NOSHA3_384)
  12685. wc_Sha3 sha3;
  12686. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12687. "nopnopq";
  12688. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12689. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12690. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12691. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12692. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12693. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12694. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12695. int ret = 0;
  12696. /* Init stack variables. */
  12697. XMEMSET(hash, 0, sizeof(hash));
  12698. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12699. if (ret != 0) {
  12700. return TEST_FAIL;
  12701. }
  12702. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12703. if (ret == 0) {
  12704. ret = wc_Sha3_512_Final(&sha3, hash);
  12705. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12706. ret = WOLFSSL_FATAL_ERROR;
  12707. }
  12708. }
  12709. /* Test bad args. */
  12710. if (ret == 0) {
  12711. ret = wc_Sha3_512_Final(NULL, hash);
  12712. if (ret == 0) {
  12713. ret = wc_Sha3_384_Final(&sha3, NULL);
  12714. }
  12715. if (ret == BAD_FUNC_ARG) {
  12716. ret = 0;
  12717. }
  12718. else if (ret == 0) {
  12719. ret = WOLFSSL_FATAL_ERROR;
  12720. }
  12721. }
  12722. wc_Sha3_512_Free(&sha3);
  12723. if (ret == 0) {
  12724. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12725. if (ret != 0) {
  12726. return TEST_FAIL;
  12727. }
  12728. /* Init stack variables. */
  12729. XMEMSET(hash, 0, sizeof(hash));
  12730. XMEMSET(hashRet, 0, sizeof(hashRet));
  12731. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12732. if (ret == 0) {
  12733. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  12734. }
  12735. if (ret == 0) {
  12736. ret = wc_Sha3_512_Final(&sha3, hash);
  12737. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12738. ret = WOLFSSL_FATAL_ERROR;
  12739. }
  12740. }
  12741. if (ret == 0) {
  12742. /* Test bad args. */
  12743. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  12744. if (ret == BAD_FUNC_ARG) {
  12745. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  12746. }
  12747. if (ret == BAD_FUNC_ARG) {
  12748. ret = 0;
  12749. }
  12750. else if (ret == 0) {
  12751. ret = WOLFSSL_FATAL_ERROR;
  12752. }
  12753. }
  12754. }
  12755. wc_Sha3_512_Free(&sha3);
  12756. res = TEST_RES_CHECK(ret == 0);
  12757. #endif
  12758. return res;
  12759. } /* END test_wc_Sha3_512_Final */
  12760. /*
  12761. * Testing wc_Sha3_224_Copy()
  12762. */
  12763. static int test_wc_Sha3_224_Copy(void)
  12764. {
  12765. int res = TEST_SKIPPED;
  12766. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12767. wc_Sha3 sha3, sha3Cpy;
  12768. const char* msg = TEST_STRING;
  12769. word32 msglen = (word32)TEST_STRING_SZ;
  12770. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12771. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12772. int ret = 0;
  12773. XMEMSET(hash, 0, sizeof(hash));
  12774. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12775. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12776. if (ret != 0) {
  12777. return TEST_FAIL;
  12778. }
  12779. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
  12780. if (ret != 0) {
  12781. wc_Sha3_224_Free(&sha3);
  12782. return TEST_FAIL;
  12783. }
  12784. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  12785. if (ret == 0) {
  12786. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  12787. if (ret == 0) {
  12788. ret = wc_Sha3_224_Final(&sha3, hash);
  12789. if (ret == 0) {
  12790. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  12791. }
  12792. }
  12793. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12794. ret = WOLFSSL_FATAL_ERROR;
  12795. }
  12796. }
  12797. /* Test bad args. */
  12798. if (ret == 0) {
  12799. ret = wc_Sha3_224_Copy(NULL, &sha3);
  12800. if (ret == BAD_FUNC_ARG) {
  12801. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  12802. }
  12803. if (ret == BAD_FUNC_ARG) {
  12804. ret = 0;
  12805. }
  12806. else if (ret == 0) {
  12807. ret = WOLFSSL_FATAL_ERROR;
  12808. }
  12809. }
  12810. res = TEST_RES_CHECK(ret == 0);
  12811. #endif
  12812. return res;
  12813. } /* END test_wc_Sha3_224_Copy */
  12814. /*
  12815. * Testing wc_Sha3_256_Copy()
  12816. */
  12817. static int test_wc_Sha3_256_Copy(void)
  12818. {
  12819. int res = TEST_SKIPPED;
  12820. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12821. wc_Sha3 sha3, sha3Cpy;
  12822. const char* msg = TEST_STRING;
  12823. word32 msglen = (word32)TEST_STRING_SZ;
  12824. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12825. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12826. int ret = 0;
  12827. XMEMSET(hash, 0, sizeof(hash));
  12828. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12829. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12830. if (ret != 0) {
  12831. return TEST_FAIL;
  12832. }
  12833. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
  12834. if (ret != 0) {
  12835. wc_Sha3_256_Free(&sha3);
  12836. return TEST_FAIL;
  12837. }
  12838. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  12839. if (ret == 0) {
  12840. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  12841. if (ret == 0) {
  12842. ret = wc_Sha3_256_Final(&sha3, hash);
  12843. if (ret == 0) {
  12844. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  12845. }
  12846. }
  12847. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12848. ret = WOLFSSL_FATAL_ERROR;
  12849. }
  12850. }
  12851. /* Test bad args. */
  12852. if (ret == 0) {
  12853. ret = wc_Sha3_256_Copy(NULL, &sha3);
  12854. if (ret == BAD_FUNC_ARG) {
  12855. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  12856. }
  12857. if (ret == BAD_FUNC_ARG) {
  12858. ret = 0;
  12859. }
  12860. else if (ret == 0) {
  12861. ret = WOLFSSL_FATAL_ERROR;
  12862. }
  12863. }
  12864. res = TEST_RES_CHECK(ret == 0);
  12865. #endif
  12866. return res;
  12867. } /* END test_wc_Sha3_256_Copy */
  12868. /*
  12869. * Testing wc_Sha3_384_Copy()
  12870. */
  12871. static int test_wc_Sha3_384_Copy(void)
  12872. {
  12873. int res = TEST_SKIPPED;
  12874. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12875. wc_Sha3 sha3, sha3Cpy;
  12876. const char* msg = TEST_STRING;
  12877. word32 msglen = (word32)TEST_STRING_SZ;
  12878. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12879. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12880. int ret = 0;
  12881. XMEMSET(hash, 0, sizeof(hash));
  12882. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12883. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12884. if (ret != 0) {
  12885. return TEST_FAIL;
  12886. }
  12887. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
  12888. if (ret != 0) {
  12889. wc_Sha3_384_Free(&sha3);
  12890. return TEST_FAIL;
  12891. }
  12892. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  12893. if (ret == 0) {
  12894. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  12895. if (ret == 0) {
  12896. ret = wc_Sha3_384_Final(&sha3, hash);
  12897. if (ret == 0) {
  12898. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  12899. }
  12900. }
  12901. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12902. ret = WOLFSSL_FATAL_ERROR;
  12903. }
  12904. }
  12905. /* Test bad args. */
  12906. if (ret == 0) {
  12907. ret = wc_Sha3_384_Copy(NULL, &sha3);
  12908. if (ret == BAD_FUNC_ARG) {
  12909. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  12910. }
  12911. if (ret == BAD_FUNC_ARG) {
  12912. ret = 0;
  12913. }
  12914. else if (ret == 0) {
  12915. ret = WOLFSSL_FATAL_ERROR;
  12916. }
  12917. }
  12918. res = TEST_RES_CHECK(ret == 0);
  12919. #endif
  12920. return res;
  12921. } /* END test_wc_Sha3_384_Copy */
  12922. /*
  12923. * Testing wc_Sha3_512_Copy()
  12924. */
  12925. static int test_wc_Sha3_512_Copy(void)
  12926. {
  12927. int res = TEST_SKIPPED;
  12928. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12929. wc_Sha3 sha3, sha3Cpy;
  12930. const char* msg = TEST_STRING;
  12931. word32 msglen = (word32)TEST_STRING_SZ;
  12932. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12933. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12934. int ret = 0;
  12935. XMEMSET(hash, 0, sizeof(hash));
  12936. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12937. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12938. if (ret != 0) {
  12939. return TEST_FAIL;
  12940. }
  12941. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
  12942. if (ret != 0) {
  12943. wc_Sha3_512_Free(&sha3);
  12944. return TEST_FAIL;
  12945. }
  12946. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  12947. if (ret == 0) {
  12948. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  12949. if (ret == 0) {
  12950. ret = wc_Sha3_512_Final(&sha3, hash);
  12951. if (ret == 0) {
  12952. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  12953. }
  12954. }
  12955. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12956. ret = WOLFSSL_FATAL_ERROR;
  12957. }
  12958. }
  12959. /* Test bad args. */
  12960. if (ret == 0) {
  12961. ret = wc_Sha3_512_Copy(NULL, &sha3);
  12962. if (ret == BAD_FUNC_ARG) {
  12963. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  12964. }
  12965. if (ret == BAD_FUNC_ARG) {
  12966. ret = 0;
  12967. }
  12968. else if (ret == 0) {
  12969. ret = WOLFSSL_FATAL_ERROR;
  12970. }
  12971. }
  12972. res = TEST_RES_CHECK(ret == 0);
  12973. #endif
  12974. return res;
  12975. } /* END test_wc_Sha3_512_Copy */
  12976. /*
  12977. * Unit test function for wc_Sha3_GetFlags()
  12978. */
  12979. static int test_wc_Sha3_GetFlags(void)
  12980. {
  12981. int res = TEST_SKIPPED;
  12982. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12983. wc_Sha3 sha3;
  12984. word32 flags = 0;
  12985. int ret = 0;
  12986. /* Initialize */
  12987. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12988. if (ret != 0) {
  12989. return TEST_FAIL;
  12990. }
  12991. if (ret == 0) {
  12992. ret = wc_Sha3_GetFlags(&sha3, &flags);
  12993. }
  12994. if (ret == 0) {
  12995. if (flags & WC_HASH_FLAG_ISCOPY) {
  12996. ret = 0;
  12997. }
  12998. }
  12999. wc_Sha3_224_Free(&sha3);
  13000. res = TEST_RES_CHECK(ret == 0);
  13001. #endif
  13002. return res;
  13003. } /* END test_wc_Sha3_GetFlags */
  13004. static int test_wc_InitShake256(void)
  13005. {
  13006. int res = TEST_SKIPPED;
  13007. #ifdef WOLFSSL_SHAKE256
  13008. wc_Shake shake;
  13009. int ret = 0;
  13010. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13011. /* Test bad args. */
  13012. if (ret == 0) {
  13013. ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
  13014. if (ret == BAD_FUNC_ARG) {
  13015. ret = 0;
  13016. }
  13017. else if (ret == 0) {
  13018. ret = WOLFSSL_FATAL_ERROR;
  13019. }
  13020. }
  13021. wc_Shake256_Free(&shake);
  13022. res = TEST_RES_CHECK(ret == 0);
  13023. #endif
  13024. return res;
  13025. } /* END test_wc_InitSha3 */
  13026. static int testing_wc_Shake256_Update(void)
  13027. {
  13028. int res = TEST_SKIPPED;
  13029. #ifdef WOLFSSL_SHAKE256
  13030. wc_Shake shake;
  13031. byte msg[] = "Everybody's working for the weekend.";
  13032. byte msg2[] = "Everybody gets Friday off.";
  13033. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13034. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13035. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13036. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13037. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13038. word32 msglen = sizeof(msg) - 1;
  13039. word32 msg2len = sizeof(msg2);
  13040. word32 msgCmplen = sizeof(msgCmp);
  13041. int ret = 0;
  13042. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13043. if (ret != 0) {
  13044. return TEST_FAIL;
  13045. }
  13046. ret = wc_Shake256_Update(&shake, msg, msglen);
  13047. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  13048. ret = WOLFSSL_FATAL_ERROR;
  13049. }
  13050. if (ret == 0) {
  13051. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  13052. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  13053. ret = WOLFSSL_FATAL_ERROR;
  13054. }
  13055. }
  13056. /* Pass bad args. */
  13057. if (ret == 0) {
  13058. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  13059. if (ret == BAD_FUNC_ARG) {
  13060. ret = wc_Shake256_Update(&shake, NULL, 5);
  13061. }
  13062. if (ret == BAD_FUNC_ARG) {
  13063. wc_Shake256_Free(&shake);
  13064. if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
  13065. return TEST_FAIL;
  13066. }
  13067. ret = wc_Shake256_Update(&shake, NULL, 0);
  13068. if (ret == 0) {
  13069. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  13070. }
  13071. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  13072. ret = WOLFSSL_FATAL_ERROR;
  13073. }
  13074. }
  13075. }
  13076. wc_Shake256_Free(&shake);
  13077. res = TEST_RES_CHECK(ret == 0);
  13078. #endif /* WOLFSSL_SHAKE256 */
  13079. return res;
  13080. }
  13081. static int test_wc_Shake256_Final(void)
  13082. {
  13083. int res = TEST_SKIPPED;
  13084. #ifdef WOLFSSL_SHAKE256
  13085. wc_Shake shake;
  13086. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13087. "nopnopq";
  13088. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13089. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13090. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13091. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13092. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13093. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13094. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13095. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13096. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13097. byte hash[114];
  13098. int ret = 0;
  13099. /* Init stack variables. */
  13100. XMEMSET(hash, 0, sizeof(hash));
  13101. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13102. if (ret != 0) {
  13103. return TEST_FAIL;
  13104. }
  13105. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  13106. if (ret == 0) {
  13107. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  13108. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  13109. ret = WOLFSSL_FATAL_ERROR;
  13110. }
  13111. }
  13112. /* Test bad args. */
  13113. if (ret == 0) {
  13114. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  13115. if (ret == 0) {
  13116. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  13117. }
  13118. if (ret == BAD_FUNC_ARG) {
  13119. ret = 0;
  13120. }
  13121. else if (ret == 0) {
  13122. ret = WOLFSSL_FATAL_ERROR;
  13123. }
  13124. }
  13125. wc_Shake256_Free(&shake);
  13126. res = TEST_RES_CHECK(ret == 0);
  13127. #endif
  13128. return res;
  13129. }
  13130. /*
  13131. * Testing wc_Shake256_Copy()
  13132. */
  13133. static int test_wc_Shake256_Copy(void)
  13134. {
  13135. int res = TEST_SKIPPED;
  13136. #ifdef WOLFSSL_SHAKE256
  13137. wc_Shake shake, shakeCpy;
  13138. const char* msg = TEST_STRING;
  13139. word32 msglen = (word32)TEST_STRING_SZ;
  13140. byte hash[144];
  13141. byte hashCpy[144];
  13142. word32 hashLen = sizeof(hash);
  13143. word32 hashLenCpy = sizeof(hashCpy);
  13144. int ret;
  13145. XMEMSET(hash, 0, sizeof(hash));
  13146. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13147. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13148. if (ret != 0) {
  13149. return TEST_FAIL;
  13150. }
  13151. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
  13152. if (ret != 0) {
  13153. wc_Shake256_Free(&shake);
  13154. return TEST_FAIL;
  13155. }
  13156. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  13157. if (ret == 0) {
  13158. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  13159. if (ret == 0) {
  13160. ret = wc_Shake256_Final(&shake, hash, hashLen);
  13161. if (ret == 0) {
  13162. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  13163. }
  13164. }
  13165. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  13166. ret = WOLFSSL_FATAL_ERROR;
  13167. }
  13168. }
  13169. /* Test bad args. */
  13170. if (ret == 0) {
  13171. ret = wc_Shake256_Copy(NULL, &shake);
  13172. if (ret == BAD_FUNC_ARG) {
  13173. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  13174. }
  13175. if (ret == BAD_FUNC_ARG) {
  13176. ret = 0;
  13177. }
  13178. else if (ret == 0) {
  13179. ret = WOLFSSL_FATAL_ERROR;
  13180. }
  13181. }
  13182. wc_Shake256_Free(&shake);
  13183. res = TEST_RES_CHECK(ret == 0);
  13184. #endif
  13185. return res;
  13186. } /* END test_wc_Shake256_Copy */
  13187. /*
  13188. * Unit test function for wc_Shake256Hash()
  13189. */
  13190. static int test_wc_Shake256Hash(void)
  13191. {
  13192. int res = TEST_SKIPPED;
  13193. #ifdef WOLFSSL_SHAKE256
  13194. const byte data[] = { /* Hello World */
  13195. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13196. 0x72,0x6c,0x64
  13197. };
  13198. word32 len = sizeof(data);
  13199. byte hash[144];
  13200. word32 hashLen = sizeof(hash);
  13201. int ret;
  13202. ret = wc_Shake256Hash(data, len, hash, hashLen);
  13203. res = TEST_RES_CHECK(ret == 0);
  13204. #endif
  13205. return res;
  13206. } /* END test_wc_Shake256Hash */
  13207. /*
  13208. * Test function for wc_HmacSetKey
  13209. */
  13210. static int test_wc_Md5HmacSetKey(void)
  13211. {
  13212. int res = TEST_SKIPPED;
  13213. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13214. Hmac hmac;
  13215. int ret, times, itr;
  13216. int flag = 0;
  13217. const char* keys[]=
  13218. {
  13219. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13220. #ifndef HAVE_FIPS
  13221. "Jefe", /* smaller than minimum FIPS key size */
  13222. #endif
  13223. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13224. };
  13225. times = sizeof(keys) / sizeof(char*);
  13226. flag = 0;
  13227. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13228. if (ret != 0)
  13229. return TEST_FAIL;
  13230. for (itr = 0; itr < times; itr++) {
  13231. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13232. (word32)XSTRLEN(keys[itr]));
  13233. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13234. wc_HmacFree(&hmac);
  13235. if (ret == BAD_FUNC_ARG) {
  13236. return TEST_SUCCESS;
  13237. }
  13238. else {
  13239. return TEST_FAIL;
  13240. }
  13241. #else
  13242. if (ret != 0) {
  13243. flag = ret;
  13244. }
  13245. #endif
  13246. }
  13247. /* Bad args. */
  13248. if (!flag) {
  13249. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13250. (word32)XSTRLEN(keys[0]));
  13251. if (ret != BAD_FUNC_ARG) {
  13252. flag = WOLFSSL_FATAL_ERROR;
  13253. }
  13254. }
  13255. if (!flag) {
  13256. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  13257. if (ret != BAD_FUNC_ARG) {
  13258. flag = WOLFSSL_FATAL_ERROR;
  13259. }
  13260. }
  13261. if (!flag) {
  13262. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13263. (word32)XSTRLEN(keys[0]));
  13264. if (ret != BAD_FUNC_ARG) {
  13265. flag = WOLFSSL_FATAL_ERROR;
  13266. }
  13267. }
  13268. if (!flag) {
  13269. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13270. #ifdef HAVE_FIPS
  13271. if (ret != HMAC_MIN_KEYLEN_E) {
  13272. flag = WOLFSSL_FATAL_ERROR;
  13273. }
  13274. #else
  13275. if (ret != 0) {
  13276. flag = WOLFSSL_FATAL_ERROR;
  13277. }
  13278. #endif
  13279. }
  13280. wc_HmacFree(&hmac);
  13281. res = TEST_RES_CHECK(flag == 0);
  13282. #endif
  13283. return res;
  13284. } /* END test_wc_Md5HmacSetKey */
  13285. /*
  13286. * testing wc_HmacSetKey() on wc_Sha hash.
  13287. */
  13288. static int test_wc_ShaHmacSetKey(void)
  13289. {
  13290. int res = TEST_SKIPPED;
  13291. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13292. Hmac hmac;
  13293. int ret, times, itr;
  13294. int flag = 0;
  13295. const char* keys[]=
  13296. {
  13297. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13298. "\x0b\x0b\x0b",
  13299. #ifndef HAVE_FIPS
  13300. "Jefe", /* smaller than minimum FIPS key size */
  13301. #endif
  13302. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13303. "\xAA\xAA\xAA"
  13304. };
  13305. times = sizeof(keys) / sizeof(char*);
  13306. flag = 0;
  13307. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13308. if (ret != 0)
  13309. return ret;
  13310. for (itr = 0; itr < times; itr++) {
  13311. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13312. (word32)XSTRLEN(keys[itr]));
  13313. if (ret != 0) {
  13314. flag = ret;
  13315. }
  13316. }
  13317. /* Bad args. */
  13318. if (!flag) {
  13319. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13320. (word32)XSTRLEN(keys[0]));
  13321. if (ret != BAD_FUNC_ARG) {
  13322. flag = WOLFSSL_FATAL_ERROR;
  13323. }
  13324. }
  13325. if (!flag) {
  13326. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  13327. if (ret != BAD_FUNC_ARG) {
  13328. flag = WOLFSSL_FATAL_ERROR;
  13329. }
  13330. }
  13331. if (!flag) {
  13332. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13333. (word32)XSTRLEN(keys[0]));
  13334. if (ret != BAD_FUNC_ARG) {
  13335. flag = WOLFSSL_FATAL_ERROR;
  13336. }
  13337. }
  13338. if (!flag) {
  13339. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13340. #ifdef HAVE_FIPS
  13341. if (ret != HMAC_MIN_KEYLEN_E) {
  13342. flag = WOLFSSL_FATAL_ERROR;
  13343. }
  13344. #else
  13345. if (ret != 0) {
  13346. flag = WOLFSSL_FATAL_ERROR;
  13347. }
  13348. #endif
  13349. }
  13350. wc_HmacFree(&hmac);
  13351. res = TEST_RES_CHECK(flag == 0);
  13352. #endif
  13353. return res;
  13354. } /* END test_wc_ShaHmacSetKey() */
  13355. /*
  13356. * testing wc_HmacSetKey() on Sha224 hash.
  13357. */
  13358. static int test_wc_Sha224HmacSetKey(void)
  13359. {
  13360. int res = TEST_SKIPPED;
  13361. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13362. Hmac hmac;
  13363. int ret, times, itr;
  13364. int flag = 0;
  13365. const char* keys[]=
  13366. {
  13367. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13368. "\x0b\x0b\x0b",
  13369. #ifndef HAVE_FIPS
  13370. "Jefe", /* smaller than minimum FIPS key size */
  13371. #endif
  13372. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13373. "\xAA\xAA\xAA"
  13374. };
  13375. times = sizeof(keys) / sizeof(char*);
  13376. flag = 0;
  13377. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13378. if (ret != 0)
  13379. return ret;
  13380. for (itr = 0; itr < times; itr++) {
  13381. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13382. (word32)XSTRLEN(keys[itr]));
  13383. if (ret != 0) {
  13384. flag = ret;
  13385. }
  13386. }
  13387. /* Bad args. */
  13388. if (!flag) {
  13389. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13390. (word32)XSTRLEN(keys[0]));
  13391. if (ret != BAD_FUNC_ARG) {
  13392. flag = WOLFSSL_FATAL_ERROR;
  13393. }
  13394. }
  13395. if (!flag) {
  13396. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  13397. if (ret != BAD_FUNC_ARG) {
  13398. flag = WOLFSSL_FATAL_ERROR;
  13399. }
  13400. }
  13401. if (!flag) {
  13402. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13403. (word32)XSTRLEN(keys[0]));
  13404. if (ret != BAD_FUNC_ARG) {
  13405. flag = WOLFSSL_FATAL_ERROR;
  13406. }
  13407. }
  13408. if (!flag) {
  13409. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13410. #ifdef HAVE_FIPS
  13411. if (ret != HMAC_MIN_KEYLEN_E) {
  13412. flag = WOLFSSL_FATAL_ERROR;
  13413. }
  13414. #else
  13415. if (ret != 0) {
  13416. flag = WOLFSSL_FATAL_ERROR;
  13417. }
  13418. #endif
  13419. }
  13420. wc_HmacFree(&hmac);
  13421. res = TEST_RES_CHECK(flag == 0);
  13422. #endif
  13423. return res;
  13424. } /* END test_wc_Sha224HmacSetKey() */
  13425. /*
  13426. * testing wc_HmacSetKey() on Sha256 hash
  13427. */
  13428. static int test_wc_Sha256HmacSetKey(void)
  13429. {
  13430. int res = TEST_SKIPPED;
  13431. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13432. Hmac hmac;
  13433. int ret, times, itr;
  13434. int flag = 0;
  13435. const char* keys[]=
  13436. {
  13437. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13438. "\x0b\x0b\x0b",
  13439. #ifndef HAVE_FIPS
  13440. "Jefe", /* smaller than minimum FIPS key size */
  13441. #endif
  13442. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13443. "\xAA\xAA\xAA"
  13444. };
  13445. times = sizeof(keys) / sizeof(char*);
  13446. flag = 0;
  13447. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13448. if (ret != 0)
  13449. return ret;
  13450. for (itr = 0; itr < times; itr++) {
  13451. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13452. (word32)XSTRLEN(keys[itr]));
  13453. if (ret != 0) {
  13454. flag = ret;
  13455. }
  13456. }
  13457. /* Bad args. */
  13458. if (!flag) {
  13459. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13460. (word32)XSTRLEN(keys[0]));
  13461. if (ret != BAD_FUNC_ARG) {
  13462. flag = WOLFSSL_FATAL_ERROR;
  13463. }
  13464. }
  13465. if (!flag) {
  13466. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  13467. if (ret != BAD_FUNC_ARG) {
  13468. flag = WOLFSSL_FATAL_ERROR;
  13469. }
  13470. }
  13471. if (!flag) {
  13472. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13473. (word32)XSTRLEN(keys[0]));
  13474. if (ret != BAD_FUNC_ARG) {
  13475. flag = WOLFSSL_FATAL_ERROR;
  13476. }
  13477. }
  13478. if (!flag) {
  13479. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13480. #ifdef HAVE_FIPS
  13481. if (ret != HMAC_MIN_KEYLEN_E) {
  13482. flag = WOLFSSL_FATAL_ERROR;
  13483. }
  13484. #else
  13485. if (ret != 0) {
  13486. flag = WOLFSSL_FATAL_ERROR;
  13487. }
  13488. #endif
  13489. }
  13490. wc_HmacFree(&hmac);
  13491. res = TEST_RES_CHECK(flag == 0);
  13492. #endif
  13493. return res;
  13494. } /* END test_wc_Sha256HmacSetKey() */
  13495. /*
  13496. * testing wc_HmacSetKey on Sha384 hash.
  13497. */
  13498. static int test_wc_Sha384HmacSetKey(void)
  13499. {
  13500. int res = TEST_SKIPPED;
  13501. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13502. Hmac hmac;
  13503. int ret, times, itr;
  13504. int flag = 0;
  13505. const char* keys[]=
  13506. {
  13507. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13508. "\x0b\x0b\x0b",
  13509. #ifndef HAVE_FIPS
  13510. "Jefe", /* smaller than minimum FIPS key size */
  13511. #endif
  13512. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13513. "\xAA\xAA\xAA"
  13514. };
  13515. times = sizeof(keys) / sizeof(char*);
  13516. flag = 0;
  13517. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13518. if (ret != 0)
  13519. return ret;
  13520. for (itr = 0; itr < times; itr++) {
  13521. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13522. (word32)XSTRLEN(keys[itr]));
  13523. if (ret != 0) {
  13524. flag = ret;
  13525. }
  13526. }
  13527. /* Bad args. */
  13528. if (!flag) {
  13529. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13530. (word32)XSTRLEN(keys[0]));
  13531. if (ret != BAD_FUNC_ARG) {
  13532. flag = WOLFSSL_FATAL_ERROR;
  13533. }
  13534. }
  13535. if (!flag) {
  13536. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  13537. if (ret != BAD_FUNC_ARG) {
  13538. flag = WOLFSSL_FATAL_ERROR;
  13539. }
  13540. }
  13541. if (!flag) {
  13542. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13543. (word32)XSTRLEN(keys[0]));
  13544. if (ret != BAD_FUNC_ARG) {
  13545. flag = WOLFSSL_FATAL_ERROR;
  13546. }
  13547. }
  13548. if (!flag) {
  13549. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13550. #ifdef HAVE_FIPS
  13551. if (ret != HMAC_MIN_KEYLEN_E) {
  13552. flag = WOLFSSL_FATAL_ERROR;
  13553. }
  13554. #else
  13555. if (ret != 0) {
  13556. flag = WOLFSSL_FATAL_ERROR;
  13557. }
  13558. #endif
  13559. }
  13560. wc_HmacFree(&hmac);
  13561. res = TEST_RES_CHECK(flag == 0);
  13562. #endif
  13563. return res;
  13564. } /* END test_wc_Sha384HmacSetKey() */
  13565. /*
  13566. * testing wc_HmacUpdate on wc_Md5 hash.
  13567. */
  13568. static int test_wc_Md5HmacUpdate(void)
  13569. {
  13570. int res = TEST_SKIPPED;
  13571. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13572. Hmac hmac;
  13573. testVector a, b;
  13574. int ret;
  13575. int flag = 0;
  13576. #ifdef HAVE_FIPS
  13577. const char* keys =
  13578. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13579. #else
  13580. const char* keys = "Jefe";
  13581. #endif
  13582. a.input = "what do ya want for nothing?";
  13583. a.inLen = XSTRLEN(a.input);
  13584. b.input = "Hi There";
  13585. b.inLen = XSTRLEN(b.input);
  13586. flag = 0;
  13587. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13588. if (ret != 0)
  13589. return ret;
  13590. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  13591. if (ret != 0) {
  13592. flag = ret;
  13593. }
  13594. if (!flag) {
  13595. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13596. if (ret != 0) {
  13597. flag = ret;
  13598. }
  13599. }
  13600. /* Update Hmac. */
  13601. if (!flag) {
  13602. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13603. if (ret != 0) {
  13604. flag = ret;
  13605. }
  13606. }
  13607. /* Test bad args. */
  13608. if (!flag) {
  13609. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13610. if (ret != BAD_FUNC_ARG) {
  13611. flag = WOLFSSL_FATAL_ERROR;
  13612. }
  13613. }
  13614. if (!flag) {
  13615. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13616. if (ret != BAD_FUNC_ARG) {
  13617. flag = WOLFSSL_FATAL_ERROR;
  13618. }
  13619. }
  13620. if (!flag) {
  13621. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13622. if (ret != 0) {
  13623. flag = ret;
  13624. }
  13625. }
  13626. wc_HmacFree(&hmac);
  13627. res = TEST_RES_CHECK(flag == 0);
  13628. #endif
  13629. return res;
  13630. } /* END test_wc_Md5HmacUpdate */
  13631. /*
  13632. * testing wc_HmacUpdate on SHA hash.
  13633. */
  13634. static int test_wc_ShaHmacUpdate(void)
  13635. {
  13636. int res = TEST_SKIPPED;
  13637. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13638. Hmac hmac;
  13639. testVector a, b;
  13640. int ret;
  13641. int flag = 0;
  13642. #ifdef HAVE_FIPS
  13643. const char* keys =
  13644. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13645. #else
  13646. const char* keys = "Jefe";
  13647. #endif
  13648. a.input = "what do ya want for nothing?";
  13649. a.inLen = XSTRLEN(a.input);
  13650. b.input = "Hi There";
  13651. b.inLen = XSTRLEN(b.input);
  13652. flag = 0;
  13653. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13654. if (ret != 0)
  13655. return ret;
  13656. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  13657. if (ret != 0) {
  13658. flag = ret;
  13659. }
  13660. if (!flag) {
  13661. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13662. if (ret != 0) {
  13663. flag = ret;
  13664. }
  13665. }
  13666. /* Update Hmac. */
  13667. if (!flag) {
  13668. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13669. if (ret != 0) {
  13670. flag = ret;
  13671. }
  13672. }
  13673. /* Test bad args. */
  13674. if (!flag) {
  13675. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13676. if (ret != BAD_FUNC_ARG) {
  13677. flag = WOLFSSL_FATAL_ERROR;
  13678. }
  13679. }
  13680. if (!flag) {
  13681. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13682. if (ret != BAD_FUNC_ARG) {
  13683. flag = WOLFSSL_FATAL_ERROR;
  13684. }
  13685. }
  13686. if (!flag) {
  13687. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13688. if (ret != 0) {
  13689. flag = ret;
  13690. }
  13691. }
  13692. wc_HmacFree(&hmac);
  13693. res = TEST_RES_CHECK(flag == 0);
  13694. #endif
  13695. return res;
  13696. } /* END test_wc_ShaHmacUpdate */
  13697. /*
  13698. * testing wc_HmacUpdate on SHA224 hash.
  13699. */
  13700. static int test_wc_Sha224HmacUpdate(void)
  13701. {
  13702. int res = TEST_SKIPPED;
  13703. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13704. Hmac hmac;
  13705. testVector a, b;
  13706. int ret;
  13707. int flag = 0;
  13708. #ifdef HAVE_FIPS
  13709. const char* keys =
  13710. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13711. #else
  13712. const char* keys = "Jefe";
  13713. #endif
  13714. a.input = "what do ya want for nothing?";
  13715. a.inLen = XSTRLEN(a.input);
  13716. b.input = "Hi There";
  13717. b.inLen = XSTRLEN(b.input);
  13718. flag = 0;
  13719. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13720. if (ret != 0)
  13721. return ret;
  13722. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  13723. if (ret != 0) {
  13724. flag = ret;
  13725. }
  13726. if (!flag) {
  13727. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13728. if (ret != 0) {
  13729. flag = ret;
  13730. }
  13731. }
  13732. /* Update Hmac. */
  13733. if (!flag) {
  13734. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13735. if (ret != 0) {
  13736. flag = ret;
  13737. }
  13738. }
  13739. /* Test bad args. */
  13740. if (!flag) {
  13741. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13742. if (ret != BAD_FUNC_ARG) {
  13743. flag = WOLFSSL_FATAL_ERROR;
  13744. }
  13745. }
  13746. if (!flag) {
  13747. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13748. if (ret != BAD_FUNC_ARG) {
  13749. flag = WOLFSSL_FATAL_ERROR;
  13750. }
  13751. }
  13752. if (!flag) {
  13753. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13754. if (ret != 0) {
  13755. flag = ret;
  13756. }
  13757. }
  13758. wc_HmacFree(&hmac);
  13759. res = TEST_RES_CHECK(flag == 0);
  13760. #endif
  13761. return res;
  13762. } /* END test_wc_Sha224HmacUpdate */
  13763. /*
  13764. * testing wc_HmacUpdate on SHA256 hash.
  13765. */
  13766. static int test_wc_Sha256HmacUpdate(void)
  13767. {
  13768. int res = TEST_SKIPPED;
  13769. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13770. Hmac hmac;
  13771. testVector a, b;
  13772. int ret;
  13773. int flag = 0;
  13774. #ifdef HAVE_FIPS
  13775. const char* keys =
  13776. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13777. #else
  13778. const char* keys = "Jefe";
  13779. #endif
  13780. a.input = "what do ya want for nothing?";
  13781. a.inLen = XSTRLEN(a.input);
  13782. b.input = "Hi There";
  13783. b.inLen = XSTRLEN(b.input);
  13784. flag = 0;
  13785. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13786. if (ret != 0)
  13787. return ret;
  13788. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  13789. if (ret != 0) {
  13790. flag = ret;
  13791. }
  13792. if (!flag) {
  13793. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13794. if (ret != 0) {
  13795. flag = ret;
  13796. }
  13797. }
  13798. /* Update Hmac. */
  13799. if (!flag) {
  13800. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13801. if (ret != 0) {
  13802. flag = ret;
  13803. }
  13804. }
  13805. /* Test bad args. */
  13806. if (!flag) {
  13807. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13808. if (ret != BAD_FUNC_ARG) {
  13809. flag = WOLFSSL_FATAL_ERROR;
  13810. }
  13811. }
  13812. if (!flag) {
  13813. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13814. if (ret != BAD_FUNC_ARG) {
  13815. flag = WOLFSSL_FATAL_ERROR;
  13816. }
  13817. }
  13818. if (!flag) {
  13819. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13820. if (ret != 0) {
  13821. flag = ret;
  13822. }
  13823. }
  13824. wc_HmacFree(&hmac);
  13825. res = TEST_RES_CHECK(flag == 0);
  13826. #endif
  13827. return res;
  13828. } /* END test_wc_Sha256HmacUpdate */
  13829. /*
  13830. * testing wc_HmacUpdate on SHA384 hash.
  13831. */
  13832. static int test_wc_Sha384HmacUpdate(void)
  13833. {
  13834. int res = TEST_SKIPPED;
  13835. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13836. Hmac hmac;
  13837. testVector a, b;
  13838. int ret;
  13839. int flag = 0;
  13840. #ifdef HAVE_FIPS
  13841. const char* keys =
  13842. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13843. #else
  13844. const char* keys = "Jefe";
  13845. #endif
  13846. a.input = "what do ya want for nothing?";
  13847. a.inLen = XSTRLEN(a.input);
  13848. b.input = "Hi There";
  13849. b.inLen = XSTRLEN(b.input);
  13850. flag = 0;
  13851. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13852. if (ret != 0)
  13853. return ret;
  13854. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  13855. if (ret != 0) {
  13856. flag = ret;
  13857. }
  13858. if (!flag) {
  13859. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13860. if (ret != 0) {
  13861. flag = ret;
  13862. }
  13863. }
  13864. /* Update Hmac. */
  13865. if (!flag) {
  13866. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13867. if (ret != 0) {
  13868. flag = ret;
  13869. }
  13870. }
  13871. /* Test bad args. */
  13872. if (!flag) {
  13873. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13874. if (ret != BAD_FUNC_ARG) {
  13875. flag = WOLFSSL_FATAL_ERROR;
  13876. }
  13877. }
  13878. if (!flag) {
  13879. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13880. if (ret != BAD_FUNC_ARG) {
  13881. flag = WOLFSSL_FATAL_ERROR;
  13882. }
  13883. }
  13884. if (!flag) {
  13885. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13886. if (ret != 0) {
  13887. flag = ret;
  13888. }
  13889. }
  13890. wc_HmacFree(&hmac);
  13891. res = TEST_RES_CHECK(flag == 0);
  13892. #endif
  13893. return res;
  13894. } /* END test_wc_Sha384HmacUpdate */
  13895. /*
  13896. * Testing wc_HmacFinal() with MD5
  13897. */
  13898. static int test_wc_Md5HmacFinal(void)
  13899. {
  13900. int res = TEST_SKIPPED;
  13901. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13902. Hmac hmac;
  13903. byte hash[WC_MD5_DIGEST_SIZE];
  13904. testVector a;
  13905. int ret;
  13906. const char* key;
  13907. int flag = 0;
  13908. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13909. a.input = "Hi There";
  13910. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13911. "\x9d";
  13912. a.inLen = XSTRLEN(a.input);
  13913. a.outLen = XSTRLEN(a.output);
  13914. flag = 0;
  13915. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13916. if (ret != 0)
  13917. return ret;
  13918. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  13919. if (ret != 0) {
  13920. flag = ret;
  13921. }
  13922. if (!flag) {
  13923. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13924. if (ret != 0) {
  13925. flag = ret;
  13926. }
  13927. }
  13928. if (!flag) {
  13929. ret = wc_HmacFinal(&hmac, hash);
  13930. if (ret != 0) {
  13931. flag = ret;
  13932. }
  13933. }
  13934. if (!flag) {
  13935. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  13936. flag = WOLFSSL_FATAL_ERROR;
  13937. }
  13938. }
  13939. /* Try bad parameters. */
  13940. if (!flag) {
  13941. ret = wc_HmacFinal(NULL, hash);
  13942. if (ret != BAD_FUNC_ARG) {
  13943. flag = WOLFSSL_FATAL_ERROR;
  13944. }
  13945. }
  13946. #ifndef HAVE_FIPS
  13947. if (!flag) {
  13948. ret = wc_HmacFinal(&hmac, NULL);
  13949. if (ret != BAD_FUNC_ARG) {
  13950. flag = WOLFSSL_FATAL_ERROR;
  13951. }
  13952. }
  13953. #endif
  13954. wc_HmacFree(&hmac);
  13955. res = TEST_RES_CHECK(flag == 0);
  13956. #endif
  13957. return res;
  13958. } /* END test_wc_Md5HmacFinal */
  13959. /*
  13960. * Testing wc_HmacFinal() with SHA
  13961. */
  13962. static int test_wc_ShaHmacFinal(void)
  13963. {
  13964. int res = TEST_SKIPPED;
  13965. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13966. Hmac hmac;
  13967. byte hash[WC_SHA_DIGEST_SIZE];
  13968. testVector a;
  13969. int ret;
  13970. int flag = 0;
  13971. const char* key;
  13972. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13973. "\x0b\x0b\x0b";
  13974. a.input = "Hi There";
  13975. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13976. "\x8e\xf1\x46\xbe\x00";
  13977. a.inLen = XSTRLEN(a.input);
  13978. a.outLen = XSTRLEN(a.output);
  13979. flag = 0;
  13980. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13981. if (ret != 0)
  13982. return ret;
  13983. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  13984. if (ret != 0) {
  13985. flag = ret;
  13986. }
  13987. if (!flag) {
  13988. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13989. if (ret != 0) {
  13990. flag = ret;
  13991. }
  13992. }
  13993. if (!flag) {
  13994. ret = wc_HmacFinal(&hmac, hash);
  13995. if (ret != 0) {
  13996. flag = ret;
  13997. }
  13998. }
  13999. if (!flag) {
  14000. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  14001. flag = WOLFSSL_FATAL_ERROR;
  14002. }
  14003. }
  14004. /* Try bad parameters. */
  14005. if (!flag) {
  14006. ret = wc_HmacFinal(NULL, hash);
  14007. if (ret != BAD_FUNC_ARG) {
  14008. flag = WOLFSSL_FATAL_ERROR;
  14009. }
  14010. }
  14011. #ifndef HAVE_FIPS
  14012. if (!flag) {
  14013. ret = wc_HmacFinal(&hmac, NULL);
  14014. if (ret != BAD_FUNC_ARG) {
  14015. flag = WOLFSSL_FATAL_ERROR;
  14016. }
  14017. }
  14018. #endif
  14019. wc_HmacFree(&hmac);
  14020. res = TEST_RES_CHECK(flag == 0);
  14021. #endif
  14022. return res;
  14023. } /* END test_wc_ShaHmacFinal */
  14024. /*
  14025. * Testing wc_HmacFinal() with SHA224
  14026. */
  14027. static int test_wc_Sha224HmacFinal(void)
  14028. {
  14029. int res = TEST_SKIPPED;
  14030. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14031. Hmac hmac;
  14032. byte hash[WC_SHA224_DIGEST_SIZE];
  14033. testVector a;
  14034. int ret;
  14035. int flag = 0;
  14036. const char* key;
  14037. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14038. "\x0b\x0b\x0b";
  14039. a.input = "Hi There";
  14040. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14041. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14042. a.inLen = XSTRLEN(a.input);
  14043. a.outLen = XSTRLEN(a.output);
  14044. flag = 0;
  14045. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14046. if (ret != 0)
  14047. return ret;
  14048. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  14049. if (ret != 0) {
  14050. flag = ret;
  14051. }
  14052. if (!flag) {
  14053. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14054. if (ret != 0) {
  14055. flag = ret;
  14056. }
  14057. }
  14058. if (!flag) {
  14059. ret = wc_HmacFinal(&hmac, hash);
  14060. if (ret != 0) {
  14061. flag = ret;
  14062. }
  14063. }
  14064. if (!flag) {
  14065. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  14066. flag = WOLFSSL_FATAL_ERROR;
  14067. }
  14068. }
  14069. /* Try bad parameters. */
  14070. if (!flag) {
  14071. ret = wc_HmacFinal(NULL, hash);
  14072. if (ret != BAD_FUNC_ARG) {
  14073. flag = WOLFSSL_FATAL_ERROR;
  14074. }
  14075. }
  14076. #ifndef HAVE_FIPS
  14077. if (!flag) {
  14078. ret = wc_HmacFinal(&hmac, NULL);
  14079. if (ret != BAD_FUNC_ARG) {
  14080. flag = WOLFSSL_FATAL_ERROR;
  14081. }
  14082. }
  14083. #endif
  14084. wc_HmacFree(&hmac);
  14085. res = TEST_RES_CHECK(flag == 0);
  14086. #endif
  14087. return res;
  14088. } /* END test_wc_Sha224HmacFinal */
  14089. /*
  14090. * Testing wc_HmacFinal() with SHA256
  14091. */
  14092. static int test_wc_Sha256HmacFinal(void)
  14093. {
  14094. int res = TEST_SKIPPED;
  14095. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14096. Hmac hmac;
  14097. byte hash[WC_SHA256_DIGEST_SIZE];
  14098. testVector a;
  14099. int ret;
  14100. int flag = 0;
  14101. const char* key;
  14102. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14103. "\x0b\x0b\x0b";
  14104. a.input = "Hi There";
  14105. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14106. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14107. "\xcf\xf7";
  14108. a.inLen = XSTRLEN(a.input);
  14109. a.outLen = XSTRLEN(a.output);
  14110. flag = 0;
  14111. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14112. if (ret != 0)
  14113. return TEST_FAIL;
  14114. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  14115. if (ret != 0) {
  14116. flag = ret;
  14117. }
  14118. if (!flag) {
  14119. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14120. if (ret != 0) {
  14121. flag = ret;
  14122. }
  14123. }
  14124. if (!flag) {
  14125. ret = wc_HmacFinal(&hmac, hash);
  14126. if (ret != 0) {
  14127. flag = ret;
  14128. }
  14129. }
  14130. if (!flag) {
  14131. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  14132. flag = WOLFSSL_FATAL_ERROR;
  14133. }
  14134. }
  14135. /* Try bad parameters. */
  14136. if (!flag) {
  14137. ret = wc_HmacFinal(NULL, hash);
  14138. if (ret != BAD_FUNC_ARG) {
  14139. flag = WOLFSSL_FATAL_ERROR;
  14140. }
  14141. }
  14142. #ifndef HAVE_FIPS
  14143. if (!flag) {
  14144. ret = wc_HmacFinal(&hmac, NULL);
  14145. if (ret != BAD_FUNC_ARG) {
  14146. flag = WOLFSSL_FATAL_ERROR;
  14147. }
  14148. }
  14149. #endif
  14150. wc_HmacFree(&hmac);
  14151. res = TEST_RES_CHECK(flag == 0);
  14152. #endif
  14153. return res;
  14154. } /* END test_wc_Sha256HmacFinal */
  14155. /*
  14156. * Testing wc_HmacFinal() with SHA384
  14157. */
  14158. static int test_wc_Sha384HmacFinal(void)
  14159. {
  14160. int res = TEST_SKIPPED;
  14161. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14162. Hmac hmac;
  14163. byte hash[WC_SHA384_DIGEST_SIZE];
  14164. testVector a;
  14165. int ret;
  14166. int flag = 0;
  14167. const char* key;
  14168. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14169. "\x0b\x0b\x0b";
  14170. a.input = "Hi There";
  14171. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14172. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14173. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14174. "\xfa\x9c\xb6";
  14175. a.inLen = XSTRLEN(a.input);
  14176. a.outLen = XSTRLEN(a.output);
  14177. flag = 0;
  14178. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14179. if (ret != 0)
  14180. return ret;
  14181. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  14182. if (ret != 0) {
  14183. flag = ret;
  14184. }
  14185. if (!flag) {
  14186. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14187. if (ret != 0) {
  14188. flag = ret;
  14189. }
  14190. }
  14191. if (!flag) {
  14192. ret = wc_HmacFinal(&hmac, hash);
  14193. if (ret != 0) {
  14194. flag = ret;
  14195. }
  14196. }
  14197. if (!flag) {
  14198. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  14199. flag = WOLFSSL_FATAL_ERROR;
  14200. }
  14201. }
  14202. /* Try bad parameters. */
  14203. if (!flag) {
  14204. ret = wc_HmacFinal(NULL, hash);
  14205. if (ret != BAD_FUNC_ARG) {
  14206. flag = WOLFSSL_FATAL_ERROR;
  14207. }
  14208. }
  14209. #ifndef HAVE_FIPS
  14210. if (!flag) {
  14211. ret = wc_HmacFinal(&hmac, NULL);
  14212. if (ret != BAD_FUNC_ARG) {
  14213. flag = WOLFSSL_FATAL_ERROR;
  14214. }
  14215. }
  14216. #endif
  14217. wc_HmacFree(&hmac);
  14218. res = TEST_RES_CHECK(flag == 0);
  14219. #endif
  14220. return res;
  14221. } /* END test_wc_Sha384HmacFinal */
  14222. /*
  14223. * Testing wc_InitCmac()
  14224. */
  14225. static int test_wc_InitCmac(void)
  14226. {
  14227. int res = TEST_SKIPPED;
  14228. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14229. Cmac cmac1, cmac2, cmac3;
  14230. /* AES 128 key. */
  14231. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14232. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14233. /* AES 192 key. */
  14234. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14235. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14236. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14237. /* AES 256 key. */
  14238. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14239. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14240. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14241. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14242. word32 key1Sz = (word32)sizeof(key1) - 1;
  14243. word32 key2Sz = (word32)sizeof(key2) - 1;
  14244. word32 key3Sz = (word32)sizeof(key3) - 1;
  14245. int type = WC_CMAC_AES;
  14246. int ret = 0;
  14247. #ifdef WOLFSSL_AES_128
  14248. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  14249. #endif
  14250. #ifdef WOLFSSL_AES_192
  14251. if (ret == 0) {
  14252. wc_AesFree(&cmac1.aes);
  14253. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  14254. }
  14255. #endif
  14256. #ifdef WOLFSSL_AES_256
  14257. if (ret == 0) {
  14258. wc_AesFree(&cmac2.aes);
  14259. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  14260. }
  14261. #endif
  14262. /* Test bad args. */
  14263. if (ret == 0) {
  14264. wc_AesFree(&cmac3.aes);
  14265. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  14266. if (ret == BAD_FUNC_ARG) {
  14267. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  14268. }
  14269. if (ret == BAD_FUNC_ARG) {
  14270. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  14271. }
  14272. if (ret == BAD_FUNC_ARG) {
  14273. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  14274. }
  14275. if (ret == BAD_FUNC_ARG) {
  14276. ret = 0;
  14277. }
  14278. else {
  14279. ret = WOLFSSL_FATAL_ERROR;
  14280. }
  14281. }
  14282. (void)key1;
  14283. (void)key1Sz;
  14284. (void)key2;
  14285. (void)key2Sz;
  14286. (void)cmac1;
  14287. (void)cmac2;
  14288. res = TEST_RES_CHECK(ret == 0);
  14289. #endif
  14290. return res;
  14291. } /* END test_wc_InitCmac */
  14292. /*
  14293. * Testing wc_CmacUpdate()
  14294. */
  14295. static int test_wc_CmacUpdate(void)
  14296. {
  14297. int res = TEST_SKIPPED;
  14298. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14299. Cmac cmac;
  14300. byte key[] =
  14301. {
  14302. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14303. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14304. };
  14305. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14306. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14307. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14308. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14309. "\xf4";
  14310. word32 inSz = (word32)sizeof(in) - 1;
  14311. word32 keySz = (word32)sizeof(key);
  14312. int type = WC_CMAC_AES;
  14313. int ret = 0;
  14314. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14315. if (ret != 0) {
  14316. return ret;
  14317. }
  14318. ret = wc_CmacUpdate(&cmac, in, inSz);
  14319. /* Test bad args. */
  14320. if (ret == 0) {
  14321. ret = wc_CmacUpdate(NULL, in, inSz);
  14322. if (ret == BAD_FUNC_ARG) {
  14323. ret = wc_CmacUpdate(&cmac, NULL, 30);
  14324. }
  14325. if (ret == BAD_FUNC_ARG) {
  14326. ret = 0;
  14327. }
  14328. else if (ret == 0) {
  14329. ret = WOLFSSL_FATAL_ERROR;
  14330. }
  14331. wc_AesFree(&cmac.aes);
  14332. }
  14333. res = TEST_RES_CHECK(ret == 0);
  14334. #endif
  14335. return res;
  14336. } /* END test_wc_CmacUpdate */
  14337. /*
  14338. * Testing wc_CmacFinal()
  14339. */
  14340. static int test_wc_CmacFinal(void)
  14341. {
  14342. int res = TEST_SKIPPED;
  14343. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14344. Cmac cmac;
  14345. byte key[] =
  14346. {
  14347. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14348. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14349. };
  14350. byte msg[] =
  14351. {
  14352. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14353. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14354. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14355. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14356. 0xf4
  14357. };
  14358. /* Test vectors from CMACGenAES128.rsp from
  14359. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14360. * Per RFC4493 truncation of lsb is possible.
  14361. */
  14362. byte expMac[] =
  14363. {
  14364. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14365. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14366. };
  14367. byte mac[AES_BLOCK_SIZE];
  14368. word32 msgSz = (word32)sizeof(msg);
  14369. word32 keySz = (word32)sizeof(key);
  14370. word32 macSz = sizeof(mac);
  14371. word32 badMacSz = 17;
  14372. int expMacSz = sizeof(expMac);
  14373. int type = WC_CMAC_AES;
  14374. int ret = 0;
  14375. XMEMSET(mac, 0, macSz);
  14376. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14377. if (ret != 0) {
  14378. return ret;
  14379. }
  14380. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  14381. if (ret == 0) {
  14382. ret = wc_CmacFinal(&cmac, mac, &macSz);
  14383. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  14384. ret = WOLFSSL_FATAL_ERROR;
  14385. }
  14386. /* Pass in bad args. */
  14387. if (ret == 0) {
  14388. ret = wc_CmacFinal(NULL, mac, &macSz);
  14389. if (ret == BAD_FUNC_ARG) {
  14390. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  14391. }
  14392. if (ret == BAD_FUNC_ARG) {
  14393. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  14394. if (ret == BUFFER_E) {
  14395. ret = 0;
  14396. }
  14397. }
  14398. else if (ret == 0) {
  14399. ret = WOLFSSL_FATAL_ERROR;
  14400. }
  14401. }
  14402. }
  14403. res = TEST_RES_CHECK(ret == 0);
  14404. #endif
  14405. return res;
  14406. } /* END test_wc_CmacFinal */
  14407. /*
  14408. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14409. */
  14410. static int test_wc_AesCmacGenerate(void)
  14411. {
  14412. int res = TEST_SKIPPED;
  14413. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14414. Cmac cmac;
  14415. byte key[] =
  14416. {
  14417. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14418. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14419. };
  14420. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14421. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14422. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14423. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14424. byte mac[AES_BLOCK_SIZE];
  14425. word32 keySz = sizeof(key);
  14426. word32 macSz = sizeof(mac);
  14427. word32 msgSz = sizeof(msg) - 1;
  14428. word32 expMacSz = sizeof(expMac) - 1;
  14429. int type = WC_CMAC_AES;
  14430. int ret = 0;
  14431. XMEMSET(mac, 0, macSz);
  14432. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14433. if (ret != 0) {
  14434. return ret;
  14435. }
  14436. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  14437. if (ret != 0) {
  14438. return ret;
  14439. }
  14440. else {
  14441. wc_AesFree(&cmac.aes);
  14442. }
  14443. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  14444. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  14445. ret = WOLFSSL_FATAL_ERROR;
  14446. }
  14447. /* Pass in bad args. */
  14448. if (ret == 0) {
  14449. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  14450. if (ret == BAD_FUNC_ARG) {
  14451. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  14452. }
  14453. if (ret == BAD_FUNC_ARG) {
  14454. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  14455. }
  14456. if (ret == BAD_FUNC_ARG) {
  14457. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  14458. }
  14459. if (ret == BAD_FUNC_ARG) {
  14460. ret = 0;
  14461. }
  14462. else if (ret == 0) {
  14463. ret = WOLFSSL_FATAL_ERROR;
  14464. }
  14465. }
  14466. if (ret == 0) {
  14467. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  14468. /* Test bad args. */
  14469. if (ret == 0) {
  14470. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  14471. if (ret == BAD_FUNC_ARG) {
  14472. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  14473. }
  14474. if (ret == BAD_FUNC_ARG) {
  14475. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  14476. }
  14477. if (ret == BAD_FUNC_ARG) {
  14478. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  14479. }
  14480. if (ret == BAD_FUNC_ARG) {
  14481. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  14482. }
  14483. if (ret == BAD_FUNC_ARG) {
  14484. ret = 0;
  14485. }
  14486. else if (ret == 0) {
  14487. ret = WOLFSSL_FATAL_ERROR;
  14488. }
  14489. }
  14490. }
  14491. res = TEST_RES_CHECK(ret == 0);
  14492. #endif
  14493. return res;
  14494. } /* END test_wc_AesCmacGenerate */
  14495. /*
  14496. * Testing streaming AES-GCM API.
  14497. */
  14498. static int test_wc_AesGcmStream(void)
  14499. {
  14500. int res = TEST_SKIPPED;
  14501. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14502. defined(WOLFSSL_AESGCM_STREAM)
  14503. int ret = 0;
  14504. int i;
  14505. WC_RNG rng[1];
  14506. Aes aesEnc[1];
  14507. Aes aesDec[1];
  14508. byte tag[AES_BLOCK_SIZE];
  14509. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14510. byte out[AES_BLOCK_SIZE * 3 + 2];
  14511. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14512. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14513. byte key[AES_128_KEY_SIZE] = { 0, };
  14514. byte iv[AES_IV_SIZE] = { 1, };
  14515. byte ivOut[AES_IV_SIZE];
  14516. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14517. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14518. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14519. };
  14520. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14521. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14522. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14523. };
  14524. static const byte expTag[AES_BLOCK_SIZE] = {
  14525. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14526. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14527. };
  14528. /* Create a random for generating IV/nonce. */
  14529. AssertIntEQ(wc_InitRng(rng), 0);
  14530. /* Initialize data structures. */
  14531. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14532. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14533. /* BadParameters to streaming init. */
  14534. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14535. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14536. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14537. BAD_FUNC_ARG);
  14538. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14539. BAD_FUNC_ARG);
  14540. /* Bad parameters to encrypt update. */
  14541. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14542. BAD_FUNC_ARG);
  14543. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14544. BAD_FUNC_ARG);
  14545. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14546. BAD_FUNC_ARG);
  14547. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14548. BAD_FUNC_ARG);
  14549. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14550. BAD_FUNC_ARG);
  14551. /* Bad parameters to decrypt update. */
  14552. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14553. BAD_FUNC_ARG);
  14554. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14555. BAD_FUNC_ARG);
  14556. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14557. BAD_FUNC_ARG);
  14558. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14559. BAD_FUNC_ARG);
  14560. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14561. BAD_FUNC_ARG);
  14562. /* Bad parameters to encrypt final. */
  14563. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14564. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14565. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14566. BAD_FUNC_ARG);
  14567. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14568. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14569. BAD_FUNC_ARG);
  14570. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14571. BAD_FUNC_ARG);
  14572. /* Bad parameters to decrypt final. */
  14573. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14574. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14575. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14576. BAD_FUNC_ARG);
  14577. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14578. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14579. BAD_FUNC_ARG);
  14580. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14581. BAD_FUNC_ARG);
  14582. /* Check calling final before setting key fails. */
  14583. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14584. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14585. /* Check calling update before setting key else fails. */
  14586. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14587. MISSING_KEY);
  14588. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14589. MISSING_KEY);
  14590. /* Set key but not IV. */
  14591. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14592. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14593. /* Check calling final before setting IV fails. */
  14594. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14595. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14596. /* Check calling update before setting IV else fails. */
  14597. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14598. MISSING_IV);
  14599. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14600. MISSING_IV);
  14601. /* Set IV using fixed part IV and external IV APIs. */
  14602. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14603. rng), 0);
  14604. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14605. GCM_NONCE_MID_SZ), 0);
  14606. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14607. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14608. /* Encrypt and decrypt data. */
  14609. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14610. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14611. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14612. /* Finalize and check tag matches. */
  14613. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14614. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14615. /* Set key and IV through streaming init API. */
  14616. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14617. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14618. /* Encrypt/decrypt one block and AAD of one block. */
  14619. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14620. AES_BLOCK_SIZE), 0);
  14621. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14622. AES_BLOCK_SIZE), 0);
  14623. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14624. /* Finalize and check tag matches. */
  14625. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14626. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14627. /* Set key and IV through streaming init API. */
  14628. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14629. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14630. /* No data to encrypt/decrypt one byte of AAD. */
  14631. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14632. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14633. /* Finalize and check tag matches. */
  14634. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14635. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14636. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14637. /* Set key and IV through streaming init API. */
  14638. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14639. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14640. /* Encrypt/decrypt one byte and no AAD. */
  14641. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14642. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14643. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14644. /* Finalize and check tag matches. */
  14645. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14646. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14647. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14648. /* Set key and IV through streaming init API. */
  14649. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14650. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14651. /* Encryption AES is one byte at a time */
  14652. for (i = 0; i < (int)sizeof(aad); i++) {
  14653. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14654. 0);
  14655. }
  14656. for (i = 0; i < (int)sizeof(in); i++) {
  14657. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14658. 0);
  14659. }
  14660. /* Decryption AES is two bytes at a time */
  14661. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14662. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14663. 0);
  14664. }
  14665. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14666. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14667. 0), 0);
  14668. }
  14669. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14670. /* Finalize and check tag matches. */
  14671. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14672. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14673. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14674. /* Check streaming encryption can be decrypted with one shot. */
  14675. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14676. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14677. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14678. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14679. wc_AesFree(aesEnc);
  14680. wc_AesFree(aesDec);
  14681. wc_FreeRng(rng);
  14682. res = TEST_RES_CHECK(ret == 0);
  14683. #endif
  14684. return res;
  14685. } /* END test_wc_AesGcmStream */
  14686. /*
  14687. * unit test for wc_Des3_SetIV()
  14688. */
  14689. static int test_wc_Des3_SetIV(void)
  14690. {
  14691. int res = TEST_SKIPPED;
  14692. #ifndef NO_DES3
  14693. Des3 des;
  14694. int ret = 0;
  14695. const byte key[] =
  14696. {
  14697. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14698. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14699. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14700. };
  14701. const byte iv[] =
  14702. {
  14703. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14704. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14705. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14706. };
  14707. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14708. if (ret != 0)
  14709. return ret;
  14710. /* DES_ENCRYPTION or DES_DECRYPTION */
  14711. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14712. if (ret == 0) {
  14713. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14714. ret = WOLFSSL_FATAL_ERROR;
  14715. }
  14716. }
  14717. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14718. /* Test explicitly wc_Des3_SetIV() */
  14719. if (ret == 0) {
  14720. ret = wc_Des3_SetIV(NULL, iv);
  14721. if (ret == BAD_FUNC_ARG) {
  14722. ret = wc_Des3_SetIV(&des, NULL);
  14723. }
  14724. else if (ret == 0) {
  14725. ret = WOLFSSL_FATAL_ERROR;
  14726. }
  14727. }
  14728. #endif
  14729. wc_Des3Free(&des);
  14730. res = TEST_RES_CHECK(ret == 0);
  14731. #endif
  14732. return res;
  14733. } /* END test_wc_Des3_SetIV */
  14734. /*
  14735. * unit test for wc_Des3_SetKey()
  14736. */
  14737. static int test_wc_Des3_SetKey(void)
  14738. {
  14739. int res = TEST_SKIPPED;
  14740. #ifndef NO_DES3
  14741. Des3 des;
  14742. int ret = 0;
  14743. const byte key[] =
  14744. {
  14745. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14746. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14747. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14748. };
  14749. const byte iv[] =
  14750. {
  14751. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14752. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14753. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14754. };
  14755. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14756. if (ret != 0)
  14757. return ret;
  14758. /* DES_ENCRYPTION or DES_DECRYPTION */
  14759. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14760. if (ret == 0) {
  14761. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14762. ret = WOLFSSL_FATAL_ERROR;
  14763. }
  14764. }
  14765. /* Test bad args. */
  14766. if (ret == 0) {
  14767. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  14768. if (ret == BAD_FUNC_ARG) {
  14769. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  14770. }
  14771. if (ret == BAD_FUNC_ARG) {
  14772. ret = wc_Des3_SetKey(&des, key, iv, -1);
  14773. }
  14774. if (ret == BAD_FUNC_ARG) {
  14775. /* Default case. Should return 0. */
  14776. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  14777. }
  14778. } /* END if ret != 0 */
  14779. wc_Des3Free(&des);
  14780. res = TEST_RES_CHECK(ret == 0);
  14781. #endif
  14782. return res;
  14783. } /* END test_wc_Des3_SetKey */
  14784. /*
  14785. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14786. */
  14787. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14788. {
  14789. int res = TEST_SKIPPED;
  14790. #ifndef NO_DES3
  14791. Des3 des;
  14792. int ret = 0;
  14793. byte cipher[24];
  14794. byte plain[24];
  14795. const byte key[] =
  14796. {
  14797. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14798. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14799. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14800. };
  14801. const byte iv[] =
  14802. {
  14803. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14804. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14805. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14806. };
  14807. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14808. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14809. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14810. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14811. };
  14812. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14813. if (ret != 0)
  14814. return ret;
  14815. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14816. if (ret == 0) {
  14817. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  14818. if (ret == 0) {
  14819. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  14820. }
  14821. if (ret == 0) {
  14822. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  14823. }
  14824. }
  14825. if (ret == 0) {
  14826. if (XMEMCMP(plain, vector, 24) != 0) {
  14827. ret = WOLFSSL_FATAL_ERROR;
  14828. }
  14829. }
  14830. /* Pass in bad args. */
  14831. if (ret == 0) {
  14832. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  14833. if (ret == BAD_FUNC_ARG) {
  14834. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  14835. }
  14836. if (ret == BAD_FUNC_ARG) {
  14837. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  14838. }
  14839. if (ret != BAD_FUNC_ARG) {
  14840. ret = WOLFSSL_FATAL_ERROR;
  14841. }
  14842. else {
  14843. ret = 0;
  14844. }
  14845. }
  14846. if (ret == 0) {
  14847. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  14848. if (ret == BAD_FUNC_ARG) {
  14849. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  14850. }
  14851. if (ret == BAD_FUNC_ARG) {
  14852. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  14853. }
  14854. if (ret != BAD_FUNC_ARG) {
  14855. ret = WOLFSSL_FATAL_ERROR;
  14856. }
  14857. else {
  14858. ret = 0;
  14859. }
  14860. }
  14861. wc_Des3Free(&des);
  14862. res = TEST_RES_CHECK(ret == 0);
  14863. #endif
  14864. return res;
  14865. } /* END wc_Des3_CbcEncrypt */
  14866. /*
  14867. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14868. */
  14869. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14870. {
  14871. int res = TEST_SKIPPED;
  14872. #ifndef NO_DES3
  14873. int ret = 0;
  14874. word32 vectorSz, cipherSz;
  14875. byte cipher[24];
  14876. byte plain[24];
  14877. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14878. {
  14879. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14880. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14881. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14882. };
  14883. byte key[] =
  14884. {
  14885. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14886. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14887. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14888. };
  14889. byte iv[] =
  14890. {
  14891. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14892. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14893. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14894. };
  14895. vectorSz = sizeof(byte) * 24;
  14896. cipherSz = sizeof(byte) * 24;
  14897. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  14898. if (ret == 0) {
  14899. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  14900. if (ret == 0) {
  14901. if (XMEMCMP(plain, vector, 24) != 0) {
  14902. ret = WOLFSSL_FATAL_ERROR;
  14903. }
  14904. }
  14905. }
  14906. /* pass in bad args. */
  14907. if (ret == 0) {
  14908. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  14909. if (ret == BAD_FUNC_ARG) {
  14910. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  14911. }
  14912. if (ret == BAD_FUNC_ARG) {
  14913. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  14914. }
  14915. if (ret == BAD_FUNC_ARG) {
  14916. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  14917. key, NULL);
  14918. }
  14919. else {
  14920. /* Return code catch. */
  14921. ret = WOLFSSL_FAILURE;
  14922. }
  14923. }
  14924. if (ret == 0) {
  14925. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  14926. if (ret == BAD_FUNC_ARG) {
  14927. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  14928. }
  14929. if (ret == BAD_FUNC_ARG) {
  14930. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  14931. }
  14932. if (ret == BAD_FUNC_ARG) {
  14933. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  14934. }
  14935. else {
  14936. ret = WOLFSSL_FAILURE;
  14937. }
  14938. }
  14939. res = TEST_RES_CHECK(ret == 0);
  14940. #endif
  14941. return res;
  14942. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14943. /*
  14944. * Unit test for wc_Des3_EcbEncrypt
  14945. */
  14946. static int test_wc_Des3_EcbEncrypt(void)
  14947. {
  14948. int res = TEST_SKIPPED;
  14949. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14950. Des3 des;
  14951. int ret = 0;
  14952. byte cipher[24];
  14953. word32 cipherSz = sizeof(cipher);
  14954. const byte key[] =
  14955. {
  14956. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14957. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14958. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14959. };
  14960. const byte iv[] =
  14961. {
  14962. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14963. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14964. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14965. };
  14966. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14967. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14968. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14969. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14970. };
  14971. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14972. if (ret != 0) {
  14973. return ret;
  14974. }
  14975. if (ret == 0 ) {
  14976. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14977. }
  14978. /* Bad Cases */
  14979. if (ret == 0) {
  14980. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  14981. if (ret == BAD_FUNC_ARG) {
  14982. ret = 0;
  14983. }
  14984. }
  14985. if (ret == 0) {
  14986. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  14987. if (ret == BAD_FUNC_ARG) {
  14988. ret = 0;
  14989. }
  14990. }
  14991. if (ret == 0) {
  14992. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  14993. if (ret == BAD_FUNC_ARG) {
  14994. ret = 0;
  14995. }
  14996. }
  14997. if (ret == 0) {
  14998. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  14999. if (ret == BAD_FUNC_ARG) {
  15000. ret = 0;
  15001. }
  15002. }
  15003. if (ret == 0) {
  15004. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  15005. if (ret == BAD_FUNC_ARG) {
  15006. ret = 0;
  15007. }
  15008. }
  15009. /* Good Cases */
  15010. if (ret == 0) {
  15011. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  15012. }
  15013. wc_Des3Free(&des);
  15014. res = TEST_RES_CHECK(ret == 0);
  15015. #endif
  15016. return res;
  15017. } /* END test_wc_Des3_EcbEncrypt */
  15018. /*
  15019. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15020. */
  15021. static int test_wc_Chacha_SetKey(void)
  15022. {
  15023. int res = TEST_SKIPPED;
  15024. #ifdef HAVE_CHACHA
  15025. ChaCha ctx;
  15026. const byte key[] =
  15027. {
  15028. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15029. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15030. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15031. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15032. };
  15033. byte cipher[128];
  15034. int ret = 0;
  15035. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  15036. /* Test bad args. */
  15037. if (ret == 0) {
  15038. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  15039. if (ret == BAD_FUNC_ARG) {
  15040. ret = wc_Chacha_SetKey(&ctx, key, 18);
  15041. }
  15042. if (ret == BAD_FUNC_ARG) {
  15043. ret = 0;
  15044. }
  15045. else {
  15046. ret = WOLFSSL_FATAL_ERROR;
  15047. }
  15048. }
  15049. if (ret == 0) {
  15050. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  15051. }
  15052. if (ret == 0) {
  15053. /* Test bad args. */
  15054. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  15055. if (ret == BAD_FUNC_ARG) {
  15056. ret = 0;
  15057. }
  15058. else {
  15059. ret = WOLFSSL_FAILURE;
  15060. }
  15061. }
  15062. res = TEST_RES_CHECK(ret == 0);
  15063. #endif
  15064. return res;
  15065. } /* END test_wc_Chacha_SetKey */
  15066. /*
  15067. * unit test for wc_Poly1305SetKey()
  15068. */
  15069. static int test_wc_Poly1305SetKey(void)
  15070. {
  15071. int res = TEST_SKIPPED;
  15072. #ifdef HAVE_POLY1305
  15073. Poly1305 ctx;
  15074. const byte key[] =
  15075. {
  15076. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15077. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15078. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15079. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15080. };
  15081. int ret = 0;
  15082. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  15083. /* Test bad args. */
  15084. if (ret == 0) {
  15085. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  15086. if (ret == BAD_FUNC_ARG) {
  15087. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  15088. }
  15089. if (ret == BAD_FUNC_ARG) {
  15090. ret = wc_Poly1305SetKey(&ctx, key, 18);
  15091. }
  15092. if (ret == BAD_FUNC_ARG) {
  15093. ret = 0;
  15094. }
  15095. else {
  15096. ret = WOLFSSL_FATAL_ERROR;
  15097. }
  15098. }
  15099. res = TEST_RES_CHECK(ret == 0);
  15100. #endif
  15101. return res;
  15102. } /* END test_wc_Poly1305_SetKey() */
  15103. /*
  15104. * Testing wc_Chacha_Process()
  15105. */
  15106. static int test_wc_Chacha_Process(void)
  15107. {
  15108. int res = TEST_SKIPPED;
  15109. #ifdef HAVE_CHACHA
  15110. ChaCha enc, dec;
  15111. byte cipher[128];
  15112. byte plain[128];
  15113. const byte key[] =
  15114. {
  15115. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15116. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15117. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15118. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15119. };
  15120. const char* input = "Everybody gets Friday off.";
  15121. word32 keySz = sizeof(key)/sizeof(byte);
  15122. unsigned long int inlen = XSTRLEN(input);
  15123. int ret = 0;
  15124. /*Initialize stack varialbes.*/
  15125. XMEMSET(cipher, 0, 128);
  15126. XMEMSET(plain, 0, 128);
  15127. ret = wc_Chacha_SetKey(&enc, key, keySz);
  15128. AssertIntEQ(ret, 0);
  15129. ret = wc_Chacha_SetKey(&dec, key, keySz);
  15130. AssertIntEQ(ret, 0);
  15131. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  15132. AssertIntEQ(ret, 0);
  15133. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  15134. AssertIntEQ(ret, 0);
  15135. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  15136. AssertIntEQ(ret, 0);
  15137. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  15138. AssertIntEQ(ret, 0);
  15139. ret = XMEMCMP(input, plain, (int)inlen);
  15140. AssertIntEQ(ret, 0);
  15141. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15142. /* test checking and using leftovers, currently just in C code */
  15143. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  15144. AssertIntEQ(ret, 0);
  15145. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  15146. AssertIntEQ(ret, 0);
  15147. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  15148. AssertIntEQ(ret, 0);
  15149. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15150. (byte*)input + (inlen - 2), 2);
  15151. AssertIntEQ(ret, 0);
  15152. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  15153. AssertIntEQ(ret, 0);
  15154. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15155. (byte*)input + (inlen - 2), 2);
  15156. AssertIntEQ(ret, 0);
  15157. ret = XMEMCMP(input, plain, (int)inlen);
  15158. AssertIntEQ(ret, 0);
  15159. /* check edge cases with counter increment */
  15160. {
  15161. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15162. const byte expected[] = {
  15163. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15164. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15165. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15166. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15167. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15168. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15169. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15170. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15171. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15172. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15173. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15174. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15175. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15176. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15177. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15178. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15179. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15180. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15181. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15182. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15183. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15184. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15185. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15186. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15187. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15188. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15189. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15190. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15191. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15192. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15193. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15194. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15195. };
  15196. const byte iv2[] = {
  15197. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15198. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15199. };
  15200. byte input2[256];
  15201. int i;
  15202. for (i = 0; i < 256; i++)
  15203. input2[i] = i;
  15204. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  15205. AssertIntEQ(ret, 0);
  15206. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  15207. AssertIntEQ(ret, 0);
  15208. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15209. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  15210. AssertIntEQ(ret, 0);
  15211. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15212. /* partial */
  15213. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  15214. AssertIntEQ(ret, 0);
  15215. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15216. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  15217. AssertIntEQ(ret, 0);
  15218. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15219. }
  15220. #endif
  15221. /* Test bad args. */
  15222. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  15223. AssertIntEQ(ret, BAD_FUNC_ARG);
  15224. if (ret == BAD_FUNC_ARG) {
  15225. ret = 0;
  15226. }
  15227. res = TEST_RES_CHECK(ret == 0);
  15228. #endif
  15229. return res;
  15230. } /* END test_wc_Chacha_Process */
  15231. /*
  15232. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15233. */
  15234. static int test_wc_ChaCha20Poly1305_aead(void)
  15235. {
  15236. int res = TEST_SKIPPED;
  15237. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15238. const byte key[] = {
  15239. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15240. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15241. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15242. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15243. };
  15244. const byte plaintext[] = {
  15245. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15246. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15247. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15248. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15249. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15250. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15251. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15252. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15253. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15254. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15255. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15256. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15257. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15258. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15259. 0x74, 0x2e
  15260. };
  15261. const byte iv[] = {
  15262. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15263. 0x44, 0x45, 0x46, 0x47
  15264. };
  15265. const byte aad[] = { /* additional data */
  15266. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15267. 0xc4, 0xc5, 0xc6, 0xc7
  15268. };
  15269. const byte cipher[] = { /* expected output from operation */
  15270. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15271. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15272. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15273. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15274. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15275. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15276. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15277. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15278. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15279. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15280. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15281. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15282. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15283. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15284. 0x61, 0x16
  15285. };
  15286. const byte authTag[] = { /* expected output from operation */
  15287. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15288. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15289. };
  15290. byte generatedCiphertext[272];
  15291. byte generatedPlaintext[272];
  15292. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15293. int ret = 0;
  15294. /* Initialize stack variables. */
  15295. XMEMSET(generatedCiphertext, 0, 272);
  15296. XMEMSET(generatedPlaintext, 0, 272);
  15297. /* Test Encrypt */
  15298. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  15299. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15300. AssertIntEQ(ret, 0);
  15301. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  15302. AssertIntEQ(ret, 0);
  15303. /* Test bad args. */
  15304. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  15305. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15306. AssertIntEQ(ret, BAD_FUNC_ARG);
  15307. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15308. plaintext, sizeof(plaintext),
  15309. generatedCiphertext, generatedAuthTag);
  15310. AssertIntEQ(ret, BAD_FUNC_ARG);
  15311. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15312. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15313. AssertIntEQ(ret, BAD_FUNC_ARG);
  15314. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15315. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15316. AssertIntEQ(ret, BAD_FUNC_ARG);
  15317. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15318. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  15319. AssertIntEQ(ret, BAD_FUNC_ARG);
  15320. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15321. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  15322. if (ret == BAD_FUNC_ARG) {
  15323. ret = 0;
  15324. (void)ret; /* suppress never read */
  15325. }
  15326. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15327. sizeof(cipher), authTag, generatedPlaintext);
  15328. AssertIntEQ(ret, 0);
  15329. ret = XMEMCMP(generatedPlaintext, plaintext,
  15330. sizeof(plaintext)/sizeof(byte));
  15331. AssertIntEQ(ret, 0);
  15332. /* Test bad args. */
  15333. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15334. sizeof(cipher), authTag, generatedPlaintext);
  15335. AssertIntEQ(ret, BAD_FUNC_ARG);
  15336. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15337. cipher, sizeof(cipher), authTag, generatedPlaintext);
  15338. AssertIntEQ(ret, BAD_FUNC_ARG);
  15339. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15340. sizeof(cipher), authTag, generatedPlaintext);
  15341. AssertIntEQ(ret, BAD_FUNC_ARG);
  15342. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15343. sizeof(cipher), NULL, generatedPlaintext);
  15344. AssertIntEQ(ret, BAD_FUNC_ARG);
  15345. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15346. sizeof(cipher), authTag, NULL);
  15347. AssertIntEQ(ret, BAD_FUNC_ARG);
  15348. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15349. sizeof(cipher), authTag, generatedPlaintext);
  15350. AssertIntEQ(ret, BAD_FUNC_ARG);
  15351. if (ret == BAD_FUNC_ARG) {
  15352. ret = 0;
  15353. }
  15354. res = TEST_RES_CHECK(ret == 0);
  15355. #endif
  15356. return res;
  15357. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  15358. /*
  15359. * Testing function for wc_Rc2SetKey().
  15360. */
  15361. static int test_wc_Rc2SetKey(void)
  15362. {
  15363. int res = TEST_SKIPPED;
  15364. #ifdef WC_RC2
  15365. Rc2 rc2;
  15366. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15367. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15368. int ret = 0;
  15369. /* valid key and IV */
  15370. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15371. iv, 40);
  15372. if (ret == 0) {
  15373. /* valid key, no IV */
  15374. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15375. NULL, 40);
  15376. }
  15377. /* bad arguments */
  15378. if (ret == 0) {
  15379. /* null Rc2 struct */
  15380. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15381. iv, 40);
  15382. if (ret == BAD_FUNC_ARG) {
  15383. ret = 0;
  15384. }
  15385. }
  15386. if (ret == 0) {
  15387. /* null key */
  15388. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15389. iv, 40);
  15390. if (ret == BAD_FUNC_ARG) {
  15391. ret = 0;
  15392. }
  15393. }
  15394. if (ret == 0) {
  15395. /* key size == 0 */
  15396. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  15397. if (ret == WC_KEY_SIZE_E) {
  15398. ret = 0;
  15399. }
  15400. }
  15401. if (ret == 0) {
  15402. /* key size > 128 */
  15403. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  15404. if (ret == WC_KEY_SIZE_E) {
  15405. ret = 0;
  15406. }
  15407. }
  15408. if (ret == 0) {
  15409. /* effective bits == 0 */
  15410. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15411. iv, 0);
  15412. if (ret == WC_KEY_SIZE_E) {
  15413. ret = 0;
  15414. }
  15415. }
  15416. if (ret == 0) {
  15417. /* effective bits > 1024 */
  15418. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15419. iv, 1025);
  15420. if (ret == WC_KEY_SIZE_E) {
  15421. ret = 0;
  15422. }
  15423. }
  15424. res = TEST_RES_CHECK(ret == 0);
  15425. #endif
  15426. return res;
  15427. } /* END test_wc_Rc2SetKey */
  15428. /*
  15429. * Testing function for wc_Rc2SetIV().
  15430. */
  15431. static int test_wc_Rc2SetIV(void)
  15432. {
  15433. int res = TEST_SKIPPED;
  15434. #ifdef WC_RC2
  15435. Rc2 rc2;
  15436. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15437. int ret = 0;
  15438. /* valid IV */
  15439. ret = wc_Rc2SetIV(&rc2, iv);
  15440. if (ret == 0) {
  15441. /* valid NULL IV */
  15442. ret = wc_Rc2SetIV(&rc2, NULL);
  15443. }
  15444. /* bad arguments */
  15445. if (ret == 0) {
  15446. ret = wc_Rc2SetIV(NULL, iv);
  15447. if (ret == BAD_FUNC_ARG) {
  15448. ret = 0;
  15449. }
  15450. }
  15451. res = TEST_RES_CHECK(ret == 0);
  15452. #endif
  15453. return res;
  15454. } /* END test_wc_Rc2SetKey */
  15455. /*
  15456. * Testing function for wc_Rc2EcbEncrypt().
  15457. */
  15458. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15459. {
  15460. int res = TEST_SKIPPED;
  15461. #ifdef WC_RC2
  15462. Rc2 rc2;
  15463. int ret = 0;
  15464. int effectiveKeyBits = 63;
  15465. byte cipher[RC2_BLOCK_SIZE];
  15466. byte plain[RC2_BLOCK_SIZE];
  15467. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15468. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15469. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15470. XMEMSET(cipher, 0, sizeof(cipher));
  15471. XMEMSET(plain, 0, sizeof(plain));
  15472. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15473. NULL, effectiveKeyBits);
  15474. if (ret == 0) {
  15475. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  15476. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  15477. ret = WOLFSSL_FATAL_ERROR;
  15478. }
  15479. if (ret == 0) {
  15480. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  15481. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  15482. ret = WOLFSSL_FATAL_ERROR;
  15483. }
  15484. }
  15485. }
  15486. /* Rc2EcbEncrypt bad arguments */
  15487. if (ret == 0) {
  15488. /* null Rc2 struct */
  15489. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  15490. if (ret == BAD_FUNC_ARG) {
  15491. ret = 0;
  15492. }
  15493. }
  15494. if (ret == 0) {
  15495. /* null out buffer */
  15496. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  15497. if (ret == BAD_FUNC_ARG) {
  15498. ret = 0;
  15499. }
  15500. }
  15501. if (ret == 0) {
  15502. /* null input buffer */
  15503. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  15504. if (ret == BAD_FUNC_ARG) {
  15505. ret = 0;
  15506. }
  15507. }
  15508. if (ret == 0) {
  15509. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15510. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  15511. if (ret == BUFFER_E) {
  15512. ret = 0;
  15513. }
  15514. }
  15515. /* Rc2EcbDecrypt bad arguments */
  15516. if (ret == 0) {
  15517. /* null Rc2 struct */
  15518. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  15519. if (ret == BAD_FUNC_ARG) {
  15520. ret = 0;
  15521. }
  15522. }
  15523. if (ret == 0) {
  15524. /* null out buffer */
  15525. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  15526. if (ret == BAD_FUNC_ARG) {
  15527. ret = 0;
  15528. }
  15529. }
  15530. if (ret == 0) {
  15531. /* null input buffer */
  15532. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  15533. if (ret == BAD_FUNC_ARG) {
  15534. ret = 0;
  15535. }
  15536. }
  15537. if (ret == 0) {
  15538. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15539. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  15540. if (ret == BUFFER_E) {
  15541. ret = 0;
  15542. }
  15543. }
  15544. res = TEST_RES_CHECK(ret == 0);
  15545. #endif
  15546. return res;
  15547. } /* END test_wc_Rc2SetKey */
  15548. /*
  15549. * Testing function for wc_Rc2CbcEncrypt().
  15550. */
  15551. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15552. {
  15553. int res = TEST_SKIPPED;
  15554. #ifdef WC_RC2
  15555. Rc2 rc2;
  15556. int ret = 0;
  15557. int effectiveKeyBits = 63;
  15558. byte cipher[RC2_BLOCK_SIZE*2];
  15559. byte plain[RC2_BLOCK_SIZE*2];
  15560. /* vector taken from test.c */
  15561. byte key[] = {
  15562. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15563. };
  15564. byte iv[] = {
  15565. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15566. };
  15567. byte input[] = {
  15568. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15569. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15570. };
  15571. byte output[] = {
  15572. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15573. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15574. };
  15575. XMEMSET(cipher, 0, sizeof(cipher));
  15576. XMEMSET(plain, 0, sizeof(plain));
  15577. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15578. iv, effectiveKeyBits);
  15579. if (ret == 0) {
  15580. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  15581. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  15582. ret = WOLFSSL_FATAL_ERROR;
  15583. }
  15584. else {
  15585. /* reset IV for decrypt */
  15586. ret = wc_Rc2SetIV(&rc2, iv);
  15587. }
  15588. if (ret == 0) {
  15589. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  15590. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  15591. ret = WOLFSSL_FATAL_ERROR;
  15592. }
  15593. }
  15594. }
  15595. /* Rc2CbcEncrypt bad arguments */
  15596. if (ret == 0) {
  15597. /* null Rc2 struct */
  15598. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  15599. if (ret == BAD_FUNC_ARG) {
  15600. ret = 0;
  15601. }
  15602. }
  15603. if (ret == 0) {
  15604. /* null out buffer */
  15605. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  15606. if (ret == BAD_FUNC_ARG) {
  15607. ret = 0;
  15608. }
  15609. }
  15610. if (ret == 0) {
  15611. /* null input buffer */
  15612. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  15613. if (ret == BAD_FUNC_ARG) {
  15614. ret = 0;
  15615. }
  15616. }
  15617. /* Rc2CbcDecrypt bad arguments */
  15618. if (ret == 0) {
  15619. /* in size is 0 */
  15620. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  15621. if (ret != 0) {
  15622. ret = WOLFSSL_FATAL_ERROR;
  15623. }
  15624. }
  15625. if (ret == 0) {
  15626. /* null Rc2 struct */
  15627. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  15628. if (ret == BAD_FUNC_ARG) {
  15629. ret = 0;
  15630. }
  15631. }
  15632. if (ret == 0) {
  15633. /* null out buffer */
  15634. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  15635. if (ret == BAD_FUNC_ARG) {
  15636. ret = 0;
  15637. }
  15638. }
  15639. if (ret == 0) {
  15640. /* null input buffer */
  15641. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  15642. if (ret == BAD_FUNC_ARG) {
  15643. ret = 0;
  15644. }
  15645. }
  15646. res = TEST_RES_CHECK(ret == 0);
  15647. #endif
  15648. return res;
  15649. } /* END test_wc_Rc2SetKey */
  15650. /*
  15651. * Testing function for wc_AesSetIV
  15652. */
  15653. static int test_wc_AesSetIV(void)
  15654. {
  15655. int res = TEST_SKIPPED;
  15656. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15657. Aes aes;
  15658. int ret = 0;
  15659. byte key16[] =
  15660. {
  15661. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15662. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15663. };
  15664. byte iv1[] = "1234567890abcdef";
  15665. byte iv2[] = "0987654321fedcba";
  15666. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15667. if (ret != 0)
  15668. return ret;
  15669. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15670. iv1, AES_ENCRYPTION);
  15671. if (ret == 0) {
  15672. ret = wc_AesSetIV(&aes, iv2);
  15673. }
  15674. /* Test bad args. */
  15675. if (ret == 0) {
  15676. ret = wc_AesSetIV(NULL, iv1);
  15677. if (ret == BAD_FUNC_ARG) {
  15678. /* NULL iv should return 0. */
  15679. ret = wc_AesSetIV(&aes, NULL);
  15680. }
  15681. else {
  15682. ret = WOLFSSL_FATAL_ERROR;
  15683. }
  15684. }
  15685. wc_AesFree(&aes);
  15686. res = TEST_RES_CHECK(ret == 0);
  15687. #endif
  15688. return res;
  15689. } /* test_wc_AesSetIV */
  15690. /*
  15691. * Testing function for wc_AesSetKey().
  15692. */
  15693. static int test_wc_AesSetKey(void)
  15694. {
  15695. int res = TEST_SKIPPED;
  15696. #ifndef NO_AES
  15697. Aes aes;
  15698. int ret = 0;
  15699. byte key16[] =
  15700. {
  15701. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15702. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15703. };
  15704. #ifdef WOLFSSL_AES_192
  15705. byte key24[] =
  15706. {
  15707. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15708. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15709. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15710. };
  15711. #endif
  15712. #ifdef WOLFSSL_AES_256
  15713. byte key32[] =
  15714. {
  15715. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15716. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15717. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15718. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15719. };
  15720. #endif
  15721. byte badKey16[] =
  15722. {
  15723. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15724. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15725. };
  15726. byte iv[] = "1234567890abcdef";
  15727. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15728. if (ret != 0)
  15729. return ret;
  15730. #ifdef WOLFSSL_AES_128
  15731. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15732. iv, AES_ENCRYPTION);
  15733. #endif
  15734. #ifdef WOLFSSL_AES_192
  15735. if (ret == 0) {
  15736. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  15737. iv, AES_ENCRYPTION);
  15738. }
  15739. #endif
  15740. #ifdef WOLFSSL_AES_256
  15741. if (ret == 0) {
  15742. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  15743. iv, AES_ENCRYPTION);
  15744. }
  15745. #endif
  15746. /* Pass in bad args. */
  15747. if (ret == 0) {
  15748. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  15749. iv, AES_ENCRYPTION);
  15750. if (ret == BAD_FUNC_ARG) {
  15751. ret = wc_AesSetKey(&aes, badKey16,
  15752. (word32) sizeof(badKey16) / sizeof(byte),
  15753. iv, AES_ENCRYPTION);
  15754. }
  15755. if (ret == BAD_FUNC_ARG) {
  15756. ret = 0;
  15757. }
  15758. else {
  15759. ret = WOLFSSL_FATAL_ERROR;
  15760. }
  15761. }
  15762. wc_AesFree(&aes);
  15763. res = TEST_RES_CHECK(ret == 0);
  15764. #endif
  15765. return res;
  15766. } /* END test_wc_AesSetKey */
  15767. /*
  15768. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15769. * and wc_AesCbcDecryptWithKey()
  15770. */
  15771. static int test_wc_AesCbcEncryptDecrypt(void)
  15772. {
  15773. int res = TEST_SKIPPED;
  15774. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15775. defined(WOLFSSL_AES_256)
  15776. Aes aes;
  15777. int ret = 0;
  15778. byte key32[] =
  15779. {
  15780. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15781. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15782. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15783. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15784. };
  15785. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  15786. {
  15787. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15788. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15789. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  15790. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  15791. };
  15792. byte iv[] = "1234567890abcdef";
  15793. byte enc[sizeof(vector)];
  15794. byte dec[sizeof(vector)];
  15795. int cbcE = WOLFSSL_FATAL_ERROR;
  15796. int cbcD = WOLFSSL_FATAL_ERROR;
  15797. int cbcDWK = WOLFSSL_FATAL_ERROR;
  15798. byte dec2[sizeof(vector)];
  15799. /* Init stack variables. */
  15800. XMEMSET(enc, 0, sizeof(enc));
  15801. XMEMSET(dec, 0, sizeof(vector));
  15802. XMEMSET(dec2, 0, sizeof(vector));
  15803. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15804. if (ret != 0)
  15805. return ret;
  15806. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  15807. if (ret == 0) {
  15808. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  15809. if (ret == 0) {
  15810. /* Re init for decrypt and set flag. */
  15811. cbcE = 0;
  15812. wc_AesFree(&aes);
  15813. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  15814. iv, AES_DECRYPTION);
  15815. }
  15816. if (ret == 0) {
  15817. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  15818. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  15819. ret = WOLFSSL_FATAL_ERROR;
  15820. }
  15821. else {
  15822. /* Set flag. */
  15823. cbcD = 0;
  15824. }
  15825. }
  15826. }
  15827. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  15828. if (ret == 0 || cbcE == 0) {
  15829. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15830. key32, sizeof(key32)/sizeof(byte), iv);
  15831. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  15832. cbcDWK = 0;
  15833. }
  15834. }
  15835. /* Pass in bad args */
  15836. if (cbcE == 0) {
  15837. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  15838. if (cbcE == BAD_FUNC_ARG) {
  15839. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  15840. }
  15841. if (cbcE == BAD_FUNC_ARG) {
  15842. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  15843. }
  15844. if (cbcE == BAD_FUNC_ARG) {
  15845. cbcE = 0;
  15846. }
  15847. else {
  15848. cbcE = WOLFSSL_FATAL_ERROR;
  15849. }
  15850. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15851. if (cbcE == 0) {
  15852. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  15853. }
  15854. if (cbcE == BAD_LENGTH_E) {
  15855. cbcE = 0;
  15856. }
  15857. else {
  15858. cbcE = WOLFSSL_FATAL_ERROR;
  15859. }
  15860. #endif
  15861. }
  15862. if (cbcE == 0) {
  15863. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15864. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15865. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15866. "mode (v2), skip test");
  15867. #else
  15868. /* Test passing in size of 0 */
  15869. XMEMSET(enc, 0, sizeof(enc));
  15870. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  15871. if (cbcE == 0) {
  15872. /* Check enc was not modified */
  15873. int i;
  15874. for (i = 0; i < (int)sizeof(enc); i++)
  15875. cbcE |= enc[i];
  15876. }
  15877. #endif
  15878. }
  15879. if (cbcE != 0) {
  15880. wc_AesFree(&aes);
  15881. return TEST_FAIL;
  15882. }
  15883. if (cbcD == 0) {
  15884. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  15885. if (cbcD == BAD_FUNC_ARG) {
  15886. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  15887. }
  15888. if (cbcD == BAD_FUNC_ARG) {
  15889. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  15890. }
  15891. if (cbcD == BAD_FUNC_ARG) {
  15892. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  15893. }
  15894. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15895. if (cbcD == BAD_LENGTH_E) {
  15896. cbcD = 0;
  15897. }
  15898. else {
  15899. cbcD = WOLFSSL_FATAL_ERROR;
  15900. }
  15901. #else
  15902. if (cbcD == BAD_FUNC_ARG) {
  15903. cbcD = 0;
  15904. }
  15905. else {
  15906. cbcD = WOLFSSL_FATAL_ERROR;
  15907. }
  15908. #endif
  15909. }
  15910. if (cbcD == 0) {
  15911. /* Test passing in size of 0 */
  15912. XMEMSET(dec, 0, sizeof(dec));
  15913. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  15914. if (cbcD == 0) {
  15915. /* Check dec was not modified */
  15916. int i;
  15917. for (i = 0; i < (int)sizeof(dec); i++)
  15918. cbcD |= dec[i];
  15919. }
  15920. }
  15921. if (cbcD != 0) {
  15922. wc_AesFree(&aes);
  15923. return TEST_FAIL;
  15924. }
  15925. if (cbcDWK == 0) {
  15926. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15927. key32, sizeof(key32)/sizeof(byte), iv);
  15928. if (cbcDWK == BAD_FUNC_ARG) {
  15929. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15930. key32, sizeof(key32)/sizeof(byte), iv);
  15931. }
  15932. if (cbcDWK == BAD_FUNC_ARG) {
  15933. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15934. NULL, sizeof(key32)/sizeof(byte), iv);
  15935. }
  15936. if (cbcDWK == BAD_FUNC_ARG) {
  15937. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15938. key32, sizeof(key32)/sizeof(byte), NULL);
  15939. }
  15940. if (cbcDWK == BAD_FUNC_ARG) {
  15941. cbcDWK = 0;
  15942. }
  15943. else {
  15944. cbcDWK = WOLFSSL_FATAL_ERROR;
  15945. }
  15946. }
  15947. wc_AesFree(&aes);
  15948. res = TEST_RES_CHECK(cbcDWK == 0);
  15949. #endif
  15950. return res;
  15951. } /* END test_wc_AesCbcEncryptDecrypt */
  15952. /*
  15953. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15954. */
  15955. static int test_wc_AesCtrEncryptDecrypt(void)
  15956. {
  15957. int res = TEST_SKIPPED;
  15958. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15959. Aes aesEnc, aesDec;
  15960. int ret = 0;
  15961. byte key32[] =
  15962. {
  15963. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15964. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15965. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15966. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15967. };
  15968. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15969. {
  15970. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15971. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15972. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15973. };
  15974. byte iv[] = "1234567890abcdef";
  15975. byte enc[AES_BLOCK_SIZE * 2];
  15976. byte dec[AES_BLOCK_SIZE * 2];
  15977. /* Init stack variables. */
  15978. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15979. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15980. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  15981. if (ret != 0)
  15982. return ret;
  15983. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  15984. if (ret != 0) {
  15985. wc_AesFree(&aesEnc);
  15986. return ret;
  15987. }
  15988. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  15989. iv, AES_ENCRYPTION);
  15990. if (ret == 0) {
  15991. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15992. sizeof(vector)/sizeof(byte));
  15993. if (ret == 0) {
  15994. /* Decrypt with wc_AesCtrEncrypt() */
  15995. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  15996. iv, AES_ENCRYPTION);
  15997. }
  15998. if (ret == 0) {
  15999. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  16000. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  16001. ret = WOLFSSL_FATAL_ERROR;
  16002. }
  16003. }
  16004. }
  16005. /* Test bad args. */
  16006. if (ret == 0) {
  16007. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  16008. if (ret == BAD_FUNC_ARG) {
  16009. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  16010. }
  16011. if (ret == BAD_FUNC_ARG) {
  16012. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  16013. }
  16014. if (ret == BAD_FUNC_ARG) {
  16015. ret = 0;
  16016. }
  16017. else {
  16018. ret = WOLFSSL_FATAL_ERROR;
  16019. }
  16020. }
  16021. wc_AesFree(&aesEnc);
  16022. wc_AesFree(&aesDec);
  16023. res = TEST_RES_CHECK(ret == 0);
  16024. #endif
  16025. return res;
  16026. } /* END test_wc_AesCtrEncryptDecrypt */
  16027. /*
  16028. * test function for wc_AesGcmSetKey()
  16029. */
  16030. static int test_wc_AesGcmSetKey(void)
  16031. {
  16032. int res = TEST_SKIPPED;
  16033. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16034. Aes aes;
  16035. int ret = 0;
  16036. #ifdef WOLFSSL_AES_128
  16037. byte key16[] =
  16038. {
  16039. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16040. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16041. };
  16042. #endif
  16043. #ifdef WOLFSSL_AES_192
  16044. byte key24[] =
  16045. {
  16046. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16047. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16048. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16049. };
  16050. #endif
  16051. #ifdef WOLFSSL_AES_256
  16052. byte key32[] =
  16053. {
  16054. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16055. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16056. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16057. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16058. };
  16059. #endif
  16060. byte badKey16[] =
  16061. {
  16062. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16063. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16064. };
  16065. byte badKey24[] =
  16066. {
  16067. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16068. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16069. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16070. };
  16071. byte badKey32[] =
  16072. {
  16073. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16074. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16075. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16076. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16077. };
  16078. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16079. if (ret != 0)
  16080. return ret;
  16081. #ifdef WOLFSSL_AES_128
  16082. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  16083. #endif
  16084. #ifdef WOLFSSL_AES_192
  16085. if (ret == 0) {
  16086. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  16087. }
  16088. #endif
  16089. #ifdef WOLFSSL_AES_256
  16090. if (ret == 0) {
  16091. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  16092. }
  16093. #endif
  16094. /* Pass in bad args. */
  16095. if (ret == 0) {
  16096. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  16097. if (ret == BAD_FUNC_ARG) {
  16098. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  16099. }
  16100. if (ret == BAD_FUNC_ARG) {
  16101. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  16102. }
  16103. if (ret == BAD_FUNC_ARG) {
  16104. ret = 0;
  16105. }
  16106. else {
  16107. ret = WOLFSSL_FATAL_ERROR;
  16108. }
  16109. }
  16110. wc_AesFree(&aes);
  16111. res = TEST_RES_CHECK(ret == 0);
  16112. #endif
  16113. return res;
  16114. } /* END test_wc_AesGcmSetKey */
  16115. /*
  16116. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16117. */
  16118. static int test_wc_AesGcmEncryptDecrypt(void)
  16119. {
  16120. int res = TEST_SKIPPED;
  16121. /* WOLFSSL_AFALG requires 12 byte IV */
  16122. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16123. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16124. Aes aes;
  16125. byte key32[] =
  16126. {
  16127. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16128. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16129. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16130. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16131. };
  16132. byte vector[] = /* Now is the time for all w/o trailing 0 */
  16133. {
  16134. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16135. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16136. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16137. };
  16138. const byte a[] =
  16139. {
  16140. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16141. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16142. 0xab, 0xad, 0xda, 0xd2
  16143. };
  16144. byte iv[] = "1234567890a";
  16145. byte longIV[] = "1234567890abcdefghij";
  16146. byte enc[sizeof(vector)];
  16147. byte resultT[AES_BLOCK_SIZE];
  16148. byte dec[sizeof(vector)];
  16149. int gcmD = WOLFSSL_FATAL_ERROR;
  16150. int gcmE = WOLFSSL_FATAL_ERROR;
  16151. int ret = 0;
  16152. /* Init stack variables. */
  16153. XMEMSET(enc, 0, sizeof(vector));
  16154. XMEMSET(dec, 0, sizeof(vector));
  16155. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16156. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16157. if (ret != 0)
  16158. return ret;
  16159. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  16160. if (ret == 0) {
  16161. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  16162. iv, sizeof(iv)/sizeof(byte), resultT,
  16163. sizeof(resultT), a, sizeof(a));
  16164. }
  16165. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  16166. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  16167. iv, sizeof(iv)/sizeof(byte), resultT,
  16168. sizeof(resultT), a, sizeof(a));
  16169. if (gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  16170. gcmD = WOLFSSL_FATAL_ERROR;
  16171. }
  16172. }
  16173. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16174. if (gcmE == 0) {
  16175. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  16176. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  16177. a, sizeof(a));
  16178. if (gcmE == BAD_FUNC_ARG) {
  16179. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  16180. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  16181. resultT, sizeof(resultT) + 1, a, sizeof(a));
  16182. }
  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) - 5, a, sizeof(a));
  16187. }
  16188. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16189. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16190. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16191. /* FIPS does not check the lower bound of ivSz */
  16192. #else
  16193. if (gcmE == BAD_FUNC_ARG) {
  16194. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  16195. sizeof(vector), iv, 0,
  16196. resultT, sizeof(resultT), a, sizeof(a));
  16197. }
  16198. #endif
  16199. if (gcmE == BAD_FUNC_ARG) {
  16200. gcmE = 0;
  16201. }
  16202. else {
  16203. gcmE = WOLFSSL_FATAL_ERROR;
  16204. }
  16205. }
  16206. /* This case is now considered good. Long IVs are now allowed.
  16207. * Except for the original FIPS release, it still has an upper
  16208. * bound on the IV length. */
  16209. #if (!defined(HAVE_FIPS) || \
  16210. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16211. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16212. if (gcmE == 0) {
  16213. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16214. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  16215. a, sizeof(a));
  16216. }
  16217. #else
  16218. (void)longIV;
  16219. #endif /* Old FIPS */
  16220. /* END wc_AesGcmEncrypt */
  16221. if (gcmE != 0) {
  16222. wc_AesFree(&aes);
  16223. return TEST_FAIL;
  16224. }
  16225. #ifdef HAVE_AES_DECRYPT
  16226. if (gcmD == 0) {
  16227. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  16228. iv, sizeof(iv)/sizeof(byte), resultT,
  16229. sizeof(resultT), a, sizeof(a));
  16230. if (gcmD == BAD_FUNC_ARG) {
  16231. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  16232. iv, sizeof(iv)/sizeof(byte), resultT,
  16233. sizeof(resultT), a, sizeof(a));
  16234. }
  16235. if (gcmD == BAD_FUNC_ARG) {
  16236. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, 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, enc, sizeof(enc)/sizeof(byte),
  16242. NULL, 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. iv, sizeof(iv)/sizeof(byte), NULL,
  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), resultT,
  16253. sizeof(resultT) + 1, a, sizeof(a));
  16254. }
  16255. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16256. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16257. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16258. /* FIPS does not check the lower bound of ivSz */
  16259. #else
  16260. if (gcmD == BAD_FUNC_ARG) {
  16261. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16262. iv, 0, resultT,
  16263. sizeof(resultT), a, sizeof(a));
  16264. }
  16265. #endif
  16266. if (gcmD == BAD_FUNC_ARG) {
  16267. gcmD = 0;
  16268. }
  16269. else {
  16270. gcmD = WOLFSSL_FATAL_ERROR;
  16271. }
  16272. res = TEST_RES_CHECK(gcmD == 0);
  16273. } /* END wc_AesGcmDecrypt */
  16274. #endif /* HAVE_AES_DECRYPT */
  16275. wc_AesFree(&aes);
  16276. #endif
  16277. return res;
  16278. } /* END test_wc_AesGcmEncryptDecrypt */
  16279. /*
  16280. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16281. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16282. */
  16283. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16284. {
  16285. int ret = TEST_SKIPPED;
  16286. #if (!defined(HAVE_FIPS) || \
  16287. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16288. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16289. const byte key[] = {
  16290. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16291. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16292. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16293. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16294. };
  16295. const byte in[] = {
  16296. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16297. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16298. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16299. };
  16300. const byte aad[] = {
  16301. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16302. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16303. 0xab, 0xad, 0xda, 0xd2
  16304. };
  16305. Aes aesEnc, aesDec;
  16306. byte iv[] = "1234567890abcdefghij";
  16307. byte out[sizeof(in)];
  16308. byte plain[sizeof(in)];
  16309. byte tag[AES_BLOCK_SIZE];
  16310. XMEMSET(out, 0, sizeof(out));
  16311. XMEMSET(plain, 0, sizeof(plain));
  16312. XMEMSET(tag, 0, sizeof(tag));
  16313. /* Perform one-shot encryption using long IV */
  16314. AssertIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16315. AssertIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16316. AssertIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16317. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16318. /* Perform streaming decryption using long IV */
  16319. AssertIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16320. AssertIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16321. AssertIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16322. sizeof(aad)), 0);
  16323. AssertIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16324. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16325. /* Free resources */
  16326. wc_AesFree(&aesEnc);
  16327. wc_AesFree(&aesDec);
  16328. ret = TEST_SUCCESS;
  16329. #endif
  16330. return ret;
  16331. } /* END wc_AesGcmMixedEncDecLongIV */
  16332. /*
  16333. * unit test for wc_GmacSetKey()
  16334. */
  16335. static int test_wc_GmacSetKey(void)
  16336. {
  16337. int res = TEST_SKIPPED;
  16338. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16339. Gmac gmac;
  16340. byte key16[] =
  16341. {
  16342. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16343. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16344. };
  16345. #ifdef WOLFSSL_AES_192
  16346. byte key24[] =
  16347. {
  16348. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16349. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16350. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16351. };
  16352. #endif
  16353. #ifdef WOLFSSL_AES_256
  16354. byte key32[] =
  16355. {
  16356. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16357. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16358. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16359. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16360. };
  16361. #endif
  16362. byte badKey16[] =
  16363. {
  16364. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16365. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16366. };
  16367. byte badKey24[] =
  16368. {
  16369. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16370. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16371. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16372. };
  16373. byte badKey32[] =
  16374. {
  16375. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16376. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16377. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16378. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16379. };
  16380. int ret = 0;
  16381. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  16382. if (ret != 0)
  16383. return ret;
  16384. #ifdef WOLFSSL_AES_128
  16385. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  16386. #endif
  16387. #ifdef WOLFSSL_AES_192
  16388. if (ret == 0) {
  16389. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  16390. }
  16391. #endif
  16392. #ifdef WOLFSSL_AES_256
  16393. if (ret == 0) {
  16394. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  16395. }
  16396. #endif
  16397. /* Pass in bad args. */
  16398. if (ret == 0) {
  16399. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  16400. if (ret == BAD_FUNC_ARG) {
  16401. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  16402. }
  16403. if (ret == BAD_FUNC_ARG) {
  16404. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  16405. }
  16406. if (ret == BAD_FUNC_ARG) {
  16407. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  16408. }
  16409. if (ret == BAD_FUNC_ARG) {
  16410. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  16411. }
  16412. if (ret == BAD_FUNC_ARG) {
  16413. ret = 0;
  16414. }
  16415. else {
  16416. ret = WOLFSSL_FATAL_ERROR;
  16417. }
  16418. }
  16419. wc_AesFree(&gmac.aes);
  16420. res = TEST_RES_CHECK(ret == 0);
  16421. #endif
  16422. return res;
  16423. } /* END test_wc_GmacSetKey */
  16424. /*
  16425. * unit test for wc_GmacUpdate
  16426. */
  16427. static int test_wc_GmacUpdate(void)
  16428. {
  16429. int res = TEST_SKIPPED;
  16430. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16431. Gmac gmac;
  16432. #ifdef WOLFSSL_AES_128
  16433. const byte key16[] =
  16434. {
  16435. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16436. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16437. };
  16438. #endif
  16439. #ifdef WOLFSSL_AES_192
  16440. byte key24[] =
  16441. {
  16442. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16443. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16444. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16445. };
  16446. #endif
  16447. #ifdef WOLFSSL_AES_256
  16448. byte key32[] =
  16449. {
  16450. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16451. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16452. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16453. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16454. };
  16455. #endif
  16456. #ifdef WOLFSSL_AES_128
  16457. const byte authIn[] =
  16458. {
  16459. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16460. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16461. };
  16462. #endif
  16463. #ifdef WOLFSSL_AES_192
  16464. const byte authIn2[] =
  16465. {
  16466. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16467. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16468. };
  16469. #endif
  16470. const byte authIn3[] =
  16471. {
  16472. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16473. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16474. };
  16475. #ifdef WOLFSSL_AES_128
  16476. const byte tag1[] = /* Known. */
  16477. {
  16478. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16479. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16480. };
  16481. #endif
  16482. #ifdef WOLFSSL_AES_192
  16483. const byte tag2[] = /* Known */
  16484. {
  16485. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16486. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16487. };
  16488. #endif
  16489. const byte tag3[] = /* Known */
  16490. {
  16491. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16492. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16493. };
  16494. #ifdef WOLFSSL_AES_128
  16495. const byte iv[] =
  16496. {
  16497. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16498. 0xe2, 0x8c, 0x8f, 0x16
  16499. };
  16500. #endif
  16501. #ifdef WOLFSSL_AES_192
  16502. const byte iv2[] =
  16503. {
  16504. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16505. 0x7e, 0x1a, 0x6f, 0xbc
  16506. };
  16507. #endif
  16508. const byte iv3[] =
  16509. {
  16510. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16511. 0xc3, 0xfb, 0x6c, 0x8a
  16512. };
  16513. byte tagOut[16];
  16514. byte tagOut2[24];
  16515. byte tagOut3[32];
  16516. int ret = 0;
  16517. /* Init stack variables. */
  16518. XMEMSET(tagOut, 0, sizeof(tagOut));
  16519. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16520. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16521. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  16522. if (ret != 0)
  16523. return ret;
  16524. #ifdef WOLFSSL_AES_128
  16525. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  16526. if (ret == 0) {
  16527. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16528. tagOut, sizeof(tag1));
  16529. if (ret == 0) {
  16530. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  16531. }
  16532. wc_AesFree(&gmac.aes);
  16533. }
  16534. #endif
  16535. #ifdef WOLFSSL_AES_192
  16536. if (ret == 0) {
  16537. XMEMSET(&gmac, 0, sizeof(Gmac));
  16538. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  16539. }
  16540. if (ret == 0) {
  16541. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  16542. sizeof(authIn2), tagOut2, sizeof(tag2));
  16543. }
  16544. if (ret == 0) {
  16545. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  16546. wc_AesFree(&gmac.aes);
  16547. }
  16548. #endif
  16549. #ifdef WOLFSSL_AES_256
  16550. if (ret == 0) {
  16551. XMEMSET(&gmac, 0, sizeof(Gmac));
  16552. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  16553. }
  16554. if (ret == 0) {
  16555. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  16556. sizeof(authIn3), tagOut3, sizeof(tag3));
  16557. }
  16558. if (ret == 0) {
  16559. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  16560. }
  16561. #endif
  16562. /*Pass bad args. */
  16563. if (ret == 0) {
  16564. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  16565. sizeof(authIn3), tagOut3, sizeof(tag3));
  16566. if (ret == BAD_FUNC_ARG) {
  16567. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  16568. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  16569. }
  16570. if (ret == BAD_FUNC_ARG) {
  16571. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  16572. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  16573. }
  16574. if (ret == BAD_FUNC_ARG) {
  16575. ret = 0;
  16576. }
  16577. else {
  16578. ret = WOLFSSL_FATAL_ERROR;
  16579. }
  16580. }
  16581. wc_AesFree(&gmac.aes);
  16582. res = TEST_RES_CHECK(ret == 0);
  16583. #endif
  16584. return res;
  16585. } /* END test_wc_GmacUpdate */
  16586. /*
  16587. * testing wc_CamelliaSetKey
  16588. */
  16589. static int test_wc_CamelliaSetKey(void)
  16590. {
  16591. int res = TEST_SKIPPED;
  16592. #ifdef HAVE_CAMELLIA
  16593. Camellia camellia;
  16594. /*128-bit key*/
  16595. static const byte key16[] =
  16596. {
  16597. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16598. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16599. };
  16600. /* 192-bit key */
  16601. static const byte key24[] =
  16602. {
  16603. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16604. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16605. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16606. };
  16607. /* 256-bit key */
  16608. static const byte key32[] =
  16609. {
  16610. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16611. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16612. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16613. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16614. };
  16615. static const byte iv[] =
  16616. {
  16617. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16618. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16619. };
  16620. int ret = 0;
  16621. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  16622. if (ret == 0) {
  16623. ret = wc_CamelliaSetKey(&camellia, key16,
  16624. (word32)sizeof(key16), NULL);
  16625. if (ret == 0) {
  16626. ret = wc_CamelliaSetKey(&camellia, key24,
  16627. (word32)sizeof(key24), iv);
  16628. }
  16629. if (ret == 0) {
  16630. ret = wc_CamelliaSetKey(&camellia, key24,
  16631. (word32)sizeof(key24), NULL);
  16632. }
  16633. if (ret == 0) {
  16634. ret = wc_CamelliaSetKey(&camellia, key32,
  16635. (word32)sizeof(key32), iv);
  16636. }
  16637. if (ret == 0) {
  16638. ret = wc_CamelliaSetKey(&camellia, key32,
  16639. (word32)sizeof(key32), NULL);
  16640. }
  16641. }
  16642. /* Bad args. */
  16643. if (ret == 0) {
  16644. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  16645. if (ret != BAD_FUNC_ARG) {
  16646. ret = WOLFSSL_FATAL_ERROR;
  16647. }
  16648. else {
  16649. ret = 0;
  16650. }
  16651. } /* END bad args. */
  16652. res = TEST_RES_CHECK(ret == 0);
  16653. #endif
  16654. return res;
  16655. } /* END test_wc_CammeliaSetKey */
  16656. /*
  16657. * Testing wc_CamelliaSetIV()
  16658. */
  16659. static int test_wc_CamelliaSetIV(void)
  16660. {
  16661. int res = TEST_SKIPPED;
  16662. #ifdef HAVE_CAMELLIA
  16663. Camellia camellia;
  16664. static const byte iv[] =
  16665. {
  16666. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16667. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16668. };
  16669. int ret = 0;
  16670. ret = wc_CamelliaSetIV(&camellia, iv);
  16671. if (ret == 0) {
  16672. ret = wc_CamelliaSetIV(&camellia, NULL);
  16673. }
  16674. /* Bad args. */
  16675. if (ret == 0) {
  16676. ret = wc_CamelliaSetIV(NULL, NULL);
  16677. if (ret != BAD_FUNC_ARG) {
  16678. ret = WOLFSSL_FATAL_ERROR;
  16679. }
  16680. else {
  16681. ret = 0;
  16682. }
  16683. }
  16684. res = TEST_RES_CHECK(ret == 0);
  16685. #endif
  16686. return res;
  16687. } /*END test_wc_CamelliaSetIV*/
  16688. /*
  16689. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16690. */
  16691. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16692. {
  16693. int res = TEST_SKIPPED;
  16694. #ifdef HAVE_CAMELLIA
  16695. Camellia camellia;
  16696. static const byte key24[] =
  16697. {
  16698. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16699. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16700. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16701. };
  16702. static const byte iv[] =
  16703. {
  16704. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16705. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16706. };
  16707. static const byte plainT[] =
  16708. {
  16709. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16710. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16711. };
  16712. byte enc[sizeof(plainT)];
  16713. byte dec[sizeof(enc)];
  16714. int camE = WOLFSSL_FATAL_ERROR;
  16715. int camD = WOLFSSL_FATAL_ERROR;
  16716. int ret = 0;
  16717. /*Init stack variables.*/
  16718. XMEMSET(enc, 0, 16);
  16719. XMEMSET(enc, 0, 16);
  16720. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  16721. if (ret == 0) {
  16722. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  16723. if (ret == 0) {
  16724. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  16725. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16726. ret = WOLFSSL_FATAL_ERROR;
  16727. }
  16728. }
  16729. }
  16730. /* Pass bad args. */
  16731. if (ret == 0) {
  16732. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  16733. if (camE == BAD_FUNC_ARG) {
  16734. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  16735. }
  16736. if (camE == BAD_FUNC_ARG) {
  16737. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  16738. }
  16739. if (camE == BAD_FUNC_ARG) {
  16740. camE = 0;
  16741. }
  16742. else {
  16743. camE = WOLFSSL_FATAL_ERROR;
  16744. }
  16745. }
  16746. if (camE != 0) {
  16747. return TEST_FAIL;
  16748. }
  16749. if (ret == 0) {
  16750. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  16751. if (camD == BAD_FUNC_ARG) {
  16752. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  16753. }
  16754. if (camD == BAD_FUNC_ARG) {
  16755. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  16756. }
  16757. if (camD == BAD_FUNC_ARG) {
  16758. camD = 0;
  16759. }
  16760. else {
  16761. camD = WOLFSSL_FATAL_ERROR;
  16762. }
  16763. }
  16764. res = TEST_RES_CHECK(camD == 0);
  16765. #endif
  16766. return res;
  16767. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16768. /*
  16769. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16770. */
  16771. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16772. {
  16773. int res = TEST_SKIPPED;
  16774. #ifdef HAVE_CAMELLIA
  16775. Camellia camellia;
  16776. static const byte key24[] =
  16777. {
  16778. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16779. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16780. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16781. };
  16782. static const byte plainT[] =
  16783. {
  16784. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16785. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16786. };
  16787. byte enc[CAMELLIA_BLOCK_SIZE];
  16788. byte dec[CAMELLIA_BLOCK_SIZE];
  16789. int camCbcE = WOLFSSL_FATAL_ERROR;
  16790. int camCbcD = WOLFSSL_FATAL_ERROR;
  16791. int ret = 0;
  16792. /* Init stack variables. */
  16793. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16794. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16795. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16796. if (ret == 0) {
  16797. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16798. if (ret != 0) {
  16799. ret = WOLFSSL_FATAL_ERROR;
  16800. }
  16801. }
  16802. if (ret == 0) {
  16803. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16804. if (ret == 0) {
  16805. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  16806. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16807. ret = WOLFSSL_FATAL_ERROR;
  16808. }
  16809. }
  16810. }
  16811. /* Pass in bad args. */
  16812. if (ret == 0) {
  16813. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16814. if (camCbcE == BAD_FUNC_ARG) {
  16815. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16816. CAMELLIA_BLOCK_SIZE);
  16817. }
  16818. if (camCbcE == BAD_FUNC_ARG) {
  16819. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16820. CAMELLIA_BLOCK_SIZE);
  16821. }
  16822. if (camCbcE == BAD_FUNC_ARG) {
  16823. camCbcE = 0;
  16824. }
  16825. else {
  16826. camCbcE = WOLFSSL_FATAL_ERROR;
  16827. }
  16828. }
  16829. if (camCbcE != 0) {
  16830. return TEST_FAIL;
  16831. }
  16832. if (ret == 0) {
  16833. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  16834. if (camCbcD == BAD_FUNC_ARG) {
  16835. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16836. CAMELLIA_BLOCK_SIZE);
  16837. }
  16838. if (camCbcD == BAD_FUNC_ARG) {
  16839. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16840. CAMELLIA_BLOCK_SIZE);
  16841. }
  16842. if (camCbcD == BAD_FUNC_ARG) {
  16843. camCbcD = 0;
  16844. }
  16845. else {
  16846. camCbcD = WOLFSSL_FATAL_ERROR;
  16847. }
  16848. } /* END bad args. */
  16849. res = TEST_RES_CHECK(camCbcD == 0);
  16850. #endif
  16851. return res;
  16852. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16853. /*
  16854. * Testing wc_Arc4SetKey()
  16855. */
  16856. static int test_wc_Arc4SetKey(void)
  16857. {
  16858. int res = TEST_SKIPPED;
  16859. #ifndef NO_RC4
  16860. Arc4 arc;
  16861. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16862. int keyLen = 8;
  16863. int ret = 0;
  16864. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  16865. /* Test bad args. */
  16866. if (ret == 0) {
  16867. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  16868. if (ret == BAD_FUNC_ARG)
  16869. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  16870. if (ret == BAD_FUNC_ARG)
  16871. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  16872. if (ret == BAD_FUNC_ARG)
  16873. ret = WOLFSSL_ERROR_NONE;
  16874. else
  16875. ret = WOLFSSL_FATAL_ERROR;
  16876. } /* END test bad args. */
  16877. res = TEST_RES_CHECK(ret == 0);
  16878. #endif
  16879. return res;
  16880. } /* END test_wc_Arc4SetKey */
  16881. /*
  16882. * Testing wc_Arc4Process for ENC/DEC.
  16883. */
  16884. static int test_wc_Arc4Process(void)
  16885. {
  16886. int res = TEST_SKIPPED;
  16887. #ifndef NO_RC4
  16888. Arc4 enc, dec;
  16889. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16890. int keyLen = 8;
  16891. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16892. byte cipher[8];
  16893. byte plain[8];
  16894. int ret;
  16895. /* Init stack variables */
  16896. XMEMSET(cipher, 0, sizeof(cipher));
  16897. XMEMSET(plain, 0, sizeof(plain));
  16898. /* Use for async. */
  16899. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  16900. if (ret == 0) {
  16901. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  16902. }
  16903. if (ret == 0) {
  16904. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  16905. }
  16906. if (ret == 0) {
  16907. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  16908. }
  16909. if (ret == 0) {
  16910. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  16911. }
  16912. if (ret == 0) {
  16913. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  16914. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  16915. ret = WOLFSSL_FATAL_ERROR;
  16916. }
  16917. else {
  16918. ret = 0;
  16919. }
  16920. }
  16921. /* Bad args. */
  16922. if (ret == 0) {
  16923. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  16924. if (ret == BAD_FUNC_ARG) {
  16925. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  16926. }
  16927. if (ret == BAD_FUNC_ARG) {
  16928. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  16929. }
  16930. if (ret == BAD_FUNC_ARG) {
  16931. ret = 0;
  16932. }
  16933. else {
  16934. ret = WOLFSSL_FATAL_ERROR;
  16935. }
  16936. }
  16937. wc_Arc4Free(&enc);
  16938. wc_Arc4Free(&dec);
  16939. res = TEST_RES_CHECK(ret == 0);
  16940. #endif
  16941. return res;
  16942. }/* END test_wc_Arc4Process */
  16943. /*
  16944. * Testing wc_Init RsaKey()
  16945. */
  16946. static int test_wc_InitRsaKey(void)
  16947. {
  16948. int res = TEST_SKIPPED;
  16949. #ifndef NO_RSA
  16950. RsaKey key;
  16951. int ret = 0;
  16952. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16953. /* Test bad args. */
  16954. if (ret == 0) {
  16955. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  16956. #ifndef HAVE_USER_RSA
  16957. if (ret == BAD_FUNC_ARG) {
  16958. ret = 0;
  16959. }
  16960. else {
  16961. #else
  16962. if (ret == USER_CRYPTO_ERROR) {
  16963. ret = 0;
  16964. }
  16965. else {
  16966. #endif
  16967. ret = WOLFSSL_FATAL_ERROR;
  16968. }
  16969. } /* end if */
  16970. if (wc_FreeRsaKey(&key) || ret != 0) {
  16971. ret = WOLFSSL_FATAL_ERROR;
  16972. }
  16973. res = TEST_RES_CHECK(ret == 0);
  16974. #endif
  16975. return res;
  16976. } /* END test_wc_InitRsaKey */
  16977. /*
  16978. * Testing wc_RsaPrivateKeyDecode()
  16979. */
  16980. static int test_wc_RsaPrivateKeyDecode(void)
  16981. {
  16982. int res = TEST_SKIPPED;
  16983. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16984. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16985. RsaKey key;
  16986. byte* tmp;
  16987. word32 idx = 0;
  16988. int bytes = 0;
  16989. int ret = 0;
  16990. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16991. if (tmp == NULL) {
  16992. ret = WOLFSSL_FATAL_ERROR;
  16993. }
  16994. if (ret == 0) {
  16995. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16996. }
  16997. if (ret == 0) {
  16998. #ifdef USE_CERT_BUFFERS_1024
  16999. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17000. bytes = sizeof_client_key_der_1024;
  17001. #else
  17002. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17003. bytes = sizeof_client_key_der_2048;
  17004. #endif /* Use cert buffers. */
  17005. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  17006. }
  17007. #ifndef HAVE_USER_RSA
  17008. /* Test bad args. */
  17009. if (ret == 0) {
  17010. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  17011. if (ret == BAD_FUNC_ARG) {
  17012. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  17013. }
  17014. if (ret == BAD_FUNC_ARG) {
  17015. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17016. }
  17017. if (ret == BAD_FUNC_ARG) {
  17018. ret = 0;
  17019. }
  17020. else {
  17021. ret = WOLFSSL_FATAL_ERROR;
  17022. }
  17023. }
  17024. #else
  17025. /* Test bad args. User RSA. */
  17026. if (ret == 0) {
  17027. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  17028. if (ret == USER_CRYPTO_ERROR) {
  17029. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  17030. }
  17031. if (ret == USER_CRYPTO_ERROR) {
  17032. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17033. }
  17034. if (ret == USER_CRYPTO_ERROR) {
  17035. ret = 0;
  17036. }
  17037. else {
  17038. ret = WOLFSSL_FATAL_ERROR;
  17039. }
  17040. }
  17041. #endif
  17042. if (tmp != NULL) {
  17043. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17044. }
  17045. if (wc_FreeRsaKey(&key) || ret != 0) {
  17046. ret = WOLFSSL_FATAL_ERROR;
  17047. }
  17048. res = TEST_RES_CHECK(ret == 0);
  17049. #endif
  17050. return res;
  17051. } /* END test_wc_RsaPrivateKeyDecode */
  17052. /*
  17053. * Testing wc_RsaPublicKeyDecode()
  17054. */
  17055. static int test_wc_RsaPublicKeyDecode(void)
  17056. {
  17057. int res = TEST_SKIPPED;
  17058. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17059. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17060. RsaKey keyPub;
  17061. byte* tmp;
  17062. word32 idx = 0;
  17063. int bytes = 0;
  17064. word32 keySz = 0;
  17065. word32 tstKeySz = 0;
  17066. int ret = 0;
  17067. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17068. XFILE f;
  17069. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17070. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17071. byte buf[4096];
  17072. #endif
  17073. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17074. if (tmp == NULL) {
  17075. ret = WOLFSSL_FATAL_ERROR;
  17076. }
  17077. if (ret == 0) {
  17078. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  17079. }
  17080. if (ret == 0) {
  17081. #ifdef USE_CERT_BUFFERS_1024
  17082. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17083. bytes = sizeof_client_keypub_der_1024;
  17084. keySz = 1024;
  17085. #else
  17086. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17087. bytes = sizeof_client_keypub_der_2048;
  17088. keySz = 2048;
  17089. #endif
  17090. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  17091. }
  17092. #ifndef HAVE_USER_RSA
  17093. /* Pass in bad args. */
  17094. if (ret == 0) {
  17095. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  17096. if (ret == BAD_FUNC_ARG) {
  17097. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  17098. }
  17099. if (ret == BAD_FUNC_ARG) {
  17100. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17101. }
  17102. if (ret == BAD_FUNC_ARG) {
  17103. ret = 0;
  17104. }
  17105. else {
  17106. ret = WOLFSSL_FATAL_ERROR;
  17107. }
  17108. }
  17109. #else
  17110. /* Pass in bad args. */
  17111. if (ret == 0) {
  17112. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  17113. if (ret == USER_CRYPTO_ERROR) {
  17114. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  17115. }
  17116. if (ret == USER_CRYPTO_ERROR) {
  17117. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17118. }
  17119. if (ret == USER_CRYPTO_ERROR) {
  17120. ret = 0;
  17121. }
  17122. else {
  17123. ret = WOLFSSL_FATAL_ERROR;
  17124. }
  17125. }
  17126. #endif
  17127. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  17128. ret = WOLFSSL_FATAL_ERROR;
  17129. }
  17130. if (ret == 0) {
  17131. /* Test for getting modulus key size */
  17132. idx = 0;
  17133. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17134. &tstKeySz, NULL, NULL);
  17135. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  17136. }
  17137. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17138. f = XFOPEN(rsaPssPubKey, "rb");
  17139. AssertTrue((f != XBADFILE));
  17140. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  17141. XFCLOSE(f);
  17142. idx = 0;
  17143. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17144. NULL), 0);
  17145. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  17146. AssertTrue((f != XBADFILE));
  17147. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  17148. XFCLOSE(f);
  17149. idx = 0;
  17150. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17151. NULL), 0);
  17152. #endif
  17153. if (tmp != NULL) {
  17154. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17155. }
  17156. res = TEST_RES_CHECK(ret == 0);
  17157. #endif
  17158. return res;
  17159. } /* END test_wc_RsaPublicKeyDecode */
  17160. /*
  17161. * Testing wc_RsaPublicKeyDecodeRaw()
  17162. */
  17163. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17164. {
  17165. int res = TEST_SKIPPED;
  17166. #if !defined(NO_RSA)
  17167. RsaKey key;
  17168. const byte n = 0x23;
  17169. const byte e = 0x03;
  17170. int nSz = sizeof(n);
  17171. int eSz = sizeof(e);
  17172. int ret;
  17173. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17174. if (ret == 0) {
  17175. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  17176. }
  17177. #ifndef HAVE_USER_RSA
  17178. /* Pass in bad args. */
  17179. if (ret == 0) {
  17180. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  17181. if (ret == BAD_FUNC_ARG) {
  17182. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  17183. }
  17184. if (ret == BAD_FUNC_ARG) {
  17185. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  17186. }
  17187. if (ret == BAD_FUNC_ARG) {
  17188. ret = 0;
  17189. }
  17190. else {
  17191. ret = WOLFSSL_FATAL_ERROR;
  17192. }
  17193. }
  17194. #else
  17195. /* Pass in bad args. User RSA. */
  17196. if (ret == 0) {
  17197. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  17198. if (ret == USER_CRYPTO_ERROR) {
  17199. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  17200. }
  17201. if (ret == USER_CRYPTO_ERROR) {
  17202. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  17203. }
  17204. if (ret == USER_CRYPTO_ERROR) {
  17205. ret = 0;
  17206. }
  17207. else {
  17208. ret = WOLFSSL_FATAL_ERROR;
  17209. }
  17210. }
  17211. #endif
  17212. if (wc_FreeRsaKey(&key) || ret != 0) {
  17213. ret = WOLFSSL_FATAL_ERROR;
  17214. }
  17215. res = TEST_RES_CHECK(ret == 0);
  17216. #endif
  17217. return res;
  17218. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17219. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  17220. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17221. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17222. * trying until it gets a probable prime. */
  17223. #ifdef HAVE_FIPS
  17224. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17225. {
  17226. int ret;
  17227. for (;;) {
  17228. ret = wc_MakeRsaKey(key, size, e, rng);
  17229. if (ret != PRIME_GEN_E) break;
  17230. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17231. "trying again.\n");
  17232. }
  17233. return ret;
  17234. }
  17235. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17236. #else
  17237. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17238. #endif
  17239. #endif
  17240. /*
  17241. * Testing wc_MakeRsaKey()
  17242. */
  17243. static int test_wc_MakeRsaKey(void)
  17244. {
  17245. int res = TEST_SKIPPED;
  17246. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17247. RsaKey genKey;
  17248. WC_RNG rng;
  17249. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17250. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17251. int bits = 1024;
  17252. #else
  17253. int bits = 2048;
  17254. #endif
  17255. int ret = 0;
  17256. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17257. if (ret == 0) {
  17258. ret = wc_InitRng(&rng);
  17259. if (ret == 0) {
  17260. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17261. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  17262. ret = WOLFSSL_FATAL_ERROR;
  17263. }
  17264. }
  17265. }
  17266. #ifndef HAVE_USER_RSA
  17267. /* Test bad args. */
  17268. if (ret == 0) {
  17269. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  17270. if (ret == BAD_FUNC_ARG) {
  17271. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  17272. }
  17273. if (ret == BAD_FUNC_ARG) {
  17274. /* e < 3 */
  17275. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  17276. }
  17277. if (ret == BAD_FUNC_ARG) {
  17278. /* e & 1 == 0 */
  17279. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  17280. }
  17281. if (ret == BAD_FUNC_ARG) {
  17282. ret = 0;
  17283. }
  17284. else {
  17285. ret = WOLFSSL_FATAL_ERROR;
  17286. }
  17287. }
  17288. #else
  17289. /* Test bad args. */
  17290. if (ret == 0) {
  17291. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  17292. if (ret == USER_CRYPTO_ERROR) {
  17293. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  17294. }
  17295. if (ret == USER_CRYPTO_ERROR) {
  17296. /* e < 3 */
  17297. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  17298. }
  17299. if (ret == USER_CRYPTO_ERROR) {
  17300. /* e & 1 == 0 */
  17301. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  17302. }
  17303. if (ret == USER_CRYPTO_ERROR) {
  17304. ret = 0;
  17305. }
  17306. else {
  17307. ret = WOLFSSL_FATAL_ERROR;
  17308. }
  17309. }
  17310. #endif
  17311. if (wc_FreeRng(&rng) || ret != 0) {
  17312. ret = WOLFSSL_FATAL_ERROR;
  17313. }
  17314. res = TEST_RES_CHECK(ret == 0);
  17315. #endif
  17316. return res;
  17317. } /* END test_wc_MakeRsaKey */
  17318. /*
  17319. * Test the bounds checking on the cipher text versus the key modulus.
  17320. * 1. Make a new RSA key.
  17321. * 2. Set c to 1.
  17322. * 3. Decrypt c into k. (error)
  17323. * 4. Copy the key modulus to c and sub 1 from the copy.
  17324. * 5. Decrypt c into k. (error)
  17325. * Valid bounds test cases are covered by all the other RSA tests.
  17326. */
  17327. static int test_RsaDecryptBoundsCheck(void)
  17328. {
  17329. int res = TEST_SKIPPED;
  17330. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17331. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17332. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17333. WC_RNG rng;
  17334. RsaKey key;
  17335. byte flatC[256];
  17336. word32 flatCSz;
  17337. byte out[256];
  17338. word32 outSz = sizeof(out);
  17339. int ret;
  17340. XMEMSET(&rng, 0, sizeof(rng));
  17341. ret = wc_InitRng(&rng);
  17342. if (ret == 0)
  17343. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17344. if (ret == 0) {
  17345. const byte* derKey;
  17346. word32 derKeySz;
  17347. word32 idx = 0;
  17348. #ifdef USE_CERT_BUFFERS_1024
  17349. derKey = server_key_der_1024;
  17350. derKeySz = (word32)sizeof_server_key_der_1024;
  17351. flatCSz = 128;
  17352. #else
  17353. derKey = server_key_der_2048;
  17354. derKeySz = (word32)sizeof_server_key_der_2048;
  17355. flatCSz = 256;
  17356. #endif
  17357. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  17358. }
  17359. if (ret == 0) {
  17360. XMEMSET(flatC, 0, flatCSz);
  17361. flatC[flatCSz-1] = 1;
  17362. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17363. RSA_PRIVATE_DECRYPT, &rng);
  17364. if (ret == RSA_OUT_OF_RANGE_E) {
  17365. mp_int c;
  17366. mp_init_copy(&c, &key.n);
  17367. mp_sub_d(&c, 1, &c);
  17368. mp_to_unsigned_bin(&c, flatC);
  17369. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17370. RSA_PRIVATE_DECRYPT, NULL);
  17371. mp_clear(&c);
  17372. }
  17373. if (ret == RSA_OUT_OF_RANGE_E)
  17374. ret = 0;
  17375. else
  17376. ret = WOLFSSL_FATAL_ERROR;
  17377. }
  17378. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  17379. ret = WOLFSSL_FATAL_ERROR;
  17380. res = TEST_RES_CHECK(ret == 0);
  17381. #endif
  17382. return res;
  17383. } /* END test_wc_RsaDecryptBoundsCheck */
  17384. /*
  17385. * Testing wc_SetKeyUsage()
  17386. */
  17387. static int test_wc_SetKeyUsage(void)
  17388. {
  17389. int res = TEST_SKIPPED;
  17390. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17391. Cert myCert;
  17392. int ret = 0;
  17393. ret = wc_InitCert(&myCert);
  17394. if (ret == 0) {
  17395. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  17396. if (ret == 0) {
  17397. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  17398. }
  17399. if (ret == 0) {
  17400. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  17401. }
  17402. if (ret == 0) {
  17403. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  17404. }
  17405. if (ret == 0) {
  17406. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  17407. }
  17408. }
  17409. /* Test bad args. */
  17410. if (ret == 0) {
  17411. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  17412. if (ret == BAD_FUNC_ARG) {
  17413. ret = wc_SetKeyUsage(&myCert, NULL);
  17414. }
  17415. if (ret == BAD_FUNC_ARG) {
  17416. ret = wc_SetKeyUsage(&myCert, "");
  17417. }
  17418. if (ret == KEYUSAGE_E) {
  17419. ret = wc_SetKeyUsage(&myCert, ",");
  17420. }
  17421. if (ret == KEYUSAGE_E) {
  17422. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  17423. }
  17424. if (ret == KEYUSAGE_E) {
  17425. ret = 0;
  17426. }
  17427. else {
  17428. ret = WOLFSSL_FATAL_ERROR;
  17429. }
  17430. }
  17431. res = TEST_RES_CHECK(ret == 0);
  17432. #endif
  17433. return res;
  17434. } /* END test_wc_SetKeyUsage */
  17435. /*
  17436. * Testing wc_CheckProbablePrime()
  17437. */
  17438. static int test_wc_CheckProbablePrime(void)
  17439. {
  17440. int res = TEST_SKIPPED;
  17441. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17442. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17443. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17444. RsaKey key;
  17445. WC_RNG rng;
  17446. byte e[3];
  17447. word32 eSz = (word32)sizeof(e);
  17448. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17449. word32 nSz = (word32)sizeof(n);
  17450. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17451. word32 dSz = (word32)sizeof(d);
  17452. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17453. word32 pSz = (word32)sizeof(p);
  17454. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17455. word32 qSz = (word32)sizeof(q);
  17456. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17457. int ret = 0;
  17458. int* isPrime;
  17459. int test[5];
  17460. isPrime = test;
  17461. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17462. if (ret == 0) {
  17463. ret = wc_InitRng(&rng);
  17464. }
  17465. if (ret == 0) {
  17466. ret = wc_RsaSetRNG(&key, &rng);
  17467. }
  17468. if (ret == 0) {
  17469. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  17470. }
  17471. if (ret == 0) {
  17472. PRIVATE_KEY_UNLOCK();
  17473. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  17474. p, &pSz, q, &qSz);
  17475. PRIVATE_KEY_LOCK();
  17476. }
  17477. /* Bad cases */
  17478. if (ret == 0) {
  17479. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  17480. nlen, isPrime);
  17481. if (ret == BAD_FUNC_ARG) {
  17482. ret = 0;
  17483. }
  17484. }
  17485. if (ret == 0) {
  17486. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  17487. nlen, isPrime);
  17488. if (ret == BAD_FUNC_ARG) {
  17489. ret = 0;
  17490. }
  17491. }
  17492. if (ret == 0) {
  17493. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  17494. nlen, isPrime);
  17495. if (ret == BAD_FUNC_ARG) {
  17496. ret = 0;
  17497. }
  17498. }
  17499. if (ret == 0) {
  17500. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  17501. nlen, isPrime);
  17502. if (ret == BAD_FUNC_ARG) {
  17503. ret = 0;
  17504. }
  17505. }
  17506. if (ret == 0) {
  17507. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  17508. nlen, isPrime);
  17509. if (ret == BAD_FUNC_ARG) {
  17510. ret = 0;
  17511. }
  17512. }
  17513. if (ret == 0) {
  17514. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  17515. nlen, isPrime);
  17516. if (ret == BAD_FUNC_ARG) {
  17517. ret = 0;
  17518. }
  17519. }
  17520. if (ret == 0) {
  17521. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  17522. nlen, isPrime);
  17523. if (ret == BAD_FUNC_ARG) {
  17524. ret = 0;
  17525. }
  17526. }
  17527. /* Good case */
  17528. if (ret == 0) {
  17529. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  17530. nlen, isPrime);
  17531. }
  17532. wc_FreeRsaKey(&key);
  17533. wc_FreeRng(&rng);
  17534. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17535. res = TEST_RES_CHECK(ret == 0);
  17536. #endif
  17537. return res;
  17538. } /* END test_wc_CheckProbablePrime */
  17539. /*
  17540. * Testing wc_RsaPSS_Verify()
  17541. */
  17542. static int test_wc_RsaPSS_Verify(void)
  17543. {
  17544. int res = TEST_SKIPPED;
  17545. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17546. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17547. RsaKey key;
  17548. WC_RNG rng;
  17549. int sz = 256;
  17550. byte* pt;
  17551. const char* szMessage = "This is the string to be signed";
  17552. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17553. unsigned char pDecrypted[2048/8];
  17554. word32 outLen = sizeof(pDecrypted);
  17555. int ret = 0;
  17556. pt = pDecrypted;
  17557. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17558. if (ret == 0) {
  17559. ret = wc_InitRng(&rng);
  17560. }
  17561. if (ret == 0) {
  17562. ret = wc_RsaSetRNG(&key, &rng);
  17563. }
  17564. if (ret == 0) {
  17565. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17566. }
  17567. if (ret == 0) {
  17568. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  17569. pSignature, sizeof(pSignature),
  17570. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17571. if (ret > 0) {
  17572. sz = ret;
  17573. ret = 0;
  17574. }
  17575. }
  17576. /* Bad cases */
  17577. if (ret == 0) {
  17578. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  17579. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17580. if (ret == BAD_FUNC_ARG) {
  17581. ret = 0;
  17582. }
  17583. }
  17584. if (ret == 0) {
  17585. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17586. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17587. if (ret == BAD_FUNC_ARG) {
  17588. ret = 0;
  17589. }
  17590. }
  17591. if (ret == 0) {
  17592. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  17593. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17594. if (ret == BAD_FUNC_ARG) {
  17595. ret = 0;
  17596. }
  17597. }
  17598. if (ret == 0) {
  17599. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17600. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17601. if (ret == BAD_FUNC_ARG) {
  17602. ret = 0;
  17603. }
  17604. }
  17605. /* Good case */
  17606. if (ret == 0) {
  17607. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17608. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17609. if (ret > 0) {
  17610. ret = 0;
  17611. }
  17612. }
  17613. wc_FreeRsaKey(&key);
  17614. wc_FreeRng(&rng);
  17615. res = TEST_RES_CHECK(ret == 0);
  17616. #endif
  17617. return res;
  17618. } /* END test_wc_RsaPSS_Verify */
  17619. /*
  17620. * Testing wc_RsaPSS_VerifyCheck()
  17621. */
  17622. static int test_wc_RsaPSS_VerifyCheck(void)
  17623. {
  17624. int res = TEST_SKIPPED;
  17625. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17626. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17627. RsaKey key;
  17628. WC_RNG rng;
  17629. int sz = 256; /* 2048/8 */
  17630. byte* pt;
  17631. byte digest[32];
  17632. word32 digestSz = sizeof(digest);
  17633. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17634. word32 pSignatureSz = sizeof(pSignature);
  17635. unsigned char pDecrypted[2048/8];
  17636. word32 outLen = sizeof(pDecrypted);
  17637. int ret = 0;
  17638. pt = pDecrypted;
  17639. XMEMSET(digest, 0, sizeof(digest));
  17640. XMEMSET(pSignature, 0, sizeof(pSignature));
  17641. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17642. if (ret == 0) {
  17643. ret = wc_InitRng(&rng);
  17644. }
  17645. if (ret == 0) {
  17646. ret = wc_RsaSetRNG(&key, &rng);
  17647. }
  17648. if (ret == 0) {
  17649. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17650. }
  17651. if (ret == 0) {
  17652. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17653. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17654. }
  17655. if (ret == 0) {
  17656. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17657. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17658. if (ret > 0) {
  17659. sz = ret;
  17660. ret = 0;
  17661. }
  17662. }
  17663. /* Bad cases */
  17664. if (ret == 0) {
  17665. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  17666. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17667. if (ret == BAD_FUNC_ARG) {
  17668. ret = 0;
  17669. }
  17670. }
  17671. if (ret == 0) {
  17672. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  17673. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17674. if (ret == BAD_FUNC_ARG) {
  17675. ret = 0;
  17676. }
  17677. }
  17678. if (ret == 0) {
  17679. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  17680. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17681. if (ret == BAD_FUNC_ARG) {
  17682. ret = 0;
  17683. }
  17684. }
  17685. if (ret == 0) {
  17686. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  17687. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17688. if (ret == BAD_FUNC_ARG) {
  17689. ret = 0;
  17690. }
  17691. }
  17692. /* Good case */
  17693. if (ret == 0) {
  17694. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  17695. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17696. if (ret > 0) {
  17697. ret = 0;
  17698. }
  17699. }
  17700. wc_FreeRsaKey(&key);
  17701. wc_FreeRng(&rng);
  17702. res = TEST_RES_CHECK(ret == 0);
  17703. #endif
  17704. return res;
  17705. } /* END test_wc_RsaPSS_VerifyCheck */
  17706. /*
  17707. * Testing wc_RsaPSS_VerifyCheckInline()
  17708. */
  17709. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17710. {
  17711. int res = TEST_SKIPPED;
  17712. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17713. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17714. RsaKey key;
  17715. WC_RNG rng;
  17716. int sz = 256;
  17717. byte* pt;
  17718. byte digest[32];
  17719. word32 digestSz = sizeof(digest);
  17720. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17721. unsigned char pDecrypted[2048/8];
  17722. int ret;
  17723. pt = pDecrypted;
  17724. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17725. XMEMSET(digest, 0, sizeof(digest));
  17726. XMEMSET(pSignature, 0, sizeof(pSignature));
  17727. if (ret == 0) {
  17728. ret = wc_InitRng(&rng);
  17729. }
  17730. if (ret == 0) {
  17731. ret = wc_RsaSetRNG(&key, &rng);
  17732. }
  17733. if (ret == 0) {
  17734. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17735. }
  17736. if (ret == 0) {
  17737. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17738. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17739. }
  17740. if (ret == 0) {
  17741. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  17742. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17743. if (ret > 0) {
  17744. sz = ret;
  17745. ret = 0;
  17746. }
  17747. }
  17748. /* Bad Cases */
  17749. if (ret == 0) {
  17750. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  17751. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17752. if (ret == BAD_FUNC_ARG) {
  17753. ret = 0;
  17754. }
  17755. }
  17756. if (ret == 0) {
  17757. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  17758. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17759. if (ret == BAD_FUNC_ARG) {
  17760. ret = 0;
  17761. }
  17762. }
  17763. if (ret == 0) {
  17764. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  17765. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17766. if (ret == BAD_FUNC_ARG) {
  17767. ret = 0;
  17768. }
  17769. }
  17770. if (ret == 0) {
  17771. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17772. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  17773. if (ret == BAD_FUNC_ARG) {
  17774. ret = 0;
  17775. }
  17776. }
  17777. /* Good case */
  17778. if (ret == 0) {
  17779. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17780. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17781. if (ret > 0) {
  17782. ret = 0;
  17783. }
  17784. }
  17785. wc_FreeRsaKey(&key);
  17786. wc_FreeRng(&rng);
  17787. res = TEST_RES_CHECK(ret == 0);
  17788. #endif
  17789. return res;
  17790. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17791. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17792. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17793. {
  17794. (void)flag;
  17795. (void)type;
  17796. (void)file;
  17797. (void)line;
  17798. }
  17799. #endif
  17800. /*
  17801. * Testing wc_LockMutex_ex
  17802. */
  17803. static int test_wc_LockMutex_ex(void)
  17804. {
  17805. int res = TEST_SKIPPED;
  17806. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17807. int ret = 0;
  17808. int flag = CRYPTO_LOCK;
  17809. int type = 0;
  17810. const char* file = "./test-LockMutex_ex.txt";
  17811. int line = 0;
  17812. /* without SetMutexCb */
  17813. ret = wc_LockMutex_ex(flag, type, file, line);
  17814. if (ret == BAD_STATE_E) {
  17815. ret = 0;
  17816. }
  17817. /* with SetMutexCb */
  17818. if (ret == 0) {
  17819. ret = wc_SetMutexCb(sample_mutex_cb);
  17820. if (ret == 0) {
  17821. ret = wc_LockMutex_ex(flag, type, file, line);
  17822. }
  17823. }
  17824. res = TEST_RES_CHECK(ret == 0);
  17825. #endif
  17826. return res;
  17827. }/*End test_wc_LockMutex_ex*/
  17828. /*
  17829. * Testing wc_SetMutexCb
  17830. */
  17831. static int test_wc_SetMutexCb(void)
  17832. {
  17833. int res = TEST_SKIPPED;
  17834. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17835. int ret = wc_SetMutexCb(sample_mutex_cb);
  17836. res = TEST_RES_CHECK(ret == 0);
  17837. #endif
  17838. return res;
  17839. }/*End test_wc_SetMutexCb*/
  17840. /*
  17841. * Testing wc_RsaKeyToDer()
  17842. */
  17843. static int test_wc_RsaKeyToDer(void)
  17844. {
  17845. int res = TEST_SKIPPED;
  17846. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17847. RsaKey genKey;
  17848. WC_RNG rng;
  17849. byte* der;
  17850. int ret = 0;
  17851. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17852. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17853. int bits = 1024;
  17854. word32 derSz = 611;
  17855. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17856. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17857. #else
  17858. int bits = 2048;
  17859. word32 derSz = 1196;
  17860. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17861. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17862. #endif
  17863. XMEMSET(&rng, 0, sizeof(rng));
  17864. XMEMSET(&genKey, 0, sizeof(genKey));
  17865. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17866. if (der == NULL) {
  17867. ret = WOLFSSL_FATAL_ERROR;
  17868. }
  17869. /* Init structures. */
  17870. if (ret == 0) {
  17871. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17872. }
  17873. if (ret == 0) {
  17874. ret = wc_InitRng(&rng);
  17875. }
  17876. /* Make key. */
  17877. if (ret == 0) {
  17878. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17879. if (ret != 0) {
  17880. ret = WOLFSSL_FATAL_ERROR;
  17881. }
  17882. }
  17883. if (ret == 0) {
  17884. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  17885. if (ret > 0) {
  17886. ret = 0;
  17887. }
  17888. else {
  17889. ret = WOLFSSL_FATAL_ERROR;
  17890. }
  17891. }
  17892. #ifndef HAVE_USER_RSA
  17893. /* Pass good/bad args. */
  17894. if (ret == 0) {
  17895. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17896. if (ret == BAD_FUNC_ARG) {
  17897. /* Get just the output length */
  17898. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17899. }
  17900. if (ret > 0) {
  17901. /* Try Public Key. */
  17902. genKey.type = 0;
  17903. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17904. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17905. /* Put back to Private Key */
  17906. genKey.type = 1;
  17907. #endif
  17908. }
  17909. if (ret == BAD_FUNC_ARG) {
  17910. ret = 0;
  17911. }
  17912. else {
  17913. ret = WOLFSSL_FATAL_ERROR;
  17914. }
  17915. }
  17916. #else
  17917. /* Pass good/bad args. */
  17918. if (ret == 0) {
  17919. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17920. if (ret == USER_CRYPTO_ERROR) {
  17921. /* Get just the output length */
  17922. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17923. }
  17924. if (ret > 0) {
  17925. /* Try Public Key. */
  17926. genKey.type = 0;
  17927. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17928. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17929. /* Put back to Private Key */
  17930. genKey.type = 1;
  17931. #endif
  17932. }
  17933. if (ret == USER_CRYPTO_ERROR) {
  17934. ret = 0;
  17935. }
  17936. else {
  17937. ret = WOLFSSL_FATAL_ERROR;
  17938. }
  17939. }
  17940. #endif
  17941. if (der != NULL) {
  17942. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17943. }
  17944. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  17945. ret = WOLFSSL_FATAL_ERROR;
  17946. }
  17947. if (wc_FreeRng(&rng) || ret != 0) {
  17948. ret = WOLFSSL_FATAL_ERROR;
  17949. }
  17950. res = TEST_RES_CHECK(ret == 0);
  17951. #endif
  17952. return res;
  17953. } /* END test_wc_RsaKeyToDer */
  17954. /*
  17955. * Testing wc_RsaKeyToPublicDer()
  17956. */
  17957. static int test_wc_RsaKeyToPublicDer(void)
  17958. {
  17959. int res = TEST_SKIPPED;
  17960. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17961. RsaKey key;
  17962. WC_RNG rng;
  17963. byte* der;
  17964. int ret = 0;
  17965. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17966. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17967. int bits = 1024;
  17968. word32 derLen = 162;
  17969. #else
  17970. int bits = 2048;
  17971. word32 derLen = 294;
  17972. #endif
  17973. XMEMSET(&rng, 0, sizeof(rng));
  17974. XMEMSET(&key, 0, sizeof(key));
  17975. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17976. if (der == NULL) {
  17977. ret = WOLFSSL_FATAL_ERROR;
  17978. }
  17979. if (ret == 0) {
  17980. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17981. }
  17982. if (ret == 0) {
  17983. ret = wc_InitRng(&rng);
  17984. }
  17985. if (ret == 0) {
  17986. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17987. }
  17988. if (ret == 0) {
  17989. /* test getting size only */
  17990. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  17991. if (ret >= 0)
  17992. ret = 0;
  17993. }
  17994. if (ret == 0) {
  17995. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  17996. if (ret >= 0) {
  17997. ret = 0;
  17998. }
  17999. else {
  18000. ret = WOLFSSL_FATAL_ERROR;
  18001. }
  18002. }
  18003. if (ret == 0) {
  18004. /* test getting size only */
  18005. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  18006. if (ret >= 0)
  18007. ret = 0;
  18008. }
  18009. if (ret == 0) {
  18010. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  18011. if (ret >= 0) {
  18012. ret = 0;
  18013. }
  18014. else {
  18015. ret = WOLFSSL_FATAL_ERROR;
  18016. }
  18017. }
  18018. #ifndef HAVE_USER_RSA
  18019. /* Pass in bad args. */
  18020. if (ret == 0) {
  18021. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  18022. if (ret == BAD_FUNC_ARG) {
  18023. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  18024. }
  18025. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  18026. ret = 0;
  18027. }
  18028. else {
  18029. ret = WOLFSSL_FATAL_ERROR;
  18030. }
  18031. }
  18032. #else
  18033. /* Pass in bad args. */
  18034. if (ret == 0) {
  18035. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  18036. if (ret == USER_CRYPTO_ERROR) {
  18037. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  18038. }
  18039. if (ret == USER_CRYPTO_ERROR) {
  18040. ret = 0;
  18041. }
  18042. else {
  18043. ret = WOLFSSL_FATAL_ERROR;
  18044. }
  18045. }
  18046. #endif
  18047. if (der != NULL) {
  18048. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18049. }
  18050. if (wc_FreeRsaKey(&key) || ret != 0) {
  18051. ret = WOLFSSL_FATAL_ERROR;
  18052. }
  18053. if (wc_FreeRng(&rng) || ret != 0) {
  18054. ret = WOLFSSL_FATAL_ERROR;
  18055. }
  18056. res = TEST_RES_CHECK(ret == 0);
  18057. #endif
  18058. return res;
  18059. } /* END test_wc_RsaKeyToPublicDer */
  18060. /*
  18061. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  18062. */
  18063. static int test_wc_RsaPublicEncryptDecrypt(void)
  18064. {
  18065. int res = TEST_SKIPPED;
  18066. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18067. RsaKey key;
  18068. WC_RNG rng;
  18069. int ret = 0;
  18070. const char inStr[] = TEST_STRING;
  18071. const word32 plainLen = (word32)TEST_STRING_SZ;
  18072. const word32 inLen = (word32)TEST_STRING_SZ;
  18073. int bits = TEST_RSA_BITS;
  18074. const word32 cipherLen = TEST_RSA_BYTES;
  18075. word32 cipherLenResult = cipherLen;
  18076. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18077. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18078. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18079. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18080. if (in == NULL || plain == NULL || cipher == NULL) {
  18081. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  18082. return MEMORY_E;
  18083. }
  18084. #endif
  18085. XMEMCPY(in, inStr, inLen);
  18086. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18087. if (ret == 0) {
  18088. ret = wc_InitRng(&rng);
  18089. }
  18090. if (ret == 0) {
  18091. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18092. }
  18093. /* Encrypt. */
  18094. if (ret == 0) {
  18095. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  18096. if (ret >= 0) {
  18097. cipherLenResult = ret;
  18098. ret = 0;
  18099. }
  18100. else {
  18101. ret = WOLFSSL_FATAL_ERROR;
  18102. }
  18103. }
  18104. /* Pass bad args. */
  18105. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  18106. if (ret != 0) {
  18107. return TEST_FAIL;
  18108. }
  18109. /* Decrypt */
  18110. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  18111. /* Bind rng */
  18112. if (ret == 0) {
  18113. ret = wc_RsaSetRNG(&key, &rng);
  18114. }
  18115. #endif
  18116. if (ret == 0) {
  18117. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  18118. }
  18119. if (ret >= 0) {
  18120. ret = XMEMCMP(plain, inStr, plainLen);
  18121. }
  18122. /* Pass in bad args. */
  18123. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  18124. WC_FREE_VAR(in, NULL);
  18125. WC_FREE_VAR(plain, NULL);
  18126. WC_FREE_VAR(cipher, NULL);
  18127. if (wc_FreeRsaKey(&key) || ret != 0) {
  18128. ret = WOLFSSL_FATAL_ERROR;
  18129. }
  18130. if (wc_FreeRng(&rng) || ret != 0) {
  18131. ret = WOLFSSL_FATAL_ERROR;
  18132. }
  18133. res = TEST_RES_CHECK(ret == 0);
  18134. #endif
  18135. return res;
  18136. } /* END test_wc_RsaPublicEncryptDecrypt */
  18137. /*
  18138. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  18139. */
  18140. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  18141. {
  18142. int result = TEST_SKIPPED;
  18143. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  18144. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  18145. && !defined(NO_SHA256)
  18146. RsaKey key;
  18147. WC_RNG rng;
  18148. int ret;
  18149. const char inStr[] = TEST_STRING;
  18150. const word32 inLen = (word32)TEST_STRING_SZ;
  18151. const word32 plainSz = (word32)TEST_STRING_SZ;
  18152. byte* res = NULL;
  18153. int idx = 0;
  18154. int bits = TEST_RSA_BITS;
  18155. const word32 cipherSz = TEST_RSA_BYTES;
  18156. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18157. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18158. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18159. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18160. if (in == NULL || plain == NULL || cipher == NULL) {
  18161. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  18162. return TEST_FAIL;
  18163. }
  18164. #endif
  18165. XMEMCPY(in, inStr, inLen);
  18166. /* Initialize stack structures. */
  18167. XMEMSET(&rng, 0, sizeof(rng));
  18168. XMEMSET(&key, 0, sizeof(key));
  18169. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  18170. if (ret == 0) {
  18171. ret = wc_InitRng(&rng);
  18172. }
  18173. if (ret == 0) {
  18174. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18175. }
  18176. /* Encrypt */
  18177. if (ret == 0) {
  18178. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  18179. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18180. if (ret >= 0) {
  18181. idx = ret;
  18182. ret = 0;
  18183. }
  18184. else {
  18185. ret = WOLFSSL_FATAL_ERROR;
  18186. }
  18187. }
  18188. /* Pass bad args. */
  18189. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  18190. if (ret != 0) {
  18191. return TEST_FAIL;
  18192. }
  18193. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18194. /* Decrypt */
  18195. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  18196. if (ret == 0) {
  18197. ret = wc_RsaSetRNG(&key, &rng);
  18198. }
  18199. #endif
  18200. if (ret == 0) {
  18201. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  18202. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  18203. WC_MGF1SHA256, NULL, 0);
  18204. }
  18205. if (ret >= 0) {
  18206. if (!XMEMCMP(plain, inStr, plainSz)) {
  18207. ret = 0;
  18208. }
  18209. else {
  18210. ret = WOLFSSL_FATAL_ERROR;
  18211. }
  18212. }
  18213. /*Pass bad args.*/
  18214. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  18215. if (ret != 0) {
  18216. return TEST_FAIL;
  18217. }
  18218. if (ret == 0) {
  18219. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  18220. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  18221. WC_MGF1SHA256, NULL, 0);
  18222. if (ret >= 0) {
  18223. if (!XMEMCMP(inStr, res, plainSz)) {
  18224. ret = 0;
  18225. }
  18226. else {
  18227. ret = WOLFSSL_FATAL_ERROR;
  18228. }
  18229. }
  18230. }
  18231. #endif
  18232. WC_FREE_VAR(in, NULL);
  18233. WC_FREE_VAR(plain, NULL);
  18234. WC_FREE_VAR(cipher, NULL);
  18235. if (wc_FreeRsaKey(&key) || ret != 0) {
  18236. ret = WOLFSSL_FATAL_ERROR;
  18237. }
  18238. if (wc_FreeRng(&rng) || ret != 0) {
  18239. ret = WOLFSSL_FATAL_ERROR;
  18240. }
  18241. result = TEST_RES_CHECK(ret == 0);
  18242. #endif
  18243. return result;
  18244. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  18245. /*
  18246. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  18247. */
  18248. static int test_wc_RsaSSL_SignVerify(void)
  18249. {
  18250. int res = TEST_SKIPPED;
  18251. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18252. RsaKey key;
  18253. WC_RNG rng;
  18254. int ret = 0;
  18255. const char inStr[] = TEST_STRING;
  18256. const word32 plainSz = (word32)TEST_STRING_SZ;
  18257. const word32 inLen = (word32)TEST_STRING_SZ;
  18258. word32 idx = 0;
  18259. int bits = TEST_RSA_BITS;
  18260. const word32 outSz = TEST_RSA_BYTES;
  18261. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18262. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  18263. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18264. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18265. if (in == NULL || out == NULL || plain == NULL) {
  18266. fprintf(stderr, "test_wc_RsaSSL_SignVerify failed\n");
  18267. return TEST_FAIL;
  18268. }
  18269. #endif
  18270. XMEMCPY(in, inStr, inLen);
  18271. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18272. if (ret == 0) {
  18273. ret = wc_InitRng(&rng);
  18274. }
  18275. if (ret == 0) {
  18276. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18277. }
  18278. /* Sign. */
  18279. if (ret == 0) {
  18280. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  18281. if (ret == (int)outSz) {
  18282. idx = ret;
  18283. ret = 0;
  18284. }
  18285. else {
  18286. ret = WOLFSSL_FATAL_ERROR;
  18287. }
  18288. }
  18289. #ifndef HAVE_USER_RSA
  18290. /* Test bad args. */
  18291. if (ret == 0) {
  18292. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  18293. if (ret == BAD_FUNC_ARG) {
  18294. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  18295. }
  18296. if (ret == BAD_FUNC_ARG) {
  18297. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  18298. }
  18299. if (ret == BAD_FUNC_ARG) {
  18300. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  18301. }
  18302. if (ret == BAD_FUNC_ARG) {
  18303. ret = 0;
  18304. }
  18305. else {
  18306. ret = WOLFSSL_FATAL_ERROR;
  18307. }
  18308. }
  18309. #else
  18310. /* Test bad args. */
  18311. if (ret == 0) {
  18312. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  18313. if (ret == USER_CRYPTO_ERROR) {
  18314. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  18315. }
  18316. if (ret == USER_CRYPTO_ERROR) {
  18317. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  18318. }
  18319. if (ret == USER_CRYPTO_ERROR) {
  18320. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  18321. }
  18322. if (ret == USER_CRYPTO_ERROR) {
  18323. ret = 0;
  18324. }
  18325. else {
  18326. ret = WOLFSSL_FATAL_ERROR;
  18327. }
  18328. }
  18329. #endif
  18330. if (ret != 0) {
  18331. return TEST_FAIL;
  18332. }
  18333. /* Verify. */
  18334. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  18335. if (ret == (int)inLen) {
  18336. ret = 0;
  18337. }
  18338. else {
  18339. ret = WOLFSSL_FATAL_ERROR;
  18340. }
  18341. #ifndef HAVE_USER_RSA
  18342. /* Pass bad args. */
  18343. if (ret == 0) {
  18344. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  18345. if (ret == BAD_FUNC_ARG) {
  18346. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  18347. }
  18348. if (ret == BAD_FUNC_ARG) {
  18349. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  18350. }
  18351. if (ret == BAD_FUNC_ARG) {
  18352. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  18353. }
  18354. if (ret == BAD_FUNC_ARG) {
  18355. ret = 0;
  18356. }
  18357. else {
  18358. ret = WOLFSSL_FATAL_ERROR;
  18359. }
  18360. }
  18361. #else
  18362. /* Pass bad args. */
  18363. if (ret == 0) {
  18364. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  18365. if (ret == USER_CRYPTO_ERROR) {
  18366. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  18367. }
  18368. if (ret == USER_CRYPTO_ERROR) {
  18369. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  18370. }
  18371. if (ret == USER_CRYPTO_ERROR) {
  18372. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  18373. }
  18374. if (ret == USER_CRYPTO_ERROR) {
  18375. ret = 0;
  18376. }
  18377. else {
  18378. ret = WOLFSSL_FATAL_ERROR;
  18379. }
  18380. }
  18381. #endif
  18382. WC_FREE_VAR(in, NULL);
  18383. WC_FREE_VAR(out, NULL);
  18384. WC_FREE_VAR(plain, NULL);
  18385. if (wc_FreeRsaKey(&key) || ret != 0) {
  18386. ret = WOLFSSL_FATAL_ERROR;
  18387. }
  18388. if (wc_FreeRng(&rng) || ret != 0) {
  18389. ret = WOLFSSL_FATAL_ERROR;
  18390. }
  18391. res = TEST_RES_CHECK(ret == 0);
  18392. #endif
  18393. return res;
  18394. } /* END test_wc_RsaSSL_SignVerify */
  18395. /*
  18396. * Testing wc_RsaEncryptSize()
  18397. */
  18398. static int test_wc_RsaEncryptSize(void)
  18399. {
  18400. int res = TEST_SKIPPED;
  18401. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18402. RsaKey key;
  18403. WC_RNG rng;
  18404. int ret;
  18405. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18406. if (ret == 0) {
  18407. ret = wc_InitRng(&rng);
  18408. }
  18409. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18410. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18411. if (ret == 0) {
  18412. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  18413. if (ret == 0) {
  18414. ret = wc_RsaEncryptSize(&key);
  18415. }
  18416. if (ret == 128) {
  18417. ret = 0;
  18418. }
  18419. else {
  18420. ret = WOLFSSL_FATAL_ERROR;
  18421. }
  18422. }
  18423. if (wc_FreeRsaKey(&key) || ret != 0) {
  18424. ret = WOLFSSL_FATAL_ERROR;
  18425. }
  18426. else {
  18427. ret = 0;
  18428. }
  18429. #endif
  18430. if (ret == 0) {
  18431. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  18432. if (ret == 0) {
  18433. ret = wc_RsaEncryptSize(&key);
  18434. }
  18435. if (ret == 256) {
  18436. ret = 0;
  18437. }
  18438. else {
  18439. ret = WOLFSSL_FATAL_ERROR;
  18440. }
  18441. }
  18442. /* Pass in bad arg. */
  18443. if (ret == 0) {
  18444. ret = wc_RsaEncryptSize(NULL);
  18445. #ifndef HAVE_USER_RSA
  18446. if (ret == BAD_FUNC_ARG) {
  18447. ret = 0;
  18448. }
  18449. else {
  18450. ret = WOLFSSL_FATAL_ERROR;
  18451. }
  18452. #endif
  18453. }
  18454. if (wc_FreeRsaKey(&key) || ret != 0) {
  18455. ret = WOLFSSL_FATAL_ERROR;
  18456. }
  18457. if (wc_FreeRng(&rng) || ret != 0) {
  18458. ret = WOLFSSL_FATAL_ERROR;
  18459. }
  18460. res = TEST_RES_CHECK(ret == 0);
  18461. #endif
  18462. return res;
  18463. } /* END test_wc_RsaEncryptSize*/
  18464. /*
  18465. * Testing wc_RsaFlattenPublicKey()
  18466. */
  18467. static int test_wc_RsaFlattenPublicKey(void)
  18468. {
  18469. int res = TEST_SKIPPED;
  18470. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18471. RsaKey key;
  18472. WC_RNG rng;
  18473. int ret = 0;
  18474. byte e[256];
  18475. byte n[256];
  18476. word32 eSz = sizeof(e);
  18477. word32 nSz = sizeof(n);
  18478. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18479. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18480. int bits = 1024;
  18481. #else
  18482. int bits = 2048;
  18483. #endif
  18484. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18485. if (ret == 0) {
  18486. ret = wc_InitRng(&rng);
  18487. }
  18488. if (ret == 0) {
  18489. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18490. if (ret >= 0) {
  18491. ret = 0;
  18492. }
  18493. else {
  18494. ret = WOLFSSL_FATAL_ERROR;
  18495. }
  18496. }
  18497. if (ret == 0) {
  18498. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  18499. }
  18500. #ifndef HAVE_USER_RSA
  18501. /* Pass bad args. */
  18502. if (ret == 0) {
  18503. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  18504. if (ret == BAD_FUNC_ARG) {
  18505. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  18506. }
  18507. if (ret == BAD_FUNC_ARG) {
  18508. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  18509. }
  18510. if (ret == BAD_FUNC_ARG) {
  18511. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  18512. }
  18513. if (ret == BAD_FUNC_ARG) {
  18514. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  18515. }
  18516. if (ret == BAD_FUNC_ARG) {
  18517. ret = 0;
  18518. }
  18519. else {
  18520. ret = WOLFSSL_FATAL_ERROR;
  18521. }
  18522. }
  18523. #else
  18524. /* Pass bad args. */
  18525. if (ret == 0) {
  18526. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  18527. if (ret == USER_CRYPTO_ERROR) {
  18528. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  18529. }
  18530. if (ret == USER_CRYPTO_ERROR) {
  18531. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  18532. }
  18533. if (ret == USER_CRYPTO_ERROR) {
  18534. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  18535. }
  18536. if (ret == USER_CRYPTO_ERROR) {
  18537. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  18538. }
  18539. if (ret == USER_CRYPTO_ERROR) {
  18540. ret = 0;
  18541. }
  18542. else {
  18543. ret = WOLFSSL_FATAL_ERROR;
  18544. }
  18545. }
  18546. #endif
  18547. if (wc_FreeRsaKey(&key) || ret != 0) {
  18548. ret = WOLFSSL_FATAL_ERROR;
  18549. }
  18550. if (wc_FreeRng(&rng) || ret != 0) {
  18551. ret = WOLFSSL_FATAL_ERROR;
  18552. }
  18553. res = TEST_RES_CHECK(ret == 0);
  18554. #endif
  18555. return res;
  18556. } /* END test_wc_RsaFlattenPublicKey */
  18557. /*
  18558. * unit test for wc_AesCcmSetKey
  18559. */
  18560. static int test_wc_AesCcmSetKey(void)
  18561. {
  18562. int res = TEST_SKIPPED;
  18563. #ifdef HAVE_AESCCM
  18564. Aes aes;
  18565. int ret = 0;
  18566. const byte key16[] =
  18567. {
  18568. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18569. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18570. };
  18571. const byte key24[] =
  18572. {
  18573. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18574. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  18575. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  18576. };
  18577. const byte key32[] =
  18578. {
  18579. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18580. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  18581. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18582. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  18583. };
  18584. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  18585. if (ret != 0)
  18586. return ret;
  18587. #ifdef WOLFSSL_AES_128
  18588. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  18589. #endif
  18590. #ifdef WOLFSSL_AES_192
  18591. if (ret == 0) {
  18592. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  18593. }
  18594. #endif
  18595. #ifdef WOLFSSL_AES_256
  18596. if (ret == 0) {
  18597. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  18598. }
  18599. #endif
  18600. /* Test bad args. */
  18601. if (ret == 0) {
  18602. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  18603. if (ret == BAD_FUNC_ARG) {
  18604. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  18605. }
  18606. if (ret == BAD_FUNC_ARG) {
  18607. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  18608. }
  18609. if (ret != BAD_FUNC_ARG) {
  18610. ret = WOLFSSL_FATAL_ERROR;
  18611. }
  18612. else {
  18613. ret = 0;
  18614. }
  18615. }
  18616. wc_AesFree(&aes);
  18617. res = TEST_RES_CHECK(ret == 0);
  18618. #endif
  18619. return res;
  18620. } /* END test_wc_AesCcmSetKey */
  18621. /*
  18622. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  18623. */
  18624. static int test_wc_AesCcmEncryptDecrypt(void)
  18625. {
  18626. int res = TEST_SKIPPED;
  18627. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  18628. Aes aes;
  18629. int ret = 0;
  18630. const byte key16[] =
  18631. {
  18632. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18633. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18634. };
  18635. /* plaintext */
  18636. const byte plainT[] =
  18637. {
  18638. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18639. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18640. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18641. };
  18642. /* nonce */
  18643. const byte iv[] =
  18644. {
  18645. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18646. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18647. };
  18648. const byte c[] = /* cipher text. */
  18649. {
  18650. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18651. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18652. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18653. };
  18654. const byte t[] = /* Auth tag */
  18655. {
  18656. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18657. };
  18658. const byte authIn[] =
  18659. {
  18660. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18661. };
  18662. byte cipherOut[sizeof(plainT)];
  18663. byte authTag[sizeof(t)];
  18664. int ccmE = WOLFSSL_FATAL_ERROR;
  18665. #ifdef HAVE_AES_DECRYPT
  18666. int ccmD = WOLFSSL_FATAL_ERROR;
  18667. byte plainOut[sizeof(cipherOut)];
  18668. #endif
  18669. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  18670. if (ret != 0)
  18671. return ret;
  18672. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  18673. if (ret == 0) {
  18674. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18675. iv, sizeof(iv), authTag, sizeof(authTag),
  18676. authIn , sizeof(authIn));
  18677. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  18678. XMEMCMP(t, authTag, sizeof(t))) {
  18679. ccmE = WOLFSSL_FATAL_ERROR;
  18680. ret = WOLFSSL_FATAL_ERROR;
  18681. }
  18682. #ifdef HAVE_AES_DECRYPT
  18683. if (ret == 0) {
  18684. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18685. sizeof(plainOut), iv, sizeof(iv),
  18686. authTag, sizeof(authTag),
  18687. authIn, sizeof(authIn));
  18688. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  18689. ccmD = WOLFSSL_FATAL_ERROR;
  18690. }
  18691. }
  18692. #endif
  18693. }
  18694. /* Pass in bad args. Encrypt*/
  18695. if (ret == 0 && ccmE == 0) {
  18696. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18697. iv, sizeof(iv), authTag, sizeof(authTag),
  18698. authIn , sizeof(authIn));
  18699. if (ccmE == BAD_FUNC_ARG) {
  18700. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18701. iv, sizeof(iv), authTag, sizeof(authTag),
  18702. authIn , sizeof(authIn));
  18703. }
  18704. if (ccmE == BAD_FUNC_ARG) {
  18705. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, 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, plainT, sizeof(cipherOut),
  18711. NULL, 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. iv, sizeof(iv), NULL, 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) + 1, authTag, 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) - 7, authTag, sizeof(authTag),
  18727. authIn , sizeof(authIn));
  18728. }
  18729. if (ccmE != BAD_FUNC_ARG) {
  18730. ccmE = WOLFSSL_FATAL_ERROR;
  18731. }
  18732. else {
  18733. ccmE = 0;
  18734. }
  18735. } /* End Encrypt */
  18736. if (ccmE != 0) {
  18737. wc_AesFree(&aes);
  18738. return TEST_FAIL;
  18739. }
  18740. #ifdef HAVE_AES_DECRYPT
  18741. /* Pass in bad args. Decrypt*/
  18742. if (ret == 0 && ccmD == 0) {
  18743. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18744. iv, sizeof(iv), authTag, sizeof(authTag),
  18745. authIn, sizeof(authIn));
  18746. if (ccmD == BAD_FUNC_ARG) {
  18747. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18748. iv, sizeof(iv), authTag, sizeof(authTag),
  18749. authIn, sizeof(authIn));
  18750. }
  18751. if (ccmD == BAD_FUNC_ARG) {
  18752. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, 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, cipherOut,
  18758. sizeof(plainOut), NULL, sizeof(iv),
  18759. authTag, sizeof(authTag),
  18760. authIn, sizeof(authIn));
  18761. }
  18762. if (ccmD == BAD_FUNC_ARG) {
  18763. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18764. sizeof(plainOut), iv, sizeof(iv), NULL,
  18765. sizeof(authTag), authIn, sizeof(authIn));
  18766. }
  18767. if (ccmD == BAD_FUNC_ARG) {
  18768. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18769. sizeof(plainOut), iv, sizeof(iv) + 1,
  18770. authTag, sizeof(authTag),
  18771. authIn, sizeof(authIn));
  18772. }
  18773. if (ccmD == BAD_FUNC_ARG) {
  18774. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18775. sizeof(plainOut), iv, sizeof(iv) - 7,
  18776. authTag, sizeof(authTag),
  18777. authIn, sizeof(authIn));
  18778. }
  18779. if (ccmD != BAD_FUNC_ARG) {
  18780. ccmD = WOLFSSL_FATAL_ERROR;
  18781. }
  18782. else {
  18783. ccmD = 0;
  18784. }
  18785. } /* END Decrypt */
  18786. res = TEST_RES_CHECK(ccmD == 0);
  18787. #endif
  18788. wc_AesFree(&aes);
  18789. #endif /* HAVE_AESCCM */
  18790. return res;
  18791. } /* END test_wc_AesCcmEncryptDecrypt */
  18792. /*
  18793. * Testing wc_InitDsaKey()
  18794. */
  18795. static int test_wc_InitDsaKey(void)
  18796. {
  18797. int res = TEST_SKIPPED;
  18798. #ifndef NO_DSA
  18799. DsaKey key;
  18800. int ret = 0;
  18801. ret = wc_InitDsaKey(&key);
  18802. /* Pass in bad args. */
  18803. if (ret == 0) {
  18804. ret = wc_InitDsaKey(NULL);
  18805. if (ret == BAD_FUNC_ARG) {
  18806. ret = 0;
  18807. }
  18808. else {
  18809. ret = WOLFSSL_FATAL_ERROR;
  18810. }
  18811. }
  18812. wc_FreeDsaKey(&key);
  18813. res = TEST_RES_CHECK(ret == 0);
  18814. #endif
  18815. return res;
  18816. } /* END test_wc_InitDsaKey */
  18817. /*
  18818. * Testing wc_DsaSign() and wc_DsaVerify()
  18819. */
  18820. static int test_wc_DsaSignVerify(void)
  18821. {
  18822. int res = TEST_SKIPPED;
  18823. #if !defined(NO_DSA)
  18824. DsaKey key;
  18825. WC_RNG rng;
  18826. wc_Sha sha;
  18827. int ret = 0;
  18828. byte signature[DSA_SIG_SIZE];
  18829. byte hash[WC_SHA_DIGEST_SIZE];
  18830. word32 idx = 0;
  18831. word32 bytes;
  18832. int answer;
  18833. #ifdef USE_CERT_BUFFERS_1024
  18834. byte tmp[ONEK_BUF];
  18835. XMEMSET(tmp, 0, sizeof(tmp));
  18836. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18837. bytes = sizeof_dsa_key_der_1024;
  18838. #elif defined(USE_CERT_BUFFERS_2048)
  18839. byte tmp[TWOK_BUF];
  18840. XMEMSET(tmp, 0, sizeof(tmp));
  18841. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18842. bytes = sizeof_dsa_key_der_2048;
  18843. #else
  18844. byte tmp[TWOK_BUF];
  18845. XMEMSET(tmp, 0, sizeof(tmp));
  18846. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18847. if (fp == XBADFILE) {
  18848. return WOLFSSL_BAD_FILE;
  18849. }
  18850. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18851. XFCLOSE(fp);
  18852. #endif /* END USE_CERT_BUFFERS_1024 */
  18853. ret = wc_InitSha(&sha);
  18854. if (ret == 0) {
  18855. ret = wc_ShaUpdate(&sha, tmp, bytes);
  18856. if (ret == 0) {
  18857. ret = wc_ShaFinal(&sha, hash);
  18858. }
  18859. if (ret == 0) {
  18860. ret = wc_InitDsaKey(&key);
  18861. }
  18862. if (ret == 0) {
  18863. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18864. }
  18865. if (ret == 0) {
  18866. ret = wc_InitRng(&rng);
  18867. }
  18868. }
  18869. /* Sign. */
  18870. if (ret == 0) {
  18871. ret = wc_DsaSign(hash, signature, &key, &rng);
  18872. }
  18873. /* Test bad args. */
  18874. if (ret == 0) {
  18875. ret = wc_DsaSign(NULL, signature, &key, &rng);
  18876. if (ret == BAD_FUNC_ARG) {
  18877. ret = wc_DsaSign(hash, NULL, &key, &rng);
  18878. }
  18879. if (ret == BAD_FUNC_ARG) {
  18880. ret = wc_DsaSign(hash, signature, NULL, &rng);
  18881. }
  18882. if (ret == BAD_FUNC_ARG) {
  18883. ret = wc_DsaSign(hash, signature, &key, NULL);
  18884. }
  18885. if (ret == BAD_FUNC_ARG) {
  18886. ret = 0;
  18887. }
  18888. else {
  18889. ret = WOLFSSL_FATAL_ERROR;
  18890. }
  18891. }
  18892. if (ret == 0) {
  18893. /* Verify. */
  18894. ret = wc_DsaVerify(hash, signature, &key, &answer);
  18895. if (ret != 0 || answer != 1) {
  18896. ret = WOLFSSL_FATAL_ERROR;
  18897. }
  18898. else {
  18899. ret = 0;
  18900. }
  18901. }
  18902. /* Pass in bad args. */
  18903. if (ret == 0) {
  18904. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  18905. if (ret == BAD_FUNC_ARG) {
  18906. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  18907. }
  18908. if (ret == BAD_FUNC_ARG) {
  18909. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  18910. }
  18911. if (ret == BAD_FUNC_ARG) {
  18912. ret = wc_DsaVerify(hash, signature, &key, NULL);
  18913. }
  18914. if (ret == BAD_FUNC_ARG) {
  18915. ret = 0;
  18916. }
  18917. else {
  18918. ret = WOLFSSL_FATAL_ERROR;
  18919. }
  18920. }
  18921. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18922. /* hard set q to 0 and test fail case */
  18923. mp_free(&key.q);
  18924. mp_init(&key.q);
  18925. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18926. mp_set(&key.q, 1);
  18927. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18928. #endif
  18929. if (wc_FreeRng(&rng) && ret == 0) {
  18930. ret = WOLFSSL_FATAL_ERROR;
  18931. }
  18932. wc_FreeDsaKey(&key);
  18933. wc_ShaFree(&sha);
  18934. res = TEST_RES_CHECK(ret == 0);
  18935. #endif
  18936. return res;
  18937. } /* END test_wc_DsaSign */
  18938. /*
  18939. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18940. */
  18941. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18942. {
  18943. int res = TEST_SKIPPED;
  18944. #if !defined(NO_DSA)
  18945. DsaKey key;
  18946. word32 bytes;
  18947. word32 idx = 0;
  18948. int priv = 0;
  18949. int pub = 0;
  18950. int ret = 0;
  18951. #ifdef USE_CERT_BUFFERS_1024
  18952. byte tmp[ONEK_BUF];
  18953. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18954. bytes = sizeof_dsa_key_der_1024;
  18955. #elif defined(USE_CERT_BUFFERS_2048)
  18956. byte tmp[TWOK_BUF];
  18957. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18958. bytes = sizeof_dsa_key_der_2048;
  18959. #else
  18960. byte tmp[TWOK_BUF];
  18961. XMEMSET(tmp, 0, sizeof(tmp));
  18962. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18963. if (fp == XBADFILE)
  18964. {
  18965. return WOLFSSL_BAD_FILE;
  18966. }
  18967. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18968. XFCLOSE(fp);
  18969. #endif /* END USE_CERT_BUFFERS_1024 */
  18970. ret = wc_InitDsaKey(&key);
  18971. if (ret == 0) {
  18972. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18973. /* Test bad args. */
  18974. if (priv == 0) {
  18975. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  18976. if (priv == BAD_FUNC_ARG) {
  18977. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  18978. }
  18979. if (priv == BAD_FUNC_ARG) {
  18980. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  18981. }
  18982. if (priv == BAD_FUNC_ARG) {
  18983. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18984. }
  18985. if (priv == ASN_PARSE_E || priv == BUFFER_E) {
  18986. priv = 0;
  18987. }
  18988. else {
  18989. priv = WOLFSSL_FATAL_ERROR;
  18990. }
  18991. }
  18992. wc_FreeDsaKey(&key);
  18993. ret = wc_InitDsaKey(&key);
  18994. }
  18995. if (ret == 0) {
  18996. idx = 0; /* Reset */
  18997. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18998. /* Test bad args. */
  18999. if (pub == 0) {
  19000. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  19001. if (pub == BAD_FUNC_ARG) {
  19002. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  19003. }
  19004. if (pub == BAD_FUNC_ARG) {
  19005. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  19006. }
  19007. if (pub == BAD_FUNC_ARG) {
  19008. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  19009. }
  19010. if (pub == ASN_PARSE_E || pub == BUFFER_E) {
  19011. pub = 0;
  19012. }
  19013. else {
  19014. pub = WOLFSSL_FATAL_ERROR;
  19015. }
  19016. }
  19017. } /* END Public Key */
  19018. wc_FreeDsaKey(&key);
  19019. res = TEST_RES_CHECK(ret == 0 && pub == 0 && priv == 0);
  19020. #endif /* !NO_DSA */
  19021. return res;
  19022. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19023. /*
  19024. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19025. */
  19026. static int test_wc_MakeDsaKey(void)
  19027. {
  19028. int res = TEST_SKIPPED;
  19029. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19030. DsaKey genKey;
  19031. WC_RNG rng;
  19032. int ret = 0;
  19033. XMEMSET(&rng, 0, sizeof(rng));
  19034. XMEMSET(&genKey, 0, sizeof(genKey));
  19035. ret = wc_InitRng(&rng);
  19036. if (ret == 0) {
  19037. ret = wc_InitDsaKey(&genKey);
  19038. }
  19039. if (ret == 0) {
  19040. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  19041. }
  19042. /* Test bad args. */
  19043. if (ret == 0) {
  19044. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  19045. if (ret == BAD_FUNC_ARG) {
  19046. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  19047. }
  19048. if (ret == BAD_FUNC_ARG) {
  19049. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  19050. }
  19051. if (ret == BAD_FUNC_ARG) {
  19052. ret = 0;
  19053. }
  19054. else {
  19055. ret = WOLFSSL_FATAL_ERROR;
  19056. }
  19057. }
  19058. if (ret == 0) {
  19059. ret = wc_MakeDsaKey(&rng, &genKey);
  19060. }
  19061. /* Test bad args. */
  19062. if (ret == 0) {
  19063. ret = wc_MakeDsaKey(NULL, &genKey);
  19064. if (ret == BAD_FUNC_ARG) {
  19065. ret = wc_MakeDsaKey(&rng, NULL);
  19066. }
  19067. if (ret == BAD_FUNC_ARG) {
  19068. ret = 0;
  19069. }
  19070. else {
  19071. ret = WOLFSSL_FATAL_ERROR;
  19072. }
  19073. }
  19074. if (wc_FreeRng(&rng) && ret == 0) {
  19075. ret = WOLFSSL_FAILURE;
  19076. }
  19077. wc_FreeDsaKey(&genKey);
  19078. res = TEST_RES_CHECK(ret == 0);
  19079. #endif
  19080. return res;
  19081. } /* END test_wc_MakeDsaKey */
  19082. /*
  19083. * Testing wc_DsaKeyToDer()
  19084. */
  19085. static int test_wc_DsaKeyToDer(void)
  19086. {
  19087. int res = TEST_SKIPPED;
  19088. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19089. DsaKey genKey;
  19090. WC_RNG rng;
  19091. word32 bytes;
  19092. word32 idx = 0;
  19093. int ret = 0;
  19094. #ifdef USE_CERT_BUFFERS_1024
  19095. byte tmp[ONEK_BUF];
  19096. byte der[ONEK_BUF];
  19097. XMEMSET(tmp, 0, sizeof(tmp));
  19098. XMEMSET(der, 0, sizeof(der));
  19099. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19100. bytes = sizeof_dsa_key_der_1024;
  19101. #elif defined(USE_CERT_BUFFERS_2048)
  19102. byte tmp[TWOK_BUF];
  19103. byte der[TWOK_BUF];
  19104. XMEMSET(tmp, 0, sizeof(tmp));
  19105. XMEMSET(der, 0, sizeof(der));
  19106. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19107. bytes = sizeof_dsa_key_der_2048;
  19108. #else
  19109. byte tmp[TWOK_BUF];
  19110. byte der[TWOK_BUF];
  19111. XMEMSET(tmp, 0, sizeof(tmp));
  19112. XMEMSET(der, 0, sizeof(der));
  19113. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  19114. if (fp == XBADFILE) {
  19115. return WOLFSSL_BAD_FILE;
  19116. }
  19117. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  19118. XFCLOSE(fp);
  19119. #endif /* END USE_CERT_BUFFERS_1024 */
  19120. XMEMSET(&rng, 0, sizeof(rng));
  19121. XMEMSET(&genKey, 0, sizeof(genKey));
  19122. ret = wc_InitRng(&rng);
  19123. if (ret == 0) {
  19124. ret = wc_InitDsaKey(&genKey);
  19125. }
  19126. if (ret == 0) {
  19127. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  19128. if (ret == 0) {
  19129. wc_FreeDsaKey(&genKey);
  19130. ret = wc_InitDsaKey(&genKey);
  19131. }
  19132. }
  19133. if (ret == 0) {
  19134. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  19135. }
  19136. if (ret == 0) {
  19137. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  19138. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  19139. ret = 0;
  19140. }
  19141. }
  19142. /* Test bad args. */
  19143. if (ret == 0) {
  19144. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  19145. if (ret == BAD_FUNC_ARG) {
  19146. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  19147. }
  19148. if (ret == BAD_FUNC_ARG) {
  19149. ret = 0;
  19150. }
  19151. else {
  19152. ret = WOLFSSL_FATAL_ERROR;
  19153. }
  19154. }
  19155. if (wc_FreeRng(&rng) && ret == 0) {
  19156. ret = WOLFSSL_FATAL_ERROR;
  19157. }
  19158. wc_FreeDsaKey(&genKey);
  19159. res = TEST_RES_CHECK(ret == 0);
  19160. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19161. return res;
  19162. } /* END test_wc_DsaKeyToDer */
  19163. /*
  19164. * Testing wc_DsaKeyToPublicDer()
  19165. * (indirectly testing setDsaPublicKey())
  19166. */
  19167. static int test_wc_DsaKeyToPublicDer(void)
  19168. {
  19169. int res = TEST_SKIPPED;
  19170. #ifndef HAVE_SELFTEST
  19171. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19172. DsaKey genKey;
  19173. WC_RNG rng;
  19174. byte* der;
  19175. word32 sz;
  19176. int ret = 0;
  19177. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19178. if (der == NULL) {
  19179. ret = WOLFSSL_FATAL_ERROR;
  19180. }
  19181. if (ret == 0) {
  19182. ret = wc_InitDsaKey(&genKey);
  19183. }
  19184. if (ret == 0) {
  19185. ret = wc_InitRng(&rng);
  19186. }
  19187. if (ret == 0) {
  19188. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  19189. }
  19190. if (ret == 0) {
  19191. ret = wc_MakeDsaKey(&rng, &genKey);
  19192. }
  19193. if (ret == 0) {
  19194. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  19195. if (ret >= 0) {
  19196. sz = ret;
  19197. ret = 0;
  19198. }
  19199. else {
  19200. ret = WOLFSSL_FATAL_ERROR;
  19201. }
  19202. }
  19203. if (ret == 0) {
  19204. word32 idx = 0;
  19205. wc_FreeDsaKey(&genKey);
  19206. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  19207. }
  19208. /* Test without the SubjectPublicKeyInfo header */
  19209. if (ret == 0) {
  19210. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  19211. if (ret >= 0) {
  19212. sz = ret;
  19213. ret = 0;
  19214. }
  19215. else {
  19216. ret = WOLFSSL_FATAL_ERROR;
  19217. }
  19218. }
  19219. if (ret == 0) {
  19220. word32 idx = 0;
  19221. wc_FreeDsaKey(&genKey);
  19222. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  19223. }
  19224. /* Test bad args. */
  19225. if (ret == 0) {
  19226. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  19227. if (ret == BAD_FUNC_ARG) {
  19228. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  19229. }
  19230. if (ret == BAD_FUNC_ARG) {
  19231. ret = 0;
  19232. }
  19233. else {
  19234. ret = WOLFSSL_FATAL_ERROR;
  19235. }
  19236. }
  19237. if (wc_FreeRng(&rng) && ret == 0) {
  19238. ret = WOLFSSL_FATAL_ERROR;
  19239. }
  19240. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19241. wc_FreeDsaKey(&genKey);
  19242. res = TEST_RES_CHECK(ret == 0);
  19243. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19244. #endif /* !HAVE_SELFTEST */
  19245. return res;
  19246. } /* END test_wc_DsaKeyToPublicDer */
  19247. /*
  19248. * Testing wc_DsaImportParamsRaw()
  19249. */
  19250. static int test_wc_DsaImportParamsRaw(void)
  19251. {
  19252. int res = TEST_SKIPPED;
  19253. #if !defined(NO_DSA)
  19254. DsaKey key;
  19255. int ret = 0;
  19256. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19257. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19258. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19259. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19260. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19261. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19262. "80a0093113a8bd73";
  19263. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19264. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19265. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19266. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19267. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19268. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19269. "76341a7e7d9";
  19270. /* invalid p and q parameters */
  19271. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19272. const char* invalidQ = "96c5390a";
  19273. ret = wc_InitDsaKey(&key);
  19274. if (ret == 0) {
  19275. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  19276. }
  19277. /* test bad args */
  19278. if (ret == 0) {
  19279. /* null key struct */
  19280. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  19281. if (ret == BAD_FUNC_ARG) {
  19282. /* null param pointers */
  19283. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  19284. }
  19285. if (ret == BAD_FUNC_ARG) {
  19286. /* illegal p length */
  19287. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  19288. }
  19289. if (ret == BAD_FUNC_ARG) {
  19290. /* illegal q length */
  19291. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  19292. if (ret == BAD_FUNC_ARG)
  19293. ret = 0;
  19294. }
  19295. }
  19296. wc_FreeDsaKey(&key);
  19297. res = TEST_RES_CHECK(ret == 0);
  19298. #endif
  19299. return res;
  19300. } /* END test_wc_DsaImportParamsRaw */
  19301. /*
  19302. * Testing wc_DsaImportParamsRawCheck()
  19303. */
  19304. static int test_wc_DsaImportParamsRawCheck(void)
  19305. {
  19306. int res = TEST_SKIPPED;
  19307. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19308. DsaKey key;
  19309. int ret = 0;
  19310. int trusted = 0;
  19311. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19312. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19313. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19314. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19315. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19316. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19317. "80a0093113a8bd73";
  19318. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19319. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19320. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19321. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19322. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19323. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19324. "76341a7e7d9";
  19325. /* invalid p and q parameters */
  19326. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19327. const char* invalidQ = "96c5390a";
  19328. ret = wc_InitDsaKey(&key);
  19329. if (ret == 0) {
  19330. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  19331. }
  19332. /* test bad args */
  19333. if (ret == 0) {
  19334. /* null key struct */
  19335. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  19336. if (ret == BAD_FUNC_ARG) {
  19337. /* null param pointers */
  19338. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  19339. }
  19340. if (ret == BAD_FUNC_ARG) {
  19341. /* illegal p length */
  19342. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  19343. }
  19344. if (ret == BAD_FUNC_ARG) {
  19345. /* illegal q length */
  19346. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  19347. if (ret == BAD_FUNC_ARG)
  19348. ret = 0;
  19349. }
  19350. }
  19351. wc_FreeDsaKey(&key);
  19352. res = TEST_RES_CHECK(ret == 0);
  19353. #endif
  19354. return res;
  19355. } /* END test_wc_DsaImportParamsRawCheck */
  19356. /*
  19357. * Testing wc_DsaExportParamsRaw()
  19358. */
  19359. static int test_wc_DsaExportParamsRaw(void)
  19360. {
  19361. int res = TEST_SKIPPED;
  19362. #if !defined(NO_DSA)
  19363. DsaKey key;
  19364. int ret = 0;
  19365. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19366. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19367. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19368. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19369. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19370. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19371. "80a0093113a8bd73";
  19372. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19373. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19374. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19375. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19376. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19377. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19378. "76341a7e7d9";
  19379. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19380. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19381. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19382. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19383. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19384. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19385. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19386. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19387. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19388. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19389. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19390. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19391. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19392. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19393. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19394. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19395. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19396. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19397. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19398. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19399. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19400. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19401. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19402. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19403. byte pOut[MAX_DSA_PARAM_SIZE];
  19404. byte qOut[MAX_DSA_PARAM_SIZE];
  19405. byte gOut[MAX_DSA_PARAM_SIZE];
  19406. word32 pOutSz, qOutSz, gOutSz;
  19407. ret = wc_InitDsaKey(&key);
  19408. if (ret == 0) {
  19409. /* first test using imported raw parameters, for expected */
  19410. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  19411. }
  19412. if (ret == 0) {
  19413. pOutSz = sizeof(pOut);
  19414. qOutSz = sizeof(qOut);
  19415. gOutSz = sizeof(gOut);
  19416. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19417. gOut, &gOutSz);
  19418. }
  19419. if (ret == 0) {
  19420. /* validate exported parameters are correct */
  19421. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  19422. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  19423. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  19424. ret = -1;
  19425. }
  19426. }
  19427. /* test bad args */
  19428. if (ret == 0) {
  19429. /* null key struct */
  19430. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  19431. gOut, &gOutSz);
  19432. if (ret == BAD_FUNC_ARG) {
  19433. /* null output pointers */
  19434. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  19435. NULL, &gOutSz);
  19436. }
  19437. if (ret == LENGTH_ONLY_E) {
  19438. /* null output size pointers */
  19439. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  19440. gOut, NULL);
  19441. }
  19442. if (ret == BAD_FUNC_ARG) {
  19443. /* p output buffer size too small */
  19444. pOutSz = 1;
  19445. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19446. gOut, &gOutSz);
  19447. pOutSz = sizeof(pOut);
  19448. }
  19449. if (ret == BUFFER_E) {
  19450. /* q output buffer size too small */
  19451. qOutSz = 1;
  19452. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19453. gOut, &gOutSz);
  19454. qOutSz = sizeof(qOut);
  19455. }
  19456. if (ret == BUFFER_E) {
  19457. /* g output buffer size too small */
  19458. gOutSz = 1;
  19459. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19460. gOut, &gOutSz);
  19461. if (ret == BUFFER_E)
  19462. ret = 0;
  19463. }
  19464. }
  19465. wc_FreeDsaKey(&key);
  19466. res = TEST_RES_CHECK(ret == 0);
  19467. #endif
  19468. return res;
  19469. } /* END test_wc_DsaExportParamsRaw */
  19470. /*
  19471. * Testing wc_DsaExportKeyRaw()
  19472. */
  19473. static int test_wc_DsaExportKeyRaw(void)
  19474. {
  19475. int res = TEST_SKIPPED;
  19476. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19477. DsaKey key;
  19478. WC_RNG rng;
  19479. int ret = 0;
  19480. byte xOut[MAX_DSA_PARAM_SIZE];
  19481. byte yOut[MAX_DSA_PARAM_SIZE];
  19482. word32 xOutSz, yOutSz;
  19483. XMEMSET(&rng, 0, sizeof(rng));
  19484. XMEMSET(&key, 0, sizeof(key));
  19485. ret = wc_InitRng(&rng);
  19486. if (ret == 0) {
  19487. ret = wc_InitDsaKey(&key);
  19488. }
  19489. if (ret == 0) {
  19490. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  19491. if (ret == 0) {
  19492. ret = wc_MakeDsaKey(&rng, &key);
  19493. }
  19494. }
  19495. /* try successful export */
  19496. if (ret == 0) {
  19497. xOutSz = sizeof(xOut);
  19498. yOutSz = sizeof(yOut);
  19499. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  19500. }
  19501. /* test bad args */
  19502. if (ret == 0) {
  19503. /* null key struct */
  19504. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  19505. if (ret == BAD_FUNC_ARG) {
  19506. /* null output pointers */
  19507. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  19508. }
  19509. if (ret == LENGTH_ONLY_E) {
  19510. /* null output size pointers */
  19511. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  19512. }
  19513. if (ret == BAD_FUNC_ARG) {
  19514. /* x output buffer size too small */
  19515. xOutSz = 1;
  19516. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  19517. xOutSz = sizeof(xOut);
  19518. }
  19519. if (ret == BUFFER_E) {
  19520. /* y output buffer size too small */
  19521. yOutSz = 1;
  19522. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  19523. if (ret == BUFFER_E)
  19524. ret = 0;
  19525. }
  19526. }
  19527. wc_FreeDsaKey(&key);
  19528. wc_FreeRng(&rng);
  19529. res = TEST_RES_CHECK(ret == 0);
  19530. #endif
  19531. return res;
  19532. } /* END test_wc_DsaExportParamsRaw */
  19533. /*
  19534. * Testing wc_ed25519_make_key().
  19535. */
  19536. static int test_wc_ed25519_make_key(void)
  19537. {
  19538. int res = TEST_SKIPPED;
  19539. #if defined(HAVE_ED25519)
  19540. ed25519_key key;
  19541. WC_RNG rng;
  19542. unsigned char pubkey[ED25519_PUB_KEY_SIZE];
  19543. int ret = 0;
  19544. ret = wc_InitRng(&rng);
  19545. if (ret == 0) {
  19546. ret = wc_ed25519_init(&key);
  19547. }
  19548. if (ret == 0) {
  19549. ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
  19550. if (ret == ECC_PRIV_KEY_E) {
  19551. ret = 0;
  19552. }
  19553. else if (ret == 0) {
  19554. ret = -1;
  19555. }
  19556. }
  19557. if (ret == 0) {
  19558. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19559. }
  19560. /* Test bad args. */
  19561. if (ret == 0) {
  19562. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  19563. if (ret == BAD_FUNC_ARG) {
  19564. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  19565. }
  19566. if (ret == BAD_FUNC_ARG) {
  19567. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  19568. }
  19569. if (ret == BAD_FUNC_ARG) {
  19570. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  19571. }
  19572. if (ret == BAD_FUNC_ARG) {
  19573. ret = 0;
  19574. }
  19575. else if (ret == 0) {
  19576. ret = WOLFSSL_FATAL_ERROR;
  19577. }
  19578. }
  19579. if (wc_FreeRng(&rng) && ret == 0) {
  19580. ret = WOLFSSL_FATAL_ERROR;
  19581. }
  19582. wc_ed25519_free(&key);
  19583. res = TEST_RES_CHECK(ret == 0);
  19584. #endif
  19585. return res;
  19586. } /* END test_wc_ed25519_make_key */
  19587. /*
  19588. * Testing wc_ed25519_init()
  19589. */
  19590. static int test_wc_ed25519_init(void)
  19591. {
  19592. int res = TEST_SKIPPED;
  19593. #if defined(HAVE_ED25519)
  19594. ed25519_key key;
  19595. int ret = 0;
  19596. ret = wc_ed25519_init(&key);
  19597. /* Test bad args. */
  19598. if (ret == 0) {
  19599. ret = wc_ed25519_init(NULL);
  19600. if (ret == BAD_FUNC_ARG) {
  19601. ret = 0;
  19602. }
  19603. else if (ret == 0) {
  19604. ret = WOLFSSL_FATAL_ERROR;
  19605. }
  19606. }
  19607. wc_ed25519_free(&key);
  19608. res = TEST_RES_CHECK(ret == 0);
  19609. #endif
  19610. return res;
  19611. } /* END test_wc_ed25519_init */
  19612. /*
  19613. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19614. */
  19615. static int test_wc_ed25519_sign_msg(void)
  19616. {
  19617. int res = TEST_SKIPPED;
  19618. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19619. WC_RNG rng;
  19620. ed25519_key key;
  19621. int ret = 0;
  19622. byte msg[] = "Everybody gets Friday off.\n";
  19623. byte sig[ED25519_SIG_SIZE];
  19624. word32 msglen = sizeof(msg);
  19625. word32 siglen = sizeof(sig);
  19626. word32 badSigLen = sizeof(sig) - 1;
  19627. #ifdef HAVE_ED25519_VERIFY
  19628. int verify_ok = 0; /*1 = Verify success.*/
  19629. #endif
  19630. /* Initialize stack variables. */
  19631. XMEMSET(sig, 0, siglen);
  19632. /* Initialize key. */
  19633. ret = wc_InitRng(&rng);
  19634. if (ret == 0) {
  19635. ret = wc_ed25519_init(&key);
  19636. if (ret == 0) {
  19637. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19638. }
  19639. }
  19640. if (ret == 0) {
  19641. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  19642. }
  19643. /* Test bad args. */
  19644. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  19645. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  19646. if (ret == BAD_FUNC_ARG) {
  19647. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  19648. }
  19649. if (ret == BAD_FUNC_ARG) {
  19650. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  19651. }
  19652. if (ret == BAD_FUNC_ARG) {
  19653. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  19654. }
  19655. if (ret == BAD_FUNC_ARG) {
  19656. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  19657. }
  19658. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  19659. badSigLen -= 1;
  19660. ret = 0;
  19661. }
  19662. else if (ret == 0) {
  19663. ret = WOLFSSL_FATAL_ERROR;
  19664. }
  19665. } /* END sign */
  19666. #ifdef HAVE_ED25519_VERIFY
  19667. if (ret == 0) {
  19668. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  19669. if (ret == 0 && verify_ok == 1) {
  19670. ret = 0;
  19671. }
  19672. else if (ret == 0) {
  19673. ret = WOLFSSL_FATAL_ERROR;
  19674. }
  19675. /* Test bad args. */
  19676. if (ret == 0) {
  19677. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  19678. msglen, &verify_ok, &key),
  19679. BAD_FUNC_ARG);
  19680. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  19681. msglen, &verify_ok, &key),
  19682. BAD_FUNC_ARG);
  19683. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19684. &key);
  19685. if (ret == BAD_FUNC_ARG) {
  19686. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  19687. &verify_ok, &key);
  19688. }
  19689. if (ret == BAD_FUNC_ARG) {
  19690. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19691. NULL, &key);
  19692. }
  19693. if (ret == BAD_FUNC_ARG) {
  19694. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19695. &verify_ok, NULL);
  19696. }
  19697. if (ret == BAD_FUNC_ARG) {
  19698. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  19699. &verify_ok, &key);
  19700. }
  19701. if (ret == BAD_FUNC_ARG) {
  19702. ret = 0;
  19703. }
  19704. else if (ret == 0) {
  19705. ret = WOLFSSL_FATAL_ERROR;
  19706. }
  19707. }
  19708. } /* END verify. */
  19709. #endif /* Verify. */
  19710. if (wc_FreeRng(&rng) && ret == 0) {
  19711. ret = WOLFSSL_FATAL_ERROR;
  19712. }
  19713. wc_ed25519_free(&key);
  19714. res = TEST_RES_CHECK(ret == 0);
  19715. #endif
  19716. return res;
  19717. } /* END test_wc_ed25519_sign_msg */
  19718. /*
  19719. * Testing wc_ed25519_import_public()
  19720. */
  19721. static int test_wc_ed25519_import_public(void)
  19722. {
  19723. int res = TEST_SKIPPED;
  19724. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19725. WC_RNG rng;
  19726. ed25519_key pubKey;
  19727. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19728. word32 inlen = sizeof(in);
  19729. int ret = 0;
  19730. ret = wc_InitRng(&rng);
  19731. if (ret == 0) {
  19732. ret = wc_ed25519_init(&pubKey);
  19733. if (ret == 0) {
  19734. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  19735. }
  19736. }
  19737. if (ret == 0) {
  19738. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  19739. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19740. ret = 0;
  19741. }
  19742. else {
  19743. ret = WOLFSSL_FATAL_ERROR;
  19744. }
  19745. /* Test bad args. */
  19746. if (ret == 0) {
  19747. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  19748. if (ret == BAD_FUNC_ARG) {
  19749. ret = wc_ed25519_import_public(in, inlen, NULL);
  19750. }
  19751. if (ret == BAD_FUNC_ARG) {
  19752. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  19753. }
  19754. if (ret == BAD_FUNC_ARG) {
  19755. ret = 0;
  19756. }
  19757. else if (ret == 0) {
  19758. ret = WOLFSSL_FATAL_ERROR;
  19759. }
  19760. }
  19761. }
  19762. if (wc_FreeRng(&rng) && ret == 0) {
  19763. ret = WOLFSSL_FATAL_ERROR;
  19764. }
  19765. wc_ed25519_free(&pubKey);
  19766. res = TEST_RES_CHECK(ret == 0);
  19767. #endif
  19768. return res;
  19769. } /* END wc_ed25519_import_public */
  19770. /*
  19771. * Testing wc_ed25519_import_private_key()
  19772. */
  19773. static int test_wc_ed25519_import_private_key(void)
  19774. {
  19775. int res = TEST_SKIPPED;
  19776. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19777. WC_RNG rng;
  19778. ed25519_key key;
  19779. int ret;
  19780. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19781. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19782. word32 privKeySz = sizeof(privKey);
  19783. word32 pubKeySz = sizeof(pubKey);
  19784. #ifdef HAVE_ED25519_KEY_EXPORT
  19785. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19786. word32 bothKeysSz = sizeof(bothKeys);
  19787. #endif
  19788. ret = wc_InitRng(&rng);
  19789. if (ret != 0) {
  19790. return ret;
  19791. }
  19792. ret = wc_ed25519_init(&key);
  19793. if (ret != 0) {
  19794. wc_FreeRng(&rng);
  19795. return ret;
  19796. }
  19797. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19798. if (ret == 0) {
  19799. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19800. pubKeySz, &key, 1);
  19801. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19802. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19803. ret = WOLFSSL_FATAL_ERROR;
  19804. }
  19805. }
  19806. #ifdef HAVE_ED25519_KEY_EXPORT
  19807. if (ret == 0)
  19808. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  19809. if (ret == 0) {
  19810. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19811. &key, 1);
  19812. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19813. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19814. ret = WOLFSSL_FATAL_ERROR;
  19815. }
  19816. }
  19817. #endif
  19818. /* Test bad args. */
  19819. if (ret == 0) {
  19820. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19821. &key);
  19822. if (ret == BAD_FUNC_ARG) {
  19823. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19824. pubKeySz, &key);
  19825. }
  19826. if (ret == BAD_FUNC_ARG) {
  19827. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19828. pubKeySz, NULL);
  19829. }
  19830. if (ret == BAD_FUNC_ARG) {
  19831. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19832. pubKeySz, &key);
  19833. }
  19834. if (ret == BAD_FUNC_ARG) {
  19835. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19836. pubKeySz - 1, &key);
  19837. }
  19838. if (ret == BAD_FUNC_ARG) {
  19839. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19840. 0, &key);
  19841. }
  19842. if (ret == BAD_FUNC_ARG) {
  19843. ret = 0;
  19844. }
  19845. else if (ret == 0) {
  19846. ret = WOLFSSL_FATAL_ERROR;
  19847. }
  19848. }
  19849. if (wc_FreeRng(&rng) && ret == 0) {
  19850. ret = WOLFSSL_FATAL_ERROR;
  19851. }
  19852. wc_ed25519_free(&key);
  19853. res = TEST_RES_CHECK(ret == 0);
  19854. #endif
  19855. return res;
  19856. } /* END test_wc_ed25519_import_private_key */
  19857. /*
  19858. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19859. */
  19860. static int test_wc_ed25519_export(void)
  19861. {
  19862. int res = TEST_SKIPPED;
  19863. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19864. WC_RNG rng;
  19865. ed25519_key key;
  19866. int ret = 0;
  19867. byte priv[ED25519_PRV_KEY_SIZE];
  19868. byte pub[ED25519_PUB_KEY_SIZE];
  19869. word32 privSz = sizeof(priv);
  19870. word32 pubSz = sizeof(pub);
  19871. ret = wc_InitRng(&rng);
  19872. if (ret != 0) {
  19873. return ret;
  19874. }
  19875. ret = wc_ed25519_init(&key);
  19876. if (ret != 0) {
  19877. wc_FreeRng(&rng);
  19878. return ret;
  19879. }
  19880. if (ret == 0) {
  19881. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19882. }
  19883. if (ret == 0) {
  19884. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  19885. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  19886. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  19887. ret = WOLFSSL_FATAL_ERROR;
  19888. }
  19889. if (ret == 0) {
  19890. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  19891. if (ret == BAD_FUNC_ARG) {
  19892. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  19893. }
  19894. if (ret == BAD_FUNC_ARG) {
  19895. ret = wc_ed25519_export_public(&key, pub, NULL);
  19896. }
  19897. if (ret == BAD_FUNC_ARG) {
  19898. ret = 0;
  19899. }
  19900. else if (ret == 0) {
  19901. ret = WOLFSSL_FATAL_ERROR;
  19902. }
  19903. }
  19904. }
  19905. if (ret == 0) {
  19906. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  19907. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  19908. || XMEMCMP(key.k, priv, privSz) != 0)) {
  19909. ret = WOLFSSL_FATAL_ERROR;
  19910. }
  19911. if (ret == 0) {
  19912. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  19913. if (ret == BAD_FUNC_ARG) {
  19914. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  19915. }
  19916. if (ret == BAD_FUNC_ARG) {
  19917. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  19918. }
  19919. if (ret == BAD_FUNC_ARG) {
  19920. ret = 0;
  19921. }
  19922. else if (ret == 0) {
  19923. ret = WOLFSSL_FATAL_ERROR;
  19924. }
  19925. }
  19926. }
  19927. if (wc_FreeRng(&rng) && ret == 0) {
  19928. ret = WOLFSSL_FATAL_ERROR;
  19929. }
  19930. wc_ed25519_free(&key);
  19931. res = TEST_RES_CHECK(ret == 0);
  19932. #endif
  19933. return res;
  19934. } /* END test_wc_ed25519_export */
  19935. /*
  19936. * Testing wc_ed25519_size()
  19937. */
  19938. static int test_wc_ed25519_size(void)
  19939. {
  19940. int res = TEST_SKIPPED;
  19941. #if defined(HAVE_ED25519)
  19942. WC_RNG rng;
  19943. ed25519_key key;
  19944. int ret;
  19945. ret = wc_InitRng(&rng);
  19946. if (ret != 0) {
  19947. return ret;
  19948. }
  19949. ret = wc_ed25519_init(&key);
  19950. if (ret != 0) {
  19951. wc_FreeRng(&rng);
  19952. return ret;
  19953. }
  19954. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19955. if (ret != 0) {
  19956. wc_FreeRng(&rng);
  19957. wc_ed25519_free(&key);
  19958. return ret;
  19959. }
  19960. ret = wc_ed25519_size(&key);
  19961. /* Test bad args. */
  19962. if (ret == ED25519_KEY_SIZE) {
  19963. ret = wc_ed25519_size(NULL);
  19964. if (ret == BAD_FUNC_ARG) {
  19965. ret = 0;
  19966. }
  19967. }
  19968. if (ret == 0) {
  19969. ret = wc_ed25519_sig_size(&key);
  19970. if (ret == ED25519_SIG_SIZE) {
  19971. ret = 0;
  19972. }
  19973. /* Test bad args. */
  19974. if (ret == 0) {
  19975. ret = wc_ed25519_sig_size(NULL);
  19976. if (ret == BAD_FUNC_ARG) {
  19977. ret = 0;
  19978. }
  19979. }
  19980. } /* END wc_ed25519_sig_size() */
  19981. if (ret == 0) {
  19982. ret = wc_ed25519_pub_size(&key);
  19983. if (ret == ED25519_PUB_KEY_SIZE) {
  19984. ret = 0;
  19985. }
  19986. if (ret == 0) {
  19987. ret = wc_ed25519_pub_size(NULL);
  19988. if (ret == BAD_FUNC_ARG) {
  19989. ret = 0;
  19990. }
  19991. }
  19992. } /* END wc_ed25519_pub_size */
  19993. if (ret == 0) {
  19994. ret = wc_ed25519_priv_size(&key);
  19995. if (ret == ED25519_PRV_KEY_SIZE) {
  19996. ret = 0;
  19997. }
  19998. if (ret == 0) {
  19999. ret = wc_ed25519_priv_size(NULL);
  20000. if (ret == BAD_FUNC_ARG) {
  20001. ret = 0;
  20002. }
  20003. }
  20004. } /* END wc_ed25519_pub_size */
  20005. if (wc_FreeRng(&rng) && ret == 0) {
  20006. ret = WOLFSSL_FATAL_ERROR;
  20007. }
  20008. wc_ed25519_free(&key);
  20009. res = TEST_RES_CHECK(ret == 0);
  20010. #endif
  20011. return res;
  20012. } /* END test_wc_ed25519_size */
  20013. /*
  20014. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20015. */
  20016. static int test_wc_ed25519_exportKey(void)
  20017. {
  20018. int res = TEST_SKIPPED;
  20019. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20020. WC_RNG rng;
  20021. ed25519_key key;
  20022. int ret = 0;
  20023. byte priv[ED25519_PRV_KEY_SIZE];
  20024. byte pub[ED25519_PUB_KEY_SIZE];
  20025. byte privOnly[ED25519_PRV_KEY_SIZE];
  20026. word32 privSz = sizeof(priv);
  20027. word32 pubSz = sizeof(pub);
  20028. word32 privOnlySz = sizeof(privOnly);
  20029. ret = wc_InitRng(&rng);
  20030. if (ret != 0) {
  20031. return TEST_FAIL;
  20032. }
  20033. ret = wc_ed25519_init(&key);
  20034. if (ret != 0) {
  20035. wc_FreeRng(&rng);
  20036. return TEST_FAIL;
  20037. }
  20038. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20039. if (ret != 0) {
  20040. wc_FreeRng(&rng);
  20041. wc_ed25519_free(&key);
  20042. return TEST_FAIL;
  20043. }
  20044. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  20045. if (ret == 0) {
  20046. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  20047. if (ret == BAD_FUNC_ARG) {
  20048. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  20049. }
  20050. if (ret == BAD_FUNC_ARG) {
  20051. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  20052. }
  20053. if (ret == BAD_FUNC_ARG) {
  20054. ret = 0;
  20055. }
  20056. else if (ret == 0) {
  20057. ret = WOLFSSL_FATAL_ERROR;
  20058. }
  20059. }
  20060. if (ret == 0) {
  20061. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  20062. if (ret == 0) {
  20063. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  20064. if (ret == BAD_FUNC_ARG) {
  20065. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  20066. }
  20067. if (ret == BAD_FUNC_ARG) {
  20068. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  20069. }
  20070. if (ret == BAD_FUNC_ARG) {
  20071. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  20072. }
  20073. if (ret == BAD_FUNC_ARG) {
  20074. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  20075. }
  20076. if (ret == BAD_FUNC_ARG) {
  20077. ret = 0;
  20078. }
  20079. else if (ret == 0) {
  20080. ret = WOLFSSL_FATAL_ERROR;
  20081. }
  20082. }
  20083. } /* END wc_ed25519_export_key() */
  20084. /* Cross check output. */
  20085. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20086. ret = WOLFSSL_FATAL_ERROR;
  20087. }
  20088. if (wc_FreeRng(&rng) && ret == 0) {
  20089. ret = WOLFSSL_FATAL_ERROR;
  20090. }
  20091. wc_ed25519_free(&key);
  20092. res = TEST_RES_CHECK(ret == 0);
  20093. #endif
  20094. return res;
  20095. } /* END test_wc_ed25519_exportKey */
  20096. /*
  20097. * Testing wc_Ed25519PublicKeyToDer
  20098. */
  20099. static int test_wc_Ed25519PublicKeyToDer(void)
  20100. {
  20101. int res = TEST_SKIPPED;
  20102. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20103. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20104. int tmp;
  20105. ed25519_key key;
  20106. byte derBuf[1024];
  20107. int ret = 0;
  20108. /* Test bad args */
  20109. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  20110. if (tmp != BAD_FUNC_ARG) {
  20111. ret = WOLFSSL_FATAL_ERROR;
  20112. }
  20113. if (ret == 0) {
  20114. wc_ed25519_init(&key);
  20115. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  20116. if (tmp != BUFFER_E) {
  20117. ret = WOLFSSL_FATAL_ERROR;
  20118. }
  20119. wc_ed25519_free(&key);
  20120. }
  20121. /* Test good args */
  20122. if (ret == 0) {
  20123. WC_RNG rng;
  20124. ret = wc_InitRng(&rng);
  20125. if (ret != 0) {
  20126. return TEST_FAIL;
  20127. }
  20128. ret = wc_ed25519_init(&key);
  20129. if (ret != 0) {
  20130. wc_FreeRng(&rng);
  20131. return TEST_FAIL;
  20132. }
  20133. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20134. if (ret != 0) {
  20135. wc_FreeRng(&rng);
  20136. wc_ed25519_free(&key);
  20137. return TEST_FAIL;
  20138. }
  20139. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  20140. if (tmp <= 0) {
  20141. ret = WOLFSSL_FATAL_ERROR;
  20142. }
  20143. wc_FreeRng(&rng);
  20144. wc_ed25519_free(&key);
  20145. }
  20146. res = TEST_RES_CHECK(ret == 0);
  20147. #endif
  20148. return res;
  20149. } /* END testing wc_Ed25519PublicKeyToDer */
  20150. /*
  20151. * Testing wc_curve25519_init and wc_curve25519_free.
  20152. */
  20153. static int test_wc_curve25519_init(void)
  20154. {
  20155. int res = TEST_SKIPPED;
  20156. #if defined(HAVE_CURVE25519)
  20157. curve25519_key key;
  20158. int ret = 0;
  20159. ret = wc_curve25519_init(&key);
  20160. /* Test bad args for wc_curve25519_init */
  20161. if (ret == 0) {
  20162. ret = wc_curve25519_init(NULL);
  20163. if (ret == BAD_FUNC_ARG) {
  20164. ret = 0;
  20165. }
  20166. else if (ret == 0) {
  20167. ret = WOLFSSL_FATAL_ERROR;
  20168. }
  20169. }
  20170. /* Test good args for wc_curve_25519_free */
  20171. wc_curve25519_free(&key);
  20172. wc_curve25519_free(NULL);
  20173. res = TEST_RES_CHECK(ret == 0);
  20174. #endif
  20175. return res;
  20176. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20177. /*
  20178. * Testing test_wc_curve25519_size.
  20179. */
  20180. static int test_wc_curve25519_size(void)
  20181. {
  20182. int res = TEST_SKIPPED;
  20183. #if defined(HAVE_CURVE25519)
  20184. curve25519_key key;
  20185. int ret = 0;
  20186. ret = wc_curve25519_init(&key);
  20187. /* Test good args for wc_curve25519_size */
  20188. if (ret == 0) {
  20189. ret = wc_curve25519_size(&key);
  20190. }
  20191. /* Test bad args for wc_curve25519_size */
  20192. if (ret != 0) {
  20193. ret = wc_curve25519_size(NULL);
  20194. }
  20195. wc_curve25519_free(&key);
  20196. res = TEST_RES_CHECK(ret == 0);
  20197. #endif
  20198. return res;
  20199. } /* END test_wc_curve25519_size*/
  20200. /*
  20201. * Testing test_wc_curve25519_export_key_raw().
  20202. */
  20203. static int test_wc_curve25519_export_key_raw(void)
  20204. {
  20205. int res = TEST_SKIPPED;
  20206. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20207. curve25519_key key;
  20208. WC_RNG rng;
  20209. int ret = 0;
  20210. byte privateKey[CURVE25519_KEYSIZE];
  20211. byte publicKey[CURVE25519_KEYSIZE];
  20212. word32 prvkSz;
  20213. word32 pubkSz;
  20214. byte prik[CURVE25519_KEYSIZE];
  20215. byte pubk[CURVE25519_KEYSIZE];
  20216. word32 prksz;
  20217. word32 pbksz;
  20218. if (0 != wc_InitRng(&rng)) {
  20219. return TEST_FAIL;
  20220. }
  20221. if (0 != wc_curve25519_init(&key)) {
  20222. wc_FreeRng(&rng);
  20223. return TEST_FAIL;
  20224. }
  20225. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20226. /*
  20227. bad-argument-test cases
  20228. target function sould return BAD_FUNC_ARG
  20229. */
  20230. if (ret == 0) {
  20231. prvkSz = CURVE25519_KEYSIZE;
  20232. pubkSz = CURVE25519_KEYSIZE;
  20233. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20234. NULL, privateKey, &prvkSz, publicKey, &pubkSz)) {
  20235. ret = -1;
  20236. }
  20237. }
  20238. if (ret == 0) {
  20239. prvkSz = CURVE25519_KEYSIZE;
  20240. pubkSz = CURVE25519_KEYSIZE;
  20241. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20242. &key, NULL, &prvkSz, publicKey, &pubkSz)) {
  20243. ret = -1;
  20244. }
  20245. }
  20246. if (ret == 0) {
  20247. prvkSz = CURVE25519_KEYSIZE;
  20248. pubkSz = CURVE25519_KEYSIZE;
  20249. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20250. &key, privateKey, NULL, publicKey, &pubkSz)) {
  20251. ret = -1;
  20252. }
  20253. }
  20254. if (ret == 0) {
  20255. /* prvkSz = CURVE25519_KEYSIZE; */
  20256. pubkSz = CURVE25519_KEYSIZE;
  20257. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20258. &key, privateKey, &prvkSz, NULL, &pubkSz)) {
  20259. ret = -1;
  20260. }
  20261. }
  20262. if (ret == 0) {
  20263. prvkSz = CURVE25519_KEYSIZE;
  20264. pubkSz = CURVE25519_KEYSIZE;
  20265. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20266. &key, privateKey, &prvkSz, publicKey, NULL )) {
  20267. ret = -1;
  20268. }
  20269. }
  20270. /*
  20271. cross-testing
  20272. */
  20273. if (ret == 0) {
  20274. prksz = CURVE25519_KEYSIZE;
  20275. ret = wc_curve25519_export_private_raw(&key, prik, &prksz);
  20276. }
  20277. if (ret == 0) {
  20278. pbksz = CURVE25519_KEYSIZE;
  20279. ret = wc_curve25519_export_public(&key, pubk, &pbksz);
  20280. }
  20281. if (ret == 0) {
  20282. prvkSz = CURVE25519_KEYSIZE;
  20283. /* pubkSz = CURVE25519_KEYSIZE; */
  20284. ret = wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20285. publicKey, &pubkSz);
  20286. }
  20287. if (ret == 0) {
  20288. if ((prksz == CURVE25519_KEYSIZE) &&
  20289. (pbksz == CURVE25519_KEYSIZE) &&
  20290. (prvkSz == CURVE25519_KEYSIZE) &&
  20291. (pubkSz == CURVE25519_KEYSIZE)) {
  20292. if (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  20293. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)) {
  20294. ret = -1;
  20295. }
  20296. }
  20297. }
  20298. wc_curve25519_free(&key);
  20299. wc_FreeRng(&rng);
  20300. res = TEST_RES_CHECK(ret == 0);
  20301. #endif
  20302. return res;
  20303. } /* end of test_wc_curve25519_export_key_raw */
  20304. /*
  20305. * Testing test_wc_curve25519_export_key_raw_ex().
  20306. */
  20307. static int test_wc_curve25519_export_key_raw_ex(void)
  20308. {
  20309. int res = TEST_SKIPPED;
  20310. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20311. curve25519_key key;
  20312. WC_RNG rng;
  20313. int ret;
  20314. byte privateKey[CURVE25519_KEYSIZE];
  20315. byte publicKey[CURVE25519_KEYSIZE];
  20316. word32 prvkSz;
  20317. word32 pubkSz;
  20318. byte prik[CURVE25519_KEYSIZE];
  20319. byte pubk[CURVE25519_KEYSIZE];
  20320. word32 prksz;
  20321. word32 pbksz;
  20322. if (0 != wc_InitRng(&rng)) {
  20323. return TEST_FAIL;
  20324. }
  20325. if (0 != wc_curve25519_init(&key)) {
  20326. wc_FreeRng(&rng);
  20327. return TEST_FAIL;
  20328. }
  20329. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20330. /*
  20331. bad-argument-test cases
  20332. target function sould return BAD_FUNC_ARG
  20333. */
  20334. if (ret == 0) {
  20335. prvkSz = CURVE25519_KEYSIZE;
  20336. pubkSz = CURVE25519_KEYSIZE;
  20337. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  20338. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20339. ret = -1;
  20340. }
  20341. }
  20342. if (ret == 0) {
  20343. prvkSz = CURVE25519_KEYSIZE;
  20344. pubkSz = CURVE25519_KEYSIZE;
  20345. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  20346. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20347. ret = -1;
  20348. }
  20349. }
  20350. if (ret == 0) {
  20351. prvkSz = CURVE25519_KEYSIZE;
  20352. pubkSz = CURVE25519_KEYSIZE;
  20353. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  20354. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20355. ret = -1;
  20356. }
  20357. }
  20358. if (ret == 0) {
  20359. /* prvkSz = CURVE25519_KEYSIZE; */
  20360. pubkSz = CURVE25519_KEYSIZE;
  20361. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20362. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20363. ret = -1;
  20364. }
  20365. }
  20366. if (ret == 0) {
  20367. prvkSz = CURVE25519_KEYSIZE;
  20368. pubkSz = CURVE25519_KEYSIZE;
  20369. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20370. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)) {
  20371. ret = -1;
  20372. }
  20373. }
  20374. if (ret == 0) {
  20375. prvkSz = CURVE25519_KEYSIZE;
  20376. /* pubkSz = CURVE25519_KEYSIZE; */
  20377. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  20378. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20379. ret = -1;
  20380. }
  20381. }
  20382. if (ret == 0) {
  20383. prvkSz = CURVE25519_KEYSIZE;
  20384. pubkSz = CURVE25519_KEYSIZE;
  20385. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL,
  20386. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20387. ret = -1;
  20388. }
  20389. }
  20390. if (ret == 0) {
  20391. prvkSz = CURVE25519_KEYSIZE;
  20392. pubkSz = CURVE25519_KEYSIZE;
  20393. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20394. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20395. ret = -1;
  20396. }
  20397. }
  20398. if (ret == 0) {
  20399. /* prvkSz = CURVE25519_KEYSIZE; */
  20400. pubkSz = CURVE25519_KEYSIZE;
  20401. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20402. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)) {
  20403. ret = -1;
  20404. }
  20405. }
  20406. if (ret == 0) {
  20407. prvkSz = CURVE25519_KEYSIZE;
  20408. pubkSz = CURVE25519_KEYSIZE;
  20409. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20410. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)) {
  20411. ret = -1;
  20412. }
  20413. }
  20414. /* illegal value for endien */
  20415. if (ret == 0) {
  20416. prvkSz = CURVE25519_KEYSIZE;
  20417. /* pubkSz = CURVE25519_KEYSIZE; */
  20418. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex(&key, privateKey,
  20419. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10)) {
  20420. ret = -1;
  20421. }
  20422. }
  20423. /*
  20424. cross-testing
  20425. */
  20426. if (ret == 0) {
  20427. prksz = CURVE25519_KEYSIZE;
  20428. ret = wc_curve25519_export_private_raw( &key, prik, &prksz);
  20429. }
  20430. if (ret == 0) {
  20431. pbksz = CURVE25519_KEYSIZE;
  20432. ret = wc_curve25519_export_public( &key, pubk, &pbksz);
  20433. }
  20434. if (ret == 0) {
  20435. prvkSz = CURVE25519_KEYSIZE;
  20436. /* pubkSz = CURVE25519_KEYSIZE; */
  20437. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20438. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  20439. }
  20440. if (ret == 0 && (prksz != CURVE25519_KEYSIZE ||
  20441. pbksz != CURVE25519_KEYSIZE ||
  20442. prvkSz != CURVE25519_KEYSIZE ||
  20443. pubkSz != CURVE25519_KEYSIZE)) {
  20444. ret = -1;
  20445. }
  20446. if (ret == 0 && (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  20447. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE))) {
  20448. ret = -1;
  20449. }
  20450. if (ret == 0) {
  20451. ret = wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20452. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN);
  20453. }
  20454. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  20455. pubkSz != CURVE25519_KEYSIZE)) {
  20456. ret = -1;
  20457. }
  20458. /*
  20459. try once with another endian
  20460. */
  20461. if (ret == 0) {
  20462. prvkSz = CURVE25519_KEYSIZE;
  20463. pubkSz = CURVE25519_KEYSIZE;
  20464. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20465. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  20466. }
  20467. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  20468. pubkSz != CURVE25519_KEYSIZE)) {
  20469. ret = -1;
  20470. }
  20471. wc_curve25519_free(&key);
  20472. wc_FreeRng(&rng);
  20473. res = TEST_RES_CHECK(ret == 0);
  20474. #endif
  20475. return res;
  20476. } /* end of test_wc_curve25519_export_key_raw_ex */
  20477. /*
  20478. * Testing wc_curve25519_make_key
  20479. */
  20480. static int test_wc_curve25519_make_key(void)
  20481. {
  20482. int res = TEST_SKIPPED;
  20483. #if defined(HAVE_CURVE25519)
  20484. WC_RNG rng;
  20485. curve25519_key key;
  20486. int keysize;
  20487. int ret;
  20488. ret = wc_curve25519_init(&key);
  20489. if (ret == 0) {
  20490. ret = wc_InitRng(&rng);
  20491. }
  20492. if (ret == 0) {
  20493. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20494. if (ret == 0) {
  20495. keysize = wc_curve25519_size(&key);
  20496. if (keysize != CURVE25519_KEYSIZE) {
  20497. ret = WOLFSSL_FATAL_ERROR;
  20498. }
  20499. }
  20500. if (ret == 0) {
  20501. ret = wc_curve25519_make_key(&rng, keysize, &key);
  20502. }
  20503. }
  20504. /*test bad cases*/
  20505. if (ret == 0) {
  20506. ret = wc_curve25519_make_key(NULL, 0, NULL);
  20507. if (ret == BAD_FUNC_ARG) {
  20508. ret = 0;
  20509. }
  20510. }
  20511. if (ret == 0) {
  20512. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  20513. if (ret == BAD_FUNC_ARG) {
  20514. ret = 0;
  20515. }
  20516. }
  20517. if (ret == 0) {
  20518. ret = wc_curve25519_make_key(NULL, keysize, &key);
  20519. if (ret == BAD_FUNC_ARG) {
  20520. ret = 0;
  20521. }
  20522. }
  20523. if (ret == 0) {
  20524. ret = wc_curve25519_make_key(&rng, 0, &key);
  20525. if (ret == ECC_BAD_ARG_E) {
  20526. ret = 0;
  20527. }
  20528. }
  20529. wc_curve25519_free(&key);
  20530. wc_FreeRng(&rng);
  20531. res = TEST_RES_CHECK(ret == 0);
  20532. #endif
  20533. return res;
  20534. } /*END test_wc_curve25519_make_key*/
  20535. /*
  20536. * Testing wc_curve25519_shared_secret_ex
  20537. */
  20538. static int test_wc_curve25519_shared_secret_ex(void)
  20539. {
  20540. int res = TEST_SKIPPED;
  20541. #if defined(HAVE_CURVE25519)
  20542. WC_RNG rng;
  20543. curve25519_key private_key, public_key;
  20544. byte out[CURVE25519_KEYSIZE];
  20545. word32 outLen = sizeof(out);
  20546. int endian = EC25519_BIG_ENDIAN;
  20547. int ret;
  20548. ret = wc_curve25519_init(&private_key);
  20549. if (ret == 0) {
  20550. ret = wc_curve25519_init(&public_key);
  20551. }
  20552. if (ret == 0) {
  20553. ret = wc_InitRng(&rng);
  20554. }
  20555. if (ret == 0) {
  20556. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  20557. }
  20558. if (ret == 0) {
  20559. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  20560. }
  20561. if (ret == 0) {
  20562. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20563. &outLen, endian);
  20564. }
  20565. /*test bad cases*/
  20566. if (ret == 0) {
  20567. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  20568. 0, endian);
  20569. if (ret == 0) {
  20570. ret = -1;
  20571. }
  20572. if (ret == BAD_FUNC_ARG) {
  20573. ret = 0;
  20574. }
  20575. }
  20576. if (ret == 0) {
  20577. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  20578. &outLen, endian);
  20579. if (ret == 0) {
  20580. ret = -1;
  20581. }
  20582. else if (ret == BAD_FUNC_ARG) {
  20583. ret = 0;
  20584. }
  20585. }
  20586. if (ret == 0) {
  20587. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  20588. &outLen, endian);
  20589. if (ret == 0) {
  20590. ret = -1;
  20591. }
  20592. else if (ret == BAD_FUNC_ARG) {
  20593. ret = 0;
  20594. }
  20595. }
  20596. if (ret == 0) {
  20597. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20598. &outLen, endian);
  20599. if (ret == 0) {
  20600. ret = -1;
  20601. }
  20602. else if (ret == BAD_FUNC_ARG) {
  20603. ret = 0;
  20604. }
  20605. }
  20606. if (ret == 0) {
  20607. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20608. NULL, endian);
  20609. if (ret == 0) {
  20610. ret = -1;
  20611. }
  20612. else if (ret == BAD_FUNC_ARG) {
  20613. ret = 0;
  20614. }
  20615. }
  20616. if (ret == 0) {
  20617. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  20618. *increasing to 0x8f checks for error being returned*/
  20619. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20620. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20621. &outLen, endian);
  20622. if (ret == 0) {
  20623. ret = -1;
  20624. }
  20625. else if (ret == ECC_BAD_ARG_E) {
  20626. ret = 0;
  20627. }
  20628. }
  20629. outLen = outLen - 2;
  20630. if (ret == 0) {
  20631. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20632. &outLen, endian);
  20633. if (ret == 0) {
  20634. ret = -1;
  20635. }
  20636. else if (ret == BAD_FUNC_ARG) {
  20637. ret = 0;
  20638. }
  20639. }
  20640. wc_curve25519_free(&private_key);
  20641. wc_curve25519_free(&public_key);
  20642. wc_FreeRng(&rng);
  20643. res = TEST_RES_CHECK(ret == 0);
  20644. #endif
  20645. return res;
  20646. } /*END test_wc_curve25519_shared_secret_ex*/
  20647. /*
  20648. * Testing wc_curve25519_make_pub
  20649. */
  20650. static int test_wc_curve25519_make_pub(void)
  20651. {
  20652. int res = TEST_SKIPPED;
  20653. #ifdef HAVE_CURVE25519
  20654. WC_RNG rng;
  20655. curve25519_key key;
  20656. byte out[CURVE25519_KEYSIZE];
  20657. int ret;
  20658. ret = wc_curve25519_init(&key);
  20659. if (ret == 0) {
  20660. ret = wc_InitRng(&rng);
  20661. if (ret == 0) {
  20662. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20663. }
  20664. }
  20665. if (ret == 0) {
  20666. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  20667. }
  20668. /*test bad cases*/
  20669. if (ret == 0) {
  20670. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  20671. if (ret == ECC_BAD_ARG_E) {
  20672. ret = 0;
  20673. }
  20674. }
  20675. if (ret == 0) {
  20676. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  20677. if (ret == ECC_BAD_ARG_E) {
  20678. ret = 0;
  20679. }
  20680. }
  20681. if (ret == 0) {
  20682. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  20683. if (ret == ECC_BAD_ARG_E) {
  20684. ret = 0;
  20685. }
  20686. }
  20687. if (ret == 0) {
  20688. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  20689. if (ret == ECC_BAD_ARG_E) {
  20690. ret = 0;
  20691. }
  20692. }
  20693. if (ret == 0) {
  20694. /* verify clamping test */
  20695. key.k[0] |= ~248;
  20696. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20697. if (ret == ECC_BAD_ARG_E) {
  20698. ret = 0;
  20699. }
  20700. key.k[0] &= 248;
  20701. }
  20702. /* repeat the expected-to-succeed test. */
  20703. if (ret == 0) {
  20704. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20705. }
  20706. wc_curve25519_free(&key);
  20707. wc_FreeRng(&rng);
  20708. res = TEST_RES_CHECK(ret == 0);
  20709. #endif
  20710. return res;
  20711. } /*END test_wc_curve25519_make_pub */
  20712. /*
  20713. * Testing test_wc_curve25519_export_public_ex
  20714. */
  20715. static int test_wc_curve25519_export_public_ex(void)
  20716. {
  20717. int res = TEST_SKIPPED;
  20718. #if defined(HAVE_CURVE25519)
  20719. WC_RNG rng;
  20720. curve25519_key key;
  20721. byte out[CURVE25519_KEYSIZE];
  20722. word32 outLen = sizeof(out);
  20723. int endian = EC25519_BIG_ENDIAN;
  20724. int ret;
  20725. ret = wc_curve25519_init(&key);
  20726. if (ret == 0) {
  20727. ret = wc_InitRng(&rng);
  20728. }
  20729. if (ret == 0) {
  20730. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20731. if (ret == 0) {
  20732. ret = wc_curve25519_export_public(&key, out, &outLen);
  20733. }
  20734. if (ret == 0) {
  20735. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20736. }
  20737. }
  20738. /*test bad cases*/
  20739. if (ret == 0) {
  20740. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  20741. if (ret == BAD_FUNC_ARG) {
  20742. ret = 0;
  20743. }
  20744. }
  20745. if (ret == 0) {
  20746. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  20747. if (ret == BAD_FUNC_ARG) {
  20748. ret = 0;
  20749. }
  20750. }
  20751. if (ret == 0) {
  20752. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  20753. if (ret == BAD_FUNC_ARG) {
  20754. ret = 0;
  20755. }
  20756. }
  20757. if (ret == 0) {
  20758. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  20759. if (ret == BAD_FUNC_ARG) {
  20760. ret = 0;
  20761. }
  20762. }
  20763. outLen = outLen - 2;
  20764. if (ret == 0) {
  20765. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20766. if (ret == ECC_BAD_ARG_E) {
  20767. ret = 0;
  20768. }
  20769. }
  20770. wc_curve25519_free(&key);
  20771. wc_FreeRng(&rng);
  20772. res = TEST_RES_CHECK(ret == 0);
  20773. #endif
  20774. return res;
  20775. } /*END test_wc_curve25519_export_public_ex*/
  20776. /*
  20777. * Testing test_wc_curve25519_import_private_raw_ex
  20778. */
  20779. static int test_wc_curve25519_import_private_raw_ex(void)
  20780. {
  20781. int res = TEST_SKIPPED;
  20782. #if defined(HAVE_CURVE25519)
  20783. WC_RNG rng;
  20784. curve25519_key key;
  20785. byte priv[CURVE25519_KEYSIZE];
  20786. byte pub[CURVE25519_KEYSIZE];
  20787. word32 privSz = sizeof(priv);
  20788. word32 pubSz = sizeof(pub);
  20789. int endian = EC25519_BIG_ENDIAN;
  20790. int ret;
  20791. ret = wc_curve25519_init(&key);
  20792. if (ret == 0) {
  20793. ret = wc_InitRng(&rng);
  20794. }
  20795. if (ret == 0) {
  20796. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20797. if (ret == 0) {
  20798. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  20799. }
  20800. if (ret == 0) {
  20801. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  20802. }
  20803. if (ret == 0) {
  20804. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20805. &key, endian);
  20806. }
  20807. }
  20808. /*test bad cases*/
  20809. if (ret == 0) {
  20810. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20811. endian);
  20812. if (ret == BAD_FUNC_ARG) {
  20813. ret = 0;
  20814. }
  20815. }
  20816. if (ret == 0) {
  20817. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20818. &key, endian);
  20819. if (ret == BAD_FUNC_ARG) {
  20820. ret = 0;
  20821. }
  20822. }
  20823. if (ret == 0) {
  20824. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20825. &key, endian);
  20826. if (ret == BAD_FUNC_ARG) {
  20827. ret = 0;
  20828. }
  20829. }
  20830. if (ret == 0) {
  20831. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20832. NULL, endian);
  20833. if (ret == BAD_FUNC_ARG) {
  20834. ret = 0;
  20835. }
  20836. }
  20837. if (ret == 0) {
  20838. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20839. &key, endian);
  20840. if (ret == ECC_BAD_ARG_E) {
  20841. ret = 0;
  20842. }
  20843. }
  20844. if (ret == 0) {
  20845. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20846. &key, endian);
  20847. if (ret == ECC_BAD_ARG_E) {
  20848. ret = 0;
  20849. }
  20850. }
  20851. if (ret == 0) {
  20852. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20853. &key, EC25519_LITTLE_ENDIAN);
  20854. }
  20855. wc_curve25519_free(&key);
  20856. wc_FreeRng(&rng);
  20857. res = TEST_RES_CHECK(ret == 0);
  20858. #endif
  20859. return res;
  20860. } /*END test_wc_curve25519_import_private_raw_ex*/
  20861. /*
  20862. * Testing test_wc_curve25519_import_private
  20863. */
  20864. static int test_wc_curve25519_import_private(void)
  20865. {
  20866. int res = TEST_SKIPPED;
  20867. #if defined(HAVE_CURVE25519)
  20868. curve25519_key key;
  20869. WC_RNG rng;
  20870. byte priv[CURVE25519_KEYSIZE];
  20871. word32 privSz = sizeof(priv);
  20872. int ret;
  20873. ret = wc_curve25519_init(&key);
  20874. if (ret == 0) {
  20875. ret = wc_InitRng(&rng);
  20876. }
  20877. if (ret == 0) {
  20878. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20879. if (ret == 0) {
  20880. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  20881. }
  20882. }
  20883. if (ret == 0) {
  20884. ret = wc_curve25519_import_private(priv, privSz, &key);
  20885. }
  20886. wc_curve25519_free(&key);
  20887. wc_FreeRng(&rng);
  20888. res = TEST_RES_CHECK(ret == 0);
  20889. #endif
  20890. return res;
  20891. } /*END test_wc_curve25519_import*/
  20892. /*
  20893. * Testing test_wc_curve25519_export_private_raw_ex
  20894. */
  20895. static int test_wc_curve25519_export_private_raw_ex(void)
  20896. {
  20897. int res = TEST_SKIPPED;
  20898. #if defined(HAVE_CURVE25519)
  20899. curve25519_key key;
  20900. byte out[CURVE25519_KEYSIZE];
  20901. word32 outLen = sizeof(out);
  20902. int endian = EC25519_BIG_ENDIAN;
  20903. int ret;
  20904. ret = wc_curve25519_init(&key);
  20905. if (ret == 0) {
  20906. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20907. }
  20908. /*test bad cases*/
  20909. if (ret == 0) {
  20910. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  20911. if (ret == BAD_FUNC_ARG) {
  20912. ret = 0;
  20913. }
  20914. }
  20915. if (ret == 0) {
  20916. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  20917. if (ret == BAD_FUNC_ARG) {
  20918. ret = 0;
  20919. }
  20920. }
  20921. if (ret == 0) {
  20922. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  20923. if (ret == BAD_FUNC_ARG) {
  20924. ret = 0;
  20925. }
  20926. }
  20927. if (ret == 0) {
  20928. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  20929. if (ret == BAD_FUNC_ARG) {
  20930. ret = 0;
  20931. }
  20932. }
  20933. if (ret == 0) {
  20934. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20935. EC25519_LITTLE_ENDIAN);
  20936. }
  20937. outLen = outLen - 2;
  20938. if (ret == 0) {
  20939. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20940. if (ret == ECC_BAD_ARG_E) {
  20941. ret = 0;
  20942. }
  20943. }
  20944. wc_curve25519_free(&key);
  20945. res = TEST_RES_CHECK(ret == 0);
  20946. #endif
  20947. return res;
  20948. }/*END test_wc_curve25519_export_private_raw_ex*/
  20949. /*
  20950. * Testing wc_ed448_make_key().
  20951. */
  20952. static int test_wc_ed448_make_key(void)
  20953. {
  20954. int res = TEST_SKIPPED;
  20955. #if defined(HAVE_ED448)
  20956. ed448_key key;
  20957. WC_RNG rng;
  20958. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20959. int ret;
  20960. ret = wc_InitRng(&rng);
  20961. if (ret == 0) {
  20962. ret = wc_ed448_init(&key);
  20963. }
  20964. if (ret == 0) {
  20965. ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
  20966. if (ret == ECC_PRIV_KEY_E) {
  20967. ret = 0;
  20968. }
  20969. else if (ret == 0) {
  20970. ret = -1;
  20971. }
  20972. }
  20973. if (ret == 0) {
  20974. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20975. }
  20976. /* Test bad args. */
  20977. if (ret == 0) {
  20978. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  20979. if (ret == BAD_FUNC_ARG) {
  20980. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  20981. }
  20982. if (ret == BAD_FUNC_ARG) {
  20983. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  20984. }
  20985. if (ret == BAD_FUNC_ARG) {
  20986. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  20987. }
  20988. if (ret == BAD_FUNC_ARG) {
  20989. ret = 0;
  20990. }
  20991. else if (ret == 0) {
  20992. ret = WOLFSSL_FATAL_ERROR;
  20993. }
  20994. }
  20995. if (wc_FreeRng(&rng) && ret == 0) {
  20996. ret = WOLFSSL_FATAL_ERROR;
  20997. }
  20998. wc_ed448_free(&key);
  20999. res = TEST_RES_CHECK(ret == 0);
  21000. #endif
  21001. return res;
  21002. } /* END test_wc_ed448_make_key */
  21003. /*
  21004. * Testing wc_ed448_init()
  21005. */
  21006. static int test_wc_ed448_init(void)
  21007. {
  21008. int res = TEST_SKIPPED;
  21009. #if defined(HAVE_ED448)
  21010. ed448_key key;
  21011. int ret;
  21012. ret = wc_ed448_init(&key);
  21013. /* Test bad args. */
  21014. if (ret == 0) {
  21015. ret = wc_ed448_init(NULL);
  21016. if (ret == BAD_FUNC_ARG) {
  21017. ret = 0;
  21018. }
  21019. else if (ret == 0) {
  21020. ret = WOLFSSL_FATAL_ERROR;
  21021. }
  21022. }
  21023. wc_ed448_free(&key);
  21024. res = TEST_RES_CHECK(ret == 0);
  21025. #endif
  21026. return res;
  21027. } /* END test_wc_ed448_init */
  21028. /*
  21029. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  21030. */
  21031. static int test_wc_ed448_sign_msg(void)
  21032. {
  21033. int res = TEST_SKIPPED;
  21034. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  21035. WC_RNG rng;
  21036. ed448_key key;
  21037. byte msg[] = "Everybody gets Friday off.\n";
  21038. byte sig[ED448_SIG_SIZE];
  21039. word32 msglen = sizeof(msg);
  21040. word32 siglen = sizeof(sig);
  21041. word32 badSigLen = sizeof(sig) - 1;
  21042. #ifdef HAVE_ED448_VERIFY
  21043. int verify_ok = 0; /*1 = Verify success.*/
  21044. #endif
  21045. int ret;
  21046. /* Initialize stack variables. */
  21047. XMEMSET(sig, 0, siglen);
  21048. /* Initialize key. */
  21049. ret = wc_InitRng(&rng);
  21050. if (ret == 0) {
  21051. ret = wc_ed448_init(&key);
  21052. if (ret == 0) {
  21053. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21054. }
  21055. }
  21056. if (ret == 0) {
  21057. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  21058. }
  21059. /* Test bad args. */
  21060. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  21061. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  21062. if (ret == BAD_FUNC_ARG) {
  21063. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  21064. }
  21065. if (ret == BAD_FUNC_ARG) {
  21066. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  21067. }
  21068. if (ret == BAD_FUNC_ARG) {
  21069. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  21070. }
  21071. if (ret == BAD_FUNC_ARG) {
  21072. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  21073. NULL, 0);
  21074. }
  21075. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  21076. badSigLen -= 1;
  21077. ret = 0;
  21078. }
  21079. else if (ret == 0) {
  21080. ret = WOLFSSL_FATAL_ERROR;
  21081. }
  21082. } /* END sign */
  21083. #ifdef HAVE_ED448_VERIFY
  21084. if (ret == 0) {
  21085. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  21086. &key, NULL, 0);
  21087. if (ret == 0 && verify_ok == 1) {
  21088. ret = 0;
  21089. }
  21090. else if (ret == 0) {
  21091. ret = WOLFSSL_FATAL_ERROR;
  21092. }
  21093. /* Test bad args. */
  21094. if (ret == 0) {
  21095. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  21096. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  21097. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  21098. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  21099. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  21100. &key, NULL, 0);
  21101. if (ret == BAD_FUNC_ARG) {
  21102. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  21103. &verify_ok, &key, NULL, 0);
  21104. }
  21105. if (ret == BAD_FUNC_ARG) {
  21106. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  21107. NULL, &key, NULL, 0);
  21108. }
  21109. if (ret == BAD_FUNC_ARG) {
  21110. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  21111. &verify_ok, NULL, NULL, 0);
  21112. }
  21113. if (ret == BAD_FUNC_ARG) {
  21114. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  21115. &verify_ok, &key, NULL, 0);
  21116. }
  21117. if (ret == BAD_FUNC_ARG) {
  21118. ret = 0;
  21119. }
  21120. else if (ret == 0) {
  21121. ret = WOLFSSL_FATAL_ERROR;
  21122. }
  21123. }
  21124. } /* END verify. */
  21125. #endif /* Verify. */
  21126. if (wc_FreeRng(&rng) && ret == 0) {
  21127. ret = WOLFSSL_FATAL_ERROR;
  21128. }
  21129. wc_ed448_free(&key);
  21130. res = TEST_RES_CHECK(ret == 0);
  21131. #endif
  21132. return res;
  21133. } /* END test_wc_ed448_sign_msg */
  21134. /*
  21135. * Testing wc_ed448_import_public()
  21136. */
  21137. static int test_wc_ed448_import_public(void)
  21138. {
  21139. int res = TEST_SKIPPED;
  21140. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21141. WC_RNG rng;
  21142. ed448_key pubKey;
  21143. const byte in[] =
  21144. "Ed448PublicKeyUnitTest.................................\n";
  21145. word32 inlen = sizeof(in);
  21146. int ret = 0;
  21147. ret = wc_InitRng(&rng);
  21148. if (ret == 0) {
  21149. ret = wc_ed448_init(&pubKey);
  21150. if (ret == 0) {
  21151. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  21152. }
  21153. }
  21154. if (ret == 0) {
  21155. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  21156. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  21157. ret = 0;
  21158. }
  21159. else {
  21160. ret = WOLFSSL_FATAL_ERROR;
  21161. }
  21162. /* Test bad args. */
  21163. if (ret == 0) {
  21164. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  21165. if (ret == BAD_FUNC_ARG) {
  21166. ret = wc_ed448_import_public(in, inlen, NULL);
  21167. }
  21168. if (ret == BAD_FUNC_ARG) {
  21169. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  21170. }
  21171. if (ret == BAD_FUNC_ARG) {
  21172. ret = 0;
  21173. }
  21174. else if (ret == 0) {
  21175. ret = WOLFSSL_FATAL_ERROR;
  21176. }
  21177. }
  21178. }
  21179. if (wc_FreeRng(&rng) && ret == 0) {
  21180. ret = WOLFSSL_FATAL_ERROR;
  21181. }
  21182. wc_ed448_free(&pubKey);
  21183. res = TEST_RES_CHECK(ret == 0);
  21184. #endif
  21185. return res;
  21186. } /* END wc_ed448_import_public */
  21187. /*
  21188. * Testing wc_ed448_import_private_key()
  21189. */
  21190. static int test_wc_ed448_import_private_key(void)
  21191. {
  21192. int res = TEST_SKIPPED;
  21193. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21194. WC_RNG rng;
  21195. ed448_key key;
  21196. const byte privKey[] =
  21197. "Ed448PrivateKeyUnitTest................................\n";
  21198. const byte pubKey[] =
  21199. "Ed448PublicKeyUnitTest.................................\n";
  21200. word32 privKeySz = sizeof(privKey);
  21201. word32 pubKeySz = sizeof(pubKey);
  21202. #ifdef HAVE_ED448_KEY_EXPORT
  21203. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  21204. word32 bothKeysSz = sizeof(bothKeys);
  21205. #endif
  21206. int ret;
  21207. ret = wc_InitRng(&rng);
  21208. if (ret != 0) {
  21209. return TEST_FAIL;
  21210. }
  21211. ret = wc_ed448_init(&key);
  21212. if (ret != 0) {
  21213. wc_FreeRng(&rng);
  21214. return TEST_FAIL;
  21215. }
  21216. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21217. if (ret == 0) {
  21218. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  21219. pubKeySz, &key, 1);
  21220. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  21221. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  21222. ret = WOLFSSL_FATAL_ERROR;
  21223. }
  21224. }
  21225. #ifdef HAVE_ED448_KEY_EXPORT
  21226. if (ret == 0)
  21227. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  21228. if (ret == 0) {
  21229. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  21230. &key, 1);
  21231. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  21232. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  21233. ret = WOLFSSL_FATAL_ERROR;
  21234. }
  21235. }
  21236. #endif
  21237. /* Test bad args. */
  21238. if (ret == 0) {
  21239. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  21240. &key);
  21241. if (ret == BAD_FUNC_ARG) {
  21242. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  21243. pubKeySz, &key);
  21244. }
  21245. if (ret == BAD_FUNC_ARG) {
  21246. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21247. pubKeySz, NULL);
  21248. }
  21249. if (ret == BAD_FUNC_ARG) {
  21250. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  21251. pubKeySz, &key);
  21252. }
  21253. if (ret == BAD_FUNC_ARG) {
  21254. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21255. pubKeySz - 1, &key);
  21256. }
  21257. if (ret == BAD_FUNC_ARG) {
  21258. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  21259. 0, &key);
  21260. }
  21261. if (ret == BAD_FUNC_ARG) {
  21262. ret = 0;
  21263. }
  21264. else if (ret == 0) {
  21265. ret = WOLFSSL_FATAL_ERROR;
  21266. }
  21267. }
  21268. if (wc_FreeRng(&rng) && ret == 0) {
  21269. ret = WOLFSSL_FATAL_ERROR;
  21270. }
  21271. wc_ed448_free(&key);
  21272. res = TEST_RES_CHECK(ret == 0);
  21273. #endif
  21274. return res;
  21275. } /* END test_wc_ed448_import_private_key */
  21276. /*
  21277. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  21278. */
  21279. static int test_wc_ed448_export(void)
  21280. {
  21281. int res = TEST_SKIPPED;
  21282. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21283. WC_RNG rng;
  21284. ed448_key key;
  21285. byte priv[ED448_PRV_KEY_SIZE];
  21286. byte pub[ED448_PUB_KEY_SIZE];
  21287. word32 privSz = sizeof(priv);
  21288. word32 pubSz = sizeof(pub);
  21289. int ret;
  21290. ret = wc_InitRng(&rng);
  21291. if (ret != 0) {
  21292. return TEST_FAIL;
  21293. }
  21294. ret = wc_ed448_init(&key);
  21295. if (ret != 0) {
  21296. wc_FreeRng(&rng);
  21297. return TEST_FAIL;
  21298. }
  21299. if (ret == 0) {
  21300. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21301. }
  21302. if (ret == 0) {
  21303. ret = wc_ed448_export_public(&key, pub, &pubSz);
  21304. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  21305. XMEMCMP(key.p, pub, pubSz) != 0)) {
  21306. ret = WOLFSSL_FATAL_ERROR;
  21307. }
  21308. if (ret == 0) {
  21309. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  21310. if (ret == BAD_FUNC_ARG) {
  21311. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  21312. }
  21313. if (ret == BAD_FUNC_ARG) {
  21314. ret = wc_ed448_export_public(&key, pub, NULL);
  21315. }
  21316. if (ret == BAD_FUNC_ARG) {
  21317. ret = 0;
  21318. }
  21319. else if (ret == 0) {
  21320. ret = WOLFSSL_FATAL_ERROR;
  21321. }
  21322. }
  21323. }
  21324. if (ret == 0) {
  21325. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  21326. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  21327. XMEMCMP(key.k, priv, privSz) != 0)) {
  21328. ret = WOLFSSL_FATAL_ERROR;
  21329. }
  21330. if (ret == 0) {
  21331. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  21332. if (ret == BAD_FUNC_ARG) {
  21333. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  21334. }
  21335. if (ret == BAD_FUNC_ARG) {
  21336. ret = wc_ed448_export_private_only(&key, priv, NULL);
  21337. }
  21338. if (ret == BAD_FUNC_ARG) {
  21339. ret = 0;
  21340. }
  21341. else if (ret == 0) {
  21342. ret = WOLFSSL_FATAL_ERROR;
  21343. }
  21344. }
  21345. }
  21346. if (wc_FreeRng(&rng) && ret == 0) {
  21347. ret = WOLFSSL_FATAL_ERROR;
  21348. }
  21349. wc_ed448_free(&key);
  21350. res = TEST_RES_CHECK(ret == 0);
  21351. #endif
  21352. return res;
  21353. } /* END test_wc_ed448_export */
  21354. /*
  21355. * Testing wc_ed448_size()
  21356. */
  21357. static int test_wc_ed448_size(void)
  21358. {
  21359. int res = TEST_SKIPPED;
  21360. #if defined(HAVE_ED448)
  21361. WC_RNG rng;
  21362. ed448_key key;
  21363. int ret = 0;
  21364. ret = wc_InitRng(&rng);
  21365. if (ret != 0) {
  21366. return TEST_FAIL;
  21367. }
  21368. ret = wc_ed448_init(&key);
  21369. if (ret != 0) {
  21370. wc_FreeRng(&rng);
  21371. return TEST_FAIL;
  21372. }
  21373. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21374. if (ret != 0) {
  21375. wc_FreeRng(&rng);
  21376. wc_ed448_free(&key);
  21377. return TEST_FAIL;
  21378. }
  21379. ret = wc_ed448_size(&key);
  21380. /* Test bad args. */
  21381. if (ret == ED448_KEY_SIZE) {
  21382. ret = wc_ed448_size(NULL);
  21383. if (ret == BAD_FUNC_ARG) {
  21384. ret = 0;
  21385. }
  21386. }
  21387. if (ret == 0) {
  21388. ret = wc_ed448_sig_size(&key);
  21389. if (ret == ED448_SIG_SIZE) {
  21390. ret = 0;
  21391. }
  21392. /* Test bad args. */
  21393. if (ret == 0) {
  21394. ret = wc_ed448_sig_size(NULL);
  21395. if (ret == BAD_FUNC_ARG) {
  21396. ret = 0;
  21397. }
  21398. }
  21399. } /* END wc_ed448_sig_size() */
  21400. if (ret == 0) {
  21401. ret = wc_ed448_pub_size(&key);
  21402. if (ret == ED448_PUB_KEY_SIZE) {
  21403. ret = 0;
  21404. }
  21405. if (ret == 0) {
  21406. ret = wc_ed448_pub_size(NULL);
  21407. if (ret == BAD_FUNC_ARG) {
  21408. ret = 0;
  21409. }
  21410. }
  21411. } /* END wc_ed448_pub_size */
  21412. if (ret == 0) {
  21413. ret = wc_ed448_priv_size(&key);
  21414. if (ret == ED448_PRV_KEY_SIZE) {
  21415. ret = 0;
  21416. }
  21417. if (ret == 0) {
  21418. ret = wc_ed448_priv_size(NULL);
  21419. if (ret == BAD_FUNC_ARG) {
  21420. ret = 0;
  21421. }
  21422. }
  21423. } /* END wc_ed448_pub_size */
  21424. if (wc_FreeRng(&rng) && ret == 0) {
  21425. ret = WOLFSSL_FATAL_ERROR;
  21426. }
  21427. wc_ed448_free(&key);
  21428. res = TEST_RES_CHECK(ret == 0);
  21429. #endif
  21430. return res;
  21431. } /* END test_wc_ed448_size */
  21432. /*
  21433. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21434. */
  21435. static int test_wc_ed448_exportKey(void)
  21436. {
  21437. int res = TEST_SKIPPED;
  21438. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21439. WC_RNG rng;
  21440. ed448_key key;
  21441. byte priv[ED448_PRV_KEY_SIZE];
  21442. byte pub[ED448_PUB_KEY_SIZE];
  21443. byte privOnly[ED448_PRV_KEY_SIZE];
  21444. word32 privSz = sizeof(priv);
  21445. word32 pubSz = sizeof(pub);
  21446. word32 privOnlySz = sizeof(privOnly);
  21447. int ret;
  21448. ret = wc_InitRng(&rng);
  21449. if (ret != 0) {
  21450. return TEST_FAIL;
  21451. }
  21452. ret = wc_ed448_init(&key);
  21453. if (ret != 0) {
  21454. wc_FreeRng(&rng);
  21455. return TEST_FAIL;
  21456. }
  21457. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21458. if (ret != 0) {
  21459. wc_FreeRng(&rng);
  21460. wc_ed448_free(&key);
  21461. return TEST_FAIL;
  21462. }
  21463. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  21464. if (ret == 0) {
  21465. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  21466. if (ret == BAD_FUNC_ARG) {
  21467. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  21468. }
  21469. if (ret == BAD_FUNC_ARG) {
  21470. ret = wc_ed448_export_private(&key, privOnly, NULL);
  21471. }
  21472. if (ret == BAD_FUNC_ARG) {
  21473. ret = 0;
  21474. }
  21475. else if (ret == 0) {
  21476. ret = WOLFSSL_FATAL_ERROR;
  21477. }
  21478. }
  21479. if (ret == 0) {
  21480. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  21481. if (ret == 0) {
  21482. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  21483. if (ret == BAD_FUNC_ARG) {
  21484. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  21485. }
  21486. if (ret == BAD_FUNC_ARG) {
  21487. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  21488. }
  21489. if (ret == BAD_FUNC_ARG) {
  21490. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  21491. }
  21492. if (ret == BAD_FUNC_ARG) {
  21493. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  21494. }
  21495. if (ret == BAD_FUNC_ARG) {
  21496. ret = 0;
  21497. }
  21498. else if (ret == 0) {
  21499. ret = WOLFSSL_FATAL_ERROR;
  21500. }
  21501. }
  21502. } /* END wc_ed448_export_key() */
  21503. /* Cross check output. */
  21504. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  21505. ret = WOLFSSL_FATAL_ERROR;
  21506. }
  21507. if (wc_FreeRng(&rng) && ret == 0) {
  21508. ret = WOLFSSL_FATAL_ERROR;
  21509. }
  21510. wc_ed448_free(&key);
  21511. res = TEST_RES_CHECK(ret == 0);
  21512. #endif
  21513. return res;
  21514. } /* END test_wc_ed448_exportKey */
  21515. /*
  21516. * Testing wc_Ed448PublicKeyToDer
  21517. */
  21518. static int test_wc_Ed448PublicKeyToDer(void)
  21519. {
  21520. int res = TEST_SKIPPED;
  21521. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21522. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21523. int tmp;
  21524. ed448_key key;
  21525. byte derBuf[1024];
  21526. int ret = 0;
  21527. /* Test bad args */
  21528. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  21529. if (tmp != BAD_FUNC_ARG) {
  21530. ret = WOLFSSL_FATAL_ERROR;
  21531. }
  21532. if (ret == 0) {
  21533. wc_ed448_init(&key);
  21534. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  21535. if (tmp != BUFFER_E) {
  21536. ret = WOLFSSL_FATAL_ERROR;
  21537. }
  21538. wc_ed448_free(&key);
  21539. }
  21540. /* Test good args */
  21541. if (ret == 0) {
  21542. WC_RNG rng;
  21543. ret = wc_InitRng(&rng);
  21544. if (ret != 0) {
  21545. return TEST_FAIL;
  21546. }
  21547. ret = wc_ed448_init(&key);
  21548. if (ret != 0) {
  21549. wc_FreeRng(&rng);
  21550. return TEST_FAIL;
  21551. }
  21552. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21553. if (ret != 0) {
  21554. wc_FreeRng(&rng);
  21555. wc_ed448_free(&key);
  21556. return TEST_FAIL;
  21557. }
  21558. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  21559. if (tmp <= 0) {
  21560. ret = WOLFSSL_FATAL_ERROR;
  21561. }
  21562. wc_FreeRng(&rng);
  21563. wc_ed448_free(&key);
  21564. }
  21565. res = TEST_RES_CHECK(ret == 0);
  21566. #endif
  21567. return res;
  21568. } /* END testing wc_Ed448PublicKeyToDer */
  21569. /*
  21570. * Testing wc_curve448_init and wc_curve448_free.
  21571. */
  21572. static int test_wc_curve448_init(void)
  21573. {
  21574. int res = TEST_SKIPPED;
  21575. #if defined(HAVE_CURVE448)
  21576. curve448_key key;
  21577. int ret = 0;
  21578. ret = wc_curve448_init(&key);
  21579. /* Test bad args for wc_curve448_init */
  21580. if (ret == 0) {
  21581. ret = wc_curve448_init(NULL);
  21582. if (ret == BAD_FUNC_ARG) {
  21583. ret = 0;
  21584. }
  21585. else if (ret == 0) {
  21586. ret = WOLFSSL_FATAL_ERROR;
  21587. }
  21588. }
  21589. /* Test good args for wc_curve_448_free */
  21590. wc_curve448_free(&key);
  21591. wc_curve448_free(NULL);
  21592. res = TEST_RES_CHECK(ret == 0);
  21593. #endif
  21594. return res;
  21595. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21596. /*
  21597. * Testing wc_curve448_make_key
  21598. */
  21599. static int test_wc_curve448_make_key(void)
  21600. {
  21601. int res = TEST_SKIPPED;
  21602. #if defined(HAVE_CURVE448)
  21603. WC_RNG rng;
  21604. curve448_key key;
  21605. int keysize;
  21606. int ret;
  21607. ret = wc_curve448_init(&key);
  21608. if (ret == 0) {
  21609. ret = wc_InitRng(&rng);
  21610. }
  21611. if (ret == 0) {
  21612. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21613. if (ret == 0) {
  21614. keysize = wc_curve448_size(&key);
  21615. if (keysize != CURVE448_KEY_SIZE) {
  21616. ret = WOLFSSL_FATAL_ERROR;
  21617. }
  21618. }
  21619. if (ret == 0) {
  21620. ret = wc_curve448_make_key(&rng, keysize, &key);
  21621. }
  21622. }
  21623. /* test bad cases */
  21624. if (ret == 0) {
  21625. ret = wc_curve448_make_key(NULL, 0, NULL);
  21626. if (ret == BAD_FUNC_ARG) {
  21627. ret = 0;
  21628. }
  21629. }
  21630. if (ret == 0) {
  21631. ret = wc_curve448_make_key(&rng, keysize, NULL);
  21632. if (ret == BAD_FUNC_ARG) {
  21633. ret = 0;
  21634. }
  21635. }
  21636. if (ret == 0) {
  21637. ret = wc_curve448_make_key(NULL, keysize, &key);
  21638. if (ret == BAD_FUNC_ARG) {
  21639. ret = 0;
  21640. }
  21641. }
  21642. if (ret == 0) {
  21643. ret = wc_curve448_make_key(&rng, 0, &key);
  21644. if (ret == ECC_BAD_ARG_E) {
  21645. ret = 0;
  21646. }
  21647. }
  21648. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21649. ret = WOLFSSL_FATAL_ERROR;
  21650. }
  21651. wc_curve448_free(&key);
  21652. res = TEST_RES_CHECK(ret == 0);
  21653. #endif
  21654. return res;
  21655. } /*END test_wc_curve448_make_key*/
  21656. /*
  21657. * Testing test_wc_curve448_shared_secret_ex
  21658. */
  21659. static int test_wc_curve448_shared_secret_ex(void)
  21660. {
  21661. int res = TEST_SKIPPED;
  21662. #if defined(HAVE_CURVE448)
  21663. WC_RNG rng;
  21664. curve448_key private_key, public_key;
  21665. byte out[CURVE448_KEY_SIZE];
  21666. word32 outLen = sizeof(out);
  21667. int endian = EC448_BIG_ENDIAN;
  21668. int ret;
  21669. ret = wc_curve448_init(&private_key);
  21670. if (ret == 0) {
  21671. ret = wc_InitRng(&rng);
  21672. if (ret == 0) {
  21673. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  21674. }
  21675. }
  21676. if (ret == 0) {
  21677. ret = wc_curve448_init(&public_key);
  21678. }
  21679. if (ret == 0) {
  21680. if (ret == 0) {
  21681. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  21682. }
  21683. }
  21684. if (ret == 0) {
  21685. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21686. &outLen, endian);
  21687. }
  21688. /* test bad cases */
  21689. if (ret == 0) {
  21690. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian);
  21691. if (ret == BAD_FUNC_ARG) {
  21692. ret = 0;
  21693. }
  21694. }
  21695. if (ret == 0) {
  21696. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  21697. &outLen, endian);
  21698. if (ret == BAD_FUNC_ARG) {
  21699. ret = 0;
  21700. }
  21701. }
  21702. if (ret == 0) {
  21703. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  21704. &outLen, endian);
  21705. if (ret == BAD_FUNC_ARG) {
  21706. ret = 0;
  21707. }
  21708. }
  21709. if (ret == 0) {
  21710. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21711. &outLen, endian);
  21712. if (ret == BAD_FUNC_ARG) {
  21713. ret = 0;
  21714. }
  21715. }
  21716. if (ret == 0) {
  21717. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21718. NULL, endian);
  21719. if (ret == BAD_FUNC_ARG) {
  21720. ret = 0;
  21721. }
  21722. }
  21723. outLen = outLen - 2;
  21724. if (ret == 0) {
  21725. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21726. &outLen, endian);
  21727. if (ret == BAD_FUNC_ARG) {
  21728. ret = 0;
  21729. }
  21730. }
  21731. wc_curve448_free(&private_key);
  21732. wc_curve448_free(&public_key);
  21733. wc_FreeRng(&rng);
  21734. res = TEST_RES_CHECK(ret == 0);
  21735. #endif
  21736. return res;
  21737. } /*END test_wc_curve448_shared_secret_ex*/
  21738. /*
  21739. * Testing test_wc_curve448_export_public_ex
  21740. */
  21741. static int test_wc_curve448_export_public_ex(void)
  21742. {
  21743. int res = TEST_SKIPPED;
  21744. #if defined(HAVE_CURVE448)
  21745. WC_RNG rng;
  21746. curve448_key key;
  21747. byte out[CURVE448_KEY_SIZE];
  21748. word32 outLen = sizeof(out);
  21749. int endian = EC448_BIG_ENDIAN;
  21750. int ret;
  21751. ret = wc_curve448_init(&key);
  21752. if (ret == 0) {
  21753. ret = wc_InitRng(&rng);
  21754. }
  21755. if (ret == 0) {
  21756. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21757. if (ret == 0) {
  21758. ret = wc_curve448_export_public(&key, out, &outLen);
  21759. }
  21760. if (ret == 0) {
  21761. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21762. }
  21763. }
  21764. /*test bad cases*/
  21765. if (ret == 0) {
  21766. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  21767. if (ret == BAD_FUNC_ARG) {
  21768. ret = 0;
  21769. }
  21770. }
  21771. if (ret == 0) {
  21772. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  21773. if (ret == BAD_FUNC_ARG) {
  21774. ret = 0;
  21775. }
  21776. }
  21777. if (ret == 0) {
  21778. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  21779. if (ret == BAD_FUNC_ARG) {
  21780. ret = 0;
  21781. }
  21782. }
  21783. if (ret == 0) {
  21784. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  21785. if (ret == BAD_FUNC_ARG) {
  21786. ret = 0;
  21787. }
  21788. }
  21789. outLen = outLen - 2;
  21790. if (ret == 0) {
  21791. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21792. if (ret == ECC_BAD_ARG_E) {
  21793. ret = 0;
  21794. }
  21795. }
  21796. wc_curve448_free(&key);
  21797. wc_FreeRng(&rng);
  21798. res = TEST_RES_CHECK(ret == 0);
  21799. #endif
  21800. return res;
  21801. } /*END test_wc_curve448_export_public_ex*/
  21802. /*
  21803. * Testing test_wc_curve448_export_private_raw_ex
  21804. */
  21805. static int test_wc_curve448_export_private_raw_ex(void)
  21806. {
  21807. int res = TEST_SKIPPED;
  21808. #if defined(HAVE_CURVE448)
  21809. curve448_key key;
  21810. byte out[CURVE448_KEY_SIZE];
  21811. word32 outLen = sizeof(out);
  21812. int endian = EC448_BIG_ENDIAN;
  21813. int ret;
  21814. ret = wc_curve448_init(&key);
  21815. if (ret == 0) {
  21816. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21817. }
  21818. /*test bad cases*/
  21819. if (ret == 0) {
  21820. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  21821. if (ret == BAD_FUNC_ARG) {
  21822. ret = 0;
  21823. }
  21824. }
  21825. if (ret == 0) {
  21826. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  21827. if (ret == BAD_FUNC_ARG) {
  21828. ret = 0;
  21829. }
  21830. }
  21831. if (ret == 0) {
  21832. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  21833. if (ret == BAD_FUNC_ARG) {
  21834. ret = 0;
  21835. }
  21836. }
  21837. if (ret == 0) {
  21838. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  21839. if (ret == BAD_FUNC_ARG) {
  21840. ret = 0;
  21841. }
  21842. }
  21843. if (ret == 0) {
  21844. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21845. EC448_LITTLE_ENDIAN);
  21846. }
  21847. outLen = outLen - 2;
  21848. if (ret == 0) {
  21849. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21850. if (ret == ECC_BAD_ARG_E) {
  21851. ret = 0;
  21852. }
  21853. }
  21854. wc_curve448_free(&key);
  21855. res = TEST_RES_CHECK(ret == 0);
  21856. #endif
  21857. return res;
  21858. }/*END test_wc_curve448_export_private_raw_ex*/
  21859. /*
  21860. * Testing test_wc_curve448_import_private_raw_ex
  21861. */
  21862. static int test_wc_curve448_import_private_raw_ex(void)
  21863. {
  21864. int res = TEST_SKIPPED;
  21865. #if defined(HAVE_CURVE448)
  21866. WC_RNG rng;
  21867. curve448_key key;
  21868. byte priv[CURVE448_KEY_SIZE];
  21869. byte pub[CURVE448_KEY_SIZE];
  21870. word32 privSz = sizeof(priv);
  21871. word32 pubSz = sizeof(pub);
  21872. int endian = EC448_BIG_ENDIAN;
  21873. int ret;
  21874. ret = wc_curve448_init(&key);
  21875. if (ret == 0) {
  21876. ret = wc_InitRng(&rng);
  21877. }
  21878. if (ret == 0) {
  21879. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21880. if (ret == 0) {
  21881. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21882. }
  21883. if (ret == 0) {
  21884. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21885. }
  21886. if (ret == 0) {
  21887. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21888. &key, endian);
  21889. }
  21890. }
  21891. /* test bad cases */
  21892. if (ret == 0) {
  21893. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  21894. if (ret == BAD_FUNC_ARG) {
  21895. ret = 0;
  21896. }
  21897. }
  21898. if (ret == 0) {
  21899. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21900. &key, endian);
  21901. if (ret == BAD_FUNC_ARG) {
  21902. ret = 0;
  21903. }
  21904. }
  21905. if (ret == 0) {
  21906. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21907. &key, endian);
  21908. if (ret == BAD_FUNC_ARG) {
  21909. ret = 0;
  21910. }
  21911. }
  21912. if (ret == 0) {
  21913. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21914. NULL, endian);
  21915. if (ret == BAD_FUNC_ARG) {
  21916. ret = 0;
  21917. }
  21918. }
  21919. if (ret == 0) {
  21920. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21921. &key, endian);
  21922. if (ret == ECC_BAD_ARG_E) {
  21923. ret = 0;
  21924. }
  21925. }
  21926. if (ret == 0) {
  21927. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21928. &key, endian);
  21929. if (ret == ECC_BAD_ARG_E) {
  21930. ret = 0;
  21931. }
  21932. }
  21933. if (ret == 0) {
  21934. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21935. &key, EC448_LITTLE_ENDIAN);
  21936. }
  21937. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21938. ret = WOLFSSL_FATAL_ERROR;
  21939. }
  21940. wc_curve448_free(&key);
  21941. res = TEST_RES_CHECK(ret == 0);
  21942. #endif
  21943. return res;
  21944. } /*END test_wc_curve448_import_private_raw_ex*/
  21945. /*
  21946. * Testing test_curve448_export_key_raw
  21947. */
  21948. static int test_wc_curve448_export_key_raw(void)
  21949. {
  21950. int res = TEST_SKIPPED;
  21951. #if defined(HAVE_CURVE448)
  21952. WC_RNG rng;
  21953. curve448_key key;
  21954. byte priv[CURVE448_KEY_SIZE];
  21955. byte pub[CURVE448_KEY_SIZE];
  21956. word32 privSz = sizeof(priv);
  21957. word32 pubSz = sizeof(pub);
  21958. int ret;
  21959. ret = wc_curve448_init(&key);
  21960. if (ret == 0) {
  21961. ret = wc_InitRng(&rng);
  21962. }
  21963. if (ret == 0) {
  21964. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21965. if (ret == 0) {
  21966. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21967. }
  21968. if (ret == 0) {
  21969. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21970. }
  21971. if (ret == 0) {
  21972. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  21973. }
  21974. }
  21975. wc_curve448_free(&key);
  21976. wc_FreeRng(&rng);
  21977. res = TEST_RES_CHECK(ret == 0);
  21978. #endif
  21979. return res;
  21980. }/*END test_wc_curve448_import_private_raw_ex*/
  21981. /*
  21982. * Testing test_wc_curve448_import_private
  21983. */
  21984. static int test_wc_curve448_import_private(void)
  21985. {
  21986. int res = TEST_SKIPPED;
  21987. #if defined(HAVE_CURVE448)
  21988. curve448_key key;
  21989. WC_RNG rng;
  21990. byte priv[CURVE448_KEY_SIZE];
  21991. word32 privSz = sizeof(priv);
  21992. int ret;
  21993. ret = wc_curve448_init(&key);
  21994. if (ret == 0) {
  21995. ret = wc_InitRng(&rng);
  21996. }
  21997. if (ret == 0) {
  21998. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21999. if (ret == 0) {
  22000. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  22001. }
  22002. }
  22003. if (ret == 0) {
  22004. ret = wc_curve448_import_private(priv, privSz, &key);
  22005. }
  22006. wc_curve448_free(&key);
  22007. wc_FreeRng(&rng);
  22008. res = TEST_RES_CHECK(ret == 0);
  22009. #endif
  22010. return res;
  22011. } /*END test_wc_curve448_import*/
  22012. /*
  22013. * Testing test_wc_curve448_size.
  22014. */
  22015. static int test_wc_curve448_size(void)
  22016. {
  22017. int res = TEST_SKIPPED;
  22018. #if defined(HAVE_CURVE448)
  22019. curve448_key key;
  22020. int ret = 0;
  22021. ret = wc_curve448_init(&key);
  22022. /* Test good args for wc_curve448_size */
  22023. if (ret == 0) {
  22024. ret = wc_curve448_size(&key);
  22025. }
  22026. /* Test bad args for wc_curve448_size */
  22027. if (ret != 0) {
  22028. ret = wc_curve448_size(NULL);
  22029. }
  22030. wc_curve448_free(&key);
  22031. res = TEST_RES_CHECK(ret == 0);
  22032. #endif
  22033. return res;
  22034. } /* END test_wc_curve448_size*/
  22035. /*
  22036. * Testing wc_ecc_make_key.
  22037. */
  22038. static int test_wc_ecc_make_key(void)
  22039. {
  22040. int res = TEST_SKIPPED;
  22041. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22042. WC_RNG rng;
  22043. ecc_key key;
  22044. int ret;
  22045. ret = wc_InitRng(&rng);
  22046. if (ret != 0)
  22047. return TEST_FAIL;
  22048. ret = wc_ecc_init(&key);
  22049. if (ret == 0) {
  22050. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22051. #if defined(WOLFSSL_ASYNC_CRYPT)
  22052. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22053. #endif
  22054. /* Pass in bad args. */
  22055. if (ret == 0) {
  22056. ret = wc_ecc_make_key(NULL, KEY14, &key);
  22057. if (ret == BAD_FUNC_ARG) {
  22058. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  22059. }
  22060. if (ret == BAD_FUNC_ARG) {
  22061. ret = 0;
  22062. }
  22063. else if (ret == 0) {
  22064. ret = WOLFSSL_FATAL_ERROR;
  22065. }
  22066. }
  22067. wc_ecc_free(&key);
  22068. }
  22069. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  22070. ret = WOLFSSL_FATAL_ERROR;
  22071. }
  22072. #ifdef FP_ECC
  22073. wc_ecc_fp_free();
  22074. #endif
  22075. res = TEST_RES_CHECK(ret == 0);
  22076. #endif
  22077. return res;
  22078. } /* END test_wc_ecc_make_key */
  22079. /*
  22080. * Testing wc_ecc_init()
  22081. */
  22082. static int test_wc_ecc_init(void)
  22083. {
  22084. int res = TEST_SKIPPED;
  22085. #ifdef HAVE_ECC
  22086. ecc_key key;
  22087. int ret;
  22088. ret = wc_ecc_init(&key);
  22089. /* Pass in bad args. */
  22090. if (ret == 0) {
  22091. ret = wc_ecc_init(NULL);
  22092. if (ret == BAD_FUNC_ARG) {
  22093. ret = 0;
  22094. }
  22095. else if (ret == 0) {
  22096. ret = WOLFSSL_FATAL_ERROR;
  22097. }
  22098. }
  22099. wc_ecc_free(&key);
  22100. res = TEST_RES_CHECK(ret == 0);
  22101. #endif
  22102. return res;
  22103. } /* END test_wc_ecc_init */
  22104. /*
  22105. * Testing wc_ecc_check_key()
  22106. */
  22107. static int test_wc_ecc_check_key(void)
  22108. {
  22109. int res = TEST_SKIPPED;
  22110. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22111. WC_RNG rng;
  22112. ecc_key key;
  22113. int ret;
  22114. XMEMSET(&rng, 0, sizeof(rng));
  22115. XMEMSET(&key, 0, sizeof(key));
  22116. ret = wc_InitRng(&rng);
  22117. if (ret == 0) {
  22118. ret = wc_ecc_init(&key);
  22119. if (ret == 0) {
  22120. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22121. #if defined(WOLFSSL_ASYNC_CRYPT)
  22122. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22123. #endif
  22124. }
  22125. }
  22126. if (ret == 0) {
  22127. ret = wc_ecc_check_key(&key);
  22128. }
  22129. /* Pass in bad args. */
  22130. if (ret == 0) {
  22131. ret = wc_ecc_check_key(NULL);
  22132. if (ret == BAD_FUNC_ARG) {
  22133. ret = 0;
  22134. }
  22135. else if (ret == 0) {
  22136. ret = WOLFSSL_FATAL_ERROR;
  22137. }
  22138. }
  22139. if (wc_FreeRng(&rng) && ret == 0) {
  22140. ret = WOLFSSL_FATAL_ERROR;
  22141. }
  22142. wc_ecc_free(&key);
  22143. #ifdef FP_ECC
  22144. wc_ecc_fp_free();
  22145. #endif
  22146. res = TEST_RES_CHECK(ret == 0);
  22147. #endif
  22148. return res;
  22149. } /* END test_wc_ecc_check_key */
  22150. /*
  22151. * Testing wc_ecc_get_generator()
  22152. */
  22153. static int test_wc_ecc_get_generator(void)
  22154. {
  22155. int res = TEST_SKIPPED;
  22156. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  22157. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  22158. ecc_point* pt;
  22159. int ret = 0;
  22160. pt = wc_ecc_new_point();
  22161. if (!pt) {
  22162. ret = WOLFSSL_FATAL_ERROR;
  22163. }
  22164. if (ret == 0) {
  22165. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  22166. }
  22167. /* Test bad args. */
  22168. if (ret == MP_OKAY) {
  22169. /* Returns Zero for bad arg. */
  22170. ret = wc_ecc_get_generator(pt, -1);
  22171. if (ret != MP_OKAY)
  22172. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  22173. if (ret != MP_OKAY)
  22174. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  22175. * increase this number */
  22176. if (ret != MP_OKAY)
  22177. wc_ecc_get_generator(NULL, -1);
  22178. ret = (ret == MP_OKAY) ? WOLFSSL_FATAL_ERROR : 0;
  22179. }
  22180. wc_ecc_del_point(pt);
  22181. res = TEST_RES_CHECK(ret == 0);
  22182. #endif
  22183. return res;
  22184. } /* END test_wc_ecc_get_generator */
  22185. /*
  22186. * Testing wc_ecc_size()
  22187. */
  22188. static int test_wc_ecc_size(void)
  22189. {
  22190. int res = TEST_SKIPPED;
  22191. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22192. WC_RNG rng;
  22193. ecc_key key;
  22194. int ret;
  22195. XMEMSET(&rng, 0, sizeof(rng));
  22196. XMEMSET(&key, 0, sizeof(key));
  22197. ret = wc_InitRng(&rng);
  22198. if (ret == 0) {
  22199. ret = wc_ecc_init(&key);
  22200. if (ret == 0) {
  22201. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22202. #if defined(WOLFSSL_ASYNC_CRYPT)
  22203. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22204. #endif
  22205. }
  22206. }
  22207. if (ret == 0) {
  22208. ret = wc_ecc_size(&key);
  22209. if (ret == KEY14) {
  22210. ret = 0;
  22211. }
  22212. else if (ret == 0) {
  22213. ret = WOLFSSL_FATAL_ERROR;
  22214. }
  22215. }
  22216. /* Test bad args. */
  22217. if (ret == 0) {
  22218. /* Returns Zero for bad arg. */
  22219. ret = wc_ecc_size(NULL);
  22220. }
  22221. if (wc_FreeRng(&rng) && ret == 0) {
  22222. ret = WOLFSSL_FATAL_ERROR;
  22223. }
  22224. wc_ecc_free(&key);
  22225. res = TEST_RES_CHECK(ret == 0);
  22226. #endif
  22227. return res;
  22228. } /* END test_wc_ecc_size */
  22229. static int test_wc_ecc_params(void)
  22230. {
  22231. int res = TEST_SKIPPED;
  22232. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  22233. It was added after certifications */
  22234. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  22235. const ecc_set_type* ecc_set;
  22236. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  22237. /* Test for SECP256R1 curve */
  22238. int curve_id = ECC_SECP256R1;
  22239. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  22240. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  22241. ecc_set = wc_ecc_get_curve_params(curve_idx);
  22242. AssertNotNull(ecc_set);
  22243. AssertIntEQ(ecc_set->id, curve_id);
  22244. #endif
  22245. /* Test case when SECP256R1 is not enabled */
  22246. /* Test that we get curve params for index 0 */
  22247. ecc_set = wc_ecc_get_curve_params(0);
  22248. AssertNotNull(ecc_set);
  22249. res = TEST_RES_CHECK(1);
  22250. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  22251. return res;
  22252. }
  22253. /*
  22254. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  22255. */
  22256. static int test_wc_ecc_signVerify_hash(void)
  22257. {
  22258. int res = TEST_SKIPPED;
  22259. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  22260. WC_RNG rng;
  22261. ecc_key key;
  22262. int ret;
  22263. int signH = WOLFSSL_FATAL_ERROR;
  22264. #ifdef HAVE_ECC_VERIFY
  22265. int verifyH = WOLFSSL_FATAL_ERROR;
  22266. int verify = 0;
  22267. #endif
  22268. word32 siglen = ECC_BUFSIZE;
  22269. byte sig[ECC_BUFSIZE];
  22270. byte adjustedSig[ECC_BUFSIZE+1];
  22271. byte digest[] = TEST_STRING;
  22272. word32 digestlen = (word32)TEST_STRING_SZ;
  22273. /* Init stack var */
  22274. XMEMSET(sig, 0, siglen);
  22275. XMEMSET(&key, 0, sizeof(key));
  22276. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  22277. /* Init structs. */
  22278. ret = wc_InitRng(&rng);
  22279. if (ret == 0) {
  22280. ret = wc_ecc_init(&key);
  22281. if (ret == 0) {
  22282. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22283. #if defined(WOLFSSL_ASYNC_CRYPT)
  22284. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22285. #endif
  22286. }
  22287. }
  22288. if (ret == 0) {
  22289. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  22290. }
  22291. /* Check bad args. */
  22292. if (ret == 0) {
  22293. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  22294. if (signH == ECC_BAD_ARG_E) {
  22295. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  22296. &rng, &key);
  22297. }
  22298. if (signH == ECC_BAD_ARG_E) {
  22299. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  22300. &rng, &key);
  22301. }
  22302. if (signH == ECC_BAD_ARG_E) {
  22303. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  22304. NULL, &key);
  22305. }
  22306. if (signH == ECC_BAD_ARG_E) {
  22307. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  22308. &rng, NULL);
  22309. }
  22310. if (signH == ECC_BAD_ARG_E) {
  22311. signH = 0;
  22312. }
  22313. else if (ret == 0) {
  22314. signH = WOLFSSL_FATAL_ERROR;
  22315. }
  22316. }
  22317. #ifdef HAVE_ECC_VERIFY
  22318. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  22319. if (verify != 1 && ret == 0) {
  22320. ret = WOLFSSL_FATAL_ERROR;
  22321. }
  22322. /* test check on length of signature passed in */
  22323. XMEMCPY(adjustedSig, sig, siglen);
  22324. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  22325. #ifndef NO_STRICT_ECDSA_LEN
  22326. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  22327. &verify, &key), 0);
  22328. #else
  22329. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  22330. * is allowed */
  22331. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  22332. &verify, &key), 0);
  22333. #endif
  22334. /* Test bad args. */
  22335. if (ret == 0) {
  22336. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  22337. &verify, &key);
  22338. if (verifyH == ECC_BAD_ARG_E) {
  22339. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  22340. &verify, &key);
  22341. }
  22342. if (verifyH == ECC_BAD_ARG_E) {
  22343. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  22344. NULL, &key);
  22345. }
  22346. if (verifyH == ECC_BAD_ARG_E) {
  22347. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  22348. &verify, NULL);
  22349. }
  22350. if (verifyH == ECC_BAD_ARG_E) {
  22351. verifyH = 0;
  22352. }
  22353. else if (ret == 0) {
  22354. verifyH = WOLFSSL_FATAL_ERROR;
  22355. }
  22356. }
  22357. #endif /* HAVE_ECC_VERIFY */
  22358. if (wc_FreeRng(&rng) && ret == 0) {
  22359. ret = WOLFSSL_FATAL_ERROR;
  22360. }
  22361. wc_ecc_free(&key);
  22362. #ifdef FP_ECC
  22363. wc_ecc_fp_free();
  22364. #endif
  22365. res = TEST_RES_CHECK(ret == 0 && signH == 0 && verifyH == 0);
  22366. #endif
  22367. return res;
  22368. } /* END test_wc_ecc_sign_hash */
  22369. /*
  22370. * Testing wc_ecc_shared_secret()
  22371. */
  22372. static int test_wc_ecc_shared_secret(void)
  22373. {
  22374. int res = TEST_SKIPPED;
  22375. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  22376. ecc_key key, pubKey;
  22377. WC_RNG rng;
  22378. int ret;
  22379. byte out[KEY32];
  22380. int keySz = sizeof(out);
  22381. word32 outlen = (word32)sizeof(out);
  22382. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22383. const char* qx =
  22384. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22385. const char* qy =
  22386. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22387. const char* d =
  22388. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22389. const char* curveName = "SECP256R1";
  22390. const byte expected_shared_secret[] =
  22391. {
  22392. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  22393. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  22394. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  22395. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  22396. };
  22397. #endif
  22398. PRIVATE_KEY_UNLOCK();
  22399. /* Initialize variables. */
  22400. XMEMSET(out, 0, keySz);
  22401. XMEMSET(&rng, 0, sizeof(rng));
  22402. XMEMSET(&key, 0, sizeof(key));
  22403. XMEMSET(&pubKey, 0, sizeof(pubKey));
  22404. ret = wc_InitRng(&rng);
  22405. if (ret == 0) {
  22406. ret = wc_ecc_init(&key);
  22407. if (ret == 0) {
  22408. ret = wc_ecc_init(&pubKey);
  22409. }
  22410. }
  22411. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22412. if (ret == 0) {
  22413. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22414. }
  22415. if (ret == 0) {
  22416. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  22417. }
  22418. #else
  22419. if (ret == 0) {
  22420. ret = wc_ecc_make_key(&rng, keySz, &key);
  22421. #if defined(WOLFSSL_ASYNC_CRYPT)
  22422. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22423. #endif
  22424. }
  22425. if (ret == 0) {
  22426. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  22427. #if defined(WOLFSSL_ASYNC_CRYPT)
  22428. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22429. #endif
  22430. }
  22431. #endif
  22432. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22433. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22434. !defined(HAVE_SELFTEST)
  22435. if (ret == 0) {
  22436. ret = wc_ecc_set_rng(&key, &rng);
  22437. }
  22438. #endif
  22439. if (ret == 0) {
  22440. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  22441. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22442. if (ret == 0) {
  22443. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  22444. ret = WOLFSSL_FATAL_ERROR;
  22445. }
  22446. }
  22447. #endif
  22448. /* Test bad args. */
  22449. if (ret == 0) {
  22450. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  22451. if (ret == BAD_FUNC_ARG) {
  22452. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  22453. }
  22454. if (ret == BAD_FUNC_ARG) {
  22455. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  22456. }
  22457. if (ret == BAD_FUNC_ARG) {
  22458. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  22459. }
  22460. if (ret == BAD_FUNC_ARG) {
  22461. /* Invalid length */
  22462. outlen = 1;
  22463. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  22464. }
  22465. if (ret == BUFFER_E) {
  22466. ret = 0;
  22467. }
  22468. else if (ret == 0) {
  22469. ret = WOLFSSL_FATAL_ERROR;
  22470. }
  22471. }
  22472. }
  22473. if (wc_FreeRng(&rng) && ret == 0) {
  22474. ret = WOLFSSL_FATAL_ERROR;
  22475. }
  22476. wc_ecc_free(&key);
  22477. wc_ecc_free(&pubKey);
  22478. #ifdef FP_ECC
  22479. wc_ecc_fp_free();
  22480. #endif
  22481. PRIVATE_KEY_LOCK();
  22482. res = TEST_RES_CHECK(ret == 0);
  22483. #endif
  22484. return res;
  22485. } /* END tests_wc_ecc_shared_secret */
  22486. /*
  22487. * testint wc_ecc_export_x963()
  22488. */
  22489. static int test_wc_ecc_export_x963(void)
  22490. {
  22491. int res = TEST_SKIPPED;
  22492. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22493. ecc_key key;
  22494. WC_RNG rng;
  22495. byte out[ECC_ASN963_MAX_BUF_SZ];
  22496. word32 outlen = sizeof(out);
  22497. int ret = 0;
  22498. PRIVATE_KEY_UNLOCK();
  22499. /* Initialize variables. */
  22500. XMEMSET(out, 0, outlen);
  22501. XMEMSET(&rng, 0, sizeof(rng));
  22502. XMEMSET(&key, 0, sizeof(key));
  22503. ret = wc_InitRng(&rng);
  22504. if (ret == 0) {
  22505. ret = wc_ecc_init(&key);
  22506. if (ret == 0) {
  22507. ret = wc_ecc_make_key(&rng, KEY20, &key);
  22508. #if defined(WOLFSSL_ASYNC_CRYPT)
  22509. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22510. #endif
  22511. }
  22512. }
  22513. if (ret == 0) {
  22514. ret = wc_ecc_export_x963(&key, out, &outlen);
  22515. }
  22516. /* Test bad args. */
  22517. if (ret == 0) {
  22518. ret = wc_ecc_export_x963(NULL, out, &outlen);
  22519. if (ret == ECC_BAD_ARG_E) {
  22520. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  22521. }
  22522. if (ret == LENGTH_ONLY_E) {
  22523. ret = wc_ecc_export_x963(&key, out, NULL);
  22524. }
  22525. if (ret == ECC_BAD_ARG_E) {
  22526. key.idx = -4;
  22527. ret = wc_ecc_export_x963(&key, out, &outlen);
  22528. }
  22529. if (ret == ECC_BAD_ARG_E) {
  22530. ret = 0;
  22531. }
  22532. else {
  22533. ret = WOLFSSL_FATAL_ERROR;
  22534. }
  22535. }
  22536. if (wc_FreeRng(&rng) && ret == 0) {
  22537. ret = WOLFSSL_FATAL_ERROR;
  22538. }
  22539. wc_ecc_free(&key);
  22540. #ifdef FP_ECC
  22541. wc_ecc_fp_free();
  22542. #endif
  22543. PRIVATE_KEY_LOCK();
  22544. res = TEST_RES_CHECK(ret == 0);
  22545. #endif
  22546. return res;
  22547. } /* END test_wc_ecc_export_x963 */
  22548. /*
  22549. * Testing wc_ecc_export_x963_ex()
  22550. * compile with --enable-compkey will use compression.
  22551. */
  22552. static int test_wc_ecc_export_x963_ex(void)
  22553. {
  22554. int res = TEST_SKIPPED;
  22555. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22556. ecc_key key;
  22557. WC_RNG rng;
  22558. int ret = 0;
  22559. byte out[ECC_ASN963_MAX_BUF_SZ];
  22560. word32 outlen = sizeof(out);
  22561. #ifdef HAVE_COMP_KEY
  22562. word32 badOutLen = 5;
  22563. #endif
  22564. /* Init stack variables. */
  22565. XMEMSET(out, 0, outlen);
  22566. XMEMSET(&rng, 0, sizeof(rng));
  22567. XMEMSET(&key, 0, sizeof(key));
  22568. ret = wc_InitRng(&rng);
  22569. if (ret == 0) {
  22570. ret = wc_ecc_init(&key);
  22571. if (ret == 0) {
  22572. ret = wc_ecc_make_key(&rng, KEY64, &key);
  22573. #if defined(WOLFSSL_ASYNC_CRYPT)
  22574. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22575. #endif
  22576. }
  22577. }
  22578. #ifdef HAVE_COMP_KEY
  22579. if (ret == 0) {
  22580. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  22581. }
  22582. #else
  22583. if (ret == 0) {
  22584. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  22585. }
  22586. #endif
  22587. /* Test bad args. */
  22588. #ifdef HAVE_COMP_KEY
  22589. if (ret == 0) {
  22590. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  22591. if (ret == BAD_FUNC_ARG) {
  22592. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  22593. }
  22594. if (ret == BAD_FUNC_ARG) {
  22595. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  22596. }
  22597. if (ret == BAD_FUNC_ARG) {
  22598. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  22599. }
  22600. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  22601. if (ret == BUFFER_E)
  22602. #else
  22603. if (ret == LENGTH_ONLY_E)
  22604. #endif
  22605. {
  22606. key.idx = -4;
  22607. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  22608. }
  22609. if (ret == ECC_BAD_ARG_E) {
  22610. ret = 0;
  22611. }
  22612. else {
  22613. ret = WOLFSSL_FATAL_ERROR;
  22614. }
  22615. }
  22616. #else
  22617. if (ret == 0) {
  22618. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  22619. if (ret == BAD_FUNC_ARG) {
  22620. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  22621. }
  22622. if (ret == BAD_FUNC_ARG) {
  22623. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  22624. }
  22625. if (ret == NOT_COMPILED_IN) {
  22626. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  22627. }
  22628. if (ret == BAD_FUNC_ARG) {
  22629. key.idx = -4;
  22630. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  22631. }
  22632. if (ret == ECC_BAD_ARG_E) {
  22633. ret = 0;
  22634. }
  22635. else if (ret == 0) {
  22636. ret = WOLFSSL_FATAL_ERROR;
  22637. }
  22638. }
  22639. #endif
  22640. if (wc_FreeRng(&rng) && ret == 0) {
  22641. ret = WOLFSSL_FATAL_ERROR;
  22642. }
  22643. wc_ecc_free(&key);
  22644. #ifdef FP_ECC
  22645. wc_ecc_fp_free();
  22646. #endif
  22647. res = TEST_RES_CHECK(ret == 0);
  22648. #endif
  22649. return res;
  22650. } /* END test_wc_ecc_export_x963_ex */
  22651. /*
  22652. * testing wc_ecc_import_x963()
  22653. */
  22654. static int test_wc_ecc_import_x963(void)
  22655. {
  22656. int res = TEST_SKIPPED;
  22657. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22658. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22659. ecc_key pubKey, key;
  22660. WC_RNG rng;
  22661. byte x963[ECC_ASN963_MAX_BUF_SZ];
  22662. word32 x963Len = (word32)sizeof(x963);
  22663. int ret;
  22664. /* Init stack variables. */
  22665. XMEMSET(x963, 0, x963Len);
  22666. XMEMSET(&rng, 0, sizeof(rng));
  22667. XMEMSET(&key, 0, sizeof(key));
  22668. XMEMSET(&pubKey, 0, sizeof(pubKey));
  22669. ret = wc_InitRng(&rng);
  22670. if (ret == 0) {
  22671. ret = wc_ecc_init(&pubKey);
  22672. if (ret == 0) {
  22673. ret = wc_ecc_init(&key);
  22674. }
  22675. if (ret == 0) {
  22676. ret = wc_ecc_make_key(&rng, KEY24, &key);
  22677. #if defined(WOLFSSL_ASYNC_CRYPT)
  22678. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22679. #endif
  22680. }
  22681. if (ret == 0) {
  22682. PRIVATE_KEY_UNLOCK();
  22683. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  22684. PRIVATE_KEY_LOCK();
  22685. }
  22686. }
  22687. if (ret == 0) {
  22688. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  22689. }
  22690. /* Test bad args. */
  22691. if (ret == 0) {
  22692. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  22693. if (ret == BAD_FUNC_ARG) {
  22694. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  22695. }
  22696. if (ret == BAD_FUNC_ARG) {
  22697. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  22698. }
  22699. if (ret == ECC_BAD_ARG_E) {
  22700. ret = 0;
  22701. }
  22702. else if (ret == 0) {
  22703. ret = WOLFSSL_FATAL_ERROR;
  22704. }
  22705. }
  22706. if (wc_FreeRng(&rng) && ret == 0) {
  22707. ret = WOLFSSL_FATAL_ERROR;
  22708. }
  22709. wc_ecc_free(&key);
  22710. wc_ecc_free(&pubKey);
  22711. #ifdef FP_ECC
  22712. wc_ecc_fp_free();
  22713. #endif
  22714. res = TEST_RES_CHECK(ret == 0);
  22715. #endif
  22716. return res;
  22717. } /* END wc_ecc_import_x963 */
  22718. /*
  22719. * testing wc_ecc_import_private_key()
  22720. */
  22721. static int ecc_import_private_key(void)
  22722. {
  22723. int res = TEST_SKIPPED;
  22724. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22725. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22726. ecc_key key, keyImp;
  22727. WC_RNG rng;
  22728. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  22729. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  22730. word32 privKeySz = (word32)sizeof(privKey);
  22731. word32 x963KeySz = (word32)sizeof(x963Key);
  22732. int ret;
  22733. /* Init stack variables. */
  22734. XMEMSET(privKey, 0, privKeySz);
  22735. XMEMSET(x963Key, 0, x963KeySz);
  22736. XMEMSET(&rng, 0, sizeof(rng));
  22737. XMEMSET(&key, 0, sizeof(key));
  22738. XMEMSET(&keyImp, 0, sizeof(keyImp));
  22739. ret = wc_InitRng(&rng);
  22740. if (ret == 0) {
  22741. ret = wc_ecc_init(&key);
  22742. if (ret == 0) {
  22743. ret = wc_ecc_init(&keyImp);
  22744. }
  22745. if (ret == 0) {
  22746. ret = wc_ecc_make_key(&rng, KEY48, &key);
  22747. #if defined(WOLFSSL_ASYNC_CRYPT)
  22748. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22749. #endif
  22750. }
  22751. if (ret == 0) {
  22752. PRIVATE_KEY_UNLOCK();
  22753. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  22754. PRIVATE_KEY_LOCK();
  22755. }
  22756. if (ret == 0) {
  22757. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  22758. }
  22759. }
  22760. if (ret == 0) {
  22761. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22762. x963KeySz, &keyImp);
  22763. }
  22764. /* Pass in bad args. */
  22765. if (ret == 0) {
  22766. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22767. x963KeySz, NULL);
  22768. if (ret == BAD_FUNC_ARG) {
  22769. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  22770. x963KeySz, &keyImp);
  22771. }
  22772. if (ret == BAD_FUNC_ARG) {
  22773. ret = 0;
  22774. }
  22775. else if (ret == 0) {
  22776. ret = WOLFSSL_FATAL_ERROR;
  22777. }
  22778. }
  22779. if (wc_FreeRng(&rng) && ret == 0) {
  22780. ret = WOLFSSL_FATAL_ERROR;
  22781. }
  22782. wc_ecc_free(&key);
  22783. wc_ecc_free(&keyImp);
  22784. #ifdef FP_ECC
  22785. wc_ecc_fp_free();
  22786. #endif
  22787. res = TEST_RES_CHECK(ret == 0);
  22788. #endif
  22789. return res;
  22790. } /* END wc_ecc_import_private_key */
  22791. /*
  22792. * Testing wc_ecc_export_private_only()
  22793. */
  22794. static int test_wc_ecc_export_private_only(void)
  22795. {
  22796. int res = TEST_SKIPPED;
  22797. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22798. ecc_key key;
  22799. WC_RNG rng;
  22800. byte out[ECC_PRIV_KEY_BUF];
  22801. word32 outlen = sizeof(out);
  22802. int ret;
  22803. /* Init stack variables. */
  22804. XMEMSET(out, 0, outlen);
  22805. XMEMSET(&rng, 0, sizeof(rng));
  22806. XMEMSET(&key, 0, sizeof(key));
  22807. ret = wc_InitRng(&rng);
  22808. if (ret == 0) {
  22809. ret = wc_ecc_init(&key);
  22810. if (ret == 0) {
  22811. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22812. #if defined(WOLFSSL_ASYNC_CRYPT)
  22813. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22814. #endif
  22815. }
  22816. }
  22817. if (ret == 0) {
  22818. ret = wc_ecc_export_private_only(&key, out, &outlen);
  22819. }
  22820. /* Pass in bad args. */
  22821. if (ret == 0) {
  22822. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  22823. if (ret == BAD_FUNC_ARG) {
  22824. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  22825. }
  22826. if (ret == BAD_FUNC_ARG) {
  22827. ret = wc_ecc_export_private_only(&key, out, NULL);
  22828. }
  22829. if (ret == BAD_FUNC_ARG) {
  22830. ret = 0;
  22831. }
  22832. else if (ret == 0) {
  22833. ret = WOLFSSL_FATAL_ERROR;
  22834. }
  22835. }
  22836. if (wc_FreeRng(&rng) && ret == 0) {
  22837. ret = WOLFSSL_FATAL_ERROR;
  22838. }
  22839. wc_ecc_free(&key);
  22840. #ifdef FP_ECC
  22841. wc_ecc_fp_free();
  22842. #endif
  22843. res = TEST_RES_CHECK(ret == 0);
  22844. #endif
  22845. return res;
  22846. } /* END test_wc_ecc_export_private_only */
  22847. /*
  22848. * Testing wc_ecc_rs_to_sig()
  22849. */
  22850. static int test_wc_ecc_rs_to_sig(void)
  22851. {
  22852. int res = TEST_SKIPPED;
  22853. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22854. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22855. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22856. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22857. const char* zeroStr = "0";
  22858. byte sig[ECC_MAX_SIG_SIZE];
  22859. word32 siglen = (word32)sizeof(sig);
  22860. /*R and S max size is the order of curve. 2^192.*/
  22861. int keySz = KEY24;
  22862. byte r[KEY24];
  22863. byte s[KEY24];
  22864. word32 rlen = (word32)sizeof(r);
  22865. word32 slen = (word32)sizeof(s);
  22866. int ret;
  22867. /* Init stack variables. */
  22868. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22869. XMEMSET(r, 0, keySz);
  22870. XMEMSET(s, 0, keySz);
  22871. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  22872. if (ret == 0) {
  22873. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  22874. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) || \
  22875. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  22876. if (ret == ASN_PARSE_E) {
  22877. ret = 0;
  22878. }
  22879. #endif
  22880. }
  22881. /* Test bad args. */
  22882. if (ret == 0) {
  22883. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  22884. if (ret == ECC_BAD_ARG_E) {
  22885. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  22886. }
  22887. if (ret == ECC_BAD_ARG_E) {
  22888. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  22889. }
  22890. if (ret == ECC_BAD_ARG_E) {
  22891. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  22892. }
  22893. if (ret == ECC_BAD_ARG_E) {
  22894. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  22895. }
  22896. if (ret == MP_ZERO_E) {
  22897. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  22898. }
  22899. if (ret == MP_ZERO_E) {
  22900. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  22901. }
  22902. if (ret == ECC_BAD_ARG_E) {
  22903. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  22904. }
  22905. if (ret == ECC_BAD_ARG_E) {
  22906. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  22907. }
  22908. if (ret == ECC_BAD_ARG_E) {
  22909. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  22910. }
  22911. if (ret == ECC_BAD_ARG_E) {
  22912. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  22913. }
  22914. if (ret == ECC_BAD_ARG_E) {
  22915. ret = 0;
  22916. }
  22917. else if (ret == 0) {
  22918. ret = WOLFSSL_FATAL_ERROR;
  22919. }
  22920. }
  22921. res = TEST_RES_CHECK(ret == 0);
  22922. #endif
  22923. return res;
  22924. } /* END test_wc_ecc_rs_to_sig */
  22925. static int test_wc_ecc_import_raw(void)
  22926. {
  22927. int res = TEST_SKIPPED;
  22928. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22929. ecc_key key;
  22930. int ret = 0;
  22931. const char* qx =
  22932. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22933. const char* qy =
  22934. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22935. const char* d =
  22936. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22937. const char* curveName = "SECP256R1";
  22938. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22939. const char* kNullStr = "";
  22940. #endif
  22941. ret = wc_ecc_init(&key);
  22942. /* Test good import */
  22943. if (ret == 0) {
  22944. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22945. }
  22946. /* Test bad args. */
  22947. if (ret == 0) {
  22948. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  22949. if (ret == BAD_FUNC_ARG) {
  22950. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  22951. }
  22952. if (ret == BAD_FUNC_ARG) {
  22953. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  22954. }
  22955. if (ret == BAD_FUNC_ARG) {
  22956. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  22957. }
  22958. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22959. if (ret == BAD_FUNC_ARG) {
  22960. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22961. wc_ecc_free(&key);
  22962. #endif
  22963. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  22964. if (ret == ECC_INF_E)
  22965. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22966. }
  22967. #endif
  22968. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22969. if (ret == BAD_FUNC_ARG) {
  22970. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22971. wc_ecc_free(&key);
  22972. #endif
  22973. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  22974. /* Note: SP math "is point" failure returns MP_VAL */
  22975. if (ret == ECC_INF_E || ret == MP_VAL) {
  22976. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22977. }
  22978. }
  22979. if (ret == BAD_FUNC_ARG) {
  22980. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22981. wc_ecc_free(&key);
  22982. #endif
  22983. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  22984. /* Note: SP math "is point" failure returns MP_VAL */
  22985. if (ret == ECC_INF_E || ret == MP_VAL) {
  22986. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22987. }
  22988. }
  22989. #endif
  22990. if (ret == BAD_FUNC_ARG) {
  22991. ret = 0;
  22992. }
  22993. }
  22994. wc_ecc_free(&key);
  22995. res = TEST_RES_CHECK(ret == 0);
  22996. #endif
  22997. return res;
  22998. } /* END test_wc_ecc_import_raw */
  22999. static int test_wc_ecc_import_unsigned(void)
  23000. {
  23001. int res = TEST_SKIPPED;
  23002. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23003. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  23004. ecc_key key;
  23005. const byte qx[] = {
  23006. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  23007. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  23008. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  23009. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  23010. };
  23011. const byte qy[] = {
  23012. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  23013. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  23014. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  23015. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  23016. };
  23017. const byte d[] = {
  23018. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  23019. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  23020. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  23021. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  23022. };
  23023. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  23024. const byte nullBytes[32] = {0};
  23025. #endif
  23026. int curveId = ECC_SECP256R1;
  23027. int ret;
  23028. ret = wc_ecc_init(&key);
  23029. if (ret == 0) {
  23030. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  23031. curveId);
  23032. }
  23033. /* Test bad args. */
  23034. if (ret == 0) {
  23035. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  23036. curveId);
  23037. if (ret == BAD_FUNC_ARG) {
  23038. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  23039. curveId);
  23040. }
  23041. if (ret == BAD_FUNC_ARG) {
  23042. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  23043. curveId);
  23044. }
  23045. if (ret == BAD_FUNC_ARG) {
  23046. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  23047. ECC_CURVE_INVALID);
  23048. }
  23049. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  23050. if (ret == BAD_FUNC_ARG) {
  23051. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  23052. (byte*)nullBytes, (byte*)nullBytes, curveId);
  23053. }
  23054. #endif
  23055. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  23056. ret = 0;
  23057. }
  23058. }
  23059. wc_ecc_free(&key);
  23060. res = TEST_RES_CHECK(ret == 0);
  23061. #endif
  23062. return res;
  23063. } /* END test_wc_ecc_import_unsigned */
  23064. /*
  23065. * Testing wc_ecc_sig_size()
  23066. */
  23067. static int test_wc_ecc_sig_size(void)
  23068. {
  23069. int res = TEST_SKIPPED;
  23070. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23071. ecc_key key;
  23072. WC_RNG rng;
  23073. int keySz = KEY16;
  23074. int ret = 0;
  23075. XMEMSET(&rng, 0, sizeof(rng));
  23076. XMEMSET(&key, 0, sizeof(key));
  23077. ret = wc_InitRng(&rng);
  23078. if (ret == 0) {
  23079. ret = wc_ecc_init(&key);
  23080. if (ret == 0) {
  23081. ret = wc_ecc_make_key(&rng, keySz, &key);
  23082. #if defined(WOLFSSL_ASYNC_CRYPT)
  23083. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23084. #endif
  23085. }
  23086. }
  23087. if (ret == 0) {
  23088. ret = wc_ecc_sig_size(&key);
  23089. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  23090. ret = 0;
  23091. }
  23092. }
  23093. if (wc_FreeRng(&rng) && ret == 0) {
  23094. ret = WOLFSSL_FATAL_ERROR;
  23095. }
  23096. wc_ecc_free(&key);
  23097. res = TEST_RES_CHECK(ret == 0);
  23098. #endif
  23099. return res;
  23100. } /* END test_wc_ecc_sig_size */
  23101. /*
  23102. * Testing wc_ecc_ctx_new()
  23103. */
  23104. static int test_wc_ecc_ctx_new(void)
  23105. {
  23106. int res = TEST_SKIPPED;
  23107. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23108. WC_RNG rng;
  23109. int ret = 0;
  23110. ecEncCtx* cli = NULL;
  23111. ecEncCtx* srv = NULL;
  23112. ret = wc_InitRng(&rng);
  23113. if (ret == 0) {
  23114. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  23115. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  23116. }
  23117. if (ret == 0 && (cli == NULL || srv == NULL)) {
  23118. ret = WOLFSSL_FATAL_ERROR;
  23119. }
  23120. wc_ecc_ctx_free(cli);
  23121. wc_ecc_ctx_free(srv);
  23122. /* Test bad args. */
  23123. if (ret == 0) {
  23124. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  23125. cli = wc_ecc_ctx_new(0, &rng);
  23126. if (cli != NULL) {
  23127. ret = WOLFSSL_FATAL_ERROR;
  23128. }
  23129. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  23130. if (cli != NULL) {
  23131. ret = WOLFSSL_FATAL_ERROR;
  23132. }
  23133. }
  23134. if (wc_FreeRng(&rng) && ret == 0) {
  23135. ret = WOLFSSL_FATAL_ERROR;
  23136. }
  23137. wc_ecc_ctx_free(cli);
  23138. res = TEST_RES_CHECK(ret == 0);
  23139. #endif
  23140. return res;
  23141. } /* END test_wc_ecc_ctx_new */
  23142. /*
  23143. * Tesing wc_ecc_reset()
  23144. */
  23145. static int test_wc_ecc_ctx_reset(void)
  23146. {
  23147. int res = TEST_SKIPPED;
  23148. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23149. ecEncCtx* ctx = NULL;
  23150. WC_RNG rng;
  23151. int ret = 0;
  23152. ret = wc_InitRng(&rng);
  23153. if (ret == 0) {
  23154. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  23155. ret = WOLFSSL_FATAL_ERROR;
  23156. }
  23157. }
  23158. if (ret == 0) {
  23159. ret = wc_ecc_ctx_reset(ctx, &rng);
  23160. }
  23161. /* Pass in bad args. */
  23162. if (ret == 0) {
  23163. ret = wc_ecc_ctx_reset(NULL, &rng);
  23164. if (ret == BAD_FUNC_ARG) {
  23165. ret = wc_ecc_ctx_reset(ctx, NULL);
  23166. }
  23167. if (ret == BAD_FUNC_ARG) {
  23168. ret = 0;
  23169. }
  23170. else if (ret == 0) {
  23171. ret = WOLFSSL_FATAL_ERROR;
  23172. }
  23173. }
  23174. if (wc_FreeRng(&rng) && ret == 0) {
  23175. ret = WOLFSSL_FATAL_ERROR;
  23176. }
  23177. wc_ecc_ctx_free(ctx);
  23178. res = TEST_RES_CHECK(ret == 0);
  23179. #endif
  23180. return res;
  23181. } /* END test_wc_ecc_ctx_reset */
  23182. /*
  23183. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  23184. */
  23185. static int test_wc_ecc_ctx_set_peer_salt(void)
  23186. {
  23187. int res = TEST_SKIPPED;
  23188. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23189. WC_RNG rng;
  23190. ecEncCtx* cliCtx = NULL;
  23191. ecEncCtx* servCtx = NULL;
  23192. const byte* cliSalt = NULL;
  23193. const byte* servSalt = NULL;
  23194. int ret = 0;
  23195. ret = wc_InitRng(&rng);
  23196. if (ret == 0) {
  23197. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  23198. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  23199. ret = WOLFSSL_FATAL_ERROR;
  23200. }
  23201. }
  23202. /* Test bad args. */
  23203. if (ret == 0) {
  23204. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  23205. if (cliSalt != NULL) {
  23206. ret = WOLFSSL_FATAL_ERROR;
  23207. }
  23208. }
  23209. if (ret == 0) {
  23210. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23211. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  23212. if (cliSalt == NULL || servSalt == NULL) {
  23213. ret = WOLFSSL_FATAL_ERROR;
  23214. }
  23215. }
  23216. if (ret == 0) {
  23217. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  23218. }
  23219. /* Test bad args. */
  23220. if (ret == 0) {
  23221. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  23222. if (ret == BAD_FUNC_ARG) {
  23223. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  23224. }
  23225. if (ret == BAD_FUNC_ARG) {
  23226. ret = 0;
  23227. }
  23228. else if (ret == 0) {
  23229. ret = WOLFSSL_FATAL_ERROR;
  23230. }
  23231. }
  23232. if (wc_FreeRng(&rng) && ret == 0) {
  23233. ret = WOLFSSL_FATAL_ERROR;
  23234. }
  23235. wc_ecc_ctx_free(cliCtx);
  23236. wc_ecc_ctx_free(servCtx);
  23237. res = TEST_RES_CHECK(ret == 0);
  23238. #endif
  23239. return res;
  23240. } /* END test_wc_ecc_ctx_set_peer_salt */
  23241. /*
  23242. * Testing wc_ecc_ctx_set_info()
  23243. */
  23244. static int test_wc_ecc_ctx_set_info(void)
  23245. {
  23246. int res = TEST_SKIPPED;
  23247. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23248. ecEncCtx* ctx = NULL;
  23249. WC_RNG rng;
  23250. int ret;
  23251. const char* optInfo = "Optional Test Info.";
  23252. int optInfoSz = (int)XSTRLEN(optInfo);
  23253. const char* badOptInfo = NULL;
  23254. ret = wc_InitRng(&rng);
  23255. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  23256. ret = WOLFSSL_FATAL_ERROR;
  23257. }
  23258. if (ret == 0) {
  23259. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  23260. }
  23261. /* Test bad args. */
  23262. if (ret == 0) {
  23263. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  23264. if (ret == BAD_FUNC_ARG) {
  23265. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  23266. }
  23267. if (ret == BAD_FUNC_ARG) {
  23268. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  23269. }
  23270. if (ret == BAD_FUNC_ARG) {
  23271. ret = 0;
  23272. }
  23273. else if (ret == 0) {
  23274. ret = WOLFSSL_FATAL_ERROR;
  23275. }
  23276. }
  23277. if (wc_FreeRng(&rng) && ret == 0) {
  23278. ret = WOLFSSL_FATAL_ERROR;
  23279. }
  23280. wc_ecc_ctx_free(ctx);
  23281. res = TEST_RES_CHECK(ret == 0);
  23282. #endif
  23283. return res;
  23284. } /* END test_wc_ecc_ctx_set_info */
  23285. /*
  23286. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  23287. */
  23288. static int test_wc_ecc_encryptDecrypt(void)
  23289. {
  23290. int res = TEST_SKIPPED;
  23291. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  23292. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  23293. ecc_key srvKey, cliKey, tmpKey;
  23294. WC_RNG rng;
  23295. int ret;
  23296. const char* msg = "EccBlock Size 16";
  23297. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  23298. #ifdef WOLFSSL_ECIES_OLD
  23299. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23300. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23301. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  23302. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23303. #else
  23304. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23305. #endif
  23306. word32 outSz = (word32)sizeof(out);
  23307. byte plain[sizeof("EccBlock Size 16")];
  23308. word32 plainSz = (word32)sizeof(plain);
  23309. int keySz = KEY20;
  23310. /* Init stack variables. */
  23311. XMEMSET(out, 0, outSz);
  23312. XMEMSET(plain, 0, plainSz);
  23313. XMEMSET(&rng, 0, sizeof(rng));
  23314. XMEMSET(&srvKey, 0, sizeof(srvKey));
  23315. XMEMSET(&cliKey, 0, sizeof(cliKey));
  23316. ret = wc_InitRng(&rng);
  23317. if (ret == 0) {
  23318. ret = wc_ecc_init(&cliKey);
  23319. if (ret == 0) {
  23320. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  23321. #if defined(WOLFSSL_ASYNC_CRYPT)
  23322. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23323. #endif
  23324. }
  23325. if (ret == 0) {
  23326. ret = wc_ecc_init(&srvKey);
  23327. }
  23328. if (ret == 0) {
  23329. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  23330. #if defined(WOLFSSL_ASYNC_CRYPT)
  23331. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23332. #endif
  23333. }
  23334. if (ret == 0) {
  23335. ret = wc_ecc_init(&tmpKey);
  23336. }
  23337. }
  23338. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23339. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23340. !defined(HAVE_SELFTEST)
  23341. if (ret == 0) {
  23342. ret = wc_ecc_set_rng(&srvKey, &rng);
  23343. }
  23344. if (ret == 0) {
  23345. ret = wc_ecc_set_rng(&cliKey, &rng);
  23346. }
  23347. #endif
  23348. if (ret == 0) {
  23349. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  23350. &outSz, NULL);
  23351. }
  23352. if (ret == 0) {
  23353. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  23354. &outSz, NULL);
  23355. if (ret == BAD_FUNC_ARG) {
  23356. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  23357. &outSz, NULL);
  23358. }
  23359. if (ret == BAD_FUNC_ARG) {
  23360. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  23361. &outSz, NULL);
  23362. }
  23363. if (ret == BAD_FUNC_ARG) {
  23364. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  23365. &outSz, NULL);
  23366. }
  23367. if (ret == BAD_FUNC_ARG) {
  23368. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  23369. NULL, NULL);
  23370. }
  23371. if (ret == BAD_FUNC_ARG) {
  23372. ret = 0;
  23373. }
  23374. else if (ret == 0) {
  23375. ret = WOLFSSL_FATAL_ERROR;
  23376. }
  23377. }
  23378. #ifdef WOLFSSL_ECIES_OLD
  23379. if (ret == 0) {
  23380. tmpKey.dp = cliKey.dp;
  23381. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  23382. }
  23383. #endif
  23384. if (ret == 0) {
  23385. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  23386. &plainSz, NULL);
  23387. }
  23388. if (ret == 0) {
  23389. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  23390. &plainSz, NULL);
  23391. #ifdef WOLFSSL_ECIES_OLD
  23392. /* NULL parameter allowed in new implementations - public key comes from
  23393. * the message. */
  23394. if (ret == BAD_FUNC_ARG) {
  23395. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  23396. &plainSz, NULL);
  23397. }
  23398. #endif
  23399. if (ret == BAD_FUNC_ARG) {
  23400. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  23401. &plainSz, NULL);
  23402. }
  23403. if (ret == BAD_FUNC_ARG) {
  23404. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  23405. &plainSz, NULL);
  23406. }
  23407. if (ret == BAD_FUNC_ARG) {
  23408. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  23409. plain, NULL, NULL);
  23410. }
  23411. if (ret == BAD_FUNC_ARG) {
  23412. ret = 0;
  23413. }
  23414. else if (ret == 0) {
  23415. ret = WOLFSSL_FATAL_ERROR;
  23416. }
  23417. }
  23418. if (XMEMCMP(msg, plain, msgSz) != 0) {
  23419. ret = WOLFSSL_FATAL_ERROR;
  23420. }
  23421. if (wc_FreeRng(&rng) && ret == 0) {
  23422. ret = WOLFSSL_FATAL_ERROR;
  23423. }
  23424. wc_ecc_free(&tmpKey);
  23425. wc_ecc_free(&cliKey);
  23426. wc_ecc_free(&srvKey);
  23427. res = TEST_RES_CHECK(ret == 0);
  23428. #endif
  23429. return res;
  23430. } /* END test_wc_ecc_encryptDecrypt */
  23431. /*
  23432. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  23433. */
  23434. static int test_wc_ecc_del_point(void)
  23435. {
  23436. int res = TEST_SKIPPED;
  23437. #if defined(HAVE_ECC)
  23438. ecc_point* pt;
  23439. pt = wc_ecc_new_point();
  23440. wc_ecc_del_point(pt);
  23441. res = TEST_RES_CHECK(pt != NULL);
  23442. #endif
  23443. return res;
  23444. } /* END test_wc_ecc_del_point */
  23445. /*
  23446. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  23447. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  23448. * and wc_ecc_cmp_point()
  23449. */
  23450. static int test_wc_ecc_pointFns(void)
  23451. {
  23452. int res = TEST_SKIPPED;
  23453. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  23454. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23455. !defined(WOLFSSL_ATECC608A)
  23456. ecc_key key;
  23457. WC_RNG rng;
  23458. int ret;
  23459. ecc_point* point = NULL;
  23460. ecc_point* cpypt = NULL;
  23461. int idx = 0;
  23462. int keySz = KEY32;
  23463. byte der[DER_SZ(KEY32)];
  23464. word32 derlenChk = 0;
  23465. word32 derSz = DER_SZ(KEY32);
  23466. /* Init stack variables. */
  23467. XMEMSET(der, 0, derSz);
  23468. XMEMSET(&rng, 0, sizeof(rng));
  23469. XMEMSET(&key, 0, sizeof(key));
  23470. ret = wc_InitRng(&rng);
  23471. if (ret == 0) {
  23472. ret = wc_ecc_init(&key);
  23473. if (ret == 0) {
  23474. ret = wc_ecc_make_key(&rng, keySz, &key);
  23475. #if defined(WOLFSSL_ASYNC_CRYPT)
  23476. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23477. #endif
  23478. }
  23479. }
  23480. if (ret == 0) {
  23481. point = wc_ecc_new_point();
  23482. if (!point) {
  23483. ret = WOLFSSL_FATAL_ERROR;
  23484. }
  23485. }
  23486. if (ret == 0) {
  23487. cpypt = wc_ecc_new_point();
  23488. if (!cpypt) {
  23489. ret = WOLFSSL_FATAL_ERROR;
  23490. }
  23491. }
  23492. /* Export */
  23493. if (ret == 0) {
  23494. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  23495. NULL, &derlenChk);
  23496. /* Check length value. */
  23497. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  23498. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  23499. der, &derSz);
  23500. }
  23501. }
  23502. /* Test bad args. */
  23503. if (ret == 0) {
  23504. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  23505. if (ret == ECC_BAD_ARG_E) {
  23506. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  23507. }
  23508. if (ret == ECC_BAD_ARG_E) {
  23509. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  23510. der, NULL);
  23511. }
  23512. if (ret == ECC_BAD_ARG_E) {
  23513. ret = 0;
  23514. }
  23515. else if (ret == 0) {
  23516. ret = WOLFSSL_FATAL_ERROR;
  23517. }
  23518. }
  23519. /* Import */
  23520. if (ret == 0) {
  23521. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  23522. /* Condition double checks wc_ecc_cmp_point(). */
  23523. if (ret == 0 &&
  23524. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  23525. ret = wc_ecc_cmp_point(&key.pubkey, point);
  23526. }
  23527. }
  23528. /* Test bad args. */
  23529. if (ret == 0) {
  23530. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  23531. if (ret == ECC_BAD_ARG_E) {
  23532. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  23533. }
  23534. if (ret == ECC_BAD_ARG_E) {
  23535. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  23536. }
  23537. if (ret == ECC_BAD_ARG_E) {
  23538. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  23539. }
  23540. if (ret == ECC_BAD_ARG_E) {
  23541. ret = 0;
  23542. }
  23543. else if (ret == 0) {
  23544. ret = WOLFSSL_FATAL_ERROR;
  23545. }
  23546. }
  23547. /* Copy */
  23548. if (ret == 0) {
  23549. ret = wc_ecc_copy_point(point, cpypt);
  23550. }
  23551. /* Test bad args. */
  23552. if (ret == 0) {
  23553. ret = wc_ecc_copy_point(NULL, cpypt);
  23554. if (ret == ECC_BAD_ARG_E) {
  23555. ret = wc_ecc_copy_point(point, NULL);
  23556. }
  23557. if (ret == ECC_BAD_ARG_E) {
  23558. ret = 0;
  23559. }
  23560. else if (ret == 0) {
  23561. ret = WOLFSSL_FATAL_ERROR;
  23562. }
  23563. }
  23564. /* Compare point */
  23565. if (ret == 0) {
  23566. ret = wc_ecc_cmp_point(point, cpypt);
  23567. }
  23568. /* Test bad args. */
  23569. if (ret == 0) {
  23570. ret = wc_ecc_cmp_point(NULL, cpypt);
  23571. if (ret == BAD_FUNC_ARG) {
  23572. ret = wc_ecc_cmp_point(point, NULL);
  23573. }
  23574. if (ret == BAD_FUNC_ARG) {
  23575. ret = 0;
  23576. }
  23577. else if (ret == 0) {
  23578. ret = WOLFSSL_FATAL_ERROR;
  23579. }
  23580. }
  23581. /* At infinity if return == 1, otherwise return == 0. */
  23582. if (ret == 0) {
  23583. ret = wc_ecc_point_is_at_infinity(point);
  23584. }
  23585. /* Test bad args. */
  23586. if (ret == 0) {
  23587. ret = wc_ecc_point_is_at_infinity(NULL);
  23588. if (ret == BAD_FUNC_ARG) {
  23589. ret = 0;
  23590. }
  23591. else if (ret == 0) {
  23592. ret = WOLFSSL_FATAL_ERROR;
  23593. }
  23594. }
  23595. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  23596. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  23597. #ifdef USE_ECC_B_PARAM
  23598. /* On curve if ret == 0 */
  23599. if (ret == 0) {
  23600. ret = wc_ecc_point_is_on_curve(point, idx);
  23601. }
  23602. /* Test bad args. */
  23603. if (ret == 0) {
  23604. ret = wc_ecc_point_is_on_curve(NULL, idx);
  23605. if (ret == BAD_FUNC_ARG) {
  23606. ret = wc_ecc_point_is_on_curve(point, 1000);
  23607. }
  23608. if (ret == ECC_BAD_ARG_E) {
  23609. ret = 0;
  23610. }
  23611. else if (ret == 0) {
  23612. ret = WOLFSSL_FATAL_ERROR;
  23613. }
  23614. }
  23615. #endif /* USE_ECC_B_PARAM */
  23616. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  23617. /* Free */
  23618. wc_ecc_del_point(point);
  23619. wc_ecc_del_point(cpypt);
  23620. wc_ecc_free(&key);
  23621. if (wc_FreeRng(&rng) && ret == 0) {
  23622. ret = WOLFSSL_FATAL_ERROR;
  23623. }
  23624. res = TEST_RES_CHECK(ret == 0);
  23625. #endif
  23626. return res;
  23627. } /* END test_wc_ecc_pointFns */
  23628. /*
  23629. * Testing wc_ecc_sahred_secret_ssh()
  23630. */
  23631. static int test_wc_ecc_shared_secret_ssh(void)
  23632. {
  23633. int res = TEST_SKIPPED;
  23634. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  23635. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23636. !defined(WOLFSSL_ATECC608A)
  23637. ecc_key key, key2;
  23638. WC_RNG rng;
  23639. int ret;
  23640. int keySz = KEY32;
  23641. int key2Sz = KEY24;
  23642. byte secret[KEY32];
  23643. word32 secretLen = keySz;
  23644. /* Init stack variables. */
  23645. XMEMSET(secret, 0, secretLen);
  23646. XMEMSET(&rng, 0, sizeof(rng));
  23647. XMEMSET(&key, 0, sizeof(key));
  23648. XMEMSET(&key2, 0, sizeof(key2));
  23649. /* Make keys */
  23650. ret = wc_InitRng(&rng);
  23651. if (ret == 0) {
  23652. ret = wc_ecc_init(&key);
  23653. if (ret == 0) {
  23654. ret = wc_ecc_make_key(&rng, keySz, &key);
  23655. #if defined(WOLFSSL_ASYNC_CRYPT)
  23656. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23657. #endif
  23658. }
  23659. if (wc_FreeRng(&rng) && ret == 0) {
  23660. ret = WOLFSSL_FATAL_ERROR;
  23661. }
  23662. }
  23663. if (ret == 0) {
  23664. ret = wc_InitRng(&rng);
  23665. if (ret == 0) {
  23666. ret = wc_ecc_init(&key2);
  23667. }
  23668. if (ret == 0) {
  23669. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  23670. #if defined(WOLFSSL_ASYNC_CRYPT)
  23671. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  23672. #endif
  23673. }
  23674. }
  23675. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23676. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23677. !defined(HAVE_SELFTEST)
  23678. if (ret == 0) {
  23679. ret = wc_ecc_set_rng(&key, &rng);
  23680. }
  23681. #endif
  23682. if (ret == 0) {
  23683. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23684. }
  23685. /* Pass in bad args. */
  23686. if (ret == 0) {
  23687. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  23688. if (ret == BAD_FUNC_ARG) {
  23689. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  23690. }
  23691. if (ret == BAD_FUNC_ARG) {
  23692. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  23693. }
  23694. if (ret == BAD_FUNC_ARG) {
  23695. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  23696. }
  23697. if (ret == BAD_FUNC_ARG) {
  23698. key.type = ECC_PUBLICKEY;
  23699. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23700. if (ret == ECC_BAD_ARG_E) {
  23701. ret = 0;
  23702. }
  23703. else if (ret == 0) {
  23704. ret = WOLFSSL_FATAL_ERROR;
  23705. }
  23706. }
  23707. else if (ret == 0) {
  23708. ret = WOLFSSL_FATAL_ERROR;
  23709. }
  23710. }
  23711. if (wc_FreeRng(&rng) && ret == 0) {
  23712. ret = WOLFSSL_FATAL_ERROR;
  23713. }
  23714. wc_ecc_free(&key);
  23715. wc_ecc_free(&key2);
  23716. #ifdef FP_ECC
  23717. wc_ecc_fp_free();
  23718. #endif
  23719. res = TEST_RES_CHECK(ret == 0);
  23720. #endif
  23721. return res;
  23722. } /* END test_wc_ecc_shared_secret_ssh */
  23723. /*
  23724. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  23725. */
  23726. static int test_wc_ecc_verify_hash_ex(void)
  23727. {
  23728. int res = TEST_SKIPPED;
  23729. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  23730. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23731. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  23732. ecc_key key;
  23733. WC_RNG rng;
  23734. int ret;
  23735. mp_int r;
  23736. mp_int s;
  23737. mp_int z;
  23738. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  23739. unsigned char iHash[] = "Everyone gets Friday off.......";
  23740. unsigned char shortHash[] = TEST_STRING;
  23741. word32 hashlen = sizeof(hash);
  23742. word32 iHashLen = sizeof(iHash);
  23743. word32 shortHashLen = sizeof(shortHash);
  23744. int keySz = KEY32;
  23745. int sig = WOLFSSL_FATAL_ERROR;
  23746. int ver = WOLFSSL_FATAL_ERROR;
  23747. int verify_ok = 0;
  23748. /* Initialize r and s. */
  23749. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  23750. if (ret != MP_OKAY) {
  23751. return MP_INIT_E;
  23752. }
  23753. ret = wc_InitRng(&rng);
  23754. if (ret == 0) {
  23755. ret = wc_ecc_init(&key);
  23756. if (ret == 0) {
  23757. ret = wc_ecc_make_key(&rng, keySz, &key);
  23758. #if defined(WOLFSSL_ASYNC_CRYPT)
  23759. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23760. #endif
  23761. }
  23762. }
  23763. if (ret == 0) {
  23764. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  23765. if (ret == 0) {
  23766. /* verify_ok should be 1. */
  23767. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  23768. if (verify_ok != 1 && ret == 0) {
  23769. ret = WOLFSSL_FATAL_ERROR;
  23770. }
  23771. }
  23772. if (ret == 0) {
  23773. /* verify_ok should be 0 */
  23774. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  23775. &verify_ok, &key);
  23776. if (verify_ok != 0 && ret == 0) {
  23777. ret = WOLFSSL_FATAL_ERROR;
  23778. }
  23779. }
  23780. if (ret == 0) {
  23781. /* verify_ok should be 0. */
  23782. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23783. &verify_ok, &key);
  23784. if (verify_ok != 0 && ret == 0) {
  23785. ret = WOLFSSL_FATAL_ERROR;
  23786. }
  23787. }
  23788. }
  23789. /* Test bad args. */
  23790. if (ret == 0) {
  23791. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  23792. == ECC_BAD_ARG_E) {
  23793. sig = 0;
  23794. }
  23795. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  23796. != ECC_BAD_ARG_E) {
  23797. sig = WOLFSSL_FATAL_ERROR;
  23798. }
  23799. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  23800. != ECC_BAD_ARG_E) {
  23801. sig = WOLFSSL_FATAL_ERROR;
  23802. }
  23803. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  23804. != ECC_BAD_ARG_E) {
  23805. sig = WOLFSSL_FATAL_ERROR;
  23806. }
  23807. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  23808. != ECC_BAD_ARG_E) {
  23809. sig = WOLFSSL_FATAL_ERROR;
  23810. }
  23811. }
  23812. /* Test bad args. */
  23813. if (ret == 0) {
  23814. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  23815. == ECC_BAD_ARG_E) {
  23816. ver = 0;
  23817. }
  23818. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  23819. &verify_ok, &key) != ECC_BAD_ARG_E) {
  23820. ver = WOLFSSL_FATAL_ERROR;
  23821. }
  23822. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  23823. != MP_ZERO_E) {
  23824. ver = WOLFSSL_FATAL_ERROR;
  23825. }
  23826. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  23827. != MP_ZERO_E) {
  23828. ver = WOLFSSL_FATAL_ERROR;
  23829. }
  23830. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  23831. != MP_ZERO_E) {
  23832. ver = WOLFSSL_FATAL_ERROR;
  23833. }
  23834. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  23835. &key) != ECC_BAD_ARG_E) {
  23836. ver = WOLFSSL_FATAL_ERROR;
  23837. }
  23838. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23839. NULL, &key) != ECC_BAD_ARG_E) {
  23840. ver = WOLFSSL_FATAL_ERROR;
  23841. }
  23842. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23843. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  23844. ver = WOLFSSL_FATAL_ERROR;
  23845. }
  23846. }
  23847. wc_ecc_free(&key);
  23848. mp_free(&r);
  23849. mp_free(&s);
  23850. if (wc_FreeRng(&rng)) {
  23851. return WOLFSSL_FATAL_ERROR;
  23852. }
  23853. if (ret == 0 && (sig != 0 || ver != 0)) {
  23854. ret = WOLFSSL_FATAL_ERROR;
  23855. }
  23856. res = TEST_RES_CHECK(ret == 0);
  23857. #endif
  23858. return res;
  23859. } /* END test_wc_ecc_verify_hash_ex */
  23860. /*
  23861. * Testing wc_ecc_mulmod()
  23862. */
  23863. static int test_wc_ecc_mulmod(void)
  23864. {
  23865. int res = TEST_SKIPPED;
  23866. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  23867. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  23868. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23869. ecc_key key1, key2, key3;
  23870. WC_RNG rng;
  23871. int ret = 0;
  23872. ret = wc_InitRng(&rng);
  23873. if (ret == 0) {
  23874. ret = wc_ecc_init(&key1);
  23875. if (ret == 0) {
  23876. ret = wc_ecc_init(&key2);
  23877. }
  23878. if (ret == 0) {
  23879. ret = wc_ecc_init(&key3);
  23880. }
  23881. if (ret == 0) {
  23882. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  23883. #if defined(WOLFSSL_ASYNC_CRYPT)
  23884. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  23885. #endif
  23886. }
  23887. wc_FreeRng(&rng);
  23888. }
  23889. if (ret == 0) {
  23890. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  23891. ECC_SECP256R1);
  23892. if (ret == 0) {
  23893. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  23894. key1.dp->prime, ECC_SECP256R1);
  23895. }
  23896. }
  23897. if (ret == 0) {
  23898. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  23899. &key3.k, 1);
  23900. }
  23901. /* Test bad args. */
  23902. if (ret == 0) {
  23903. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  23904. &key3.k, 1);
  23905. if (ret == ECC_BAD_ARG_E) {
  23906. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  23907. &key3.k, 1);
  23908. }
  23909. if (ret == ECC_BAD_ARG_E) {
  23910. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  23911. &key3.k, 1);
  23912. }
  23913. if (ret == ECC_BAD_ARG_E) {
  23914. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  23915. &key2.k, NULL, 1);
  23916. }
  23917. if (ret == ECC_BAD_ARG_E) {
  23918. ret = 0;
  23919. }
  23920. else if (ret == 0) {
  23921. ret = WOLFSSL_FATAL_ERROR;
  23922. }
  23923. }
  23924. wc_ecc_free(&key1);
  23925. wc_ecc_free(&key2);
  23926. wc_ecc_free(&key3);
  23927. #ifdef FP_ECC
  23928. wc_ecc_fp_free();
  23929. #endif
  23930. res = TEST_RES_CHECK(ret == 0);
  23931. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  23932. return res;
  23933. } /* END test_wc_ecc_mulmod */
  23934. /*
  23935. * Testing wc_ecc_is_valid_idx()
  23936. */
  23937. static int test_wc_ecc_is_valid_idx(void)
  23938. {
  23939. int res = TEST_SKIPPED;
  23940. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23941. ecc_key key;
  23942. WC_RNG rng;
  23943. int ret;
  23944. int iVal = -2;
  23945. int iVal2 = 3000;
  23946. XMEMSET(&rng, 0, sizeof(rng));
  23947. XMEMSET(&key, 0, sizeof(key));
  23948. ret = wc_InitRng(&rng);
  23949. if (ret == 0) {
  23950. ret = wc_ecc_init(&key);
  23951. if (ret == 0) {
  23952. ret = wc_ecc_make_key(&rng, 32, &key);
  23953. #if defined(WOLFSSL_ASYNC_CRYPT)
  23954. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23955. #endif
  23956. }
  23957. }
  23958. if (ret == 0) {
  23959. ret = wc_ecc_is_valid_idx(key.idx);
  23960. if (ret == 1) {
  23961. ret = 0;
  23962. }
  23963. else {
  23964. ret = WOLFSSL_FATAL_ERROR;
  23965. }
  23966. }
  23967. /* Test bad args. */
  23968. if (ret == 0) {
  23969. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  23970. if (ret == 0) {
  23971. ret = wc_ecc_is_valid_idx(iVal2);
  23972. }
  23973. if (ret != 0) {
  23974. ret = WOLFSSL_FATAL_ERROR;
  23975. }
  23976. }
  23977. if (wc_FreeRng(&rng) && ret == 0) {
  23978. ret = WOLFSSL_FATAL_ERROR;
  23979. }
  23980. wc_ecc_free(&key);
  23981. #ifdef FP_ECC
  23982. wc_ecc_fp_free();
  23983. #endif
  23984. res = TEST_RES_CHECK(ret == 0);
  23985. #endif
  23986. return res;
  23987. } /* END test_wc_ecc_is_valid_idx */
  23988. /*
  23989. * Testing wc_ecc_get_curve_id_from_oid()
  23990. */
  23991. static int test_wc_ecc_get_curve_id_from_oid(void)
  23992. {
  23993. int res = TEST_SKIPPED;
  23994. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23995. !defined(HAVE_FIPS)
  23996. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  23997. word32 len = sizeof(oid);
  23998. int ret;
  23999. /* Bad Cases */
  24000. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  24001. if (ret == BAD_FUNC_ARG) {
  24002. ret = 0;
  24003. }
  24004. if (ret == 0) {
  24005. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  24006. if (ret == ECC_CURVE_INVALID) {
  24007. ret = 0;
  24008. }
  24009. }
  24010. /* Good Case */
  24011. if (ret == 0) {
  24012. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  24013. if (ret == ECC_SECP256R1) {
  24014. ret = 0;
  24015. }
  24016. }
  24017. res = TEST_RES_CHECK(ret == 0);
  24018. #endif
  24019. return res;
  24020. }/* END test_wc_ecc_get_curve_id_from_oid */
  24021. /*
  24022. * Testing wc_ecc_sig_size_calc()
  24023. */
  24024. static int test_wc_ecc_sig_size_calc(void)
  24025. {
  24026. int res = TEST_SKIPPED;
  24027. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  24028. ecc_key key;
  24029. WC_RNG rng;
  24030. int sz = 0;
  24031. int ret = 0;
  24032. ret = wc_InitRng(&rng);
  24033. if (ret == 0) {
  24034. ret = wc_ecc_init(&key);
  24035. if (ret == 0) {
  24036. ret = wc_ecc_make_key(&rng, 16, &key);
  24037. #if defined(WOLFSSL_ASYNC_CRYPT)
  24038. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  24039. #endif
  24040. }
  24041. sz = key.dp->size;
  24042. }
  24043. if (ret == 0) {
  24044. ret = wc_ecc_sig_size_calc(sz);
  24045. if (ret > 0) {
  24046. ret = 0;
  24047. }
  24048. }
  24049. wc_ecc_free(&key);
  24050. wc_FreeRng(&rng);
  24051. res = TEST_RES_CHECK(ret == 0);
  24052. #endif
  24053. return res;
  24054. } /* END test_wc_ecc_sig_size_calc */
  24055. /*
  24056. * Testing ToTraditional
  24057. */
  24058. static int test_ToTraditional(void)
  24059. {
  24060. int res = TEST_SKIPPED;
  24061. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  24062. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  24063. defined(OPENSSL_EXTRA_X509_SMALL))
  24064. XFILE f;
  24065. byte input[TWOK_BUF];
  24066. word32 sz;
  24067. int ret;
  24068. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  24069. AssertTrue((f != XBADFILE));
  24070. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  24071. XFCLOSE(f);
  24072. /* Good case */
  24073. ret = ToTraditional(input, sz);
  24074. if (ret > 0) {
  24075. ret = 0;
  24076. }
  24077. /* Bad cases */
  24078. if (ret == 0) {
  24079. ret = ToTraditional(NULL, 0);
  24080. if (ret == BAD_FUNC_ARG) {
  24081. ret = 0;
  24082. }
  24083. }
  24084. if (ret == 0) {
  24085. ret = ToTraditional(NULL, sz);
  24086. if (ret == BAD_FUNC_ARG) {
  24087. ret = 0;
  24088. }
  24089. }
  24090. if (ret == 0) {
  24091. ret = ToTraditional(input, 0);
  24092. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  24093. ret = 0;
  24094. }
  24095. }
  24096. res = TEST_RES_CHECK(ret == 0);
  24097. #endif
  24098. return res;
  24099. }/* End test_ToTraditional*/
  24100. /*
  24101. * Testing wc_EccPrivateKeyToDer
  24102. */
  24103. static int test_wc_EccPrivateKeyToDer(void)
  24104. {
  24105. int res = TEST_SKIPPED;
  24106. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  24107. byte output[ONEK_BUF];
  24108. ecc_key eccKey;
  24109. WC_RNG rng;
  24110. word32 inLen;
  24111. int ret;
  24112. ret = wc_InitRng(&rng);
  24113. if (ret == 0) {
  24114. ret = wc_ecc_init(&eccKey);
  24115. if (ret == 0) {
  24116. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24117. #if defined(WOLFSSL_ASYNC_CRYPT)
  24118. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24119. #endif
  24120. }
  24121. inLen = (word32)sizeof(output);
  24122. /* Bad Cases */
  24123. if (ret == 0) {
  24124. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  24125. if (ret == BAD_FUNC_ARG) {
  24126. ret = 0;
  24127. }
  24128. }
  24129. if (ret == 0) {
  24130. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  24131. if (ret == BAD_FUNC_ARG) {
  24132. ret = 0;
  24133. }
  24134. }
  24135. if (ret == 0) {
  24136. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  24137. if (ret == LENGTH_ONLY_E) {
  24138. ret = 0;
  24139. }
  24140. }
  24141. if (ret == 0) {
  24142. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  24143. if (ret == BAD_FUNC_ARG) {
  24144. ret = 0;
  24145. }
  24146. }
  24147. /*Good Case */
  24148. if (ret == 0) {
  24149. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  24150. if (ret > 0) {
  24151. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  24152. /* test importing private only into a PKEY struct */
  24153. EC_KEY* ec;
  24154. EVP_PKEY* pkey;
  24155. const unsigned char* der = output;
  24156. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  24157. AssertNotNull(pkey);
  24158. der = output;
  24159. ec = d2i_ECPrivateKey(NULL, &der, ret);
  24160. AssertNotNull(ec);
  24161. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  24162. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  24163. #endif
  24164. ret = 0;
  24165. }
  24166. }
  24167. wc_ecc_free(&eccKey);
  24168. }
  24169. wc_FreeRng(&rng);
  24170. res = TEST_RES_CHECK(ret == 0);
  24171. #endif
  24172. return res;
  24173. }/* End test_wc_EccPrivateKeyToDer*/
  24174. /*
  24175. * Testing wc_DhPublicKeyDecode
  24176. */
  24177. static int test_wc_DhPublicKeyDecode(void)
  24178. {
  24179. int res = TEST_SKIPPED;
  24180. #ifndef NO_DH
  24181. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  24182. DhKey key;
  24183. word32 inOutIdx;
  24184. AssertIntEQ(wc_InitDhKey(&key), 0);
  24185. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  24186. BAD_FUNC_ARG);
  24187. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  24188. BAD_FUNC_ARG);
  24189. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  24190. BAD_FUNC_ARG);
  24191. inOutIdx = 0;
  24192. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  24193. BAD_FUNC_ARG);
  24194. inOutIdx = 0;
  24195. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  24196. BAD_FUNC_ARG);
  24197. inOutIdx = 0;
  24198. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  24199. sizeof_dh_pub_key_der_2048), 0);
  24200. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  24201. key.pub.used != 0 && key.priv.used == 0);
  24202. wc_FreeDhKey(&key);
  24203. res = TEST_RES_CHECK(1);
  24204. #endif
  24205. #endif /* !NO_DH */
  24206. return res;
  24207. }
  24208. /*
  24209. * Testing wc_Ed25519KeyToDer
  24210. */
  24211. static int test_wc_Ed25519KeyToDer(void)
  24212. {
  24213. int res = TEST_SKIPPED;
  24214. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24215. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24216. byte output[ONEK_BUF];
  24217. ed25519_key ed25519Key;
  24218. WC_RNG rng;
  24219. word32 inLen;
  24220. int ret;
  24221. ret = wc_InitRng(&rng);
  24222. if (ret == 0) {
  24223. ret = wc_ed25519_init(&ed25519Key);
  24224. if (ret == 0) {
  24225. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24226. }
  24227. inLen = (word32)sizeof(output);
  24228. /* Bad Cases */
  24229. if (ret == 0) {
  24230. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  24231. if (ret == BAD_FUNC_ARG) {
  24232. ret = 0;
  24233. }
  24234. }
  24235. if (ret == 0) {
  24236. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  24237. if (ret == BAD_FUNC_ARG) {
  24238. ret = 0;
  24239. }
  24240. }
  24241. if (ret == 0) {
  24242. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  24243. if (ret == BAD_FUNC_ARG) {
  24244. ret = 0;
  24245. }
  24246. }
  24247. /* Good Cases */
  24248. if (ret == 0) {
  24249. /* length only */
  24250. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  24251. if (ret > 0) {
  24252. ret = 0;
  24253. }
  24254. }
  24255. if (ret == 0) {
  24256. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  24257. if (ret > 0) {
  24258. ret = 0;
  24259. }
  24260. }
  24261. wc_ed25519_free(&ed25519Key);
  24262. }
  24263. wc_FreeRng(&rng);
  24264. res = TEST_RES_CHECK(ret == 0);
  24265. #endif
  24266. return res;
  24267. }/* End test_wc_Ed25519KeyToDer*/
  24268. /*
  24269. * Testing wc_Ed25519PrivateKeyToDer
  24270. */
  24271. static int test_wc_Ed25519PrivateKeyToDer(void)
  24272. {
  24273. int res = TEST_SKIPPED;
  24274. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24275. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24276. byte output[ONEK_BUF];
  24277. ed25519_key ed25519PrivKey;
  24278. WC_RNG rng;
  24279. word32 inLen;
  24280. int ret;
  24281. ret = wc_InitRng(&rng);
  24282. if (ret == 0) {
  24283. ret = wc_ed25519_init(&ed25519PrivKey);
  24284. if (ret == 0) {
  24285. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  24286. }
  24287. inLen = (word32)sizeof(output);
  24288. /* Bad Cases */
  24289. if (ret == 0) {
  24290. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  24291. if (ret == BAD_FUNC_ARG) {
  24292. ret = 0;
  24293. }
  24294. }
  24295. if (ret == 0) {
  24296. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  24297. if (ret == BAD_FUNC_ARG) {
  24298. ret = 0;
  24299. }
  24300. }
  24301. if (ret == 0) {
  24302. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  24303. if (ret == BAD_FUNC_ARG) {
  24304. ret = 0;
  24305. }
  24306. }
  24307. /* Good Cases */
  24308. if (ret == 0) {
  24309. /* length only */
  24310. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  24311. if (ret > 0) {
  24312. ret = 0;
  24313. }
  24314. }
  24315. if (ret == 0) {
  24316. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  24317. if (ret > 0) {
  24318. ret = 0;
  24319. }
  24320. }
  24321. wc_ed25519_free(&ed25519PrivKey);
  24322. }
  24323. wc_FreeRng(&rng);
  24324. res = TEST_RES_CHECK(ret == 0);
  24325. #endif
  24326. return res;
  24327. }/* End test_wc_Ed25519PrivateKeyToDer*/
  24328. /*
  24329. * Testing wc_Ed448KeyToDer
  24330. */
  24331. static int test_wc_Ed448KeyToDer(void)
  24332. {
  24333. int res = TEST_SKIPPED;
  24334. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  24335. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24336. byte output[ONEK_BUF];
  24337. ed448_key ed448Key;
  24338. WC_RNG rng;
  24339. word32 inLen;
  24340. int ret;
  24341. ret = wc_InitRng(&rng);
  24342. if (ret == 0) {
  24343. ret = wc_ed448_init(&ed448Key);
  24344. if (ret == 0) {
  24345. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24346. }
  24347. inLen = sizeof(output);
  24348. /* Bad Cases */
  24349. if (ret == 0) {
  24350. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  24351. if (ret == BAD_FUNC_ARG) {
  24352. ret = 0;
  24353. }
  24354. }
  24355. if (ret == 0) {
  24356. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  24357. if (ret == BAD_FUNC_ARG) {
  24358. ret = 0;
  24359. }
  24360. }
  24361. if (ret == 0) {
  24362. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  24363. if (ret == BAD_FUNC_ARG) {
  24364. ret = 0;
  24365. }
  24366. }
  24367. /* Good Cases */
  24368. if (ret == 0) {
  24369. /* length only */
  24370. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  24371. if (ret > 0) {
  24372. ret = 0;
  24373. }
  24374. }
  24375. if (ret == 0) {
  24376. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  24377. if (ret > 0) {
  24378. ret = 0;
  24379. }
  24380. }
  24381. wc_ed448_free(&ed448Key);
  24382. }
  24383. wc_FreeRng(&rng);
  24384. res = TEST_RES_CHECK(ret == 0);
  24385. #endif
  24386. return res;
  24387. }/* End test_wc_Ed448KeyToDer*/
  24388. /*
  24389. * Testing wc_Ed448PrivateKeyToDer
  24390. */
  24391. static int test_wc_Ed448PrivateKeyToDer(void)
  24392. {
  24393. int res = TEST_SKIPPED;
  24394. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  24395. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24396. byte output[ONEK_BUF];
  24397. ed448_key ed448PrivKey;
  24398. WC_RNG rng;
  24399. word32 inLen;
  24400. int ret;
  24401. ret = wc_InitRng(&rng);
  24402. if (ret == 0) {
  24403. ret = wc_ed448_init(&ed448PrivKey);
  24404. if (ret == 0) {
  24405. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  24406. }
  24407. inLen = sizeof(output);
  24408. /* Bad Cases */
  24409. if (ret == 0) {
  24410. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  24411. if (ret == BAD_FUNC_ARG) {
  24412. ret = 0;
  24413. }
  24414. }
  24415. if (ret == 0) {
  24416. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  24417. if (ret == BAD_FUNC_ARG) {
  24418. ret = 0;
  24419. }
  24420. }
  24421. if (ret == 0) {
  24422. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  24423. if (ret == BAD_FUNC_ARG) {
  24424. ret = 0;
  24425. }
  24426. }
  24427. /* Good cases */
  24428. if (ret == 0) {
  24429. /* length only */
  24430. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  24431. if (ret > 0) {
  24432. ret = 0;
  24433. }
  24434. }
  24435. if (ret == 0) {
  24436. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  24437. if (ret > 0) {
  24438. ret = 0;
  24439. }
  24440. }
  24441. wc_ed448_free(&ed448PrivKey);
  24442. }
  24443. wc_FreeRng(&rng);
  24444. res = TEST_RES_CHECK(ret == 0);
  24445. #endif
  24446. return res;
  24447. }/* End test_wc_Ed448PrivateKeyToDer*/
  24448. /*
  24449. * Testing wc_SetSubjectBuffer
  24450. */
  24451. static int test_wc_SetSubjectBuffer(void)
  24452. {
  24453. int res = TEST_SKIPPED;
  24454. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  24455. Cert cert;
  24456. FILE* file;
  24457. byte* der;
  24458. word32 derSz;
  24459. int ret = 0;
  24460. derSz = FOURK_BUF;
  24461. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24462. if (der == NULL) {
  24463. ret = -1;
  24464. }
  24465. if (ret == 0) {
  24466. file = XFOPEN("./certs/ca-cert.der", "rb");
  24467. if (file != NULL) {
  24468. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  24469. XFCLOSE(file);
  24470. }
  24471. else {
  24472. ret = -1;
  24473. }
  24474. }
  24475. if (ret == 0) {
  24476. ret = wc_InitCert(&cert);
  24477. }
  24478. if (ret == 0) {
  24479. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  24480. }
  24481. if (ret == 0) {
  24482. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  24483. if (ret == BAD_FUNC_ARG) {
  24484. ret = 0;
  24485. }
  24486. }
  24487. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24488. res = TEST_RES_CHECK(ret == 0);
  24489. #endif
  24490. return res;
  24491. }/* End test_wc_SetSubjectBuffer*/
  24492. /*
  24493. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  24494. */
  24495. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  24496. {
  24497. int res = TEST_SKIPPED;
  24498. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  24499. WC_RNG rng;
  24500. Cert cert;
  24501. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24502. ed25519_key ed25519Key;
  24503. #endif
  24504. #if !defined(NO_RSA) && defined(HAVE_RSA)
  24505. RsaKey rsaKey;
  24506. int bits = 2048;
  24507. #endif
  24508. #if defined(HAVE_ECC)
  24509. ecc_key eccKey;
  24510. #endif
  24511. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24512. ed448_key ed448Key;
  24513. #endif
  24514. int ret = 0;
  24515. #ifndef HAVE_FIPS
  24516. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  24517. #else
  24518. ret = wc_InitRng(&rng);
  24519. #endif
  24520. wc_InitCert(&cert);
  24521. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24522. if (ret == 0) { /*ED25519*/
  24523. ret = wc_ed25519_init(&ed25519Key);
  24524. if (ret == 0) {
  24525. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24526. }
  24527. if (ret == 0) {
  24528. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  24529. &ed25519Key);
  24530. }
  24531. wc_ed25519_free(&ed25519Key);
  24532. }
  24533. #endif
  24534. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  24535. if (ret == 0) { /*RSA*/
  24536. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  24537. if (ret == 0) {
  24538. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  24539. }
  24540. if (ret == 0) {
  24541. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  24542. }
  24543. wc_FreeRsaKey(&rsaKey);
  24544. }
  24545. #endif
  24546. #if defined(HAVE_ECC)
  24547. if (ret == 0) { /*ECC*/
  24548. ret = wc_ecc_init(&eccKey);
  24549. if (ret == 0) {
  24550. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24551. #if defined(WOLFSSL_ASYNC_CRYPT)
  24552. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24553. #endif
  24554. }
  24555. if (ret == 0) {
  24556. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  24557. }
  24558. wc_ecc_free(&eccKey);
  24559. }
  24560. #endif
  24561. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24562. if (ret == 0) { /*ED448*/
  24563. ret = wc_ed448_init(&ed448Key);
  24564. if (ret == 0) {
  24565. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24566. }
  24567. if (ret == 0) {
  24568. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  24569. &ed448Key);
  24570. }
  24571. wc_ed448_free(&ed448Key);
  24572. }
  24573. #endif
  24574. wc_FreeRng(&rng);
  24575. res = TEST_RES_CHECK(ret == 0);
  24576. #endif
  24577. return res;
  24578. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  24579. /*
  24580. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  24581. */
  24582. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  24583. {
  24584. int res = TEST_SKIPPED;
  24585. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  24586. WC_RNG rng;
  24587. Cert cert;
  24588. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24589. ed25519_key ed25519Key;
  24590. #endif
  24591. #if !defined(NO_RSA) && defined(HAVE_RSA)
  24592. RsaKey rsaKey;
  24593. int bits = 2048;
  24594. #endif
  24595. #if defined(HAVE_ECC)
  24596. ecc_key eccKey;
  24597. #endif
  24598. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24599. ed448_key ed448Key;
  24600. #endif
  24601. int ret = 0;
  24602. #ifndef HAVE_FIPS
  24603. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  24604. #else
  24605. ret = wc_InitRng(&rng);
  24606. #endif
  24607. wc_InitCert(&cert);
  24608. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24609. if (ret == 0) { /*ED25519*/
  24610. ret = wc_ed25519_init(&ed25519Key);
  24611. if (ret == 0) {
  24612. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24613. }
  24614. if (ret == 0) {
  24615. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  24616. &ed25519Key);
  24617. }
  24618. wc_ed25519_free(&ed25519Key);
  24619. }
  24620. #endif
  24621. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  24622. if (ret == 0) { /*RSA*/
  24623. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  24624. if (ret == 0) {
  24625. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  24626. }
  24627. if (ret == 0) {
  24628. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  24629. }
  24630. wc_FreeRsaKey(&rsaKey);
  24631. }
  24632. #endif
  24633. #if defined(HAVE_ECC)
  24634. if (ret == 0) { /*ECC*/
  24635. ret = wc_ecc_init(&eccKey);
  24636. if (ret == 0) {
  24637. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24638. #if defined(WOLFSSL_ASYNC_CRYPT)
  24639. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24640. #endif
  24641. }
  24642. if (ret == 0) {
  24643. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  24644. }
  24645. wc_ecc_free(&eccKey);
  24646. }
  24647. #endif
  24648. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24649. if (ret == 0) { /*ED448*/
  24650. ret = wc_ed448_init(&ed448Key);
  24651. if (ret == 0) {
  24652. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24653. }
  24654. if (ret == 0) {
  24655. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  24656. &ed448Key);
  24657. }
  24658. wc_ed448_free(&ed448Key);
  24659. }
  24660. #endif
  24661. wc_FreeRng(&rng);
  24662. res = TEST_RES_CHECK(ret == 0);
  24663. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  24664. return res;
  24665. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  24666. /*
  24667. * Testing wc_PKCS7_New()
  24668. */
  24669. static int test_wc_PKCS7_New(void)
  24670. {
  24671. int res = TEST_SKIPPED;
  24672. #if defined(HAVE_PKCS7)
  24673. PKCS7* pkcs7;
  24674. pkcs7 = wc_PKCS7_New(NULL, testDevId);
  24675. wc_PKCS7_Free(pkcs7);
  24676. res = TEST_RES_CHECK(pkcs7 != NULL);
  24677. #endif
  24678. return res;
  24679. } /* END test-wc_PKCS7_New */
  24680. /*
  24681. * Testing wc_PKCS7_Init()
  24682. */
  24683. static int test_wc_PKCS7_Init(void)
  24684. {
  24685. int res = TEST_SKIPPED;
  24686. #if defined(HAVE_PKCS7)
  24687. PKCS7* pkcs7;
  24688. void* heap = NULL;
  24689. pkcs7 = wc_PKCS7_New(heap, testDevId);
  24690. AssertNotNull(pkcs7);
  24691. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  24692. /* Pass in bad args. */
  24693. AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  24694. wc_PKCS7_Free(pkcs7);
  24695. res = TEST_RES_CHECK(1);
  24696. #endif
  24697. return res;
  24698. } /* END test-wc_PKCS7_Init */
  24699. /*
  24700. * Testing wc_PKCS7_InitWithCert()
  24701. */
  24702. static int test_wc_PKCS7_InitWithCert(void)
  24703. {
  24704. int res = TEST_SKIPPED;
  24705. #if defined(HAVE_PKCS7)
  24706. PKCS7* pkcs7;
  24707. #ifndef NO_RSA
  24708. #if defined(USE_CERT_BUFFERS_2048)
  24709. unsigned char cert[sizeof(client_cert_der_2048)];
  24710. int certSz = (int)sizeof(cert);
  24711. XMEMSET(cert, 0, certSz);
  24712. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  24713. #elif defined(USE_CERT_BUFFERS_1024)
  24714. unsigned char cert[sizeof(client_cert_der_1024)];
  24715. int certSz = (int)sizeof(cert);
  24716. XMEMSET(cert, 0, certSz);
  24717. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  24718. #else
  24719. unsigned char cert[ONEK_BUF];
  24720. XFILE fp;
  24721. int certSz;
  24722. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24723. AssertTrue(fp != XBADFILE);
  24724. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24725. XFCLOSE(fp);
  24726. #endif
  24727. #elif defined(HAVE_ECC)
  24728. #if defined(USE_CERT_BUFFERS_256)
  24729. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24730. int certSz = (int)sizeof(cert);
  24731. XMEMSET(cert, 0, certSz);
  24732. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  24733. #else
  24734. unsigned char cert[ONEK_BUF];
  24735. XFILE fp;
  24736. int certSz;
  24737. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24738. AssertTrue(fp != XBADFILE);
  24739. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  24740. XFCLOSE(fp);
  24741. #endif
  24742. #else
  24743. #error PKCS7 requires ECC or RSA
  24744. #endif
  24745. #ifdef HAVE_ECC
  24746. {
  24747. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  24748. static unsigned char certWithInvalidEccKey[] = {
  24749. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  24750. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  24751. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  24752. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  24753. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24754. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  24755. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24756. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24757. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24758. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24759. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24760. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24761. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24762. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24763. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24764. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  24765. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  24766. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  24767. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  24768. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  24769. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  24770. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  24771. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  24772. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  24773. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  24774. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  24775. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24776. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  24777. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24778. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  24779. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  24780. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  24781. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  24782. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  24783. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  24784. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  24785. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  24786. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  24787. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  24788. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  24789. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24790. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  24791. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  24792. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24793. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  24794. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24795. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  24796. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24797. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24798. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24799. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24800. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24801. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24802. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24803. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24804. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24805. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  24806. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  24807. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  24808. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  24809. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  24810. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  24811. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  24812. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  24813. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  24814. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  24815. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  24816. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  24817. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  24818. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  24819. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  24820. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  24821. 0x08, 0xA8, 0xB4};
  24822. #endif
  24823. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24824. /* If initialization is not successful, it's free'd in init func. */
  24825. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  24826. wc_PKCS7_Free(pkcs7);
  24827. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24828. /* Valid initialization usage. */
  24829. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24830. /* Pass in bad args. No need free for null checks, free at end.*/
  24831. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  24832. BAD_FUNC_ARG);
  24833. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  24834. BAD_FUNC_ARG);
  24835. #ifdef HAVE_ECC
  24836. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  24837. sizeof(certWithInvalidEccKey)), 0);
  24838. }
  24839. #endif
  24840. wc_PKCS7_Free(pkcs7);
  24841. res = TEST_RES_CHECK(1);
  24842. #endif
  24843. return res;
  24844. } /* END test_wc_PKCS7_InitWithCert */
  24845. /*
  24846. * Testing wc_PKCS7_EncodeData()
  24847. */
  24848. static int test_wc_PKCS7_EncodeData(void)
  24849. {
  24850. int res = TEST_SKIPPED;
  24851. #if defined(HAVE_PKCS7)
  24852. PKCS7* pkcs7;
  24853. byte output[FOURK_BUF];
  24854. byte data[] = "My encoded DER cert.";
  24855. #ifndef NO_RSA
  24856. #if defined(USE_CERT_BUFFERS_2048)
  24857. unsigned char cert[sizeof(client_cert_der_2048)];
  24858. unsigned char key[sizeof(client_key_der_2048)];
  24859. int certSz = (int)sizeof(cert);
  24860. int keySz = (int)sizeof(key);
  24861. XMEMSET(cert, 0, certSz);
  24862. XMEMSET(key, 0, keySz);
  24863. XMEMCPY(cert, client_cert_der_2048, certSz);
  24864. XMEMCPY(key, client_key_der_2048, keySz);
  24865. #elif defined(USE_CERT_BUFFERS_1024)
  24866. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  24867. unsigned char key[sizeof_client_key_der_1024];
  24868. int certSz = (int)sizeof(cert);
  24869. int keySz = (int)sizeof(key);
  24870. XMEMSET(cert, 0, certSz);
  24871. XMEMSET(key, 0, keySz);
  24872. XMEMCPY(cert, client_cert_der_1024, certSz);
  24873. XMEMCPY(key, client_key_der_1024, keySz);
  24874. #else
  24875. unsigned char cert[ONEK_BUF];
  24876. unsigned char key[ONEK_BUF];
  24877. XFILE fp;
  24878. int certSz;
  24879. int keySz;
  24880. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24881. AssertTrue(fp != XBADFILE);
  24882. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24883. XFCLOSE(fp);
  24884. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24885. AssertTrue(fp != XBADFILE);
  24886. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24887. XFCLOSE(fp);
  24888. #endif
  24889. #elif defined(HAVE_ECC)
  24890. #if defined(USE_CERT_BUFFERS_256)
  24891. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24892. unsigned char key[sizeof(ecc_clikey_der_256)];
  24893. int certSz = (int)sizeof(cert);
  24894. int keySz = (int)sizeof(key);
  24895. XMEMSET(cert, 0, certSz);
  24896. XMEMSET(key, 0, keySz);
  24897. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24898. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24899. #else
  24900. unsigned char cert[ONEK_BUF];
  24901. unsigned char key[ONEK_BUF];
  24902. XFILE fp;
  24903. int certSz, keySz;
  24904. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24905. AssertTrue(fp != XBADFILE);
  24906. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24907. XFCLOSE(fp);
  24908. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24909. AssertTrue(fp != XBADFILE);
  24910. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24911. XFCLOSE(fp);
  24912. #endif
  24913. #endif
  24914. XMEMSET(output, 0, sizeof(output));
  24915. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24916. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24917. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  24918. pkcs7->content = data;
  24919. pkcs7->contentSz = sizeof(data);
  24920. pkcs7->privateKey = key;
  24921. pkcs7->privateKeySz = keySz;
  24922. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  24923. /* Test bad args. */
  24924. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  24925. BAD_FUNC_ARG);
  24926. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  24927. BAD_FUNC_ARG);
  24928. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  24929. wc_PKCS7_Free(pkcs7);
  24930. res = TEST_RES_CHECK(1);
  24931. #endif
  24932. return res;
  24933. } /* END test_wc_PKCS7_EncodeData */
  24934. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24935. !defined(NO_RSA) && !defined(NO_SHA256)
  24936. /* RSA sign raw digest callback */
  24937. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  24938. byte* out, word32 outSz, byte* privateKey,
  24939. word32 privateKeySz, int devid, int hashOID)
  24940. {
  24941. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  24942. byte digInfoEncoding[] = {
  24943. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  24944. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  24945. 0x00, 0x04, 0x20
  24946. };
  24947. int ret;
  24948. byte digestInfo[ONEK_BUF];
  24949. byte sig[FOURK_BUF];
  24950. word32 digestInfoSz = 0;
  24951. word32 idx = 0;
  24952. RsaKey rsa;
  24953. /* SHA-256 required only for this example callback due to above
  24954. * digInfoEncoding[] */
  24955. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24956. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24957. (hashOID != SHA256h)) {
  24958. return -1;
  24959. }
  24960. /* build DigestInfo */
  24961. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24962. digestInfoSz += sizeof(digInfoEncoding);
  24963. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24964. digestInfoSz += digestSz;
  24965. /* set up RSA key */
  24966. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24967. if (ret != 0) {
  24968. return ret;
  24969. }
  24970. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24971. /* sign DigestInfo */
  24972. if (ret == 0) {
  24973. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24974. &rsa, pkcs7->rng);
  24975. if (ret > 0) {
  24976. if (ret > (int)outSz) {
  24977. /* output buffer too small */
  24978. ret = -1;
  24979. }
  24980. else {
  24981. /* success, ret holds sig size */
  24982. XMEMCPY(out, sig, ret);
  24983. }
  24984. }
  24985. }
  24986. wc_FreeRsaKey(&rsa);
  24987. return ret;
  24988. }
  24989. #endif
  24990. /*
  24991. * Testing wc_PKCS7_EncodeSignedData()
  24992. */
  24993. static int test_wc_PKCS7_EncodeSignedData(void)
  24994. {
  24995. int res = TEST_SKIPPED;
  24996. #if defined(HAVE_PKCS7)
  24997. PKCS7* pkcs7;
  24998. WC_RNG rng;
  24999. byte output[FOURK_BUF];
  25000. byte badOut[1];
  25001. word32 outputSz = (word32)sizeof(output);
  25002. word32 badOutSz = 0;
  25003. byte data[] = "Test data to encode.";
  25004. #ifndef NO_RSA
  25005. #if defined(USE_CERT_BUFFERS_2048)
  25006. byte key[sizeof(client_key_der_2048)];
  25007. byte cert[sizeof(client_cert_der_2048)];
  25008. word32 keySz = (word32)sizeof(key);
  25009. word32 certSz = (word32)sizeof(cert);
  25010. XMEMSET(key, 0, keySz);
  25011. XMEMSET(cert, 0, certSz);
  25012. XMEMCPY(key, client_key_der_2048, keySz);
  25013. XMEMCPY(cert, client_cert_der_2048, certSz);
  25014. #elif defined(USE_CERT_BUFFERS_1024)
  25015. byte key[sizeof_client_key_der_1024];
  25016. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25017. word32 keySz = (word32)sizeof(key);
  25018. word32 certSz = (word32)sizeof(cert);
  25019. XMEMSET(key, 0, keySz);
  25020. XMEMSET(cert, 0, certSz);
  25021. XMEMCPY(key, client_key_der_1024, keySz);
  25022. XMEMCPY(cert, client_cert_der_1024, certSz);
  25023. #else
  25024. unsigned char cert[ONEK_BUF];
  25025. unsigned char key[ONEK_BUF];
  25026. XFILE fp;
  25027. int certSz;
  25028. int keySz;
  25029. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  25030. AssertTrue(fp != XBADFILE);
  25031. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  25032. XFCLOSE(fp);
  25033. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  25034. AssertTrue(fp != XBADFILE);
  25035. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  25036. XFCLOSE(fp);
  25037. #endif
  25038. #elif defined(HAVE_ECC)
  25039. #if defined(USE_CERT_BUFFERS_256)
  25040. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25041. unsigned char key[sizeof(ecc_clikey_der_256)];
  25042. int certSz = (int)sizeof(cert);
  25043. int keySz = (int)sizeof(key);
  25044. XMEMSET(cert, 0, certSz);
  25045. XMEMSET(key, 0, keySz);
  25046. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  25047. XMEMCPY(key, ecc_clikey_der_256, keySz);
  25048. #else
  25049. unsigned char cert[ONEK_BUF];
  25050. unsigned char key[ONEK_BUF];
  25051. XFILE fp;
  25052. int certSz, keySz;
  25053. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  25054. AssertTrue(fp != XBADFILE);
  25055. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  25056. XFCLOSE(fp);
  25057. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  25058. AssertTrue(fp != XBADFILE);
  25059. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  25060. XFCLOSE(fp);
  25061. #endif
  25062. #endif
  25063. XMEMSET(output, 0, outputSz);
  25064. AssertIntEQ(wc_InitRng(&rng), 0);
  25065. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25066. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25067. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25068. pkcs7->content = data;
  25069. pkcs7->contentSz = (word32)sizeof(data);
  25070. pkcs7->privateKey = key;
  25071. pkcs7->privateKeySz = (word32)sizeof(key);
  25072. pkcs7->encryptOID = RSAk;
  25073. #ifdef NO_SHA
  25074. pkcs7->hashOID = SHA256h;
  25075. #else
  25076. pkcs7->hashOID = SHAh;
  25077. #endif
  25078. pkcs7->rng = &rng;
  25079. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25080. wc_PKCS7_Free(pkcs7);
  25081. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25082. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25083. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25084. /* Pass in bad args. */
  25085. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  25086. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  25087. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  25088. badOutSz), BAD_FUNC_ARG);
  25089. pkcs7->hashOID = 0; /* bad hashOID */
  25090. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  25091. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  25092. !defined(NO_RSA) && !defined(NO_SHA256)
  25093. /* test RSA sign raw digest callback, if using RSA and compiled in.
  25094. * Example callback assumes SHA-256, so only run test if compiled in. */
  25095. wc_PKCS7_Free(pkcs7);
  25096. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25097. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25098. pkcs7->content = data;
  25099. pkcs7->contentSz = (word32)sizeof(data);
  25100. pkcs7->privateKey = key;
  25101. pkcs7->privateKeySz = (word32)sizeof(key);
  25102. pkcs7->encryptOID = RSAk;
  25103. pkcs7->hashOID = SHA256h;
  25104. pkcs7->rng = &rng;
  25105. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  25106. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25107. #endif
  25108. wc_PKCS7_Free(pkcs7);
  25109. wc_FreeRng(&rng);
  25110. res = TEST_RES_CHECK(1);
  25111. #endif
  25112. return res;
  25113. } /* END test_wc_PKCS7_EncodeSignedData */
  25114. /*
  25115. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  25116. */
  25117. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  25118. {
  25119. int res = TEST_SKIPPED;
  25120. #if defined(HAVE_PKCS7)
  25121. int ret, i;
  25122. PKCS7* pkcs7;
  25123. WC_RNG rng;
  25124. byte outputHead[FOURK_BUF/2];
  25125. byte outputFoot[FOURK_BUF/2];
  25126. word32 outputHeadSz = (word32)sizeof(outputHead);
  25127. word32 outputFootSz = (word32)sizeof(outputFoot);
  25128. byte data[FOURK_BUF];
  25129. wc_HashAlg hash;
  25130. #ifdef NO_SHA
  25131. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25132. #else
  25133. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25134. #endif
  25135. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25136. word32 hashSz = wc_HashGetDigestSize(hashType);
  25137. #ifndef NO_RSA
  25138. #if defined(USE_CERT_BUFFERS_2048)
  25139. byte key[sizeof(client_key_der_2048)];
  25140. byte cert[sizeof(client_cert_der_2048)];
  25141. word32 keySz = (word32)sizeof(key);
  25142. word32 certSz = (word32)sizeof(cert);
  25143. XMEMSET(key, 0, keySz);
  25144. XMEMSET(cert, 0, certSz);
  25145. XMEMCPY(key, client_key_der_2048, keySz);
  25146. XMEMCPY(cert, client_cert_der_2048, certSz);
  25147. #elif defined(USE_CERT_BUFFERS_1024)
  25148. byte key[sizeof_client_key_der_1024];
  25149. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25150. word32 keySz = (word32)sizeof(key);
  25151. word32 certSz = (word32)sizeof(cert);
  25152. XMEMSET(key, 0, keySz);
  25153. XMEMSET(cert, 0, certSz);
  25154. XMEMCPY(key, client_key_der_1024, keySz);
  25155. XMEMCPY(cert, client_cert_der_1024, certSz);
  25156. #else
  25157. unsigned char cert[ONEK_BUF];
  25158. unsigned char key[ONEK_BUF];
  25159. XFILE fp;
  25160. int certSz;
  25161. int keySz;
  25162. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  25163. AssertTrue((fp != XBADFILE));
  25164. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  25165. XFCLOSE(fp);
  25166. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  25167. AssertTrue(fp != XBADFILE);
  25168. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  25169. XFCLOSE(fp);
  25170. #endif
  25171. #elif defined(HAVE_ECC)
  25172. #if defined(USE_CERT_BUFFERS_256)
  25173. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25174. unsigned char key[sizeof(ecc_clikey_der_256)];
  25175. int certSz = (int)sizeof(cert);
  25176. int keySz = (int)sizeof(key);
  25177. XMEMSET(cert, 0, certSz);
  25178. XMEMSET(key, 0, keySz);
  25179. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  25180. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  25181. #else
  25182. unsigned char cert[ONEK_BUF];
  25183. unsigned char key[ONEK_BUF];
  25184. XFILE fp;
  25185. int certSz, keySz;
  25186. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  25187. AssertTrue(fp != XBADFILE);
  25188. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  25189. XFCLOSE(fp);
  25190. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  25191. AssertTrue(fp != XBADFILE);
  25192. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  25193. XFCLOSE(fp);
  25194. #endif
  25195. #endif
  25196. /* initialize large data with sequence */
  25197. for (i=0; i<(int)sizeof(data); i++)
  25198. data[i] = i & 0xff;
  25199. XMEMSET(outputHead, 0, outputHeadSz);
  25200. XMEMSET(outputFoot, 0, outputFootSz);
  25201. AssertIntEQ(wc_InitRng(&rng), 0);
  25202. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25203. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25204. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25205. pkcs7->content = NULL; /* not used for ex */
  25206. pkcs7->contentSz = (word32)sizeof(data);
  25207. pkcs7->privateKey = key;
  25208. pkcs7->privateKeySz = (word32)sizeof(key);
  25209. pkcs7->encryptOID = RSAk;
  25210. #ifdef NO_SHA
  25211. pkcs7->hashOID = SHA256h;
  25212. #else
  25213. pkcs7->hashOID = SHAh;
  25214. #endif
  25215. pkcs7->rng = &rng;
  25216. /* calculate hash for content */
  25217. ret = wc_HashInit(&hash, hashType);
  25218. if (ret == 0) {
  25219. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25220. if (ret == 0) {
  25221. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25222. }
  25223. wc_HashFree(&hash, hashType);
  25224. }
  25225. AssertIntEQ(ret, 0);
  25226. /* Perform PKCS7 sign using hash directly */
  25227. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25228. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  25229. AssertIntGT(outputHeadSz, 0);
  25230. AssertIntGT(outputFootSz, 0);
  25231. wc_PKCS7_Free(pkcs7);
  25232. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25233. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25234. /* required parameter even on verify when using _ex, if using outputHead
  25235. * and outputFoot */
  25236. pkcs7->contentSz = (word32)sizeof(data);
  25237. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25238. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  25239. wc_PKCS7_Free(pkcs7);
  25240. /* assembly complete PKCS7 sign and use normal verify */
  25241. {
  25242. byte* output = (byte*)XMALLOC(
  25243. outputHeadSz + sizeof(data) + outputFootSz,
  25244. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25245. word32 outputSz = 0;
  25246. AssertNotNull(output);
  25247. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  25248. outputSz += outputHeadSz;
  25249. XMEMCPY(&output[outputSz], data, sizeof(data));
  25250. outputSz += sizeof(data);
  25251. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  25252. outputSz += outputFootSz;
  25253. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25254. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25255. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25256. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25257. }
  25258. /* Pass in bad args. */
  25259. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  25260. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25261. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  25262. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25263. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25264. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25265. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  25266. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25267. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25268. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25269. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25270. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  25271. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25272. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  25273. pkcs7->hashOID = 0; /* bad hashOID */
  25274. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25275. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25276. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  25277. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25278. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  25279. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25280. #ifndef NO_PKCS7_STREAM
  25281. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25282. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  25283. #else
  25284. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25285. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  25286. #endif
  25287. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  25288. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25289. #ifndef NO_PKCS7_STREAM
  25290. /* can pass in 0 buffer length with streaming API */
  25291. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25292. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  25293. #else
  25294. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25295. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25296. #endif
  25297. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25298. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  25299. #ifndef NO_PKCS7_STREAM
  25300. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25301. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  25302. #else
  25303. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25304. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  25305. #endif
  25306. wc_PKCS7_Free(pkcs7);
  25307. wc_FreeRng(&rng);
  25308. res = TEST_RES_CHECK(1);
  25309. #endif
  25310. return res;
  25311. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  25312. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25313. /**
  25314. * Loads certs/keys from files or buffers into the argument buffers,
  25315. * helper function called by CreatePKCS7SignedData().
  25316. *
  25317. * Returns 0 on success, negative on error.
  25318. */
  25319. static int LoadPKCS7SignedDataCerts(
  25320. int useIntermediateCertChain, int pkAlgoType,
  25321. byte* intCARoot, word32* intCARootSz,
  25322. byte* intCA1, word32* intCA1Sz,
  25323. byte* intCA2, word32* intCA2Sz,
  25324. byte* cert, word32* certSz,
  25325. byte* key, word32* keySz)
  25326. {
  25327. int ret = 0;
  25328. FILE* fp = NULL;
  25329. #ifndef NO_RSA
  25330. const char* intCARootRSA = "./certs/ca-cert.der";
  25331. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  25332. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  25333. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  25334. const char* intServKeyRSA = "./certs/server-key.der";
  25335. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  25336. const char* cli1024Cert = "./certs/1024/client-cert.der";
  25337. const char* cli1024Key = "./certs/1024/client-key.der";
  25338. #endif
  25339. #endif
  25340. #ifdef HAVE_ECC
  25341. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  25342. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  25343. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  25344. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  25345. const char* intServKeyECC = "./certs/ecc-key.der";
  25346. #ifndef USE_CERT_BUFFERS_256
  25347. const char* cliEccCert = "./certs/client-ecc-cert.der";
  25348. const char* cliEccKey = "./certs/client-ecc-key.der";
  25349. #endif
  25350. #endif
  25351. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  25352. ((useIntermediateCertChain == 1) &&
  25353. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  25354. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  25355. return BAD_FUNC_ARG;
  25356. }
  25357. /* Read/load certs and keys to use for signing based on PK type and chain */
  25358. switch (pkAlgoType) {
  25359. #ifndef NO_RSA
  25360. case RSA_TYPE:
  25361. if (useIntermediateCertChain == 1) {
  25362. fp = XFOPEN(intCARootRSA, "rb");
  25363. AssertNotNull(fp);
  25364. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  25365. XFCLOSE(fp);
  25366. AssertIntGT(*intCARootSz, 0);
  25367. fp = XFOPEN(intCA1RSA, "rb");
  25368. AssertNotNull(fp);
  25369. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  25370. XFCLOSE(fp);
  25371. AssertIntGT(*intCA1Sz, 0);
  25372. fp = XFOPEN(intCA2RSA, "rb");
  25373. AssertNotNull(fp);
  25374. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  25375. XFCLOSE(fp);
  25376. AssertIntGT(*intCA2Sz, 0);
  25377. fp = XFOPEN(intServCertRSA, "rb");
  25378. AssertNotNull(fp);
  25379. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25380. XFCLOSE(fp);
  25381. AssertIntGT(*certSz, 0);
  25382. fp = XFOPEN(intServKeyRSA, "rb");
  25383. AssertNotNull(fp);
  25384. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25385. XFCLOSE(fp);
  25386. AssertIntGT(*keySz, 0);
  25387. }
  25388. else {
  25389. #if defined(USE_CERT_BUFFERS_2048)
  25390. *keySz = sizeof_client_key_der_2048;
  25391. *certSz = sizeof_client_cert_der_2048;
  25392. XMEMCPY(key, client_key_der_2048, *keySz);
  25393. XMEMCPY(cert, client_cert_der_2048, *certSz);
  25394. #elif defined(USE_CERT_BUFFERS_1024)
  25395. *keySz = sizeof_client_key_der_1024;
  25396. *certSz = sizeof_client_cert_der_1024;
  25397. XMEMCPY(key, client_key_der_1024, *keySz);
  25398. XMEMCPY(cert, client_cert_der_1024, *certSz);
  25399. #else
  25400. fp = XFOPEN(cli1024Key, "rb");
  25401. AssertNotNull(fp);
  25402. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25403. XFCLOSE(fp);
  25404. AssertIntGT(*keySz, 0);
  25405. fp = XFOPEN(cli1024Cert, "rb");
  25406. AssertNotNull(fp);
  25407. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25408. XFCLOSE(fp);
  25409. AssertIntGT(*certSz, 0);
  25410. #endif /* USE_CERT_BUFFERS_2048 */
  25411. }
  25412. break;
  25413. #endif /* !NO_RSA */
  25414. #ifdef HAVE_ECC
  25415. case ECC_TYPE:
  25416. if (useIntermediateCertChain == 1) {
  25417. fp = XFOPEN(intCARootECC, "rb");
  25418. AssertNotNull(fp);
  25419. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  25420. XFCLOSE(fp);
  25421. AssertIntGT(*intCARootSz, 0);
  25422. fp = XFOPEN(intCA1ECC, "rb");
  25423. AssertNotNull(fp);
  25424. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  25425. XFCLOSE(fp);
  25426. AssertIntGT(*intCA1Sz, 0);
  25427. fp = XFOPEN(intCA2ECC, "rb");
  25428. AssertNotNull(fp);
  25429. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  25430. XFCLOSE(fp);
  25431. AssertIntGT(*intCA2Sz, 0);
  25432. fp = XFOPEN(intServCertECC, "rb");
  25433. AssertNotNull(fp);
  25434. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25435. XFCLOSE(fp);
  25436. AssertIntGT(*certSz, 0);
  25437. fp = XFOPEN(intServKeyECC, "rb");
  25438. AssertNotNull(fp);
  25439. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25440. XFCLOSE(fp);
  25441. AssertIntGT(*keySz, 0);
  25442. }
  25443. else {
  25444. #if defined(USE_CERT_BUFFERS_256)
  25445. *keySz = sizeof_ecc_clikey_der_256;
  25446. *certSz = sizeof_cliecc_cert_der_256;
  25447. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  25448. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  25449. #else
  25450. fp = XFOPEN(cliEccKey, "rb");
  25451. AssertNotNull(fp);
  25452. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25453. XFCLOSE(fp);
  25454. AssertIntGT(*keySz, 0);
  25455. fp = XFOPEN(cliEccCert, "rb");
  25456. AssertNotNull(fp);
  25457. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25458. XFCLOSE(fp);
  25459. AssertIntGT(*certSz, 0);
  25460. #endif /* USE_CERT_BUFFERS_256 */
  25461. }
  25462. break;
  25463. #endif /* HAVE_ECC */
  25464. default:
  25465. WOLFSSL_MSG("Unsupported SignedData PK type");
  25466. ret = BAD_FUNC_ARG;
  25467. break;
  25468. }
  25469. return ret;
  25470. }
  25471. /**
  25472. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  25473. *
  25474. * output output buffer to place SignedData
  25475. * outputSz size of output buffer
  25476. * data data buffer to be signed
  25477. * dataSz size of data buffer
  25478. * withAttribs [1/0] include attributes in SignedData message
  25479. * detachedSig [1/0] create detached signature, no content
  25480. * useIntCertChain [1/0] use certificate chain and include intermediate and
  25481. * root CAs in bundle
  25482. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  25483. *
  25484. * Return size of bundle created on success, negative on error */
  25485. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  25486. byte* data, word32 dataSz,
  25487. int withAttribs, int detachedSig,
  25488. int useIntermediateCertChain,
  25489. int pkAlgoType)
  25490. {
  25491. int ret = 0;
  25492. WC_RNG rng;
  25493. PKCS7* pkcs7 = NULL;
  25494. static byte messageTypeOid[] =
  25495. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  25496. 0x09, 0x02 };
  25497. static byte messageType[] = { 0x13, 2, '1', '9' };
  25498. PKCS7Attrib attribs[] =
  25499. {
  25500. { messageTypeOid, sizeof(messageTypeOid), messageType,
  25501. sizeof(messageType) }
  25502. };
  25503. byte intCARoot[TWOK_BUF];
  25504. byte intCA1[TWOK_BUF];
  25505. byte intCA2[TWOK_BUF];
  25506. byte cert[TWOK_BUF];
  25507. byte key[TWOK_BUF];
  25508. word32 intCARootSz = sizeof(intCARoot);
  25509. word32 intCA1Sz = sizeof(intCA1);
  25510. word32 intCA2Sz = sizeof(intCA2);
  25511. word32 certSz = sizeof(cert);
  25512. word32 keySz = sizeof(key);
  25513. XMEMSET(intCARoot, 0, intCARootSz);
  25514. XMEMSET(intCA1, 0, intCA1Sz);
  25515. XMEMSET(intCA2, 0, intCA2Sz);
  25516. XMEMSET(cert, 0, certSz);
  25517. XMEMSET(key, 0, keySz);
  25518. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  25519. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  25520. cert, &certSz, key, &keySz);
  25521. AssertIntEQ(ret, 0);
  25522. XMEMSET(output, 0, outputSz);
  25523. AssertIntEQ(wc_InitRng(&rng), 0);
  25524. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25525. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25526. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25527. if (useIntermediateCertChain == 1) {
  25528. /* Add intermediate and root CA certs into SignedData Certs SET */
  25529. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  25530. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  25531. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  25532. }
  25533. pkcs7->content = data;
  25534. pkcs7->contentSz = dataSz;
  25535. pkcs7->privateKey = key;
  25536. pkcs7->privateKeySz = (word32)sizeof(key);
  25537. if (pkAlgoType == RSA_TYPE) {
  25538. pkcs7->encryptOID = RSAk;
  25539. }
  25540. else {
  25541. pkcs7->encryptOID = ECDSAk;
  25542. }
  25543. #ifdef NO_SHA
  25544. pkcs7->hashOID = SHA256h;
  25545. #else
  25546. pkcs7->hashOID = SHAh;
  25547. #endif
  25548. pkcs7->rng = &rng;
  25549. if (withAttribs) {
  25550. /* include a signed attribute */
  25551. pkcs7->signedAttribs = attribs;
  25552. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  25553. }
  25554. if (detachedSig) {
  25555. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  25556. }
  25557. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  25558. AssertIntGT(outputSz, 0);
  25559. wc_PKCS7_Free(pkcs7);
  25560. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25561. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25562. if (detachedSig) {
  25563. pkcs7->content = data;
  25564. pkcs7->contentSz = dataSz;
  25565. }
  25566. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25567. wc_PKCS7_Free(pkcs7);
  25568. wc_FreeRng(&rng);
  25569. return outputSz;
  25570. }
  25571. #endif
  25572. /*
  25573. * Testing wc_PKCS_VerifySignedData()
  25574. */
  25575. static int test_wc_PKCS7_VerifySignedData(void)
  25576. {
  25577. int res = TEST_SKIPPED;
  25578. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25579. PKCS7* pkcs7;
  25580. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25581. word32 outputSz = sizeof(output);
  25582. byte data[] = "Test data to encode.";
  25583. byte badOut[1];
  25584. word32 badOutSz = 0;
  25585. byte badContent[] = "This is different content than was signed";
  25586. int ret;
  25587. wc_HashAlg hash;
  25588. #ifdef NO_SHA
  25589. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25590. #else
  25591. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25592. #endif
  25593. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25594. word32 hashSz = wc_HashGetDigestSize(hashType);
  25595. #ifndef NO_RSA
  25596. PKCS7DecodedAttrib* decodedAttrib = NULL;
  25597. /* contentType OID (1.2.840.113549.1.9.3) */
  25598. static const byte contentTypeOid[] =
  25599. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  25600. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  25601. static const byte dataType[] =
  25602. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  25603. /* messageDigest OID (1.2.840.113549.1.9.4) */
  25604. static const byte messageDigestOid[] =
  25605. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  25606. #ifndef NO_ASN_TIME
  25607. /* signingTime OID () */
  25608. static const byte signingTimeOid[] =
  25609. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  25610. #endif
  25611. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  25612. int dateLength = 0;
  25613. byte dateFormat;
  25614. const byte* datePart = NULL;
  25615. struct tm timearg;
  25616. time_t now;
  25617. struct tm* nowTm = NULL;
  25618. #ifdef NEED_TMP_TIME
  25619. struct tm tmpTimeStorage;
  25620. struct tm* tmpTime = &tmpTimeStorage;
  25621. #endif
  25622. #endif /* !NO_ASN && !NO_ASN_TIME */
  25623. /* Success test with RSA certs/key */
  25624. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25625. (word32)sizeof(data),
  25626. 0, 0, 0, RSA_TYPE)), 0);
  25627. /* calculate hash for content, used later */
  25628. ret = wc_HashInit(&hash, hashType);
  25629. if (ret == 0) {
  25630. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25631. if (ret == 0) {
  25632. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25633. }
  25634. wc_HashFree(&hash, hashType);
  25635. }
  25636. AssertIntEQ(ret, 0);
  25637. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25638. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25639. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25640. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25641. /* Check that decoded signed attributes are correct */
  25642. /* messageDigest should be first */
  25643. decodedAttrib = pkcs7->decodedAttrib;
  25644. AssertNotNull(decodedAttrib);
  25645. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  25646. AssertIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  25647. decodedAttrib->oidSz), 0);
  25648. /* + 2 for OCTET STRING and length bytes */
  25649. AssertIntEQ(decodedAttrib->valueSz, hashSz + 2);
  25650. AssertNotNull(decodedAttrib->value);
  25651. AssertIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  25652. #ifndef NO_ASN_TIME
  25653. /* signingTime should be second */
  25654. decodedAttrib = decodedAttrib->next;
  25655. AssertNotNull(decodedAttrib);
  25656. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  25657. AssertIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  25658. decodedAttrib->oidSz), 0);
  25659. AssertIntGT(decodedAttrib->valueSz, 0);
  25660. AssertNotNull(decodedAttrib->value);
  25661. #endif
  25662. /* Verify signingTime if ASN and time are available */
  25663. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  25664. AssertIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  25665. &datePart, &dateFormat, &dateLength), 0);
  25666. AssertNotNull(datePart);
  25667. AssertIntGT(dateLength, 0);
  25668. XMEMSET(&timearg, 0, sizeof(timearg));
  25669. AssertIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  25670. &timearg), 0);
  25671. /* Get current time and compare year/month/day against attribute value */
  25672. AssertIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  25673. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  25674. AssertNotNull(nowTm);
  25675. AssertIntEQ(timearg.tm_year, nowTm->tm_year);
  25676. AssertIntEQ(timearg.tm_mon, nowTm->tm_mon);
  25677. AssertIntEQ(timearg.tm_mday, nowTm->tm_mday);
  25678. #endif /* !NO_ASN && !NO_ASN_TIME */
  25679. /* contentType should be third */
  25680. decodedAttrib = decodedAttrib->next;
  25681. AssertNotNull(decodedAttrib);
  25682. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  25683. AssertIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  25684. decodedAttrib->oidSz), 0);
  25685. AssertIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  25686. AssertNotNull(decodedAttrib->value);
  25687. AssertIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType,
  25688. sizeof(dataType)), 0);
  25689. #endif /* !NO_RSA */
  25690. #ifdef HAVE_ECC
  25691. #ifndef NO_RSA
  25692. wc_PKCS7_Free(pkcs7);
  25693. #endif
  25694. /* Success test with ECC certs/key */
  25695. outputSz = sizeof(output);
  25696. XMEMSET(output, 0, outputSz);
  25697. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25698. (word32)sizeof(data),
  25699. 0, 0, 0, ECC_TYPE)), 0);
  25700. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25701. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25702. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25703. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25704. #endif /* HAVE_ECC */
  25705. /* Test bad args. */
  25706. #if !defined(NO_RSA) || defined(HAVE_ECC)
  25707. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  25708. BAD_FUNC_ARG);
  25709. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  25710. BAD_FUNC_ARG);
  25711. #ifndef NO_PKCS7_STREAM
  25712. /* can pass in 0 buffer length with streaming API */
  25713. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25714. badOutSz), WC_PKCS7_WANT_READ_E);
  25715. #else
  25716. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25717. badOutSz), BAD_FUNC_ARG);
  25718. #endif
  25719. wc_PKCS7_Free(pkcs7);
  25720. #endif /* !NO_RSA || HAVE_ECC */
  25721. /* Invalid content should error, use detached signature so we can
  25722. * easily change content */
  25723. #ifndef NO_RSA
  25724. /* Try RSA certs/key/sig first */
  25725. outputSz = sizeof(output);
  25726. XMEMSET(output, 0, outputSz);
  25727. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25728. (word32)sizeof(data),
  25729. 1, 1, 0, RSA_TYPE)), 0);
  25730. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25731. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25732. pkcs7->content = badContent;
  25733. pkcs7->contentSz = sizeof(badContent);
  25734. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25735. SIG_VERIFY_E);
  25736. wc_PKCS7_Free(pkcs7);
  25737. /* Test success case with detached signature and valid content */
  25738. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25739. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25740. pkcs7->content = data;
  25741. pkcs7->contentSz = sizeof(data);
  25742. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25743. wc_PKCS7_Free(pkcs7);
  25744. /* verify using pre-computed content digest only (no content) */
  25745. {
  25746. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25747. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25748. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25749. output, outputSz,
  25750. NULL, 0), 0);
  25751. wc_PKCS7_Free(pkcs7);
  25752. }
  25753. #endif /* !NO_RSA */
  25754. #ifdef HAVE_ECC
  25755. /* Try ECC certs/key/sig next */
  25756. outputSz = sizeof(output);
  25757. XMEMSET(output, 0, outputSz);
  25758. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25759. (word32)sizeof(data),
  25760. 1, 1, 0, ECC_TYPE)), 0);
  25761. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25762. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25763. pkcs7->content = badContent;
  25764. pkcs7->contentSz = sizeof(badContent);
  25765. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25766. SIG_VERIFY_E);
  25767. wc_PKCS7_Free(pkcs7);
  25768. /* Test success case with detached signature and valid content */
  25769. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25770. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25771. pkcs7->content = data;
  25772. pkcs7->contentSz = sizeof(data);
  25773. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25774. wc_PKCS7_Free(pkcs7);
  25775. /* verify using pre-computed content digest only (no content) */
  25776. {
  25777. /* calculate hash for content */
  25778. ret = wc_HashInit(&hash, hashType);
  25779. if (ret == 0) {
  25780. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25781. if (ret == 0) {
  25782. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25783. }
  25784. wc_HashFree(&hash, hashType);
  25785. }
  25786. AssertIntEQ(ret, 0);
  25787. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25788. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25789. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25790. output, outputSz,
  25791. NULL, 0), 0);
  25792. wc_PKCS7_Free(pkcs7);
  25793. }
  25794. #endif
  25795. /* Test verify on signedData containing intermediate/root CA certs */
  25796. #ifndef NO_RSA
  25797. outputSz = sizeof(output);
  25798. XMEMSET(output, 0, outputSz);
  25799. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25800. (word32)sizeof(data),
  25801. 0, 0, 1, RSA_TYPE)), 0);
  25802. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25803. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25804. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25805. wc_PKCS7_Free(pkcs7);
  25806. #endif /* !NO_RSA */
  25807. #ifdef HAVE_ECC
  25808. outputSz = sizeof(output);
  25809. XMEMSET(output, 0, outputSz);
  25810. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25811. (word32)sizeof(data),
  25812. 0, 0, 1, ECC_TYPE)), 0);
  25813. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25814. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25815. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25816. wc_PKCS7_Free(pkcs7);
  25817. #endif /* HAVE_ECC */
  25818. res = TEST_RES_CHECK(1);
  25819. #endif
  25820. return res;
  25821. } /* END test_wc_PKCS7_VerifySignedData() */
  25822. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25823. !defined(NO_AES_256)
  25824. static const byte defKey[] = {
  25825. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25826. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25827. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25828. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25829. };
  25830. static byte aesHandle[32]; /* simulated hardware key handle */
  25831. /* return 0 on success */
  25832. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25833. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25834. byte* in, int inSz, byte* out, void* usrCtx)
  25835. {
  25836. int ret;
  25837. Aes aes;
  25838. if (usrCtx == NULL) {
  25839. /* no simulated handle passed in */
  25840. return -1;
  25841. }
  25842. switch (encryptOID) {
  25843. case AES256CBCb:
  25844. if (ivSz != AES_BLOCK_SIZE)
  25845. return BAD_FUNC_ARG;
  25846. break;
  25847. default:
  25848. WOLFSSL_MSG("Unsupported content cipher type for test");
  25849. return ALGO_ID_E;
  25850. };
  25851. /* simulate using handle to get key */
  25852. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25853. if (ret == 0) {
  25854. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25855. if (ret == 0)
  25856. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25857. wc_AesFree(&aes);
  25858. }
  25859. (void)aad;
  25860. (void)aadSz;
  25861. (void)authTag;
  25862. (void)authTagSz;
  25863. (void)pkcs7;
  25864. return ret;
  25865. }
  25866. /* returns key size on success */
  25867. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25868. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25869. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25870. {
  25871. int ret = -1;
  25872. if (out == NULL)
  25873. return BAD_FUNC_ARG;
  25874. if (keyId[0] != 0x00) {
  25875. return -1;
  25876. }
  25877. if (type != (int)PKCS7_KEKRI) {
  25878. return -1;
  25879. }
  25880. switch (keyWrapAlgo) {
  25881. case AES256_WRAP:
  25882. /* simulate setting a handle for later decryption but use key
  25883. * as handle in the test case here */
  25884. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25885. aesHandle, sizeof(aesHandle), NULL);
  25886. if (ret < 0)
  25887. return ret;
  25888. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25889. if (ret < 0)
  25890. return ret;
  25891. /* return key size on success */
  25892. return sizeof(defKey);
  25893. default:
  25894. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25895. return BAD_KEYWRAP_ALG_E;
  25896. };
  25897. (void)cekSz;
  25898. (void)cek;
  25899. (void)outSz;
  25900. (void)keyIdSz;
  25901. (void)direction;
  25902. (void)orginKey; /* used with KAKRI */
  25903. (void)orginKeySz;
  25904. return ret;
  25905. }
  25906. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25907. /*
  25908. * Testing wc_PKCS7_EncodeEnvelopedData()
  25909. */
  25910. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25911. {
  25912. int res = TEST_SKIPPED;
  25913. #if defined(HAVE_PKCS7)
  25914. PKCS7* pkcs7;
  25915. #ifdef ECC_TIMING_RESISTANT
  25916. WC_RNG rng;
  25917. #endif
  25918. word32 tempWrd32 = 0;
  25919. byte* tmpBytePtr = NULL;
  25920. const char input[] = "Test data to encode.";
  25921. int i;
  25922. int testSz = 0;
  25923. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25924. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25925. byte* rsaCert = NULL;
  25926. byte* rsaPrivKey = NULL;
  25927. word32 rsaCertSz;
  25928. word32 rsaPrivKeySz;
  25929. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25930. !defined(USE_CERT_BUFFERS_2048) )
  25931. static const char* rsaClientCert = "./certs/client-cert.der";
  25932. static const char* rsaClientKey = "./certs/client-key.der";
  25933. rsaCertSz = (word32)sizeof(rsaClientCert);
  25934. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25935. #endif
  25936. #endif
  25937. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25938. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25939. byte* eccCert = NULL;
  25940. byte* eccPrivKey = NULL;
  25941. word32 eccCertSz;
  25942. word32 eccPrivKeySz;
  25943. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25944. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25945. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25946. #endif
  25947. #endif
  25948. /* Generic buffer size. */
  25949. byte output[ONEK_BUF];
  25950. byte decoded[sizeof(input)/sizeof(char)];
  25951. int decodedSz = 0;
  25952. #ifndef NO_FILESYSTEM
  25953. XFILE certFile;
  25954. XFILE keyFile;
  25955. #endif
  25956. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25957. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25958. /* RSA certs and keys. */
  25959. #if defined(USE_CERT_BUFFERS_1024)
  25960. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25961. /* Allocate buffer space. */
  25962. AssertNotNull(rsaCert =
  25963. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25964. /* Init buffer. */
  25965. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25966. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25967. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25968. DYNAMIC_TYPE_TMP_BUFFER));
  25969. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25970. #elif defined(USE_CERT_BUFFERS_2048)
  25971. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25972. /* Allocate buffer */
  25973. AssertNotNull(rsaCert =
  25974. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25975. /* Init buffer. */
  25976. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25977. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25978. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25979. DYNAMIC_TYPE_TMP_BUFFER));
  25980. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25981. #else
  25982. /* File system. */
  25983. certFile = XFOPEN(rsaClientCert, "rb");
  25984. AssertTrue(certFile != XBADFILE);
  25985. rsaCertSz = (word32)FOURK_BUF;
  25986. AssertNotNull(rsaCert =
  25987. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25988. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  25989. XFCLOSE(certFile);
  25990. keyFile = XFOPEN(rsaClientKey, "rb");
  25991. AssertTrue(keyFile != XBADFILE);
  25992. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25993. DYNAMIC_TYPE_TMP_BUFFER));
  25994. rsaPrivKeySz = (word32)FOURK_BUF;
  25995. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  25996. XFCLOSE(keyFile);
  25997. #endif /* USE_CERT_BUFFERS */
  25998. #endif /* NO_RSA */
  25999. /* ECC */
  26000. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  26001. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26002. #ifdef USE_CERT_BUFFERS_256
  26003. AssertNotNull(eccCert =
  26004. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26005. /* Init buffer. */
  26006. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  26007. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  26008. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  26009. DYNAMIC_TYPE_TMP_BUFFER));
  26010. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  26011. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  26012. #else /* File system. */
  26013. certFile = XFOPEN(eccClientCert, "rb");
  26014. AssertTrue(certFile != XBADFILE);
  26015. eccCertSz = (word32)FOURK_BUF;
  26016. AssertNotNull(eccCert =
  26017. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26018. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  26019. XFCLOSE(certFile);
  26020. keyFile = XFOPEN(eccClientKey, "rb");
  26021. AssertTrue(keyFile != XBADFILE);
  26022. eccPrivKeySz = (word32)FOURK_BUF;
  26023. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26024. DYNAMIC_TYPE_TMP_BUFFER));
  26025. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  26026. XFCLOSE(keyFile);
  26027. #endif /* USE_CERT_BUFFERS_256 */
  26028. #endif /* END HAVE_ECC */
  26029. #ifndef NO_FILESYSTEM
  26030. /* Silence. */
  26031. (void)keyFile;
  26032. (void)certFile;
  26033. #endif
  26034. {
  26035. const pkcs7EnvelopedVector testVectors[] = {
  26036. /* DATA is a global variable defined in the makefile. */
  26037. #if !defined(NO_RSA)
  26038. #ifndef NO_DES3
  26039. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  26040. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26041. #endif /* NO_DES3 */
  26042. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26043. #ifndef NO_AES_128
  26044. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  26045. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26046. #endif
  26047. #ifndef NO_AES_192
  26048. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  26049. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26050. #endif
  26051. #ifndef NO_AES_256
  26052. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26053. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26054. #endif
  26055. #endif /* NO_AES && HAVE_AES_CBC */
  26056. #endif /* NO_RSA */
  26057. #if defined(HAVE_ECC)
  26058. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26059. #if !defined(NO_SHA) && !defined(NO_AES_128)
  26060. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  26061. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  26062. eccCertSz, eccPrivKey, eccPrivKeySz},
  26063. #endif
  26064. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  26065. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26066. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  26067. eccCertSz, eccPrivKey, eccPrivKeySz},
  26068. #endif
  26069. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  26070. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26071. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  26072. eccCertSz, eccPrivKey, eccPrivKeySz},
  26073. #endif
  26074. #endif /* NO_AES && HAVE_AES_CBC*/
  26075. #endif /* END HAVE_ECC */
  26076. }; /* END pkcs7EnvelopedVector */
  26077. #ifdef ECC_TIMING_RESISTANT
  26078. AssertIntEQ(wc_InitRng(&rng), 0);
  26079. #endif
  26080. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26081. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26082. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  26083. for (i = 0; i < testSz; i++) {
  26084. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  26085. (word32)(testVectors + i)->certSz), 0);
  26086. #ifdef ECC_TIMING_RESISTANT
  26087. pkcs7->rng = &rng;
  26088. #endif
  26089. pkcs7->content = (byte*)(testVectors + i)->content;
  26090. pkcs7->contentSz = (testVectors + i)->contentSz;
  26091. pkcs7->contentOID = (testVectors + i)->contentOID;
  26092. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  26093. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  26094. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  26095. pkcs7->privateKey = (testVectors + i)->privateKey;
  26096. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  26097. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  26098. (word32)sizeof(output)), 0);
  26099. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26100. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  26101. AssertIntGE(decodedSz, 0);
  26102. /* Verify the size of each buffer. */
  26103. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  26104. /* Don't free the last time through the loop. */
  26105. if (i < testSz - 1) {
  26106. wc_PKCS7_Free(pkcs7);
  26107. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26108. }
  26109. } /* END test loop. */
  26110. }
  26111. /* Test bad args. */
  26112. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  26113. (word32)sizeof(output)), BAD_FUNC_ARG);
  26114. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  26115. (word32)sizeof(output)), BAD_FUNC_ARG);
  26116. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  26117. /* Decode. */
  26118. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  26119. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26120. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26121. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26122. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26123. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  26124. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  26125. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26126. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  26127. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26128. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  26129. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  26130. /* only a failure for KARI test cases */
  26131. tempWrd32 = pkcs7->singleCertSz;
  26132. pkcs7->singleCertSz = 0;
  26133. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26134. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26135. pkcs7->singleCertSz = tempWrd32;
  26136. tmpBytePtr = pkcs7->singleCert;
  26137. pkcs7->singleCert = NULL;
  26138. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26139. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26140. pkcs7->singleCert = tmpBytePtr;
  26141. #endif
  26142. tempWrd32 = pkcs7->privateKeySz;
  26143. pkcs7->privateKeySz = 0;
  26144. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26145. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26146. pkcs7->privateKeySz = tempWrd32;
  26147. tmpBytePtr = pkcs7->privateKey;
  26148. pkcs7->privateKey = NULL;
  26149. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26150. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26151. pkcs7->privateKey = tmpBytePtr;
  26152. wc_PKCS7_Free(pkcs7);
  26153. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  26154. /* test of decrypt callback with KEKRI enveloped data */
  26155. {
  26156. int envelopedSz;
  26157. const byte keyId[] = { 0x00 };
  26158. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26159. pkcs7->content = (byte*)input;
  26160. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  26161. pkcs7->contentOID = DATA;
  26162. pkcs7->encryptOID = AES256CBCb;
  26163. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  26164. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  26165. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  26166. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  26167. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  26168. (word32)sizeof(output))), 0);
  26169. wc_PKCS7_Free(pkcs7);
  26170. /* decode envelopedData */
  26171. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26172. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  26173. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  26174. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26175. envelopedSz, decoded, sizeof(decoded))), 0);
  26176. wc_PKCS7_Free(pkcs7);
  26177. }
  26178. #endif /* !NO_AES && !NO_AES_256 */
  26179. #ifndef NO_RSA
  26180. if (rsaCert) {
  26181. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26182. }
  26183. if (rsaPrivKey) {
  26184. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26185. }
  26186. #endif /*NO_RSA */
  26187. #ifdef HAVE_ECC
  26188. if (eccCert) {
  26189. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26190. }
  26191. if (eccPrivKey) {
  26192. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26193. }
  26194. #endif /* HAVE_ECC */
  26195. #ifdef ECC_TIMING_RESISTANT
  26196. wc_FreeRng(&rng);
  26197. #endif
  26198. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  26199. !defined(NO_RSA) && !defined(NO_SHA)
  26200. {
  26201. byte out[7];
  26202. byte *cms;
  26203. word32 cmsSz;
  26204. XFILE cmsFile;
  26205. XMEMSET(out, 0, sizeof(out));
  26206. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26207. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  26208. AssertTrue(cmsFile != XBADFILE);
  26209. cmsSz = (word32)FOURK_BUF;
  26210. AssertNotNull(cms =
  26211. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26212. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  26213. XFCLOSE(cmsFile);
  26214. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  26215. sizeof_client_cert_der_2048), 0);
  26216. pkcs7->privateKey = (byte*)client_key_der_2048;
  26217. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  26218. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  26219. 2), 0);
  26220. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  26221. sizeof(out)), 0);
  26222. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26223. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  26224. wc_PKCS7_Free(pkcs7);
  26225. }
  26226. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  26227. res = TEST_RES_CHECK(1);
  26228. #endif /* HAVE_PKCS7 */
  26229. return res;
  26230. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  26231. /*
  26232. * Testing wc_PKCS7_EncodeEncryptedData()
  26233. */
  26234. static int test_wc_PKCS7_EncodeEncryptedData(void)
  26235. {
  26236. int res = TEST_SKIPPED;
  26237. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26238. PKCS7* pkcs7 = NULL;
  26239. byte* tmpBytePtr = NULL;
  26240. byte encrypted[TWOK_BUF];
  26241. byte decoded[TWOK_BUF];
  26242. word32 tmpWrd32 = 0;
  26243. int tmpInt = 0;
  26244. int decodedSz;
  26245. int encryptedSz;
  26246. int testSz;
  26247. int i;
  26248. const byte data[] = { /* Hello World */
  26249. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26250. 0x72,0x6c,0x64
  26251. };
  26252. #ifndef NO_DES3
  26253. byte desKey[] = {
  26254. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  26255. };
  26256. byte des3Key[] = {
  26257. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  26258. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  26259. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  26260. };
  26261. #endif
  26262. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26263. #ifndef NO_AES_128
  26264. byte aes128Key[] = {
  26265. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26266. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26267. };
  26268. #endif
  26269. #ifndef NO_AES_192
  26270. byte aes192Key[] = {
  26271. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26272. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26273. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26274. };
  26275. #endif
  26276. #ifndef NO_AES_256
  26277. byte aes256Key[] = {
  26278. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26279. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26280. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26281. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26282. };
  26283. #endif
  26284. #endif /* !NO_AES && HAVE_AES_CBC */
  26285. const pkcs7EncryptedVector testVectors[] =
  26286. {
  26287. #ifndef NO_DES3
  26288. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  26289. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  26290. #endif /* !NO_DES3 */
  26291. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26292. #ifndef NO_AES_128
  26293. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  26294. sizeof(aes128Key)},
  26295. #endif
  26296. #ifndef NO_AES_192
  26297. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  26298. sizeof(aes192Key)},
  26299. #endif
  26300. #ifndef NO_AES_256
  26301. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  26302. sizeof(aes256Key)},
  26303. #endif
  26304. #endif /* !NO_AES && HAVE_AES_CBC */
  26305. };
  26306. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  26307. for (i = 0; i < testSz; i++) {
  26308. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26309. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26310. pkcs7->content = (byte*)testVectors[i].content;
  26311. pkcs7->contentSz = testVectors[i].contentSz;
  26312. pkcs7->contentOID = testVectors[i].contentOID;
  26313. pkcs7->encryptOID = testVectors[i].encryptOID;
  26314. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  26315. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  26316. pkcs7->heap = HEAP_HINT;
  26317. /* encode encryptedData */
  26318. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26319. sizeof(encrypted));
  26320. AssertIntGT(encryptedSz, 0);
  26321. /* Decode encryptedData */
  26322. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26323. decoded, sizeof(decoded));
  26324. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  26325. /* Keep values for last itr. */
  26326. if (i < testSz - 1) {
  26327. wc_PKCS7_Free(pkcs7);
  26328. }
  26329. }
  26330. if (pkcs7 == NULL || testSz == 0) {
  26331. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26332. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26333. }
  26334. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  26335. sizeof(encrypted)),BAD_FUNC_ARG);
  26336. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  26337. sizeof(encrypted)), BAD_FUNC_ARG);
  26338. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26339. 0), BAD_FUNC_ARG);
  26340. /* Testing the struct. */
  26341. tmpBytePtr = pkcs7->content;
  26342. pkcs7->content = NULL;
  26343. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26344. sizeof(encrypted)), BAD_FUNC_ARG);
  26345. pkcs7->content = tmpBytePtr;
  26346. tmpWrd32 = pkcs7->contentSz;
  26347. pkcs7->contentSz = 0;
  26348. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26349. sizeof(encrypted)), BAD_FUNC_ARG);
  26350. pkcs7->contentSz = tmpWrd32;
  26351. tmpInt = pkcs7->encryptOID;
  26352. pkcs7->encryptOID = 0;
  26353. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26354. sizeof(encrypted)), BAD_FUNC_ARG);
  26355. pkcs7->encryptOID = tmpInt;
  26356. tmpBytePtr = pkcs7->encryptionKey;
  26357. pkcs7->encryptionKey = NULL;
  26358. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26359. sizeof(encrypted)), BAD_FUNC_ARG);
  26360. pkcs7->encryptionKey = tmpBytePtr;
  26361. tmpWrd32 = pkcs7->encryptionKeySz;
  26362. pkcs7->encryptionKeySz = 0;
  26363. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26364. sizeof(encrypted)), BAD_FUNC_ARG);
  26365. pkcs7->encryptionKeySz = tmpWrd32;
  26366. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  26367. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26368. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  26369. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26370. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  26371. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26372. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26373. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  26374. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26375. decoded, 0), BAD_FUNC_ARG);
  26376. /* Test struct fields */
  26377. tmpBytePtr = pkcs7->encryptionKey;
  26378. pkcs7->encryptionKey = NULL;
  26379. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26380. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26381. pkcs7->encryptionKey = tmpBytePtr;
  26382. pkcs7->encryptionKeySz = 0;
  26383. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26384. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26385. wc_PKCS7_Free(pkcs7);
  26386. res = TEST_RES_CHECK(1);
  26387. #endif
  26388. return res;
  26389. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  26390. /*
  26391. * Testing wc_PKCS7_Degenerate()
  26392. */
  26393. static int test_wc_PKCS7_Degenerate(void)
  26394. {
  26395. int res = TEST_SKIPPED;
  26396. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26397. PKCS7* pkcs7;
  26398. char fName[] = "./certs/test-degenerate.p7b";
  26399. XFILE f;
  26400. byte der[4096];
  26401. word32 derSz;
  26402. int ret;
  26403. AssertNotNull(f = XFOPEN(fName, "rb"));
  26404. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26405. derSz = (word32)ret;
  26406. XFCLOSE(f);
  26407. /* test degenerate success */
  26408. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26409. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26410. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26411. #ifndef NO_RSA
  26412. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26413. #else
  26414. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26415. #endif
  26416. wc_PKCS7_Free(pkcs7);
  26417. /* test with turning off degenerate cases */
  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. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26422. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  26423. wc_PKCS7_Free(pkcs7);
  26424. res = TEST_RES_CHECK(1);
  26425. #endif
  26426. return res;
  26427. } /* END test_wc_PKCS7_Degenerate() */
  26428. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26429. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26430. static byte berContent[] = {
  26431. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26432. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26433. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26434. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26435. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26436. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26437. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26438. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26439. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26440. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26441. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26442. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26443. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26444. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26445. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26446. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26447. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26448. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26449. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26450. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26451. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26452. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26453. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26454. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26455. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26456. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26457. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26458. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26459. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26460. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26461. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26462. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26463. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26464. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26465. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26466. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26467. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26468. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26469. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26470. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26471. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26472. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26473. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26474. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26475. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26476. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26477. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26478. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26479. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26480. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26481. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26482. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26483. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26484. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26485. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26486. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26487. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26488. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26489. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26490. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26491. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26492. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26493. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26494. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26495. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26496. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26497. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26498. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26499. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26500. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26501. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26502. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26503. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26504. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26505. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26506. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26507. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26508. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26509. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26510. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26511. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26512. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26513. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26514. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26515. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26516. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26517. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26518. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26519. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26520. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26521. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26522. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26523. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26524. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26525. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26526. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26527. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26528. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26529. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26530. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26531. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26532. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26533. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26534. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26535. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26536. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26537. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26538. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26539. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26540. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26541. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26542. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26543. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26544. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26545. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26546. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26547. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26548. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26549. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26550. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26551. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26552. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26553. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26554. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26555. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26556. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26557. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26558. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26559. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26560. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26561. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26562. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26563. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26564. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26565. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26566. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26567. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26568. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26569. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26570. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26571. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26572. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26573. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26574. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26575. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26576. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26577. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26578. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26579. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26580. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26581. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26582. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26583. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26584. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26585. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26586. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26587. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26588. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26589. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26590. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26591. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26592. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26593. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26594. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26595. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26596. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26597. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26598. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26599. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26600. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26601. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26602. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26603. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26604. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26605. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26606. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26607. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26608. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26609. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26610. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26611. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26612. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26613. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26614. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26615. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26616. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26617. 0x00, 0x00, 0x00, 0x00, 0x00
  26618. };
  26619. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26620. * !NO_DES3 && !NO_SHA
  26621. */
  26622. /*
  26623. * Testing wc_PKCS7_BER()
  26624. */
  26625. static int test_wc_PKCS7_BER(void)
  26626. {
  26627. int res = TEST_SKIPPED;
  26628. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26629. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26630. PKCS7* pkcs7;
  26631. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26632. XFILE f;
  26633. byte der[4096];
  26634. #ifndef NO_DES3
  26635. byte decoded[2048];
  26636. #endif
  26637. word32 derSz;
  26638. int ret;
  26639. AssertNotNull(f = XFOPEN(fName, "rb"));
  26640. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26641. derSz = (word32)ret;
  26642. XFCLOSE(f);
  26643. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26644. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26645. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26646. #ifndef NO_RSA
  26647. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26648. #else
  26649. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26650. #endif
  26651. wc_PKCS7_Free(pkcs7);
  26652. #ifndef NO_DES3
  26653. /* decode BER content */
  26654. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  26655. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26656. derSz = (word32)ret;
  26657. XFCLOSE(f);
  26658. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26659. #ifndef NO_RSA
  26660. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26661. #else
  26662. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26663. #endif
  26664. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  26665. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26666. derSz = (word32)ret;
  26667. XFCLOSE(f);
  26668. pkcs7->privateKey = der;
  26669. pkcs7->privateKeySz = derSz;
  26670. #ifndef NO_RSA
  26671. #ifdef WOLFSSL_SP_MATH
  26672. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26673. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26674. #else
  26675. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26676. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26677. #endif
  26678. #else
  26679. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26680. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26681. #endif
  26682. wc_PKCS7_Free(pkcs7);
  26683. #endif /* !NO_DES3 */
  26684. res = TEST_RES_CHECK(1);
  26685. #endif
  26686. return res;
  26687. } /* END test_wc_PKCS7_BER() */
  26688. static int test_PKCS7_signed_enveloped(void)
  26689. {
  26690. int res = TEST_SKIPPED;
  26691. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26692. !defined(NO_FILESYSTEM)
  26693. XFILE f;
  26694. PKCS7* pkcs7;
  26695. #ifdef HAVE_AES_CBC
  26696. PKCS7* inner;
  26697. #endif
  26698. void* pt;
  26699. WC_RNG rng;
  26700. unsigned char key[FOURK_BUF/2];
  26701. unsigned char cert[FOURK_BUF/2];
  26702. unsigned char env[FOURK_BUF];
  26703. int envSz = FOURK_BUF;
  26704. int keySz;
  26705. int certSz;
  26706. unsigned char sig[FOURK_BUF * 2];
  26707. int sigSz = FOURK_BUF * 2;
  26708. #ifdef HAVE_AES_CBC
  26709. unsigned char decoded[FOURK_BUF];
  26710. int decodedSz = FOURK_BUF;
  26711. #endif
  26712. /* load cert */
  26713. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  26714. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26715. XFCLOSE(f);
  26716. /* load key */
  26717. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  26718. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26719. XFCLOSE(f);
  26720. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  26721. /* sign cert for envelope */
  26722. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26723. AssertIntEQ(wc_InitRng(&rng), 0);
  26724. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26725. pkcs7->content = cert;
  26726. pkcs7->contentSz = certSz;
  26727. pkcs7->contentOID = DATA;
  26728. pkcs7->privateKey = key;
  26729. pkcs7->privateKeySz = keySz;
  26730. pkcs7->encryptOID = RSAk;
  26731. pkcs7->hashOID = SHA256h;
  26732. pkcs7->rng = &rng;
  26733. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26734. wc_PKCS7_Free(pkcs7);
  26735. wc_FreeRng(&rng);
  26736. #ifdef HAVE_AES_CBC
  26737. /* create envelope */
  26738. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26739. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26740. pkcs7->content = sig;
  26741. pkcs7->contentSz = sigSz;
  26742. pkcs7->contentOID = DATA;
  26743. pkcs7->encryptOID = AES256CBCb;
  26744. pkcs7->privateKey = key;
  26745. pkcs7->privateKeySz = keySz;
  26746. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26747. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26748. wc_PKCS7_Free(pkcs7);
  26749. #endif
  26750. /* create bad signed enveloped data */
  26751. sigSz = FOURK_BUF * 2;
  26752. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26753. AssertIntEQ(wc_InitRng(&rng), 0);
  26754. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26755. pkcs7->content = env;
  26756. pkcs7->contentSz = envSz;
  26757. pkcs7->contentOID = DATA;
  26758. pkcs7->privateKey = key;
  26759. pkcs7->privateKeySz = keySz;
  26760. pkcs7->encryptOID = RSAk;
  26761. pkcs7->hashOID = SHA256h;
  26762. pkcs7->rng = &rng;
  26763. /* Set no certs in bundle for this test. Hang on to the pointer though to
  26764. * free it later. */
  26765. pt = (void*)pkcs7->certList;
  26766. pkcs7->certList = NULL; /* no certs in bundle */
  26767. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26768. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  26769. wc_PKCS7_Free(pkcs7);
  26770. /* check verify fails */
  26771. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26772. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26773. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26774. PKCS7_SIGNEEDS_CHECK);
  26775. /* try verifying the signature manually */
  26776. {
  26777. RsaKey rKey;
  26778. word32 idx = 0;
  26779. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26780. WC_MAX_DIGEST_SIZE];
  26781. int digestSz;
  26782. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26783. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26784. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  26785. digest, sizeof(digest), &rKey);
  26786. AssertIntGT(digestSz, 0);
  26787. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26788. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26789. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  26790. /* verify was success */
  26791. }
  26792. wc_PKCS7_Free(pkcs7);
  26793. /* initializing the PKCS7 struct with the signing certificate should pass */
  26794. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26795. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26796. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26797. wc_PKCS7_Free(pkcs7);
  26798. /* create valid degenerate bundle */
  26799. sigSz = FOURK_BUF * 2;
  26800. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26801. pkcs7->content = env;
  26802. pkcs7->contentSz = envSz;
  26803. pkcs7->contentOID = DATA;
  26804. pkcs7->privateKey = key;
  26805. pkcs7->privateKeySz = keySz;
  26806. pkcs7->encryptOID = RSAk;
  26807. pkcs7->hashOID = SHA256h;
  26808. pkcs7->rng = &rng;
  26809. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26810. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26811. wc_PKCS7_Free(pkcs7);
  26812. wc_FreeRng(&rng);
  26813. /* check verify */
  26814. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26815. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26816. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26817. AssertNotNull(pkcs7->content);
  26818. #ifdef HAVE_AES_CBC
  26819. /* check decode */
  26820. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  26821. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26822. inner->privateKey = key;
  26823. inner->privateKeySz = keySz;
  26824. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26825. pkcs7->contentSz, decoded, decodedSz)), 0);
  26826. wc_PKCS7_Free(inner);
  26827. #endif
  26828. wc_PKCS7_Free(pkcs7);
  26829. #ifdef HAVE_AES_CBC
  26830. /* check cert set */
  26831. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26832. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26833. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26834. AssertNotNull(pkcs7->singleCert);
  26835. AssertIntNE(pkcs7->singleCertSz, 0);
  26836. wc_PKCS7_Free(pkcs7);
  26837. #endif
  26838. res = TEST_RES_CHECK(1);
  26839. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26840. return res;
  26841. }
  26842. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26843. {
  26844. int res = TEST_SKIPPED;
  26845. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26846. && !defined(NO_AES)
  26847. PKCS7* pkcs7;
  26848. void* heap = NULL;
  26849. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26850. AssertNotNull(pkcs7);
  26851. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26852. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26853. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26854. wc_PKCS7_Free(pkcs7);
  26855. res = TEST_RES_CHECK(1);
  26856. #endif
  26857. return res;
  26858. }
  26859. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26860. {
  26861. int res = TEST_SKIPPED;
  26862. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26863. && !defined(NO_AES)
  26864. PKCS7* pkcs7;
  26865. void* heap = NULL;
  26866. WOLFSSL_CTX* ctx;
  26867. ctx = NULL;
  26868. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26869. AssertNotNull(pkcs7);
  26870. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26871. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26872. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26873. wc_PKCS7_Free(pkcs7);
  26874. res = TEST_RES_CHECK(1);
  26875. #endif
  26876. return res;
  26877. }
  26878. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26879. {
  26880. int res = TEST_SKIPPED;
  26881. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26882. && !defined(NO_AES)
  26883. PKCS7* pkcs7;
  26884. void* heap = NULL;
  26885. WOLFSSL_CTX* ctx;
  26886. ctx = NULL;
  26887. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26888. AssertNotNull(pkcs7);
  26889. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26890. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26891. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26892. wc_PKCS7_Free(pkcs7);
  26893. res = TEST_RES_CHECK(1);
  26894. #endif
  26895. return res;
  26896. }
  26897. static int test_wc_PKCS7_DecodeCompressedData(void)
  26898. {
  26899. int res = TEST_SKIPPED;
  26900. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26901. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26902. PKCS7* pkcs7;
  26903. void* heap = NULL;
  26904. byte out[4096];
  26905. byte *decompressed;
  26906. int outSz, decompressedSz;
  26907. const char* cert = "./certs/client-cert.pem";
  26908. byte* cert_buf = NULL;
  26909. size_t cert_sz = 0;
  26910. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26911. AssertNotNull((decompressed =
  26912. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  26913. decompressedSz = (int)cert_sz;
  26914. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26915. pkcs7->content = (byte*)cert_buf;
  26916. pkcs7->contentSz = (word32)cert_sz;
  26917. pkcs7->contentOID = DATA;
  26918. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26919. sizeof(out))), 0);
  26920. wc_PKCS7_Free(pkcs7);
  26921. /* compressed key should be smaller than when started */
  26922. AssertIntLT(outSz, cert_sz);
  26923. /* test decompression */
  26924. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26925. AssertIntEQ(pkcs7->contentOID, 0);
  26926. /* fail case with out buffer too small */
  26927. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26928. decompressed, outSz), 0);
  26929. /* success case */
  26930. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26931. decompressed, decompressedSz), cert_sz);
  26932. AssertIntEQ(pkcs7->contentOID, DATA);
  26933. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26934. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26935. decompressed = NULL;
  26936. /* test decompression function with different 'max' inputs */
  26937. outSz = sizeof(out);
  26938. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26939. 0);
  26940. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26941. out, outSz, 0, heap), 0);
  26942. AssertNull(decompressed);
  26943. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26944. out, outSz, 0, heap), 0);
  26945. AssertNotNull(decompressed);
  26946. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26947. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26948. decompressed = NULL;
  26949. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26950. out, outSz, 0, heap), 0);
  26951. AssertNotNull(decompressed);
  26952. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26953. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26954. if (cert_buf)
  26955. free(cert_buf);
  26956. wc_PKCS7_Free(pkcs7);
  26957. res = TEST_RES_CHECK(1);
  26958. #endif
  26959. return res;
  26960. }
  26961. static int test_wc_i2d_PKCS12(void)
  26962. {
  26963. int res = TEST_SKIPPED;
  26964. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26965. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26966. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26967. WC_PKCS12* pkcs12 = NULL;
  26968. unsigned char der[FOURK_BUF * 2];
  26969. unsigned char* pt;
  26970. int derSz;
  26971. unsigned char out[FOURK_BUF * 2];
  26972. int outSz = FOURK_BUF * 2;
  26973. const char p12_f[] = "./certs/test-servercert.p12";
  26974. XFILE f;
  26975. f = XFOPEN(p12_f, "rb");
  26976. AssertNotNull(f);
  26977. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  26978. AssertIntGT(derSz, 0);
  26979. XFCLOSE(f);
  26980. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26981. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26982. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26983. AssertIntEQ(outSz, derSz);
  26984. outSz = derSz - 1;
  26985. pt = out;
  26986. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26987. outSz = derSz;
  26988. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26989. AssertIntEQ((pt == out), 0);
  26990. pt = NULL;
  26991. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26992. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26993. wc_PKCS12_free(pkcs12);
  26994. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26995. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26996. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26997. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26998. AssertIntEQ(outSz, derSz);
  26999. wc_PKCS12_free(pkcs12);
  27000. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  27001. pkcs12 = NULL;
  27002. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  27003. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  27004. AssertIntEQ(outSz, derSz);
  27005. wc_PKCS12_free(pkcs12);
  27006. res = TEST_RES_CHECK(1);
  27007. #endif
  27008. return res;
  27009. }
  27010. /* Testing wc_SignatureGetSize() for signature type ECC */
  27011. static int test_wc_SignatureGetSize_ecc(void)
  27012. {
  27013. int res = TEST_SKIPPED;
  27014. #ifndef NO_SIG_WRAPPER
  27015. int ret;
  27016. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  27017. enum wc_SignatureType sig_type;
  27018. word32 key_len;
  27019. /* Initialize ECC Key */
  27020. ecc_key ecc;
  27021. const char* qx =
  27022. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  27023. const char* qy =
  27024. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  27025. const char* d =
  27026. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  27027. ret = wc_ecc_init(&ecc);
  27028. if (ret == 0) {
  27029. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  27030. }
  27031. if (ret == 0) {
  27032. /* Input for signature type ECC */
  27033. sig_type = WC_SIGNATURE_TYPE_ECC;
  27034. key_len = sizeof(ecc_key);
  27035. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27036. /* Test bad args */
  27037. if (ret > 0) {
  27038. sig_type = (enum wc_SignatureType) 100;
  27039. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27040. if (ret == BAD_FUNC_ARG) {
  27041. sig_type = WC_SIGNATURE_TYPE_ECC;
  27042. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  27043. }
  27044. if (ret >= 0) {
  27045. key_len = (word32) 0;
  27046. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27047. }
  27048. if (ret == BAD_FUNC_ARG) {
  27049. ret = SIG_TYPE_E;
  27050. }
  27051. }
  27052. }
  27053. else {
  27054. ret = WOLFSSL_FATAL_ERROR;
  27055. }
  27056. wc_ecc_free(&ecc);
  27057. #else
  27058. ret = SIG_TYPE_E;
  27059. #endif
  27060. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  27061. #endif /* NO_SIG_WRAPPER */
  27062. return res;
  27063. }/* END test_wc_SignatureGetSize_ecc() */
  27064. /* Testing wc_SignatureGetSize() for signature type rsa */
  27065. static int test_wc_SignatureGetSize_rsa(void)
  27066. {
  27067. int res = TEST_SKIPPED;
  27068. #ifndef NO_SIG_WRAPPER
  27069. int ret = 0;
  27070. #ifndef NO_RSA
  27071. enum wc_SignatureType sig_type;
  27072. word32 key_len;
  27073. word32 idx = 0;
  27074. /* Initialize RSA Key */
  27075. RsaKey rsa_key;
  27076. byte* tmp = NULL;
  27077. size_t bytes;
  27078. #ifdef USE_CERT_BUFFERS_1024
  27079. bytes = (size_t)sizeof_client_key_der_1024;
  27080. if (bytes < (size_t)sizeof_client_key_der_1024)
  27081. bytes = (size_t)sizeof_client_cert_der_1024;
  27082. #elif defined(USE_CERT_BUFFERS_2048)
  27083. bytes = (size_t)sizeof_client_key_der_2048;
  27084. if (bytes < (size_t)sizeof_client_cert_der_2048)
  27085. bytes = (size_t)sizeof_client_cert_der_2048;
  27086. #else
  27087. bytes = FOURK_BUF;
  27088. #endif
  27089. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27090. if (tmp != NULL) {
  27091. #ifdef USE_CERT_BUFFERS_1024
  27092. XMEMCPY(tmp, client_key_der_1024,
  27093. (size_t)sizeof_client_key_der_1024);
  27094. #elif defined(USE_CERT_BUFFERS_2048)
  27095. XMEMCPY(tmp, client_key_der_2048,
  27096. (size_t)sizeof_client_key_der_2048);
  27097. #elif !defined(NO_FILESYSTEM)
  27098. file = XFOPEN(clientKey, "rb");
  27099. if (file != XBADFILE) {
  27100. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  27101. XFCLOSE(file);
  27102. }
  27103. else {
  27104. ret = WOLFSSL_FATAL_ERROR;
  27105. }
  27106. #else
  27107. ret = WOLFSSL_FATAL_ERROR;
  27108. #endif
  27109. }
  27110. else {
  27111. ret = WOLFSSL_FATAL_ERROR;
  27112. }
  27113. if (ret == 0) {
  27114. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
  27115. }
  27116. if (ret == 0) {
  27117. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  27118. }
  27119. if (ret == 0) {
  27120. /* Input for signature type RSA */
  27121. sig_type = WC_SIGNATURE_TYPE_RSA;
  27122. key_len = sizeof(RsaKey);
  27123. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27124. /* Test bad args */
  27125. if (ret > 0) {
  27126. sig_type = (enum wc_SignatureType) 100;
  27127. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27128. if (ret == BAD_FUNC_ARG) {
  27129. sig_type = WC_SIGNATURE_TYPE_RSA;
  27130. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  27131. }
  27132. #ifndef HAVE_USER_RSA
  27133. if (ret == BAD_FUNC_ARG)
  27134. #else
  27135. if (ret == 0)
  27136. #endif
  27137. {
  27138. key_len = (word32)0;
  27139. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27140. }
  27141. if (ret == BAD_FUNC_ARG) {
  27142. ret = SIG_TYPE_E;
  27143. }
  27144. }
  27145. }
  27146. else {
  27147. ret = WOLFSSL_FATAL_ERROR;
  27148. }
  27149. wc_FreeRsaKey(&rsa_key);
  27150. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27151. #else
  27152. ret = SIG_TYPE_E;
  27153. #endif
  27154. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  27155. #endif /* NO_SIG_WRAPPER */
  27156. return res;
  27157. }/* END test_wc_SignatureGetSize_rsa(void) */
  27158. /*----------------------------------------------------------------------------*
  27159. | hash.h Tests
  27160. *----------------------------------------------------------------------------*/
  27161. static int test_wc_HashInit(void)
  27162. {
  27163. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  27164. wc_HashAlg hash;
  27165. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27166. enum wc_HashType enumArray[] = {
  27167. #ifndef NO_MD5
  27168. WC_HASH_TYPE_MD5,
  27169. #endif
  27170. #ifndef NO_SHA
  27171. WC_HASH_TYPE_SHA,
  27172. #endif
  27173. #ifndef WOLFSSL_SHA224
  27174. WC_HASH_TYPE_SHA224,
  27175. #endif
  27176. #ifndef NO_SHA256
  27177. WC_HASH_TYPE_SHA256,
  27178. #endif
  27179. #ifndef WOLFSSL_SHA384
  27180. WC_HASH_TYPE_SHA384,
  27181. #endif
  27182. #ifndef WOLFSSL_SHA512
  27183. WC_HASH_TYPE_SHA512,
  27184. #endif
  27185. };
  27186. /* dynamically finds the length */
  27187. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27188. /* For loop to test various arguments... */
  27189. for (i = 0; i < enumlen; i++) {
  27190. /* check for bad args */
  27191. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  27192. ret = 1;
  27193. break;
  27194. }
  27195. wc_HashFree(&hash, enumArray[i]);
  27196. /* check for null ptr */
  27197. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  27198. ret = 1;
  27199. break;
  27200. }
  27201. } /* end of for loop */
  27202. return TEST_RES_CHECK(ret == 0);
  27203. } /* end of test_wc_HashInit */
  27204. /*
  27205. * Unit test function for wc_HashSetFlags()
  27206. */
  27207. static int test_wc_HashSetFlags(void)
  27208. {
  27209. int res = TEST_SKIPPED;
  27210. #ifdef WOLFSSL_HASH_FLAGS
  27211. wc_HashAlg hash;
  27212. int ret = 0;
  27213. word32 flags = 0;
  27214. int i, j;
  27215. int notSupportedLen;
  27216. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27217. enum wc_HashType enumArray[] = {
  27218. #ifndef NO_MD5
  27219. WC_HASH_TYPE_MD5,
  27220. #endif
  27221. #ifndef NO_SHA
  27222. WC_HASH_TYPE_SHA,
  27223. #endif
  27224. #ifdef WOLFSSL_SHA224
  27225. WC_HASH_TYPE_SHA224,
  27226. #endif
  27227. #ifndef NO_SHA256
  27228. WC_HASH_TYPE_SHA256,
  27229. #endif
  27230. #ifdef WOLFSSL_SHA384
  27231. WC_HASH_TYPE_SHA384,
  27232. #endif
  27233. #ifdef WOLFSSL_SHA512
  27234. WC_HASH_TYPE_SHA512,
  27235. #endif
  27236. #ifdef WOLFSSL_SHA3
  27237. WC_HASH_TYPE_SHA3_224,
  27238. #endif
  27239. };
  27240. enum wc_HashType notSupported[] = {
  27241. WC_HASH_TYPE_MD5_SHA,
  27242. WC_HASH_TYPE_MD2,
  27243. WC_HASH_TYPE_MD4,
  27244. WC_HASH_TYPE_BLAKE2B,
  27245. WC_HASH_TYPE_BLAKE2S,
  27246. WC_HASH_TYPE_NONE,
  27247. };
  27248. /* dynamically finds the length */
  27249. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27250. /* For loop to test various arguments... */
  27251. for (i = 0; i < enumlen; i++) {
  27252. ret = wc_HashInit(&hash, enumArray[i]);
  27253. if (ret == 0) {
  27254. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  27255. }
  27256. if (ret == 0) {
  27257. if (flags & WC_HASH_FLAG_ISCOPY) {
  27258. ret = 0;
  27259. }
  27260. }
  27261. if (ret == 0) {
  27262. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  27263. if (ret == BAD_FUNC_ARG) {
  27264. ret = 0;
  27265. }
  27266. }
  27267. wc_HashFree(&hash, enumArray[i]);
  27268. }
  27269. /* For loop to test not supported cases */
  27270. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27271. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  27272. ret = wc_HashInit(&hash, notSupported[j]);
  27273. if (ret == 0) {
  27274. ret = -1;
  27275. }
  27276. else if (ret == BAD_FUNC_ARG) {
  27277. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  27278. if (ret == 0) {
  27279. ret = -1;
  27280. }
  27281. else if (ret == BAD_FUNC_ARG) {
  27282. ret = 0;
  27283. }
  27284. }
  27285. if (ret == 0) {
  27286. ret = wc_HashFree(&hash, notSupported[j]);
  27287. if (ret == 0) {
  27288. ret = -1;
  27289. }
  27290. else if (ret == BAD_FUNC_ARG) {
  27291. ret = 0;
  27292. }
  27293. }
  27294. }
  27295. res = TEST_RES_CHECK(ret == 0);
  27296. #endif
  27297. return res;
  27298. } /* END test_wc_HashSetFlags */
  27299. /*
  27300. * Unit test function for wc_HashGetFlags()
  27301. */
  27302. static int test_wc_HashGetFlags(void)
  27303. {
  27304. int res = TEST_SKIPPED;
  27305. #ifdef WOLFSSL_HASH_FLAGS
  27306. wc_HashAlg hash;
  27307. int ret = 0;
  27308. word32 flags = 0;
  27309. int i, j;
  27310. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27311. enum wc_HashType enumArray[] = {
  27312. #ifndef NO_MD5
  27313. WC_HASH_TYPE_MD5,
  27314. #endif
  27315. #ifndef NO_SHA
  27316. WC_HASH_TYPE_SHA,
  27317. #endif
  27318. #ifdef WOLFSSL_SHA224
  27319. WC_HASH_TYPE_SHA224,
  27320. #endif
  27321. #ifndef NO_SHA256
  27322. WC_HASH_TYPE_SHA256,
  27323. #endif
  27324. #ifdef WOLFSSL_SHA384
  27325. WC_HASH_TYPE_SHA384,
  27326. #endif
  27327. #ifdef WOLFSSL_SHA512
  27328. WC_HASH_TYPE_SHA512,
  27329. #endif
  27330. #ifdef WOLFSSL_SHA3
  27331. WC_HASH_TYPE_SHA3_224,
  27332. #endif
  27333. };
  27334. enum wc_HashType notSupported[] = {
  27335. WC_HASH_TYPE_MD5_SHA,
  27336. WC_HASH_TYPE_MD2,
  27337. WC_HASH_TYPE_MD4,
  27338. WC_HASH_TYPE_BLAKE2B,
  27339. WC_HASH_TYPE_BLAKE2S,
  27340. WC_HASH_TYPE_NONE,
  27341. };
  27342. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27343. int notSupportedLen;
  27344. /* For loop to test various arguments... */
  27345. for (i = 0; i < enumlen; i++) {
  27346. ret = wc_HashInit(&hash, enumArray[i]);
  27347. if (ret == 0) {
  27348. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  27349. }
  27350. if (ret == 0) {
  27351. if (flags & WC_HASH_FLAG_ISCOPY) {
  27352. ret = 0;
  27353. }
  27354. }
  27355. if (ret == 0) {
  27356. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  27357. if (ret == BAD_FUNC_ARG) {
  27358. ret = 0;
  27359. }
  27360. }
  27361. wc_HashFree(&hash, enumArray[i]);
  27362. if (ret != 0) {
  27363. break;
  27364. }
  27365. }
  27366. /* For loop to test not supported cases */
  27367. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27368. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  27369. ret = wc_HashInit(&hash, notSupported[j]);
  27370. if (ret == 0) {
  27371. ret = -1;
  27372. }
  27373. else if (ret == BAD_FUNC_ARG) {
  27374. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  27375. if (ret == 0) {
  27376. ret = -1;
  27377. }
  27378. else if (ret == BAD_FUNC_ARG) {
  27379. ret = 0;
  27380. }
  27381. }
  27382. if (ret == 0) {
  27383. ret = wc_HashFree(&hash, notSupported[j]);
  27384. if (ret == 0) {
  27385. ret = -1;
  27386. }
  27387. if (ret == BAD_FUNC_ARG) {
  27388. ret = 0;
  27389. }
  27390. }
  27391. }
  27392. res = TEST_RES_CHECK(ret == 0);
  27393. #endif
  27394. return res;
  27395. } /* END test_wc_HashGetFlags */
  27396. /*----------------------------------------------------------------------------*
  27397. | Compatibility Tests
  27398. *----------------------------------------------------------------------------*/
  27399. /*----------------------------------------------------------------------------*
  27400. | ASN.1 Tests
  27401. *----------------------------------------------------------------------------*/
  27402. static int test_wolfSSL_ASN1_BIT_STRING(void)
  27403. {
  27404. int res = TEST_SKIPPED;
  27405. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  27406. ASN1_BIT_STRING* str;
  27407. AssertNotNull(str = ASN1_BIT_STRING_new());
  27408. /* Empty data testing. */
  27409. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  27410. ASN1_BIT_STRING_free(str);
  27411. AssertNotNull(str = ASN1_BIT_STRING_new());
  27412. /* Invalid parameter testing. */
  27413. AssertIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  27414. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  27415. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  27416. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  27417. /* No bit string - bit is always 0. */
  27418. AssertIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  27419. AssertIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  27420. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27421. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  27422. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  27423. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  27424. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  27425. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27426. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  27427. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  27428. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  27429. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  27430. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  27431. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  27432. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  27433. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  27434. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27435. ASN1_BIT_STRING_free(str);
  27436. ASN1_BIT_STRING_free(NULL);
  27437. res = TEST_RES_CHECK(1);
  27438. #endif
  27439. return res;
  27440. }
  27441. static int test_wolfSSL_ASN1_INTEGER(void)
  27442. {
  27443. int res = TEST_SKIPPED;
  27444. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27445. ASN1_INTEGER* a;
  27446. ASN1_INTEGER* dup;
  27447. const unsigned char invalidLenDer[] = {
  27448. 0x02, 0x20, 0x00
  27449. };
  27450. const unsigned char longDer[] = {
  27451. 0x02, 0x20,
  27452. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27453. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27454. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27455. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27456. };
  27457. const unsigned char* p;
  27458. /* Invalid parameter testing. */
  27459. ASN1_INTEGER_free(NULL);
  27460. AssertNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27461. AssertNotNull(a = ASN1_INTEGER_new());
  27462. AssertNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27463. ASN1_INTEGER_free(dup);
  27464. ASN1_INTEGER_free(a);
  27465. p = longDer;
  27466. AssertNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27467. p = longDer;
  27468. AssertNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27469. AssertPtrNE(p, longDer);
  27470. AssertNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27471. ASN1_INTEGER_free(dup);
  27472. ASN1_INTEGER_free(a);
  27473. res = TEST_RES_CHECK(1);
  27474. #endif
  27475. return res;
  27476. }
  27477. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27478. {
  27479. int res = TEST_SKIPPED;
  27480. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27481. ASN1_INTEGER* a;
  27482. ASN1_INTEGER* b;
  27483. AssertNotNull(a = ASN1_INTEGER_new());
  27484. AssertNotNull(b = ASN1_INTEGER_new());
  27485. AssertIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27486. AssertIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27487. /* Invalid parameter testing. */
  27488. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27489. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27490. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27491. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27492. AssertIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27493. AssertIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27494. AssertIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27495. AssertIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27496. AssertIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27497. AssertIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27498. AssertIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27499. AssertIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27500. AssertIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27501. AssertIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27502. ASN1_INTEGER_free(b);
  27503. ASN1_INTEGER_free(a);
  27504. res = TEST_RES_CHECK(1);
  27505. #endif
  27506. return res;
  27507. }
  27508. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27509. {
  27510. int res = TEST_SKIPPED;
  27511. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27512. ASN1_INTEGER* ai;
  27513. ASN1_INTEGER* ai2;
  27514. BIGNUM* bn;
  27515. BIGNUM* bn2;
  27516. ai = ASN1_INTEGER_new();
  27517. AssertNotNull(ai);
  27518. bn2 = BN_new();
  27519. AssertNotNull(bn2);
  27520. /* Invalid parameter testing. */
  27521. AssertNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27522. AssertNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27523. /* at the moment hard setting since no set function */
  27524. ai->data[0] = 0xff; /* No DER encoding. */
  27525. ai->length = 1;
  27526. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27527. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27528. BN_free(bn);
  27529. #else
  27530. AssertNull(ASN1_INTEGER_to_BN(ai, NULL));
  27531. #endif
  27532. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27533. ai->data[1] = 0x04; /* bad length of integer */
  27534. ai->data[2] = 0x03;
  27535. ai->length = 3;
  27536. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27537. /* Interpreted as a number 0x020403. */
  27538. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27539. BN_free(bn);
  27540. #else
  27541. AssertNull(ASN1_INTEGER_to_BN(ai, NULL));
  27542. #endif
  27543. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27544. ai->data[1] = 0x01; /* length of integer */
  27545. ai->data[2] = 0x03;
  27546. ai->length = 3;
  27547. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27548. AssertNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27549. AssertIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27550. AssertNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27551. AssertIntEQ(BN_cmp(bn, bn2), 0);
  27552. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27553. ai->data[1] = 0x02; /* length of integer */
  27554. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27555. ai->data[3] = 0xff;
  27556. ai->length = 4;
  27557. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27558. AssertNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  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] = 0x01; /* length of integer */
  27564. ai->data[2] = 0x00;
  27565. ai->length = 3;
  27566. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27567. AssertNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27568. AssertIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27569. AssertNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27570. AssertIntEQ(BN_cmp(bn, bn2), 0);
  27571. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27572. ai->data[1] = 0x01; /* length of integer */
  27573. ai->data[2] = 0x01;
  27574. ai->length = 3;
  27575. ai->negative = 1;
  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. BN_free(bn2);
  27582. BN_free(bn);
  27583. ASN1_INTEGER_free(ai2);
  27584. ASN1_INTEGER_free(ai);
  27585. res = TEST_RES_CHECK(1);
  27586. #endif
  27587. return res;
  27588. }
  27589. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27590. {
  27591. int res = TEST_SKIPPED;
  27592. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27593. ASN1_INTEGER *a;
  27594. long val;
  27595. int ret;
  27596. a = ASN1_INTEGER_new();
  27597. /* Invalid parameter testing. */
  27598. AssertIntEQ(ASN1_INTEGER_get(NULL), 0);
  27599. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27600. AssertIntEQ(ASN1_INTEGER_get(a), 0);
  27601. #else
  27602. AssertIntEQ(ASN1_INTEGER_get(a), -1);
  27603. #endif
  27604. ASN1_INTEGER_free(a);
  27605. a = ASN1_INTEGER_new();
  27606. val = 0;
  27607. ret = ASN1_INTEGER_set(NULL, val);
  27608. AssertIntEQ(ret, 0);
  27609. ASN1_INTEGER_free(a);
  27610. /* 0 */
  27611. a = ASN1_INTEGER_new();
  27612. val = 0;
  27613. ret = ASN1_INTEGER_set(a, val);
  27614. AssertIntEQ(ret, 1);
  27615. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27616. ASN1_INTEGER_free(a);
  27617. /* 40 */
  27618. a = ASN1_INTEGER_new();
  27619. val = 40;
  27620. ret = ASN1_INTEGER_set(a, val);
  27621. AssertIntEQ(ret, 1);
  27622. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27623. ASN1_INTEGER_free(a);
  27624. /* -40 */
  27625. a = ASN1_INTEGER_new();
  27626. val = -40;
  27627. ret = ASN1_INTEGER_set(a, val);
  27628. AssertIntEQ(ret, 1);
  27629. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27630. ASN1_INTEGER_free(a);
  27631. /* 128 */
  27632. a = ASN1_INTEGER_new();
  27633. val = 128;
  27634. ret = ASN1_INTEGER_set(a, val);
  27635. AssertIntEQ(ret, 1);
  27636. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27637. ASN1_INTEGER_free(a);
  27638. /* -128 */
  27639. a = ASN1_INTEGER_new();
  27640. val = -128;
  27641. ret = ASN1_INTEGER_set(a, val);
  27642. AssertIntEQ(ret, 1);
  27643. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27644. ASN1_INTEGER_free(a);
  27645. /* 200 */
  27646. a = ASN1_INTEGER_new();
  27647. val = 200;
  27648. ret = ASN1_INTEGER_set(a, val);
  27649. AssertIntEQ(ret, 1);
  27650. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27651. ASN1_INTEGER_free(a);
  27652. /* int max (2147483647) */
  27653. a = ASN1_INTEGER_new();
  27654. val = 2147483647;
  27655. ret = ASN1_INTEGER_set(a, val);
  27656. AssertIntEQ(ret, 1);
  27657. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27658. ASN1_INTEGER_free(a);
  27659. /* int min (-2147483648) */
  27660. a = ASN1_INTEGER_new();
  27661. val = -2147483647 - 1;
  27662. ret = ASN1_INTEGER_set(a, val);
  27663. AssertIntEQ(ret, 1);
  27664. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27665. ASN1_INTEGER_free(a);
  27666. /* long max positive */
  27667. a = ASN1_INTEGER_new();
  27668. val = (long)(((unsigned long)-1) >> 1);
  27669. ret = ASN1_INTEGER_set(a, val);
  27670. AssertIntEQ(ret, 1);
  27671. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27672. ASN1_INTEGER_free(a);
  27673. res = TEST_RES_CHECK(1);
  27674. #endif
  27675. return res;
  27676. }
  27677. #if defined(OPENSSL_EXTRA)
  27678. typedef struct ASN1IntTestVector {
  27679. const byte* der;
  27680. const size_t derSz;
  27681. const long value;
  27682. } ASN1IntTestVector;
  27683. #endif
  27684. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27685. {
  27686. int res = TEST_SKIPPED;
  27687. #if defined(OPENSSL_EXTRA)
  27688. size_t i;
  27689. WOLFSSL_ASN1_INTEGER* a = NULL;
  27690. WOLFSSL_ASN1_INTEGER* b = NULL;
  27691. WOLFSSL_ASN1_INTEGER* c = NULL;
  27692. const byte* p = NULL;
  27693. byte* reEncoded = NULL;
  27694. int reEncodedSz;
  27695. static const byte zeroDer[] = {
  27696. 0x02, 0x01, 0x00
  27697. };
  27698. static const byte oneDer[] = {
  27699. 0x02, 0x01, 0x01
  27700. };
  27701. static const byte negativeDer[] = {
  27702. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27703. };
  27704. static const byte positiveDer[] = {
  27705. 0x02, 0x03, 0x01, 0x00, 0x01
  27706. };
  27707. static const byte primeDer[] = {
  27708. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27709. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27710. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27711. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27712. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27713. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27714. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27715. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27716. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27717. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27718. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27719. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27720. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27721. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27722. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27723. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27724. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27725. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27726. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27727. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27728. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27729. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27730. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27731. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27732. };
  27733. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27734. static const ASN1IntTestVector testVectors[] = {
  27735. {zeroDer, sizeof(zeroDer), 0},
  27736. {oneDer, sizeof(oneDer), 1},
  27737. {negativeDer, sizeof(negativeDer), -4123123},
  27738. {positiveDer, sizeof(positiveDer), 65537},
  27739. {primeDer, sizeof(primeDer), 0}
  27740. };
  27741. static const size_t NUM_TEST_VECTORS =
  27742. sizeof(testVectors)/sizeof(testVectors[0]);
  27743. /* Check d2i error conditions */
  27744. /* NULL pointer to input. */
  27745. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27746. AssertNull(b);
  27747. /* NULL input. */
  27748. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27749. AssertNull(b);
  27750. /* 0 length. */
  27751. p = testVectors[0].der;
  27752. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27753. AssertNull(b);
  27754. /* Negative length. */
  27755. p = testVectors[0].der;
  27756. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27757. AssertNull(b);
  27758. /* Garbage DER input. */
  27759. p = garbageDer;
  27760. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27761. AssertNull(b);
  27762. {
  27763. /* Check i2d error conditions */
  27764. /* NULL input. */
  27765. byte* p2 = NULL;
  27766. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27767. /* 0 length input data buffer (a->length == 0). */
  27768. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27769. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27770. a->data = NULL;
  27771. /* NULL input data buffer. */
  27772. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27773. /* Reset a->data. */
  27774. a->data = a->intData;
  27775. /* Set a to valid value. */
  27776. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27777. /* NULL output buffer. */
  27778. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27779. wolfSSL_ASN1_INTEGER_free(a);
  27780. }
  27781. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27782. p = testVectors[i].der;
  27783. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  27784. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27785. if (testVectors[i].derSz <= sizeof(long)) {
  27786. c = wolfSSL_ASN1_INTEGER_new();
  27787. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  27788. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27789. wolfSSL_ASN1_INTEGER_free(c);
  27790. }
  27791. /* Convert to DER without a pre-allocated output buffer. */
  27792. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27793. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  27794. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27795. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27796. * output buffer pointer should be incremented just past the end of the
  27797. * encoded data. */
  27798. p = reEncoded;
  27799. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27800. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  27801. AssertPtrEq(p, reEncoded - reEncodedSz);
  27802. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  27803. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  27804. reEncoded = NULL;
  27805. wolfSSL_ASN1_INTEGER_free(a);
  27806. }
  27807. res = TEST_RES_CHECK(1);
  27808. #endif /* OPENSSL_EXTRA */
  27809. return res;
  27810. }
  27811. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27812. {
  27813. int res = TEST_SKIPPED;
  27814. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27815. BIO* bio;
  27816. BIO* out;
  27817. BIO* fixed;
  27818. ASN1_INTEGER* ai;
  27819. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27820. char tmp[1024];
  27821. int tmpSz;
  27822. const char expected1[] = "123456";
  27823. const char expected2[] = "112345678912345678901234567890";
  27824. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27825. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27826. AssertNotNull(out = BIO_new(BIO_s_mem()));
  27827. AssertNotNull(ai = ASN1_INTEGER_new());
  27828. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  27829. /* Invalid parameter testing. */
  27830. AssertIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27831. AssertIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27832. AssertIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27833. AssertIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27834. AssertIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27835. AssertIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27836. AssertIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27837. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27838. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27839. AssertIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27840. AssertIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27841. AssertIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27842. /* No data to read from BIO. */
  27843. AssertIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27844. /* read first line */
  27845. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27846. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27847. XMEMSET(tmp, 0, 1024);
  27848. tmpSz = BIO_read(out, tmp, 1024);
  27849. AssertIntEQ(tmpSz, 6);
  27850. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27851. /* fail on second line (not % 2) */
  27852. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27853. /* read 3rd long line */
  27854. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27855. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27856. XMEMSET(tmp, 0, 1024);
  27857. tmpSz = BIO_read(out, tmp, 1024);
  27858. AssertIntEQ(tmpSz, 30);
  27859. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27860. /* fail on empty line */
  27861. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27862. BIO_free(bio);
  27863. /* Make long integer, requiring dynamic memory, even longer. */
  27864. AssertNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27865. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27866. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27867. XMEMSET(tmp, 0, 1024);
  27868. tmpSz = BIO_read(out, tmp, 1024);
  27869. AssertIntEQ(tmpSz, 48);
  27870. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27871. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27872. XMEMSET(tmp, 0, 1024);
  27873. tmpSz = BIO_read(out, tmp, 1024);
  27874. AssertIntEQ(tmpSz, 56);
  27875. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27876. BIO_free(bio);
  27877. BIO_free(out);
  27878. AssertNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27879. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27880. /* Ensure there is 0 bytes avaialble to write into. */
  27881. AssertIntEQ(BIO_write(fixed, tmp, 1), 1);
  27882. AssertIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27883. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27884. AssertIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27885. BIO_free(fixed);
  27886. ASN1_INTEGER_free(ai);
  27887. res = TEST_RES_CHECK(1);
  27888. #endif
  27889. return res;
  27890. }
  27891. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27892. {
  27893. int res = TEST_SKIPPED;
  27894. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27895. ASN1_INTEGER *a;
  27896. unsigned char *pp,*tpp;
  27897. int ret;
  27898. a = wolfSSL_ASN1_INTEGER_new();
  27899. /* Invalid parameter testing. */
  27900. /* Set pp to an invalid value. */
  27901. pp = NULL;
  27902. AssertIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27903. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27904. AssertIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27905. /* 40 */
  27906. a->intData[0] = ASN_INTEGER;
  27907. a->intData[1] = 1;
  27908. a->intData[2] = 40;
  27909. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27910. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27911. DYNAMIC_TYPE_TMP_BUFFER));
  27912. tpp = pp;
  27913. XMEMSET(pp, 0, ret + 1);
  27914. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27915. pp--;
  27916. AssertIntEQ(*pp, 40);
  27917. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27918. /* 128 */
  27919. a->intData[0] = ASN_INTEGER;
  27920. a->intData[1] = 1;
  27921. a->intData[2] = 128;
  27922. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27923. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27924. DYNAMIC_TYPE_TMP_BUFFER));
  27925. tpp = pp;
  27926. XMEMSET(pp, 0, ret + 1);
  27927. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 2);
  27928. pp--;
  27929. AssertIntEQ(*(pp--), 128);
  27930. AssertIntEQ(*pp, 0);
  27931. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27932. /* -40 */
  27933. a->intData[0] = ASN_INTEGER;
  27934. a->intData[1] = 1;
  27935. a->intData[2] = 40;
  27936. a->negative = 1;
  27937. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27938. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27939. DYNAMIC_TYPE_TMP_BUFFER));
  27940. tpp = pp;
  27941. XMEMSET(pp, 0, ret + 1);
  27942. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27943. pp--;
  27944. AssertIntEQ(*pp, 216);
  27945. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27946. /* -128 */
  27947. a->intData[0] = ASN_INTEGER;
  27948. a->intData[1] = 1;
  27949. a->intData[2] = 128;
  27950. a->negative = 1;
  27951. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27952. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27953. DYNAMIC_TYPE_TMP_BUFFER));
  27954. tpp = pp;
  27955. XMEMSET(pp, 0, ret + 1);
  27956. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27957. pp--;
  27958. AssertIntEQ(*pp, 128);
  27959. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27960. /* -200 */
  27961. a->intData[0] = ASN_INTEGER;
  27962. a->intData[1] = 1;
  27963. a->intData[2] = 200;
  27964. a->negative = 1;
  27965. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27966. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27967. DYNAMIC_TYPE_TMP_BUFFER));
  27968. tpp = pp;
  27969. XMEMSET(pp, 0, ret + 1);
  27970. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 2);
  27971. pp--;
  27972. AssertIntEQ(*(pp--), 56);
  27973. AssertIntEQ(*pp, 255);
  27974. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27975. /* Empty */
  27976. a->intData[0] = ASN_INTEGER;
  27977. a->intData[1] = 0;
  27978. a->negative = 0;
  27979. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27980. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27981. DYNAMIC_TYPE_TMP_BUFFER));
  27982. tpp = pp;
  27983. XMEMSET(pp, 0, ret + 1);
  27984. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27985. pp--;
  27986. AssertIntEQ(*pp, 0);
  27987. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27988. /* 0 */
  27989. a->intData[0] = ASN_INTEGER;
  27990. a->intData[1] = 1;
  27991. a->intData[2] = 0;
  27992. a->negative = 1;
  27993. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27994. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27995. DYNAMIC_TYPE_TMP_BUFFER));
  27996. tpp = pp;
  27997. XMEMSET(pp, 0, ret + 1);
  27998. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27999. pp--;
  28000. AssertIntEQ(*pp, 0);
  28001. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28002. /* 0x100 */
  28003. a->intData[0] = ASN_INTEGER;
  28004. a->intData[1] = 2;
  28005. a->intData[2] = 0x01;
  28006. a->intData[3] = 0x00;
  28007. a->negative = 0;
  28008. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  28009. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28010. DYNAMIC_TYPE_TMP_BUFFER));
  28011. tpp = pp;
  28012. XMEMSET(pp, 0, ret + 1);
  28013. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 2);
  28014. pp -= 2;
  28015. AssertIntEQ(pp[0], 0x01);
  28016. AssertIntEQ(pp[1], 0x00);
  28017. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28018. /* -0x8000 => 0x8000 */
  28019. a->intData[0] = ASN_INTEGER;
  28020. a->intData[1] = 2;
  28021. a->intData[2] = 0x80;
  28022. a->intData[3] = 0x00;
  28023. a->negative = 1;
  28024. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  28025. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28026. DYNAMIC_TYPE_TMP_BUFFER));
  28027. tpp = pp;
  28028. XMEMSET(pp, 0, ret + 1);
  28029. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 2);
  28030. pp -= 2;
  28031. AssertIntEQ(pp[0], 0x80);
  28032. AssertIntEQ(pp[1], 0x00);
  28033. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28034. /* -0x8001 => 0xFF7FFF */
  28035. a->intData[0] = ASN_INTEGER;
  28036. a->intData[1] = 2;
  28037. a->intData[2] = 0x80;
  28038. a->intData[3] = 0x01;
  28039. a->negative = 1;
  28040. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  28041. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28042. DYNAMIC_TYPE_TMP_BUFFER));
  28043. tpp = pp;
  28044. XMEMSET(pp, 0, ret + 1);
  28045. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 3);
  28046. pp -= 3;
  28047. AssertIntEQ(pp[0], 0xFF);
  28048. AssertIntEQ(pp[1], 0x7F);
  28049. AssertIntEQ(pp[2], 0xFF);
  28050. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28051. wolfSSL_ASN1_INTEGER_free(a);
  28052. res = TEST_RES_CHECK(1);
  28053. #endif /* OPENSSL_EXTRA && !NO_ASN */
  28054. return res;
  28055. }
  28056. static int test_wolfSSL_ASN1_OBJECT(void)
  28057. {
  28058. int res = TEST_SKIPPED;
  28059. #if defined(OPENSSL_EXTRA)
  28060. ASN1_OBJECT* a;
  28061. ASN1_OBJECT s;
  28062. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  28063. /* Invalid parameter testing. */
  28064. ASN1_OBJECT_free(NULL);
  28065. AssertNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  28066. /* Test that a static ASN1_OBJECT can be freed. */
  28067. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  28068. ASN1_OBJECT_free(&s);
  28069. AssertNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  28070. ASN1_OBJECT_free(a);
  28071. s.obj = der;
  28072. s.objSz = sizeof(der);
  28073. AssertNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  28074. ASN1_OBJECT_free(a);
  28075. ASN1_OBJECT_free(&s);
  28076. res = TEST_RES_CHECK(1);
  28077. #endif /* OPENSSL_EXTRA */
  28078. return res;
  28079. }
  28080. static int test_wolfSSL_ASN1_get_object(void)
  28081. {
  28082. int res = TEST_SKIPPED;
  28083. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28084. const unsigned char* derBuf = cliecc_cert_der_256;
  28085. const unsigned char* nullPtr = NULL;
  28086. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  28087. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  28088. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  28089. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  28090. const unsigned char* p;
  28091. unsigned char objDer[8];
  28092. unsigned char* der;
  28093. unsigned char* derPtr;
  28094. int len = sizeof_cliecc_cert_der_256;
  28095. long asnLen = 0;
  28096. int tag = 0;
  28097. int cls = 0;
  28098. ASN1_OBJECT* a;
  28099. ASN1_OBJECT s;
  28100. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  28101. /* Invalid encoding at length. */
  28102. p = objDerInvalidLen;
  28103. AssertIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  28104. 0x80);
  28105. p = objDerBadLen;
  28106. /* Error = 0x80, Constructed = 0x20 */
  28107. AssertIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  28108. 0x80 | 0x20);
  28109. /* Read a couple TLV triplets and make sure they match the expected values
  28110. */
  28111. /* SEQUENCE */
  28112. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  28113. AssertIntEQ(asnLen, 862);
  28114. AssertIntEQ(tag, 0x10);
  28115. AssertIntEQ(cls, 0);
  28116. /* SEQUENCE */
  28117. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28118. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28119. AssertIntEQ(asnLen, 772);
  28120. AssertIntEQ(tag, 0x10);
  28121. AssertIntEQ(cls, 0);
  28122. /* [0] */
  28123. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28124. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28125. AssertIntEQ(asnLen, 3);
  28126. AssertIntEQ(tag, 0);
  28127. AssertIntEQ(cls, 0x80);
  28128. /* INTEGER */
  28129. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28130. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28131. AssertIntEQ(asnLen, 1);
  28132. AssertIntEQ(tag, 0x2);
  28133. AssertIntEQ(cls, 0);
  28134. derBuf += asnLen;
  28135. /* INTEGER */
  28136. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28137. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28138. AssertIntEQ(asnLen, 20);
  28139. AssertIntEQ(tag, 0x2);
  28140. AssertIntEQ(cls, 0);
  28141. derBuf += asnLen;
  28142. /* SEQUENCE */
  28143. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28144. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28145. AssertIntEQ(asnLen, 10);
  28146. AssertIntEQ(tag, 0x10);
  28147. AssertIntEQ(cls, 0);
  28148. /* Found OBJECT_ID. */
  28149. /* Invalid parameter testing. */
  28150. AssertIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  28151. AssertIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  28152. AssertIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  28153. AssertIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  28154. AssertIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  28155. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  28156. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  28157. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  28158. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  28159. AssertNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  28160. AssertNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28161. AssertNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28162. AssertNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  28163. AssertNull(d2i_ASN1_OBJECT(&a, NULL, len));
  28164. AssertNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  28165. AssertNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  28166. AssertNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  28167. AssertNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28168. AssertNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28169. AssertNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  28170. AssertNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  28171. /* Invalid encoding at length. */
  28172. p = objDerInvalidLen;
  28173. AssertNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  28174. p = objDerBadLen;
  28175. AssertNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  28176. p = objDerNotObj;
  28177. AssertNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  28178. p = objDerNoData;
  28179. AssertNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  28180. /* Create an ASN OBJECT from content */
  28181. p = derBuf + 2;
  28182. AssertNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  28183. ASN1_OBJECT_free(a);
  28184. /* Create an ASN OBJECT from DER */
  28185. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  28186. /* Invalid parameter testing. */
  28187. AssertIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  28188. AssertIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  28189. AssertIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  28190. der = NULL;
  28191. AssertIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  28192. derPtr = objDer;
  28193. AssertIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  28194. AssertPtrNE(derPtr, objDer);
  28195. AssertIntEQ(XMEMCMP(der, objDer, 8), 0);
  28196. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28197. ASN1_OBJECT_free(a);
  28198. res = TEST_RES_CHECK(1);
  28199. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  28200. return res;
  28201. }
  28202. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  28203. {
  28204. int res = TEST_SKIPPED;
  28205. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28206. ASN1_OBJECT* obj = NULL;
  28207. ASN1_OBJECT* a = NULL;
  28208. BIO *bio = NULL;
  28209. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  28210. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  28211. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  28212. const unsigned char* p;
  28213. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  28214. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  28215. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  28216. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  28217. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  28218. /* No DER encoding in ASN1_OBJECT. */
  28219. a = wolfSSL_ASN1_OBJECT_new();
  28220. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28221. ASN1_OBJECT_free(a);
  28222. /* DER encoding - not OBJECT_ID */
  28223. p = notObjDer;
  28224. AssertNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28225. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28226. ASN1_OBJECT_free(a);
  28227. /* Bad length encoding. */
  28228. p = badLenDer;
  28229. AssertNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28230. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28231. ASN1_OBJECT_free(a);
  28232. /* Good encoding - but unknown. */
  28233. p = goodDer;
  28234. AssertNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28235. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28236. ASN1_OBJECT_free(a);
  28237. BIO_free(bio);
  28238. ASN1_OBJECT_free(obj);
  28239. res = TEST_RES_CHECK(1);
  28240. #endif
  28241. return res;
  28242. }
  28243. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  28244. {
  28245. int res = TEST_SKIPPED;
  28246. #if defined(OPENSSL_EXTRA) && \
  28247. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  28248. char buf[50] = {0};
  28249. ASN1_OBJECT* obj;
  28250. const char* oid = "2.5.29.19";
  28251. const char* ln = "X509v3 Basic Constraints";
  28252. obj = NULL;
  28253. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  28254. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  28255. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  28256. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  28257. XMEMSET(buf, 0, sizeof(buf));
  28258. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  28259. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  28260. ASN1_OBJECT_free(obj);
  28261. res = TEST_RES_CHECK(1);
  28262. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  28263. return res;
  28264. }
  28265. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  28266. {
  28267. int res = TEST_SKIPPED;
  28268. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  28269. WOLFSSL_STACK* sk;
  28270. WOLFSSL_ASN1_OBJECT* obj;
  28271. AssertNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28272. AssertNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28273. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28274. AssertNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28275. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  28276. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  28277. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  28278. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28279. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  28280. /* obj freed in pop_free call. */
  28281. AssertNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28282. AssertNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28283. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28284. AssertPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  28285. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28286. wolfSSL_ASN1_OBJECT_free(obj);
  28287. res = TEST_RES_CHECK(1);
  28288. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  28289. return res;
  28290. }
  28291. static int test_wolfSSL_ASN1_STRING(void)
  28292. {
  28293. int res = TEST_SKIPPED;
  28294. #if defined(OPENSSL_EXTRA)
  28295. ASN1_STRING* str = NULL;
  28296. ASN1_STRING* c = NULL;
  28297. const char data[] = "hello wolfSSL";
  28298. const char data2[] = "Same len data";
  28299. const char longData[] =
  28300. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28301. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28302. ASN1_STRING_free(str);
  28303. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28304. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28305. AssertIntEQ(ASN1_STRING_type(NULL), 0);
  28306. /* Check setting to NULL works. */
  28307. AssertIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  28308. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28309. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28310. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28311. AssertIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  28312. AssertIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  28313. AssertIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  28314. AssertIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  28315. AssertNull(wolfSSL_ASN1_STRING_dup(NULL));
  28316. AssertNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  28317. AssertIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  28318. AssertIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  28319. AssertIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  28320. AssertIntEQ(ASN1_STRING_cmp(str, c), 0);
  28321. AssertIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  28322. AssertIntGT(ASN1_STRING_cmp(str, c), 0);
  28323. AssertIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  28324. AssertIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  28325. AssertIntEQ(ASN1_STRING_cmp(str, c), 0);
  28326. /* Check setting back to smaller size frees dynamic data. */
  28327. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28328. AssertIntLT(ASN1_STRING_cmp(str, c), 0);
  28329. AssertIntGT(ASN1_STRING_cmp(c, str), 0);
  28330. AssertNull(ASN1_STRING_get0_data(NULL));
  28331. AssertNotNull(ASN1_STRING_get0_data(str));
  28332. AssertNull(ASN1_STRING_data(NULL));
  28333. AssertNotNull(ASN1_STRING_data(str));
  28334. AssertIntEQ(ASN1_STRING_length(NULL), 0);
  28335. AssertIntGT(ASN1_STRING_length(str), 0);
  28336. ASN1_STRING_free(c);
  28337. ASN1_STRING_free(str);
  28338. ASN1_STRING_free(NULL);
  28339. #ifndef NO_WOLFSSL_STUB
  28340. AssertNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  28341. #endif
  28342. res = TEST_RES_CHECK(1);
  28343. #endif
  28344. return res;
  28345. }
  28346. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  28347. {
  28348. int res = TEST_SKIPPED;
  28349. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA)
  28350. WOLFSSL_X509* x509;
  28351. WOLFSSL_X509_NAME* subject;
  28352. WOLFSSL_X509_NAME_ENTRY* e;
  28353. WOLFSSL_ASN1_STRING* a;
  28354. FILE* file;
  28355. int idx = 0;
  28356. char targetOutput[16] = "www.wolfssl.com";
  28357. unsigned char* actual_output;
  28358. int len = 0;
  28359. int result = 0;
  28360. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  28361. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  28362. fclose(file);
  28363. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  28364. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  28365. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  28366. NID_commonName, -1)), 5);
  28367. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  28368. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  28369. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  28370. result = strncmp((const char*)actual_output, targetOutput, len);
  28371. AssertIntEQ(result, 0);
  28372. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  28373. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28374. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28375. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28376. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28377. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28378. wolfSSL_X509_free(x509);
  28379. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28380. AssertNotNull(a = ASN1_STRING_new());
  28381. AssertIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28382. ASN1_STRING_free(a);
  28383. res = TEST_RES_CHECK(1);
  28384. #endif
  28385. return res;
  28386. }
  28387. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28388. {
  28389. int res = TEST_SKIPPED;
  28390. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28391. WOLFSSL_ASN1_STRING* str;
  28392. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28393. char* ret;
  28394. AssertNotNull(str = ASN1_STRING_new());
  28395. AssertNull(wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28396. /* No data. */
  28397. AssertNull(wolfSSL_i2s_ASN1_STRING(NULL, str));
  28398. AssertIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28399. AssertNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28400. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28401. AssertIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28402. /* No type. */
  28403. AssertNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28404. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28405. ASN1_STRING_free(str);
  28406. res = TEST_RES_CHECK(1);
  28407. #endif
  28408. return res;
  28409. }
  28410. static int test_wolfSSL_ASN1_STRING_canon(void)
  28411. {
  28412. int res = TEST_SKIPPED;
  28413. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28414. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28415. defined(OPENSSL_EXTRA_X509_SMALL))
  28416. WOLFSSL_ASN1_STRING* orig;
  28417. WOLFSSL_ASN1_STRING* canon;
  28418. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28419. const char* whitespaceOnly = "\t\r\n";
  28420. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28421. const char* canonData = "\x01 \x02 \xff tt";
  28422. const char longData[] =
  28423. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28424. AssertNotNull(orig = ASN1_STRING_new());
  28425. AssertNotNull(canon = ASN1_STRING_new());
  28426. /* Invalid parameter testing. */
  28427. AssertIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28428. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28429. AssertIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28430. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28431. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28432. AssertIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28433. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28434. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28435. AssertIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28436. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28437. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28438. ASN1_STRING_free(orig);
  28439. AssertNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28440. AssertIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28441. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28442. AssertIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28443. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28444. ASN1_STRING_free(orig);
  28445. AssertNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28446. AssertIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28447. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28448. ASN1_STRING_free(orig);
  28449. AssertNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28450. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28451. ASN1_STRING_free(orig);
  28452. ASN1_STRING_free(canon);
  28453. res = TEST_RES_CHECK(1);
  28454. #endif
  28455. #endif
  28456. return res;
  28457. }
  28458. static int test_wolfSSL_ASN1_STRING_print(void)
  28459. {
  28460. int res = TEST_SKIPPED;
  28461. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28462. !defined(NO_BIO)
  28463. ASN1_STRING* asnStr = NULL;
  28464. const char HELLO_DATA[]= \
  28465. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28466. #define MAX_UNPRINTABLE_CHAR 32
  28467. #define MAX_BUF 255
  28468. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28469. unsigned char expected[sizeof(unprintableData)+1];
  28470. unsigned char rbuf[MAX_BUF];
  28471. BIO *bio;
  28472. int p_len, i;
  28473. /* setup */
  28474. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28475. unprintableData[i] = HELLO_DATA[i];
  28476. expected[i] = HELLO_DATA[i];
  28477. }
  28478. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28479. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28480. if (i == (int)'\n' || i == (int)'\r')
  28481. expected[sizeof(HELLO_DATA)+i] = i;
  28482. else
  28483. expected[sizeof(HELLO_DATA)+i] = '.';
  28484. }
  28485. unprintableData[sizeof(unprintableData)-1] = '\0';
  28486. expected[sizeof(expected)-1] = '\0';
  28487. XMEMSET(rbuf, 0, MAX_BUF);
  28488. bio = BIO_new(BIO_s_mem());
  28489. BIO_set_write_buf_size(bio, MAX_BUF);
  28490. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  28491. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28492. (int)sizeof(unprintableData));
  28493. /* test */
  28494. AssertIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28495. AssertIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28496. AssertIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28497. AssertIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28498. BIO_read(bio, (void*)rbuf, 46);
  28499. AssertStrEQ((char*)rbuf, (const char*)expected);
  28500. BIO_free(bio);
  28501. AssertNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28502. AssertIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28503. /* Ensure there is 0 bytes avaialble to write into. */
  28504. AssertIntEQ(BIO_write(bio, rbuf, 1), 1);
  28505. AssertIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28506. AssertIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28507. AssertIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28508. AssertIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28509. AssertIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28510. BIO_free(bio);
  28511. ASN1_STRING_free(asnStr);
  28512. res = TEST_RES_CHECK(1);
  28513. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28514. return res;
  28515. }
  28516. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28517. {
  28518. int res = TEST_SKIPPED;
  28519. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28520. ASN1_STRING* asn_str;
  28521. const char data[] = "Hello wolfSSL!";
  28522. ASN1_STRING* esc_str;
  28523. const char esc_data[] = "a+;<>";
  28524. ASN1_STRING* neg_int;
  28525. const char neg_int_data[] = "\xff";
  28526. ASN1_STRING* neg_enum;
  28527. const char neg_enum_data[] = "\xff";
  28528. BIO *bio;
  28529. BIO *fixed;
  28530. unsigned long flags;
  28531. int p_len;
  28532. unsigned char rbuf[255];
  28533. /* setup */
  28534. XMEMSET(rbuf, 0, 255);
  28535. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  28536. BIO_set_write_buf_size(bio, 255);
  28537. AssertNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28538. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  28539. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  28540. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  28541. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  28542. neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER);
  28543. ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28544. sizeof(neg_int_data) - 1);
  28545. neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED);
  28546. ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28547. sizeof(neg_enum_data) - 1);
  28548. /* Invalid parameter testing. */
  28549. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28550. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28551. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28552. /* no flags */
  28553. XMEMSET(rbuf, 0, 255);
  28554. flags = 0;
  28555. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28556. AssertIntEQ(p_len, 15);
  28557. BIO_read(bio, (void*)rbuf, 15);
  28558. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  28559. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28560. /* Ensure there is 0 bytes avaialble to write into. */
  28561. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28562. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28563. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28564. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28565. AssertIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28566. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28567. /* RFC2253 Escape */
  28568. XMEMSET(rbuf, 0, 255);
  28569. flags = ASN1_STRFLGS_ESC_2253;
  28570. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  28571. AssertIntEQ(p_len, 9);
  28572. BIO_read(bio, (void*)rbuf, 9);
  28573. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28574. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28575. /* Ensure there is 0 bytes avaialble to write into. */
  28576. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28577. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28578. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28579. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28580. AssertIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28581. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28582. /* Show type */
  28583. XMEMSET(rbuf, 0, 255);
  28584. flags = ASN1_STRFLGS_SHOW_TYPE;
  28585. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28586. AssertIntEQ(p_len, 28);
  28587. BIO_read(bio, (void*)rbuf, 28);
  28588. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28589. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28590. /* Ensure there is 0 bytes avaialble to write into. */
  28591. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28592. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28593. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28594. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28595. AssertIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28596. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28597. AssertIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28598. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28599. /* Dump All */
  28600. XMEMSET(rbuf, 0, 255);
  28601. flags = ASN1_STRFLGS_DUMP_ALL;
  28602. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28603. AssertIntEQ(p_len, 31);
  28604. BIO_read(bio, (void*)rbuf, 31);
  28605. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28606. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28607. /* Ensure there is 0 bytes avaialble to write into. */
  28608. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28609. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28610. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28611. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28612. AssertIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28613. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28614. /* Dump Der */
  28615. XMEMSET(rbuf, 0, 255);
  28616. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28617. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28618. AssertIntEQ(p_len, 35);
  28619. BIO_read(bio, (void*)rbuf, 35);
  28620. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28621. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28622. /* Ensure there is 0 bytes avaialble to write into. */
  28623. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28624. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28625. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28626. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28627. AssertIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28628. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28629. AssertIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28630. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28631. /* Dump All + Show type */
  28632. XMEMSET(rbuf, 0, 255);
  28633. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28634. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28635. AssertIntEQ(p_len, 44);
  28636. BIO_read(bio, (void*)rbuf, 44);
  28637. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28638. /* Dump All + Show type - Negative Integer. */
  28639. XMEMSET(rbuf, 0, 255);
  28640. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28641. p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags);
  28642. AssertIntEQ(p_len, 11);
  28643. BIO_read(bio, (void*)rbuf, 11);
  28644. AssertStrEQ((char*)rbuf, "INTEGER:#FF");
  28645. /* Dump All + Show type - Negative Enumerated. */
  28646. XMEMSET(rbuf, 0, 255);
  28647. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28648. p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags);
  28649. AssertIntEQ(p_len, 14);
  28650. BIO_read(bio, (void*)rbuf, 14);
  28651. AssertStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28652. BIO_free(fixed);
  28653. BIO_free(bio);
  28654. ASN1_STRING_free(asn_str);
  28655. ASN1_STRING_free(esc_str);
  28656. ASN1_STRING_free(neg_int);
  28657. ASN1_STRING_free(neg_enum);
  28658. AssertStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28659. AssertStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28660. res = TEST_RES_CHECK(1);
  28661. #endif
  28662. return res;
  28663. }
  28664. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28665. {
  28666. int res = TEST_SKIPPED;
  28667. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28668. ASN1_STRING* asn1str_test;
  28669. ASN1_STRING* asn1str_answer;
  28670. /* Each character is encoded using 4 bytes */
  28671. char input[] = {
  28672. 0, 0, 0, 'T',
  28673. 0, 0, 0, 'e',
  28674. 0, 0, 0, 's',
  28675. 0, 0, 0, 't',
  28676. };
  28677. char output[] = "Test";
  28678. char badInput[] = {
  28679. 1, 0, 0, 'T',
  28680. 0, 1, 0, 'e',
  28681. 0, 0, 1, 's',
  28682. };
  28683. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28684. /* Test wrong type. */
  28685. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28686. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28687. ASN1_STRING_free(asn1str_test);
  28688. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28689. /* Test bad length. */
  28690. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28691. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28692. /* Test bad input. */
  28693. AssertIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28694. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28695. AssertIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28696. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28697. AssertIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28698. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28699. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28700. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28701. AssertNotNull(
  28702. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28703. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28704. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28705. ASN1_STRING_free(asn1str_test);
  28706. ASN1_STRING_free(asn1str_answer);
  28707. res = TEST_RES_CHECK(1);
  28708. #endif /* OPENSSL_ALL && !NO_ASN */
  28709. return res;
  28710. }
  28711. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28712. {
  28713. int res = TEST_SKIPPED;
  28714. #if defined(OPENSSL_EXTRA)
  28715. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  28716. unsigned char nullstr[32];
  28717. XMEMSET(nullstr, 0, 32);
  28718. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  28719. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  28720. DYNAMIC_TYPE_TMP_BUFFER);
  28721. if (asn1_gtime) {
  28722. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  28723. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28724. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  28725. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28726. }
  28727. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  28728. res = TEST_RES_CHECK(1);
  28729. #endif /* OPENSSL_EXTRA */
  28730. return res;
  28731. }
  28732. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28733. {
  28734. int res = TEST_SKIPPED;
  28735. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28736. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  28737. BIO* bio;
  28738. unsigned char buf[24];
  28739. int i;
  28740. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  28741. BIO_set_write_buf_size(bio, 24);
  28742. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  28743. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28744. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  28745. /* Type not set. */
  28746. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28747. gtime.type = V_ASN1_GENERALIZEDTIME;
  28748. /* Invalid parameters testing. */
  28749. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28750. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28751. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  28752. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  28753. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28754. AssertIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28755. BIO_free(bio);
  28756. AssertNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28757. AssertIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28758. /* Ensure there is 0 bytes avaialble to write into. */
  28759. AssertIntEQ(BIO_write(bio, buf, 1), 1);
  28760. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28761. for (i = 1; i < 20; i++) {
  28762. AssertIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28763. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28764. }
  28765. BIO_free(bio);
  28766. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  28767. res = TEST_RES_CHECK(1);
  28768. #endif /* OPENSSL_EXTRA */
  28769. return res;
  28770. }
  28771. static int test_wolfSSL_ASN1_TIME(void)
  28772. {
  28773. int res = TEST_SKIPPED;
  28774. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28775. WOLFSSL_ASN1_TIME* asn_time;
  28776. unsigned char *data;
  28777. AssertNotNull(asn_time = ASN1_TIME_new());
  28778. #ifndef NO_WOLFSSL_STUB
  28779. AssertNotNull(ASN1_TIME_set(asn_time, 1));
  28780. #endif
  28781. AssertIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28782. AssertIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28783. AssertIntEQ(ASN1_TIME_set_string(NULL,
  28784. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28785. AssertIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28786. AssertIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28787. AssertIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28788. AssertIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28789. AssertNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28790. AssertNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28791. AssertIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28792. AssertIntEQ(ASN1_TIME_check(NULL), 0);
  28793. AssertIntEQ(ASN1_TIME_check(asn_time), 1);
  28794. ASN1_TIME_free(asn_time);
  28795. ASN1_TIME_free(NULL);
  28796. res = TEST_RES_CHECK(1);
  28797. #endif
  28798. return res;
  28799. }
  28800. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28801. {
  28802. int res = TEST_SKIPPED;
  28803. #ifndef NO_ASN_TIME
  28804. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28805. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28806. WOLFSSL_ASN1_TIME* t;
  28807. char buf[ASN_GENERALIZED_TIME_SIZE];
  28808. AssertNotNull((t = ASN1_TIME_new()));
  28809. AssertIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28810. /* Invalid parameter testing. */
  28811. AssertNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28812. AssertNull(ASN1_TIME_to_string(t, NULL, 4));
  28813. AssertNull(ASN1_TIME_to_string(NULL, buf, 4));
  28814. AssertNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28815. AssertNull(ASN1_TIME_to_string(NULL, buf, 5));
  28816. AssertNull(ASN1_TIME_to_string(t, NULL, 5));
  28817. AssertNull(ASN1_TIME_to_string(t, buf, 4));
  28818. /* Buffer needs to be longer than minimum of 5 characters. */
  28819. AssertNull(ASN1_TIME_to_string(t, buf, 5));
  28820. ASN1_TIME_free(t);
  28821. res = TEST_RES_CHECK(1);
  28822. #endif
  28823. #endif /* NO_ASN_TIME */
  28824. return res;
  28825. }
  28826. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28827. {
  28828. int res = TEST_SKIPPED;
  28829. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28830. ASN1_TIME* fromTime;
  28831. ASN1_TIME* closeToTime;
  28832. ASN1_TIME* toTime;
  28833. ASN1_TIME* invalidTime;
  28834. int daysDiff;
  28835. int secsDiff;
  28836. AssertNotNull((fromTime = ASN1_TIME_new()));
  28837. /* Feb 22, 2003, 21:15:15 */
  28838. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28839. AssertNotNull((closeToTime = ASN1_TIME_new()));
  28840. /* Feb 22, 2003, 21:16:15 */
  28841. AssertIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28842. AssertNotNull((toTime = ASN1_TIME_new()));
  28843. /* Dec 19, 2010, 18:10:11 */
  28844. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28845. AssertNotNull((invalidTime = ASN1_TIME_new()));
  28846. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28847. AssertIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28848. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28849. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28850. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28851. /* Error conditions. */
  28852. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28853. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28854. /* If both times are NULL, difference is 0. */
  28855. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28856. AssertIntEQ(daysDiff, 0);
  28857. AssertIntEQ(secsDiff, 0);
  28858. /* If one time is NULL, it defaults to the current time. */
  28859. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28860. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28861. /* Normal operation. Both times non-NULL. */
  28862. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28863. AssertIntEQ(daysDiff, 2856);
  28864. AssertIntEQ(secsDiff, 75296);
  28865. /* Swapping the times should return negative values. */
  28866. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28867. AssertIntEQ(daysDiff, -2856);
  28868. AssertIntEQ(secsDiff, -75296);
  28869. /* Compare with invalid time string. */
  28870. AssertIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28871. AssertIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28872. /* Compare with days difference of 0. */
  28873. AssertIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28874. AssertIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28875. /* Days and seconds differences not 0. */
  28876. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28877. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28878. /* Same time. */
  28879. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28880. /* Compare regression test: No seconds difference, just difference in days.
  28881. */
  28882. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28883. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28884. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28885. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28886. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28887. /* Edge case with Unix epoch. */
  28888. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28889. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28890. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28891. AssertIntEQ(daysDiff, 3652);
  28892. AssertIntEQ(secsDiff, 0);
  28893. /* Edge case with year > 2038 (year 2038 problem). */
  28894. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28895. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28896. AssertIntEQ(daysDiff, 2932896);
  28897. AssertIntEQ(secsDiff, 86399);
  28898. ASN1_TIME_free(fromTime);
  28899. ASN1_TIME_free(closeToTime);
  28900. ASN1_TIME_free(toTime);
  28901. ASN1_TIME_free(invalidTime);
  28902. res = TEST_RES_CHECK(1);
  28903. #endif
  28904. return res;
  28905. }
  28906. static int test_wolfSSL_ASN1_TIME_adj(void)
  28907. {
  28908. int res = TEST_SKIPPED;
  28909. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  28910. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28911. const int year = 365*24*60*60;
  28912. const int day = 24*60*60;
  28913. const int hour = 60*60;
  28914. const int mini = 60;
  28915. const byte asn_utc_time = ASN_UTC_TIME;
  28916. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28917. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28918. #endif
  28919. WOLFSSL_ASN1_TIME* asn_time;
  28920. WOLFSSL_ASN1_TIME* s;
  28921. int offset_day;
  28922. long offset_sec;
  28923. char date_str[CTC_DATE_SIZE + 1];
  28924. time_t t;
  28925. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  28926. /* UTC notation test */
  28927. /* 2000/2/15 20:30:00 */
  28928. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28929. offset_day = 7;
  28930. offset_sec = 45 * mini;
  28931. /* offset_sec = -45 * min;*/
  28932. AssertNotNull(asn_time =
  28933. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28934. AssertTrue(asn_time->type == asn_utc_time);
  28935. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28936. date_str[CTC_DATE_SIZE] = '\0';
  28937. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28938. /* negative offset */
  28939. offset_sec = -45 * mini;
  28940. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28941. AssertNotNull(asn_time);
  28942. AssertTrue(asn_time->type == asn_utc_time);
  28943. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28944. date_str[CTC_DATE_SIZE] = '\0';
  28945. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28946. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28947. XMEMSET(date_str, 0, sizeof(date_str));
  28948. /* Generalized time will overflow time_t if not long */
  28949. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28950. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28951. DYNAMIC_TYPE_OPENSSL);
  28952. /* GeneralizedTime notation test */
  28953. /* 2055/03/01 09:00:00 */
  28954. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28955. offset_day = 12;
  28956. offset_sec = 10 * mini;
  28957. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28958. AssertTrue(asn_time->type == asn_gen_time);
  28959. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28960. date_str[CTC_DATE_SIZE] = '\0';
  28961. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28962. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28963. XMEMSET(date_str, 0, sizeof(date_str));
  28964. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28965. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28966. s = NULL;
  28967. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28968. offset_day = 7;
  28969. offset_sec = 45 * mini;
  28970. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28971. AssertTrue(asn_time->type == asn_utc_time);
  28972. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28973. date_str[CTC_DATE_SIZE] = '\0';
  28974. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28975. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28976. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  28977. AssertTrue(asn_time->type == asn_utc_time);
  28978. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28979. date_str[CTC_DATE_SIZE] = '\0';
  28980. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28981. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28982. res = TEST_RES_CHECK(1);
  28983. #endif
  28984. return res;
  28985. }
  28986. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28987. {
  28988. int res = TEST_SKIPPED;
  28989. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28990. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28991. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28992. ASN1_TIME asnTime;
  28993. struct tm tm;
  28994. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28995. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28996. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28997. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28998. AssertIntEQ(tm.tm_sec, 15);
  28999. AssertIntEQ(tm.tm_min, 15);
  29000. AssertIntEQ(tm.tm_hour, 21);
  29001. AssertIntEQ(tm.tm_mday, 22);
  29002. AssertIntEQ(tm.tm_mon, 1);
  29003. AssertIntEQ(tm.tm_year, 100);
  29004. AssertIntEQ(tm.tm_isdst, 0);
  29005. #ifdef XMKTIME
  29006. AssertIntEQ(tm.tm_wday, 2);
  29007. AssertIntEQ(tm.tm_yday, 52);
  29008. #endif
  29009. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  29010. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  29011. AssertIntEQ(tm.tm_year, 50);
  29012. /* Get current time. */
  29013. AssertIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  29014. AssertIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  29015. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  29016. /* 0 length. */
  29017. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29018. /* No type. */
  29019. asnTime.length = 1;
  29020. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29021. /* Not UTCTIME length. */
  29022. asnTime.type = V_ASN1_UTCTIME;
  29023. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29024. /* Not GENERALIZEDTIME length. */
  29025. asnTime.type = V_ASN1_GENERALIZEDTIME;
  29026. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29027. /* Not Zulu timezone. */
  29028. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  29029. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29030. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  29031. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29032. res = TEST_RES_CHECK(1);
  29033. #endif
  29034. return res;
  29035. }
  29036. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  29037. {
  29038. int res = TEST_SKIPPED;
  29039. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  29040. WOLFSSL_ASN1_TIME *t;
  29041. WOLFSSL_ASN1_TIME *out;
  29042. WOLFSSL_ASN1_TIME *gtime;
  29043. int tlen = 0;
  29044. unsigned char *data;
  29045. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  29046. AssertNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  29047. /* type not set. */
  29048. AssertNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  29049. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29050. /* UTC Time test */
  29051. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  29052. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  29053. t->type = ASN_UTC_TIME;
  29054. t->length = ASN_UTC_TIME_SIZE;
  29055. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  29056. tlen = wolfSSL_ASN1_TIME_get_length(t);
  29057. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  29058. data = wolfSSL_ASN1_TIME_get_data(t);
  29059. AssertStrEQ((char*)data, "050727123456Z");
  29060. out = NULL;
  29061. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  29062. wolfSSL_ASN1_TIME_free(gtime);
  29063. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  29064. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  29065. AssertPtrEq(gtime, out);
  29066. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29067. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29068. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  29069. /* Generalized Time test */
  29070. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  29071. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  29072. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  29073. t->type = ASN_GENERALIZED_TIME;
  29074. t->length = ASN_GENERALIZED_TIME_SIZE;
  29075. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  29076. tlen = wolfSSL_ASN1_TIME_get_length(t);
  29077. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  29078. data = wolfSSL_ASN1_TIME_get_data(t);
  29079. AssertStrEQ((char*)data, "20050727123456Z");
  29080. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  29081. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29082. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29083. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  29084. /* UTC Time to Generalized Time 1900's test */
  29085. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  29086. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  29087. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  29088. t->type = ASN_UTC_TIME;
  29089. t->length = ASN_UTC_TIME_SIZE;
  29090. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  29091. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  29092. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29093. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29094. AssertStrEQ((char*)gtime->data, "19500727123456Z");
  29095. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29096. /* Null parameter test */
  29097. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  29098. gtime = NULL;
  29099. out = NULL;
  29100. t->type = ASN_UTC_TIME;
  29101. t->length = ASN_UTC_TIME_SIZE;
  29102. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  29103. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  29104. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29105. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29106. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  29107. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29108. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29109. res = TEST_RES_CHECK(1);
  29110. #endif
  29111. return res;
  29112. }
  29113. static int test_wolfSSL_ASN1_TIME_print(void)
  29114. {
  29115. int res = TEST_SKIPPED;
  29116. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  29117. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  29118. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  29119. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  29120. !defined(NO_ASN_TIME)
  29121. BIO* bio;
  29122. BIO* fixed;
  29123. X509* x509;
  29124. const unsigned char* der = client_cert_der_2048;
  29125. ASN1_TIME* notAfter;
  29126. ASN1_TIME* notBefore;
  29127. unsigned char buf[25];
  29128. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29129. AssertNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29130. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  29131. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  29132. AssertNotNull(notBefore = X509_get_notBefore(x509));
  29133. AssertIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  29134. AssertIntEQ(ASN1_TIME_print(bio, NULL), 0);
  29135. AssertIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  29136. AssertIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  29137. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29138. AssertIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  29139. /* Test BIO_write fails. */
  29140. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29141. /* Ensure there is 0 bytes avaialble to write into. */
  29142. AssertIntEQ(BIO_write(fixed, buf, 1), 1);
  29143. AssertIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29144. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29145. AssertIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29146. AssertIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  29147. AssertIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29148. /* create a bad time and test results */
  29149. AssertNotNull(notAfter = X509_get_notAfter(x509));
  29150. AssertIntEQ(ASN1_TIME_check(notAfter), 1);
  29151. notAfter->data[8] = 0;
  29152. notAfter->data[3] = 0;
  29153. AssertIntNE(ASN1_TIME_print(bio, notAfter), 1);
  29154. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29155. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29156. AssertIntEQ(ASN1_TIME_check(notAfter), 0);
  29157. BIO_free(bio);
  29158. BIO_free(fixed);
  29159. X509_free(x509);
  29160. res = TEST_RES_CHECK(1);
  29161. #endif
  29162. return res;
  29163. }
  29164. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  29165. {
  29166. int res = TEST_SKIPPED;
  29167. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  29168. BIO* bio;
  29169. ASN1_UTCTIME* utc = NULL;
  29170. unsigned char buf[25];
  29171. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  29172. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  29173. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  29174. /* Valid date */
  29175. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29176. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  29177. DYNAMIC_TYPE_ASN1));
  29178. utc->type = ASN_UTC_TIME;
  29179. utc->length = ASN_UTC_TIME_SIZE;
  29180. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  29181. AssertIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  29182. AssertIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  29183. AssertIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  29184. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  29185. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29186. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  29187. XMEMSET(buf, 0, sizeof(buf));
  29188. BIO_free(bio);
  29189. /* Invalid format */
  29190. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29191. utc->type = ASN_UTC_TIME;
  29192. utc->length = ASN_UTC_TIME_SIZE;
  29193. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  29194. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29195. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29196. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29197. /* Invalid type */
  29198. utc->type = ASN_GENERALIZED_TIME;
  29199. utc->length = ASN_GENERALIZED_TIME_SIZE;
  29200. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  29201. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29202. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  29203. BIO_free(bio);
  29204. res = TEST_RES_CHECK(1);
  29205. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  29206. return res;
  29207. }
  29208. static int test_wolfSSL_ASN1_TYPE(void)
  29209. {
  29210. int res = TEST_SKIPPED;
  29211. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  29212. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  29213. WOLFSSL_ASN1_TYPE* t;
  29214. WOLFSSL_ASN1_OBJECT* obj;
  29215. #ifndef NO_ASN_TIME
  29216. WOLFSSL_ASN1_TIME* time;
  29217. #endif
  29218. WOLFSSL_ASN1_STRING* str;
  29219. unsigned char data[] = { 0x00 };
  29220. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  29221. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29222. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  29223. wolfSSL_ASN1_TYPE_free(t);
  29224. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29225. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  29226. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  29227. wolfSSL_ASN1_TYPE_free(t);
  29228. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29229. AssertNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29230. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  29231. wolfSSL_ASN1_TYPE_free(t);
  29232. #ifndef NO_ASN_TIME
  29233. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29234. AssertNotNull(time = wolfSSL_ASN1_TIME_new());
  29235. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  29236. wolfSSL_ASN1_TYPE_free(t);
  29237. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29238. AssertNotNull(time = wolfSSL_ASN1_TIME_new());
  29239. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  29240. wolfSSL_ASN1_TYPE_free(t);
  29241. #endif
  29242. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29243. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29244. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  29245. wolfSSL_ASN1_TYPE_free(t);
  29246. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29247. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29248. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  29249. wolfSSL_ASN1_TYPE_free(t);
  29250. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29251. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29252. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  29253. wolfSSL_ASN1_TYPE_free(t);
  29254. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29255. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29256. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  29257. wolfSSL_ASN1_TYPE_free(t);
  29258. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29259. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29260. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  29261. wolfSSL_ASN1_TYPE_free(t);
  29262. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29263. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29264. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  29265. wolfSSL_ASN1_TYPE_free(t);
  29266. res = TEST_RES_CHECK(1);
  29267. #endif
  29268. return res;
  29269. }
  29270. /* Testing code used in dpp.c in hostap */
  29271. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29272. typedef struct {
  29273. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  29274. * as an OID identifying the curve */
  29275. X509_ALGOR *alg;
  29276. /* Compressed format public key per ANSI X9.63 */
  29277. ASN1_BIT_STRING *pub_key;
  29278. } DPP_BOOTSTRAPPING_KEY;
  29279. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  29280. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  29281. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  29282. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  29283. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  29284. typedef struct {
  29285. ASN1_INTEGER *integer;
  29286. } TEST_ASN1;
  29287. #define WOLFSSL_ASN1_INTEGER_ASN1 2
  29288. ASN1_SEQUENCE(TEST_ASN1) = {
  29289. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  29290. } ASN1_SEQUENCE_END(TEST_ASN1)
  29291. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  29292. #endif
  29293. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  29294. {
  29295. int res = TEST_SKIPPED;
  29296. /* Testing code used in dpp.c in hostap */
  29297. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29298. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  29299. EC_KEY *eckey;
  29300. EVP_PKEY *key;
  29301. size_t len;
  29302. unsigned char *der = NULL;
  29303. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  29304. const unsigned char *in = ecc_clikey_der_256;
  29305. const EC_GROUP *group;
  29306. const EC_POINT *point;
  29307. int nid;
  29308. TEST_ASN1 test_asn1;
  29309. const unsigned char badObjDer[] = { 0x06, 0x00 };
  29310. const unsigned char goodObjDer[] = {
  29311. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  29312. };
  29313. WOLFSSL_ASN1_ITEM emptyTemplate;
  29314. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  29315. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29316. der = NULL;
  29317. AssertIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  29318. AssertIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  29319. AssertIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29320. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29321. (long)sizeof_ecc_clikey_der_256));
  29322. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29323. AssertNotNull(group = EC_KEY_get0_group(eckey));
  29324. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  29325. nid = EC_GROUP_get_curve_name(group);
  29326. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  29327. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  29328. AssertIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29329. #ifdef HAVE_COMP_KEY
  29330. AssertIntGT((len = EC_POINT_point2oct(
  29331. group, point, POINT_CONVERSION_COMPRESSED,
  29332. NULL, 0, NULL)), 0);
  29333. #else
  29334. AssertIntGT((len = EC_POINT_point2oct(
  29335. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29336. NULL, 0, NULL)), 0);
  29337. #endif
  29338. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29339. #ifdef HAVE_COMP_KEY
  29340. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29341. der, len-1, NULL), 0);
  29342. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29343. der, len, NULL), len);
  29344. #else
  29345. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29346. der, len-1, NULL), 0);
  29347. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29348. der, len, NULL), len);
  29349. #endif
  29350. bootstrap->pub_key->data = der;
  29351. bootstrap->pub_key->length = (int)len;
  29352. /* Not actually used */
  29353. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29354. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29355. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29356. der = NULL;
  29357. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29358. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29359. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29360. EVP_PKEY_free(key);
  29361. EC_KEY_free(eckey);
  29362. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29363. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29364. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29365. * a NULL and an empty BIT_STRING. */
  29366. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29367. bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new();
  29368. bootstrap->alg->algorithm->obj = badObjDer;
  29369. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29370. bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new();
  29371. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29372. bootstrap->alg->parameter->value.ptr = NULL;
  29373. bootstrap->pub_key->data = NULL;
  29374. bootstrap->pub_key->length = 0;
  29375. /* Not actually used */
  29376. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29377. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29378. /* Encode with bad OBJECT_ID. */
  29379. der = NULL;
  29380. AssertIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29381. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29382. bootstrap->alg->algorithm->obj = goodObjDer;
  29383. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29384. bootstrap->alg->algorithm->grp = 2;
  29385. der = NULL;
  29386. AssertIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29387. AssertIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29388. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29389. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29390. /* Test error cases. */
  29391. AssertNull(TEST_ASN1_new());
  29392. AssertNull(wolfSSL_ASN1_item_new(NULL));
  29393. TEST_ASN1_free(NULL);
  29394. XMEMSET(&test_asn1, 0, sizeof(TEST_ASN1));
  29395. AssertIntEQ(i2d_TEST_ASN1(&test_asn1, &der), 0);
  29396. res = TEST_RES_CHECK(1);
  29397. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29398. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29399. return res;
  29400. }
  29401. static int test_wolfSSL_lhash(void)
  29402. {
  29403. int res = TEST_SKIPPED;
  29404. #ifdef OPENSSL_ALL
  29405. const char testStr[] = "Like a true nature's child\n"
  29406. "We were born\n"
  29407. "Born to be wild";
  29408. #ifdef NO_SHA
  29409. AssertIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29410. #else
  29411. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29412. #endif
  29413. res = TEST_RES_CHECK(1);
  29414. #endif
  29415. return res;
  29416. }
  29417. static int test_wolfSSL_X509_NAME(void)
  29418. {
  29419. int res = TEST_SKIPPED;
  29420. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29421. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29422. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29423. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29424. defined(OPENSSL_EXTRA))
  29425. X509* x509;
  29426. const unsigned char* c;
  29427. unsigned char buf[4096];
  29428. int bytes;
  29429. XFILE f;
  29430. const X509_NAME* a;
  29431. const X509_NAME* b;
  29432. X509_NAME* d2i_name = NULL;
  29433. int sz;
  29434. unsigned char* tmp;
  29435. char file[] = "./certs/ca-cert.der";
  29436. #ifndef OPENSSL_EXTRA_X509_SMALL
  29437. byte empty[] = { /* CN=empty emailAddress= */
  29438. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29439. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29440. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29441. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29442. 0x01, 0x16, 0x00
  29443. };
  29444. #endif
  29445. #ifndef OPENSSL_EXTRA_X509_SMALL
  29446. /* test compile of deprecated function, returns 0 */
  29447. AssertIntEQ(CRYPTO_thread_id(), 0);
  29448. #endif
  29449. AssertNotNull(a = X509_NAME_new());
  29450. X509_NAME_free((X509_NAME*)a);
  29451. f = XFOPEN(file, "rb");
  29452. AssertTrue(f != XBADFILE);
  29453. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  29454. XFCLOSE(f);
  29455. c = buf;
  29456. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  29457. /* test cmp function */
  29458. AssertNotNull(a = X509_get_issuer_name(x509));
  29459. AssertNotNull(b = X509_get_subject_name(x509));
  29460. #ifndef OPENSSL_EXTRA_X509_SMALL
  29461. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29462. #endif
  29463. tmp = buf;
  29464. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29465. if (sz > 0 && tmp == buf) {
  29466. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29467. __LINE__);
  29468. fprintf(stderr, " Expected pointer to be incremented\n");
  29469. abort();
  29470. }
  29471. #ifndef OPENSSL_EXTRA_X509_SMALL
  29472. tmp = buf;
  29473. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29474. #endif
  29475. /* if output parameter is NULL, should still return required size. */
  29476. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29477. /* retry but with the function creating a buffer */
  29478. tmp = NULL;
  29479. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29480. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29481. #ifdef WOLFSSL_CERT_NAME_ALL
  29482. /* test for givenName and name */
  29483. {
  29484. WOLFSSL_X509_NAME_ENTRY* entry;
  29485. const byte gName[] = "test-given-name";
  29486. const byte name[] = "test-name";
  29487. entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL, NID_givenName,
  29488. ASN_UTF8STRING, gName, sizeof(gName));
  29489. AssertNotNull(entry);
  29490. wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0);
  29491. wolfSSL_X509_NAME_ENTRY_free(entry);
  29492. entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL, NID_name,
  29493. ASN_UTF8STRING, name, sizeof(name));
  29494. AssertNotNull(entry);
  29495. wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0);
  29496. wolfSSL_X509_NAME_ENTRY_free(entry);
  29497. tmp = NULL;
  29498. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29499. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29500. }
  29501. #endif
  29502. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29503. #ifndef OPENSSL_EXTRA_X509_SMALL
  29504. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  29505. #endif
  29506. X509_NAME_free((X509_NAME*)b);
  29507. X509_NAME_free(d2i_name);
  29508. X509_free(x509);
  29509. #ifndef OPENSSL_EXTRA_X509_SMALL
  29510. /* test with an empty domain component */
  29511. tmp = empty;
  29512. sz = sizeof(empty);
  29513. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29514. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29515. /* size of empty emailAddress will be 0 */
  29516. tmp = buf;
  29517. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29518. (char*)tmp, sizeof(buf)), 0);
  29519. /* should contain no organization name */
  29520. tmp = buf;
  29521. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29522. (char*)tmp, sizeof(buf)), -1);
  29523. X509_NAME_free(d2i_name);
  29524. #endif
  29525. res = TEST_RES_CHECK(1);
  29526. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  29527. return res;
  29528. }
  29529. static int test_wolfSSL_X509_NAME_hash(void)
  29530. {
  29531. int res = TEST_SKIPPED;
  29532. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  29533. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29534. BIO* bio;
  29535. X509* x509 = NULL;
  29536. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29537. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29538. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29539. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29540. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29541. X509_free(x509);
  29542. BIO_free(bio);
  29543. res = TEST_RES_CHECK(1);
  29544. #endif
  29545. return res;
  29546. }
  29547. static int test_wolfSSL_X509_NAME_print_ex(void)
  29548. {
  29549. int res = TEST_SKIPPED;
  29550. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29551. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29552. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29553. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29554. !defined(NO_BIO) && !defined(NO_RSA)
  29555. int memSz;
  29556. byte* mem = NULL;
  29557. BIO* bio = NULL;
  29558. BIO* membio = NULL;
  29559. X509* x509 = NULL;
  29560. X509_NAME* name = NULL;
  29561. const char* expNormal = "C=US, CN=wolfssl.com";
  29562. const char* expReverse = "CN=wolfssl.com, C=US";
  29563. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29564. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29565. const char* expRFC5523 =
  29566. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29567. /* Test with real cert (svrCertFile) first */
  29568. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29569. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29570. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29571. AssertNotNull(name = X509_get_subject_name(x509));
  29572. /* Test without flags */
  29573. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29574. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29575. BIO_free(membio);
  29576. /* Test flag: XN_FLAG_RFC2253 */
  29577. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29578. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29579. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29580. BIO_free(membio);
  29581. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29582. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29583. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29584. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29585. BIO_free(membio);
  29586. X509_free(x509);
  29587. BIO_free(bio);
  29588. /* Test normal case without escaped characters */
  29589. {
  29590. /* Create name: "/C=US/CN=wolfssl.com" */
  29591. AssertNotNull(name = X509_NAME_new());
  29592. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29593. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29594. WOLFSSL_SUCCESS);
  29595. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29596. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29597. WOLFSSL_SUCCESS);
  29598. /* Test without flags */
  29599. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29600. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29601. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29602. AssertIntEQ(memSz, XSTRLEN(expNormal));
  29603. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29604. BIO_free(membio);
  29605. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29606. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29607. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29608. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29609. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29610. AssertIntEQ(memSz, XSTRLEN(expReverse));
  29611. BIO_free(membio);
  29612. /* Test flags: XN_FLAG_DN_REV - reversed */
  29613. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29614. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29615. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29616. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29617. AssertIntEQ(memSz, XSTRLEN(expReverse));
  29618. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29619. BIO_free(membio);
  29620. X509_NAME_free(name);
  29621. }
  29622. /* Test RFC2253 characters are escaped with backslashes */
  29623. {
  29624. AssertNotNull(name = X509_NAME_new());
  29625. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29626. /* space at beginning and end, and: ,+"\ */
  29627. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29628. WOLFSSL_SUCCESS);
  29629. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29630. /* # at beginning, and: <>;*/
  29631. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29632. WOLFSSL_SUCCESS);
  29633. /* Test without flags */
  29634. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29635. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29636. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29637. AssertIntEQ(memSz, XSTRLEN(expNotEscaped));
  29638. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29639. XSTRLEN(expNotEscaped)), 0);
  29640. BIO_free(membio);
  29641. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29642. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29643. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29644. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29645. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29646. AssertIntEQ(memSz, XSTRLEN(expRFC5523));
  29647. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29648. BIO_free(membio);
  29649. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29650. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29651. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29652. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29653. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29654. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29655. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29656. XSTRLEN(expNotEscapedRev)), 0);
  29657. BIO_free(membio);
  29658. X509_NAME_free(name);
  29659. }
  29660. res = TEST_RES_CHECK(1);
  29661. #endif
  29662. return res;
  29663. }
  29664. #ifndef NO_BIO
  29665. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29666. {
  29667. int res = TEST_SKIPPED;
  29668. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29669. STACK_OF(X509_INFO) *info_stack;
  29670. X509_INFO *info;
  29671. int len;
  29672. int i;
  29673. const char* files[] = {
  29674. cliCertFile,
  29675. cliKeyFile,
  29676. /* This needs to be the order as svrCertFile contains the
  29677. * intermediate cert as well. */
  29678. svrKeyFile,
  29679. svrCertFile,
  29680. NULL,
  29681. };
  29682. const char** curFile;
  29683. BIO *fileBIO;
  29684. BIO *concatBIO = NULL;
  29685. byte tmp[FOURK_BUF];
  29686. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29687. * to group objects together. */
  29688. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29689. for (curFile = files; *curFile != NULL; curFile++) {
  29690. int fileLen;
  29691. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29692. fileLen = wolfSSL_BIO_get_len(fileBIO);
  29693. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29694. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  29695. fileLen -= len;
  29696. }
  29697. /* Make sure we read the entire file */
  29698. AssertIntEQ(fileLen, 0);
  29699. BIO_free(fileBIO);
  29700. }
  29701. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29702. NULL));
  29703. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  29704. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29705. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  29706. AssertNotNull(info->x509);
  29707. AssertNull(info->crl);
  29708. if (i != 0) {
  29709. AssertNotNull(info->x_pkey);
  29710. AssertIntEQ(X509_check_private_key(info->x509,
  29711. info->x_pkey->dec_pkey), 1);
  29712. }
  29713. else {
  29714. AssertNull(info->x_pkey);
  29715. }
  29716. }
  29717. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29718. BIO_free(concatBIO);
  29719. res = TEST_RES_CHECK(1);
  29720. #endif
  29721. return res;
  29722. }
  29723. #endif
  29724. #ifndef NO_BIO
  29725. static int test_wolfSSL_X509_INFO(void)
  29726. {
  29727. int res = TEST_SKIPPED;
  29728. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29729. STACK_OF(X509_INFO) *info_stack;
  29730. X509_INFO *info;
  29731. BIO *cert;
  29732. int i;
  29733. /* PEM in hex format to avoid null terminator */
  29734. byte data[] = {
  29735. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29736. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29737. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29738. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29739. 0x2d, 0x2d, 0x2d
  29740. };
  29741. /* PEM in hex format to avoid null terminator */
  29742. byte data2[] = {
  29743. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29744. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29745. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29746. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29747. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29748. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29749. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29750. };
  29751. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29752. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29753. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29754. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  29755. AssertNotNull(info->x509);
  29756. AssertNull(info->crl);
  29757. AssertNull(info->x_pkey);
  29758. }
  29759. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29760. BIO_free(cert);
  29761. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29762. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29763. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29764. BIO_free(cert);
  29765. /* This case should fail due to invalid input. */
  29766. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  29767. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29768. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29769. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29770. BIO_free(cert);
  29771. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  29772. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29773. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29774. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29775. BIO_free(cert);
  29776. res = TEST_RES_CHECK(1);
  29777. #endif
  29778. return res;
  29779. }
  29780. #endif
  29781. static int test_wolfSSL_X509_subject_name_hash(void)
  29782. {
  29783. int res = TEST_SKIPPED;
  29784. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29785. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29786. X509* x509;
  29787. X509_NAME* subjectName = NULL;
  29788. unsigned long ret1 = 0;
  29789. unsigned long ret2 = 0;
  29790. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29791. SSL_FILETYPE_PEM));
  29792. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29793. /* These two
  29794. * - X509_subject_name_hash(x509)
  29795. * - X509_NAME_hash(X509_get_subject_name(x509))
  29796. * should give the same hash, if !defined(NO_SHA) is true. */
  29797. ret1 = X509_subject_name_hash(x509);
  29798. AssertIntNE(ret1, 0);
  29799. #if !defined(NO_SHA)
  29800. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29801. AssertIntNE(ret2, 0);
  29802. AssertIntEQ(ret1, ret2);
  29803. #else
  29804. (void) ret2;
  29805. #endif
  29806. X509_free(x509);
  29807. res = TEST_RES_CHECK(1);
  29808. #endif
  29809. return res;
  29810. }
  29811. static int test_wolfSSL_X509_issuer_name_hash(void)
  29812. {
  29813. int res = TEST_SKIPPED;
  29814. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29815. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29816. X509* x509;
  29817. X509_NAME* issuertName = NULL;
  29818. unsigned long ret1 = 0;
  29819. unsigned long ret2 = 0;
  29820. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29821. SSL_FILETYPE_PEM));
  29822. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29823. /* These two
  29824. * - X509_issuer_name_hash(x509)
  29825. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29826. * should give the same hash, if !defined(NO_SHA) is true. */
  29827. ret1 = X509_issuer_name_hash(x509);
  29828. AssertIntNE(ret1, 0);
  29829. #if !defined(NO_SHA)
  29830. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29831. AssertIntNE(ret2, 0);
  29832. AssertIntEQ(ret1, ret2);
  29833. #else
  29834. (void) ret2;
  29835. #endif
  29836. X509_free(x509);
  29837. res = TEST_RES_CHECK(1);
  29838. #endif
  29839. return res;
  29840. }
  29841. static int test_wolfSSL_X509_check_host(void)
  29842. {
  29843. int res = TEST_SKIPPED;
  29844. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29845. && !defined(NO_SHA) && !defined(NO_RSA)
  29846. X509* x509;
  29847. const char altName[] = "example.com";
  29848. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29849. SSL_FILETYPE_PEM));
  29850. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29851. WOLFSSL_SUCCESS);
  29852. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29853. WOLFSSL_FAILURE);
  29854. X509_free(x509);
  29855. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29856. WOLFSSL_FAILURE);
  29857. res = TEST_RES_CHECK(1);
  29858. #endif
  29859. return res;
  29860. }
  29861. static int test_wolfSSL_X509_check_email(void)
  29862. {
  29863. int res = TEST_SKIPPED;
  29864. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29865. X509* x509;
  29866. const char goodEmail[] = "info@wolfssl.com";
  29867. const char badEmail[] = "disinfo@wolfssl.com";
  29868. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29869. SSL_FILETYPE_PEM));
  29870. /* Should fail on non-matching email address */
  29871. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29872. WOLFSSL_FAILURE);
  29873. /* Should succeed on matching email address */
  29874. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29875. WOLFSSL_SUCCESS);
  29876. /* Should compute length internally when not provided */
  29877. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29878. WOLFSSL_SUCCESS);
  29879. /* Should fail when email address is NULL */
  29880. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29881. WOLFSSL_FAILURE);
  29882. X509_free(x509);
  29883. /* Should fail when x509 is NULL */
  29884. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29885. WOLFSSL_FAILURE);
  29886. res = TEST_RES_CHECK(1);
  29887. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29888. return res;
  29889. }
  29890. static int test_wolfSSL_DES(void)
  29891. {
  29892. int res = TEST_SKIPPED;
  29893. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  29894. const_DES_cblock myDes;
  29895. DES_cblock iv;
  29896. DES_key_schedule key;
  29897. word32 i;
  29898. DES_LONG dl;
  29899. unsigned char msg[] = "hello wolfssl";
  29900. DES_check_key(1);
  29901. DES_set_key(&myDes, &key);
  29902. /* check, check of odd parity */
  29903. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  29904. XMEMSET(key, 5, sizeof(DES_key_schedule));
  29905. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  29906. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  29907. /* set odd parity for success case */
  29908. DES_set_odd_parity(&myDes);
  29909. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  29910. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  29911. myDes[3]);
  29912. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  29913. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  29914. AssertIntEQ(key[i], myDes[i]);
  29915. }
  29916. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  29917. /* check weak key */
  29918. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  29919. XMEMSET(key, 5, sizeof(DES_key_schedule));
  29920. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  29921. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  29922. /* now do unchecked copy of a weak key over */
  29923. DES_set_key_unchecked(&myDes, &key);
  29924. /* compare arrays, should be the same */
  29925. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  29926. AssertIntEQ(key[i], myDes[i]);
  29927. }
  29928. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  29929. /* check DES_key_sched API */
  29930. XMEMSET(key, 1, sizeof(DES_key_schedule));
  29931. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  29932. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  29933. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  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. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  29939. * DES_cbc_encrypt on the input */
  29940. XMEMSET(iv, 0, sizeof(DES_cblock));
  29941. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  29942. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  29943. AssertIntEQ(dl, 480052723);
  29944. res = TEST_RES_CHECK(1);
  29945. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  29946. return res;
  29947. }
  29948. static int test_wc_PemToDer(void)
  29949. {
  29950. int res = TEST_SKIPPED;
  29951. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29952. int ret;
  29953. DerBuffer* pDer = NULL;
  29954. const char* ca_cert = "./certs/server-cert.pem";
  29955. byte* cert_buf = NULL;
  29956. size_t cert_sz = 0;
  29957. int eccKey = 0;
  29958. EncryptedInfo info;
  29959. XMEMSET(&info, 0, sizeof(info));
  29960. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  29961. if (ret == 0) {
  29962. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  29963. &pDer, NULL, &info, &eccKey);
  29964. AssertIntEQ(ret, 0);
  29965. wc_FreeDer(&pDer);
  29966. }
  29967. if (cert_buf)
  29968. free(cert_buf);
  29969. #ifdef HAVE_ECC
  29970. {
  29971. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29972. byte key_buf[256] = {0};
  29973. /* Test fail of loading a key with cert type */
  29974. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29975. key_buf[0] = '\n';
  29976. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  29977. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29978. &pDer, NULL, &info, &eccKey)), 0);
  29979. #ifdef OPENSSL_EXTRA
  29980. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29981. &pDer, NULL, &info, &eccKey)), 0);
  29982. #endif
  29983. wc_FreeDer(&pDer);
  29984. if (cert_buf)
  29985. free(cert_buf);
  29986. }
  29987. #endif
  29988. res = TEST_RES_CHECK(1);
  29989. #endif
  29990. return res;
  29991. }
  29992. static int test_wc_AllocDer(void)
  29993. {
  29994. int res = TEST_SKIPPED;
  29995. #if !defined(NO_CERTS)
  29996. int ret;
  29997. DerBuffer* pDer = NULL;
  29998. word32 testSize = 1024;
  29999. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  30000. AssertIntEQ(ret, 0);
  30001. AssertNotNull(pDer);
  30002. wc_FreeDer(&pDer);
  30003. res = TEST_RES_CHECK(1);
  30004. #endif
  30005. return res;
  30006. }
  30007. static int test_wc_CertPemToDer(void)
  30008. {
  30009. int res = TEST_SKIPPED;
  30010. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  30011. int ret;
  30012. const char* ca_cert = "./certs/ca-cert.pem";
  30013. byte* cert_buf = NULL;
  30014. size_t cert_sz = 0, cert_dersz = 0;
  30015. byte* cert_der = NULL;
  30016. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  30017. if (ret == 0) {
  30018. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30019. cert_der = (byte*)malloc(cert_dersz);
  30020. if (cert_der) {
  30021. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  30022. cert_der, (int)cert_dersz, CERT_TYPE);
  30023. AssertIntGE(ret, 0);
  30024. }
  30025. }
  30026. if (cert_der)
  30027. free(cert_der);
  30028. if (cert_buf)
  30029. free(cert_buf);
  30030. res = TEST_RES_CHECK(1);
  30031. #endif
  30032. return res;
  30033. }
  30034. static int test_wc_KeyPemToDer(void)
  30035. {
  30036. int res = TEST_SKIPPED;
  30037. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30038. int ret;
  30039. const byte cert_buf[] = \
  30040. "-----BEGIN PRIVATE KEY-----\n"
  30041. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  30042. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  30043. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  30044. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  30045. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  30046. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  30047. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  30048. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  30049. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  30050. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  30051. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  30052. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  30053. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  30054. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  30055. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  30056. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  30057. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  30058. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  30059. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  30060. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  30061. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  30062. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  30063. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  30064. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  30065. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  30066. "AQue5o6oVfhgLiJzMdo/77gw\n"
  30067. "-----END PRIVATE KEY-----\n";
  30068. const int cert_sz = sizeof(cert_buf);
  30069. const char cert_pw[] = "password";
  30070. int cert_dersz = 0;
  30071. byte* cert_der = NULL;
  30072. /* Bad arg: Cert buffer is NULL */
  30073. ret = wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, "");
  30074. AssertIntEQ(ret, BAD_FUNC_ARG);
  30075. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  30076. ret = wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, "");
  30077. AssertIntEQ(ret, BAD_FUNC_ARG);
  30078. /* Test normal operation */
  30079. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30080. cert_der = (byte*)malloc(cert_dersz);
  30081. AssertNotNull(cert_der);
  30082. if (cert_der) {
  30083. ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz, cert_pw);
  30084. AssertIntGE(ret, 0);
  30085. AssertIntLE(ret, cert_sz);
  30086. free(cert_der);
  30087. cert_der = NULL;
  30088. ret = 0;
  30089. }
  30090. if (ret == 0) {
  30091. /* Test NULL for DER buffer to return needed DER buffer size */
  30092. ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, "");
  30093. AssertIntGT(ret, 0);
  30094. AssertIntLE(ret, cert_sz);
  30095. cert_dersz = ret;
  30096. cert_der = (byte*)malloc(cert_dersz);
  30097. AssertNotNull(cert_der);
  30098. if (cert_der) {
  30099. ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz, cert_pw);
  30100. AssertIntGE(ret, 0);
  30101. AssertIntLE(ret, cert_sz);
  30102. free(cert_der);
  30103. cert_der = NULL;
  30104. }
  30105. }
  30106. res = TEST_RES_CHECK(1);
  30107. #endif
  30108. return res;
  30109. }
  30110. static int test_wc_PubKeyPemToDer(void)
  30111. {
  30112. int res = TEST_SKIPPED;
  30113. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  30114. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30115. int ret;
  30116. const char* key = "./certs/ecc-client-keyPub.pem";
  30117. byte* cert_buf = NULL;
  30118. size_t cert_sz = 0, cert_dersz = 0;
  30119. byte* cert_der = NULL;
  30120. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  30121. cert_der, (int)cert_dersz);
  30122. AssertIntGE(ret, BAD_FUNC_ARG);
  30123. ret = load_file(key, &cert_buf, &cert_sz);
  30124. if (ret == 0) {
  30125. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30126. cert_der = (byte*)malloc(cert_dersz);
  30127. AssertNotNull(cert_der);
  30128. if (cert_der) {
  30129. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30130. (int)cert_dersz);
  30131. AssertIntGE(ret, 0);
  30132. free(cert_der);
  30133. cert_der = NULL;
  30134. ret = 0;
  30135. }
  30136. }
  30137. if (ret == 0) {
  30138. /* Test NULL for DER buffer to return needed DER buffer size */
  30139. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0);
  30140. AssertIntGT(ret, 0);
  30141. AssertIntLE(ret, cert_sz);
  30142. cert_dersz = ret;
  30143. cert_der = (byte*)malloc(cert_dersz);
  30144. AssertNotNull(cert_der);
  30145. if (cert_der) {
  30146. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30147. (int)cert_dersz);
  30148. AssertIntGE(ret, 0);
  30149. free(cert_der);
  30150. cert_der = NULL;
  30151. }
  30152. }
  30153. if (cert_buf) {
  30154. free(cert_buf);
  30155. }
  30156. res = TEST_RES_CHECK(1);
  30157. #endif
  30158. return res;
  30159. }
  30160. static int test_wc_PemPubKeyToDer(void)
  30161. {
  30162. int res = TEST_SKIPPED;
  30163. #if !defined(NO_FILESYSTEM) && \
  30164. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30165. int ret;
  30166. const char* key = "./certs/ecc-client-keyPub.pem";
  30167. size_t cert_dersz = 1024;
  30168. byte* cert_der = (byte*)malloc(cert_dersz);
  30169. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  30170. AssertIntGE(ret, BAD_FUNC_ARG);
  30171. if (cert_der) {
  30172. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  30173. AssertIntGE(ret, 0);
  30174. free(cert_der);
  30175. }
  30176. res = TEST_RES_CHECK(1);
  30177. #endif
  30178. return res;
  30179. }
  30180. static int test_wc_GetPubKeyDerFromCert(void)
  30181. {
  30182. int res = TEST_SKIPPED;
  30183. #if !defined(NO_RSA) || defined(HAVE_ECC)
  30184. int ret;
  30185. word32 idx = 0;
  30186. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30187. word32 keyDerSz = (word32)sizeof(keyDer);
  30188. DecodedCert decoded;
  30189. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30190. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  30191. word32 certBufSz = sizeof(certBuf);
  30192. #endif
  30193. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  30194. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30195. XFILE fp;
  30196. #endif
  30197. #ifndef NO_RSA
  30198. RsaKey rsaKey;
  30199. #if defined(USE_CERT_BUFFERS_2048)
  30200. byte* rsaCertDer = (byte*)client_cert_der_2048;
  30201. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  30202. #elif defined(USE_CERT_BUFFERS_1024)
  30203. byte* rsaCertDer = (byte*)client_cert_der_1024;
  30204. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  30205. #else
  30206. unsigned char rsaCertDer[TWOK_BUF];
  30207. word32 rsaCertDerSz;
  30208. #endif
  30209. #endif
  30210. #ifdef HAVE_ECC
  30211. ecc_key eccKey;
  30212. #if defined(USE_CERT_BUFFERS_256)
  30213. byte* eccCert = (byte*)cliecc_cert_der_256;
  30214. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  30215. #else
  30216. unsigned char eccCert[ONEK_BUF];
  30217. word32 eccCertSz;
  30218. XFILE fp2;
  30219. #endif
  30220. #endif
  30221. #ifndef NO_RSA
  30222. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  30223. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  30224. AssertTrue((fp != XBADFILE));
  30225. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  30226. XFCLOSE(fp);
  30227. #endif
  30228. /* good test case - RSA DER cert */
  30229. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  30230. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  30231. AssertIntEQ(ret, 0);
  30232. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30233. AssertIntEQ(ret, 0);
  30234. AssertIntGT(keyDerSz, 0);
  30235. /* sanity check, verify we can import DER public key */
  30236. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30237. AssertIntEQ(ret, 0);
  30238. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  30239. AssertIntEQ(ret, 0);
  30240. wc_FreeRsaKey(&rsaKey);
  30241. /* test LENGTH_ONLY_E case */
  30242. keyDerSz = 0;
  30243. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  30244. AssertIntEQ(ret, LENGTH_ONLY_E);
  30245. AssertIntGT(keyDerSz, 0);
  30246. /* bad args: DecodedCert NULL */
  30247. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  30248. AssertIntEQ(ret, BAD_FUNC_ARG);
  30249. /* bad args: output key buff size */
  30250. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  30251. AssertIntEQ(ret, BAD_FUNC_ARG);
  30252. /* bad args: zero size output key buffer */
  30253. keyDerSz = 0;
  30254. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30255. AssertIntEQ(ret, BAD_FUNC_ARG);
  30256. wc_FreeDecodedCert(&decoded);
  30257. /* Certificate Request Tests */
  30258. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30259. {
  30260. XMEMSET(certBuf, 0, sizeof(certBuf));
  30261. fp = XFOPEN("./certs/csr.signed.der", "rb");
  30262. AssertTrue((fp != XBADFILE));
  30263. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  30264. XFCLOSE(fp);
  30265. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  30266. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
  30267. AssertIntEQ(ret, 0);
  30268. /* good test case - RSA DER certificate request */
  30269. keyDerSz = sizeof(keyDer);
  30270. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30271. AssertIntEQ(ret, 0);
  30272. AssertIntGT(keyDerSz, 0);
  30273. /* sanity check, verify we can import DER public key */
  30274. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30275. AssertIntEQ(ret, 0);
  30276. idx = 0;
  30277. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  30278. AssertIntEQ(ret, 0);
  30279. wc_FreeRsaKey(&rsaKey);
  30280. wc_FreeDecodedCert(&decoded);
  30281. }
  30282. #endif /* WOLFSSL_CERT_REQ */
  30283. #endif /* NO_RSA */
  30284. #ifdef HAVE_ECC
  30285. #ifndef USE_CERT_BUFFERS_256
  30286. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  30287. AssertTrue((fp2 != XBADFILE));
  30288. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  30289. XFCLOSE(fp2);
  30290. #endif
  30291. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  30292. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  30293. AssertIntEQ(ret, 0);
  30294. /* good test case - ECC */
  30295. XMEMSET(keyDer, 0, sizeof(keyDer));
  30296. keyDerSz = sizeof(keyDer);
  30297. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30298. AssertIntEQ(ret, 0);
  30299. AssertIntGT(keyDerSz, 0);
  30300. /* sanity check, verify we can import DER public key */
  30301. ret = wc_ecc_init(&eccKey);
  30302. AssertIntEQ(ret, 0);
  30303. idx = 0; /* reset idx to 0, used above in RSA case */
  30304. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  30305. AssertIntEQ(ret, 0);
  30306. wc_ecc_free(&eccKey);
  30307. /* test LENGTH_ONLY_E case */
  30308. keyDerSz = 0;
  30309. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  30310. AssertIntEQ(ret, LENGTH_ONLY_E);
  30311. AssertIntGT(keyDerSz, 0);
  30312. wc_FreeDecodedCert(&decoded);
  30313. #endif
  30314. res = TEST_RES_CHECK(1);
  30315. #endif /* !NO_RSA || HAVE_ECC */
  30316. return res;
  30317. }
  30318. static int test_wc_CheckCertSigPubKey(void)
  30319. {
  30320. int res = TEST_SKIPPED;
  30321. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30322. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  30323. int ret;
  30324. const char* ca_cert = "./certs/ca-cert.pem";
  30325. byte* cert_buf = NULL;
  30326. size_t cert_sz = 0;
  30327. byte* cert_der = NULL;
  30328. word32 cert_dersz = 0;
  30329. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30330. word32 keyDerSz = (word32)sizeof(keyDer);
  30331. DecodedCert decoded;
  30332. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  30333. if (ret == 0) {
  30334. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  30335. cert_der = (byte*)malloc(cert_dersz);
  30336. if (cert_der) {
  30337. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  30338. cert_der, (int)cert_dersz, CERT_TYPE);
  30339. AssertIntGE(ret, 0);
  30340. }
  30341. }
  30342. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  30343. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  30344. AssertIntEQ(ret, 0);
  30345. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30346. AssertIntEQ(ret, 0);
  30347. AssertIntGT(keyDerSz, 0);
  30348. /* Good test case. */
  30349. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  30350. RSAk);
  30351. AssertIntEQ(ret, 0);
  30352. /* No certificate. */
  30353. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  30354. ECDSAk);
  30355. AssertIntEQ(ret, BAD_FUNC_ARG);
  30356. /* Bad cert size. */
  30357. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  30358. RSAk);
  30359. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30360. /* No public key. */
  30361. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  30362. RSAk);
  30363. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  30364. /* Bad public key size. */
  30365. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30366. RSAk);
  30367. AssertIntEQ(ret, BAD_FUNC_ARG);
  30368. /* Wrong aglo. */
  30369. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  30370. ECDSAk);
  30371. AssertIntEQ(ret, ASN_PARSE_E);
  30372. wc_FreeDecodedCert(&decoded);
  30373. if (cert_der)
  30374. free(cert_der);
  30375. if (cert_buf)
  30376. free(cert_buf);
  30377. res = TEST_RES_CHECK(1);
  30378. #endif
  30379. return res;
  30380. }
  30381. static int test_wolfSSL_certs(void)
  30382. {
  30383. int res = TEST_SKIPPED;
  30384. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30385. !defined(NO_RSA)
  30386. X509* x509ext;
  30387. #ifdef OPENSSL_ALL
  30388. X509* x509;
  30389. WOLFSSL_X509_EXTENSION* ext;
  30390. ASN1_OBJECT* obj;
  30391. #endif
  30392. WOLFSSL* ssl;
  30393. WOLFSSL_CTX* ctx;
  30394. STACK_OF(ASN1_OBJECT)* sk;
  30395. ASN1_STRING* asn1_str;
  30396. AUTHORITY_KEYID* akey;
  30397. BASIC_CONSTRAINTS* bc;
  30398. int crit;
  30399. #ifndef NO_WOLFSSL_SERVER
  30400. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30401. #else
  30402. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30403. #endif
  30404. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30405. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30406. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30407. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30408. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30409. #endif
  30410. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30411. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30412. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30413. #endif
  30414. AssertNotNull(ssl = SSL_new(ctx));
  30415. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30416. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30417. #endif
  30418. #ifdef HAVE_PK_CALLBACKS
  30419. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30420. #endif /* HAVE_PK_CALLBACKS */
  30421. /* create and use x509 */
  30422. #ifdef OPENSSL_ALL
  30423. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  30424. AssertNotNull(x509);
  30425. #endif
  30426. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  30427. AssertNotNull(x509ext);
  30428. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30429. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30430. /* with loading in a new cert the check on private key should now fail */
  30431. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30432. #endif
  30433. #if defined(USE_CERT_BUFFERS_2048)
  30434. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  30435. (unsigned char*)server_cert_der_2048,
  30436. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30437. #endif
  30438. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30439. /************* Get Digest of Certificate ******************/
  30440. {
  30441. byte digest[64]; /* max digest size */
  30442. word32 digestSz;
  30443. XMEMSET(digest, 0, sizeof(digest));
  30444. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30445. WOLFSSL_SUCCESS);
  30446. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30447. WOLFSSL_SUCCESS);
  30448. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30449. WOLFSSL_FAILURE);
  30450. }
  30451. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30452. /* test and checkout X509 extensions */
  30453. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  30454. &crit, NULL);
  30455. AssertNotNull(bc);
  30456. AssertIntEQ(crit, 0);
  30457. #ifdef OPENSSL_ALL
  30458. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  30459. AssertNotNull(ext);
  30460. X509_EXTENSION_free(ext);
  30461. AssertNotNull(ext = X509_EXTENSION_new());
  30462. X509_EXTENSION_set_critical(ext, 1);
  30463. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30464. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30465. ASN1_OBJECT_free(obj);
  30466. X509_EXTENSION_free(ext);
  30467. AssertNotNull(ext = X509_EXTENSION_new());
  30468. X509_EXTENSION_set_critical(ext, 0);
  30469. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30470. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30471. NULL);
  30472. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30473. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30474. * and X509_get_ext_d2i has created new */
  30475. X509_EXTENSION_free(ext);
  30476. #endif
  30477. BASIC_CONSTRAINTS_free(bc);
  30478. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  30479. AssertNotNull(asn1_str);
  30480. AssertIntEQ(crit, 1);
  30481. AssertIntEQ(asn1_str->type, NID_key_usage);
  30482. #ifdef OPENSSL_ALL
  30483. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  30484. AssertNotNull(ext);
  30485. X509_EXTENSION_free(ext);
  30486. #endif
  30487. ASN1_STRING_free(asn1_str);
  30488. #ifdef OPENSSL_ALL
  30489. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  30490. &crit, NULL);
  30491. AssertNotNull(sk);
  30492. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  30493. AssertNotNull(ext);
  30494. X509_EXTENSION_free(ext);
  30495. sk_ASN1_OBJECT_pop_free(sk, NULL);
  30496. #else
  30497. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30498. &crit, NULL);
  30499. AssertNull(sk);
  30500. #endif
  30501. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30502. NID_authority_key_identifier, &crit, NULL);
  30503. AssertNotNull(akey);
  30504. #ifdef OPENSSL_ALL
  30505. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  30506. AssertNotNull(ext);
  30507. X509_EXTENSION_free(ext);
  30508. #endif
  30509. wolfSSL_AUTHORITY_KEYID_free(akey);
  30510. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30511. NID_private_key_usage_period, &crit, NULL);
  30512. /* AssertNotNull(sk); NID not yet supported */
  30513. AssertIntEQ(crit, -1);
  30514. sk_ASN1_OBJECT_free(sk);
  30515. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  30516. &crit, NULL);
  30517. {
  30518. int i;
  30519. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30520. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30521. AssertIntEQ(gen->type, GEN_DNS);
  30522. AssertIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30523. }
  30524. }
  30525. /* AssertNotNull(sk); no alt names set */
  30526. sk_GENERAL_NAME_free(sk);
  30527. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  30528. &crit, NULL);
  30529. /* AssertNotNull(sk); NID not yet supported */
  30530. AssertIntEQ(crit, -1);
  30531. sk_ASN1_OBJECT_free(sk);
  30532. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  30533. NULL);
  30534. /* AssertNotNull(sk); no auth info set */
  30535. sk_ASN1_OBJECT_free(sk);
  30536. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  30537. &crit, NULL);
  30538. /* AssertNotNull(sk); NID not yet supported */
  30539. AssertIntEQ(crit, -1);
  30540. sk_ASN1_OBJECT_free(sk);
  30541. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  30542. &crit, NULL);
  30543. /* AssertNotNull(sk); NID not yet supported */
  30544. AssertIntEQ(crit, -1);
  30545. sk_ASN1_OBJECT_free(sk);
  30546. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30547. NID_certificate_policies, &crit, NULL);
  30548. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  30549. AssertNull(sk);
  30550. #else
  30551. /* AssertNotNull(sk); no cert policy set */
  30552. #endif
  30553. sk_ASN1_OBJECT_free(sk);
  30554. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  30555. &crit, NULL);
  30556. /* AssertNotNull(sk); NID not yet supported */
  30557. AssertIntEQ(crit, -1);
  30558. sk_ASN1_OBJECT_free(sk);
  30559. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  30560. &crit, NULL);
  30561. /* AssertNotNull(sk); NID not yet supported */
  30562. AssertIntEQ(crit, -1);
  30563. sk_ASN1_OBJECT_free(sk);
  30564. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  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_tlsfeature, &crit,
  30570. NULL);
  30571. /* AssertNotNull(sk); NID not yet supported */
  30572. AssertIntEQ(crit, -1);
  30573. sk_ASN1_OBJECT_free(sk);
  30574. /* test invalid cases */
  30575. crit = 0;
  30576. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  30577. AssertNull(sk);
  30578. AssertIntEQ(crit, -1);
  30579. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  30580. NULL, NULL);
  30581. AssertNull(sk);
  30582. AssertIntEQ(SSL_get_hit(ssl), 0);
  30583. #ifdef OPENSSL_ALL
  30584. X509_free(x509);
  30585. #endif
  30586. X509_free(x509ext);
  30587. SSL_free(ssl);
  30588. SSL_CTX_free(ctx);
  30589. res = TEST_RES_CHECK(1);
  30590. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30591. return res;
  30592. }
  30593. static int test_wolfSSL_X509_check_private_key(void)
  30594. {
  30595. int res = TEST_SKIPPED;
  30596. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30597. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30598. X509* x509;
  30599. EVP_PKEY* pkey = NULL;
  30600. const byte* key;
  30601. /* Check with correct key */
  30602. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30603. SSL_FILETYPE_PEM)));
  30604. key = client_key_der_2048;
  30605. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30606. &key, (long)sizeof_client_key_der_2048));
  30607. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  30608. EVP_PKEY_free(pkey);
  30609. pkey = NULL;
  30610. /* Check with wrong key */
  30611. key = server_key_der_2048;
  30612. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30613. &key, (long)sizeof_server_key_der_2048));
  30614. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  30615. /* test for incorrect parameter */
  30616. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  30617. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  30618. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  30619. EVP_PKEY_free(pkey);
  30620. X509_free(x509);
  30621. res = TEST_RES_CHECK(1);
  30622. #endif
  30623. return res;
  30624. }
  30625. static int test_wolfSSL_private_keys(void)
  30626. {
  30627. int res = TEST_SKIPPED;
  30628. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30629. !defined(NO_FILESYSTEM)
  30630. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30631. WOLFSSL* ssl;
  30632. WOLFSSL_CTX* ctx;
  30633. EVP_PKEY* pkey = NULL;
  30634. OpenSSL_add_all_digests();
  30635. OpenSSL_add_all_algorithms();
  30636. #ifndef NO_RSA
  30637. #ifndef NO_WOLFSSL_SERVER
  30638. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30639. #else
  30640. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30641. #endif
  30642. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30643. /* Have to load a cert before you can check the private key against that
  30644. * certificates public key! */
  30645. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30646. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30647. #endif
  30648. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30649. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30650. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30651. #endif
  30652. AssertNotNull(ssl = SSL_new(ctx));
  30653. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30654. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30655. #endif
  30656. #ifdef USE_CERT_BUFFERS_2048
  30657. {
  30658. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30659. unsigned char buf[FOURK_BUF];
  30660. word32 bufSz;
  30661. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30662. (unsigned char*)client_key_der_2048,
  30663. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30664. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30665. /* Should mismatch now that a different private key loaded */
  30666. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30667. #endif
  30668. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30669. (unsigned char*)server_key,
  30670. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30671. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30672. /* After loading back in DER format of original key, should match */
  30673. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30674. #endif
  30675. /* test loading private key to the WOLFSSL_CTX */
  30676. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30677. (unsigned char*)client_key_der_2048,
  30678. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30679. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30680. /* Should mismatch now that a different private key loaded */
  30681. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30682. #endif
  30683. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30684. (unsigned char*)server_key,
  30685. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30686. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30687. /* After loading back in DER format of original key, should match */
  30688. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30689. #endif
  30690. /* pkey not set yet, expecting to fail */
  30691. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30692. /* set PKEY and test again */
  30693. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30694. &server_key, (long)sizeof_server_key_der_2048));
  30695. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30696. /* reuse PKEY structure and test
  30697. * this should be checked with a memory management sanity checker */
  30698. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  30699. server_key = (const unsigned char*)server_key_der_2048;
  30700. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30701. &server_key, (long)sizeof_server_key_der_2048));
  30702. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30703. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30704. bufSz = FOURK_BUF;
  30705. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  30706. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30707. RSAk, NULL, 0)), 0);
  30708. server_key = (const unsigned char*)buf;
  30709. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30710. (long)bufSz));
  30711. }
  30712. #endif
  30713. EVP_PKEY_free(pkey);
  30714. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30715. SSL_CTX_free(ctx);
  30716. #endif /* end of RSA private key match tests */
  30717. #ifdef HAVE_ECC
  30718. #ifndef NO_WOLFSSL_SERVER
  30719. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30720. #else
  30721. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30722. #endif
  30723. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30724. WOLFSSL_FILETYPE_PEM));
  30725. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30726. WOLFSSL_FILETYPE_PEM));
  30727. AssertNotNull(ssl = SSL_new(ctx));
  30728. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30729. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30730. #endif
  30731. SSL_free(ssl);
  30732. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30733. WOLFSSL_FILETYPE_PEM));
  30734. AssertNotNull(ssl = SSL_new(ctx));
  30735. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30736. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30737. #endif
  30738. SSL_free(ssl);
  30739. SSL_CTX_free(ctx);
  30740. #endif /* end of ECC private key match tests */
  30741. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30742. #ifndef NO_WOLFSSL_SERVER
  30743. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30744. #else
  30745. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30746. #endif
  30747. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30748. WOLFSSL_FILETYPE_PEM));
  30749. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30750. WOLFSSL_FILETYPE_PEM));
  30751. AssertNotNull(ssl = SSL_new(ctx));
  30752. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30753. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30754. #endif
  30755. SSL_free(ssl);
  30756. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30757. WOLFSSL_FILETYPE_PEM));
  30758. AssertNotNull(ssl = SSL_new(ctx));
  30759. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30760. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30761. #endif
  30762. SSL_free(ssl);
  30763. SSL_CTX_free(ctx);
  30764. #endif /* end of Ed25519 private key match tests */
  30765. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30766. #ifndef NO_WOLFSSL_SERVER
  30767. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30768. #else
  30769. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30770. #endif
  30771. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30772. WOLFSSL_FILETYPE_PEM));
  30773. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30774. WOLFSSL_FILETYPE_PEM));
  30775. AssertNotNull(ssl = SSL_new(ctx));
  30776. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30777. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30778. #endif
  30779. SSL_free(ssl);
  30780. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30781. WOLFSSL_FILETYPE_PEM));
  30782. AssertNotNull(ssl = SSL_new(ctx));
  30783. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30784. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30785. #endif
  30786. SSL_free(ssl);
  30787. SSL_CTX_free(ctx);
  30788. #endif /* end of Ed448 private key match tests */
  30789. EVP_cleanup();
  30790. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30791. CONF_modules_free();
  30792. ENGINE_cleanup();
  30793. CONF_modules_unload();
  30794. (void)ssl;
  30795. (void)ctx;
  30796. (void)pkey;
  30797. res = TEST_RES_CHECK(1);
  30798. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30799. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30800. return res;
  30801. }
  30802. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30803. {
  30804. int res = TEST_SKIPPED;
  30805. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30806. && !defined(NO_FILESYSTEM)
  30807. XFILE file;
  30808. const char* fname = "./certs/server-key.pem";
  30809. EVP_PKEY* pkey;
  30810. RSA* rsa;
  30811. WOLFSSL_EVP_PKEY_CTX* ctx;
  30812. unsigned char* sig;
  30813. size_t sigLen;
  30814. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30815. size_t tbsLen = sizeof(tbs);
  30816. /* Check error case. */
  30817. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30818. /* Read in an RSA key. */
  30819. file = XFOPEN(fname, "rb");
  30820. AssertTrue(file != XBADFILE);
  30821. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30822. XFCLOSE(file);
  30823. /* Make sure the key is usable by signing some data with it. */
  30824. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30825. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  30826. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30827. DYNAMIC_TYPE_TMP_BUFFER));
  30828. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30829. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30830. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30831. WOLFSSL_SUCCESS);
  30832. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30833. EVP_PKEY_CTX_free(ctx);
  30834. EVP_PKEY_free(pkey);
  30835. res = TEST_RES_CHECK(1);
  30836. #endif
  30837. return res;
  30838. }
  30839. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30840. {
  30841. int res = TEST_SKIPPED;
  30842. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30843. && !defined(NO_FILESYSTEM)
  30844. XFILE file;
  30845. const char* fname = "./certs/client-keyPub.pem";
  30846. EVP_PKEY* pkey;
  30847. /* Check error case. */
  30848. AssertNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30849. /* Read in an RSA key. */
  30850. file = XFOPEN(fname, "rb");
  30851. AssertTrue(file != XBADFILE);
  30852. AssertNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30853. EVP_PKEY_free(pkey);
  30854. XFCLOSE(file);
  30855. res = TEST_RES_CHECK(1);
  30856. #endif
  30857. return res;
  30858. }
  30859. static int test_wolfSSL_PEM_PrivateKey(void)
  30860. {
  30861. int res = TEST_SKIPPED;
  30862. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30863. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  30864. #ifndef NO_BIO
  30865. BIO* bio = NULL;
  30866. #endif
  30867. EVP_PKEY* pkey = NULL;
  30868. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30869. #ifndef NO_BIO
  30870. /* test creating new EVP_PKEY with bad arg */
  30871. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  30872. /* test loading RSA key using BIO */
  30873. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30874. {
  30875. XFILE file;
  30876. const char* fname = "./certs/server-key.pem";
  30877. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30878. size_t sz;
  30879. byte* buf;
  30880. EVP_PKEY* pkey2;
  30881. EVP_PKEY* pkey3;
  30882. RSA* rsa_key = NULL;
  30883. file = XFOPEN(fname, "rb");
  30884. AssertTrue((file != XBADFILE));
  30885. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30886. sz = XFTELL(file);
  30887. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30888. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30889. if (buf) {
  30890. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  30891. }
  30892. XFCLOSE(file);
  30893. /* Test using BIO new mem and loading PEM private key */
  30894. bio = BIO_new_mem_buf(buf, (int)sz);
  30895. AssertNotNull(bio);
  30896. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30897. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30898. BIO_free(bio);
  30899. bio = NULL;
  30900. AssertNotNull(pkey2 = EVP_PKEY_new());
  30901. pkey2->type = EVP_PKEY_RSA;
  30902. /* Test parameter copy */
  30903. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30904. EVP_PKEY_free(pkey2);
  30905. EVP_PKEY_free(pkey);
  30906. pkey = NULL;
  30907. /* Qt unit test case : rsa pkcs8 key */
  30908. file = XFOPEN(fname_rsa_p8, "rb");
  30909. AssertTrue((file != XBADFILE));
  30910. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30911. sz = XFTELL(file);
  30912. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30913. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30914. if (buf)
  30915. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  30916. XFCLOSE(file);
  30917. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30918. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30919. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30920. BIO_free(bio);
  30921. bio = NULL;
  30922. AssertNotNull(pkey3 = EVP_PKEY_new());
  30923. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30924. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30925. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30926. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30927. #else
  30928. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30929. #endif
  30930. RSA_free(rsa_key);
  30931. EVP_PKEY_free(pkey3);
  30932. EVP_PKEY_free(pkey);
  30933. pkey = NULL;
  30934. }
  30935. #endif
  30936. /* test loading ECC key using BIO */
  30937. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30938. {
  30939. XFILE file;
  30940. const char* fname = "./certs/ecc-key.pem";
  30941. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30942. size_t sz;
  30943. byte* buf;
  30944. EVP_PKEY* pkey2;
  30945. EVP_PKEY* pkey3;
  30946. EC_KEY* ec_key;
  30947. int nid = 0;
  30948. file = XFOPEN(fname, "rb");
  30949. AssertTrue((file != XBADFILE));
  30950. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30951. sz = XFTELL(file);
  30952. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30953. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30954. if (buf)
  30955. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  30956. XFCLOSE(file);
  30957. /* Test using BIO new mem and loading PEM private key */
  30958. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30959. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30960. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30961. BIO_free(bio);
  30962. bio = NULL;
  30963. AssertNotNull(pkey2 = EVP_PKEY_new());
  30964. AssertNotNull(pkey3 = EVP_PKEY_new());
  30965. pkey2->type = EVP_PKEY_EC;
  30966. /* Test parameter copy */
  30967. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30968. /* Qt unit test case 1*/
  30969. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30970. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30971. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30972. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30973. #else
  30974. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30975. #endif
  30976. /* Test default digest */
  30977. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30978. AssertIntEQ(nid, NID_sha256);
  30979. EC_KEY_free(ec_key);
  30980. EVP_PKEY_free(pkey3);
  30981. EVP_PKEY_free(pkey2);
  30982. EVP_PKEY_free(pkey);
  30983. pkey = NULL;
  30984. /* Qt unit test case ec pkcs8 key */
  30985. file = XFOPEN(fname_ecc_p8, "rb");
  30986. AssertTrue((file != XBADFILE));
  30987. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30988. sz = XFTELL(file);
  30989. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30990. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30991. if (buf)
  30992. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  30993. XFCLOSE(file);
  30994. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30995. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30996. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30997. BIO_free(bio);
  30998. bio = NULL;
  30999. AssertNotNull(pkey3 = EVP_PKEY_new());
  31000. /* Qt unit test case */
  31001. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31002. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31003. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31004. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31005. #else
  31006. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31007. #endif
  31008. EC_KEY_free(ec_key);
  31009. EVP_PKEY_free(pkey3);
  31010. EVP_PKEY_free(pkey);
  31011. pkey = NULL;
  31012. }
  31013. #endif
  31014. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  31015. defined(WOLFSSL_CERT_GEN))
  31016. {
  31017. #define BIO_PEM_TEST_CHAR 'a'
  31018. EVP_PKEY* pkey2 = NULL;
  31019. unsigned char extra[10];
  31020. int i;
  31021. BIO* pub_bio = NULL;
  31022. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  31023. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31024. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  31025. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31026. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  31027. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  31028. &server_key, (long)sizeof_server_key_der_2048));
  31029. AssertNull(pkey);
  31030. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  31031. &server_key, (long)sizeof_server_key_der_2048));
  31032. AssertIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  31033. NULL), WOLFSSL_FAILURE);
  31034. AssertIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31035. NULL), WOLFSSL_FAILURE);
  31036. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31037. NULL), WOLFSSL_SUCCESS);
  31038. AssertIntGT(BIO_pending(bio), 0);
  31039. AssertIntEQ(BIO_pending(bio), 1679);
  31040. /* Check if the pubkey API writes only the public key */
  31041. #ifdef WOLFSSL_KEY_GEN
  31042. AssertIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  31043. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  31044. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  31045. AssertIntGT(BIO_pending(pub_bio), 0);
  31046. /* Previously both the private key and the pubkey calls would write
  31047. * out the private key and the PEM header was the only difference.
  31048. * The public PEM should be significantly shorter than the
  31049. * private key versison. */
  31050. AssertIntEQ(BIO_pending(pub_bio), 451);
  31051. #endif
  31052. /* test creating new EVP_PKEY with good args */
  31053. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31054. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  31055. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  31056. /* test of reuse of EVP_PKEY */
  31057. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  31058. AssertIntEQ(BIO_pending(bio), 0);
  31059. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31060. SSL_SUCCESS);
  31061. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  31062. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  31063. AssertNotNull(pkey);
  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. }
  31067. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  31068. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  31069. for (i = 0; i < 10; i++) {
  31070. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  31071. }
  31072. BIO_free(pub_bio);
  31073. BIO_free(bio);
  31074. bio = NULL;
  31075. EVP_PKEY_free(pkey);
  31076. pkey = NULL;
  31077. EVP_PKEY_free(pkey2);
  31078. }
  31079. #endif
  31080. /* key is DES encrypted */
  31081. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  31082. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  31083. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  31084. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  31085. {
  31086. XFILE f;
  31087. wc_pem_password_cb* passwd_cb;
  31088. void* passwd_cb_userdata;
  31089. SSL_CTX* ctx;
  31090. char passwd[] = "bad password";
  31091. #ifndef WOLFSSL_NO_TLS12
  31092. #ifndef NO_WOLFSSL_SERVER
  31093. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31094. #else
  31095. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31096. #endif
  31097. #else
  31098. #ifndef NO_WOLFSSL_SERVER
  31099. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31100. #else
  31101. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31102. #endif
  31103. #endif
  31104. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31105. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31106. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31107. AssertNull(passwd_cb_userdata =
  31108. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31109. /* fail case with password call back */
  31110. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31111. (void*)passwd));
  31112. BIO_free(bio);
  31113. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31114. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31115. (void*)passwd));
  31116. BIO_free(bio);
  31117. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  31118. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31119. /* use callback that works */
  31120. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31121. (void*)"yassl123"));
  31122. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31123. EVP_PKEY_free(pkey);
  31124. pkey = NULL;
  31125. BIO_free(bio);
  31126. bio = NULL;
  31127. SSL_CTX_free(ctx);
  31128. }
  31129. #endif /* !defined(NO_DES3) */
  31130. #endif /* !NO_BIO */
  31131. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31132. {
  31133. unsigned char buf[2048];
  31134. size_t bytes;
  31135. XFILE f;
  31136. SSL_CTX* ctx;
  31137. #ifndef WOLFSSL_NO_TLS12
  31138. #ifndef NO_WOLFSSL_SERVER
  31139. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31140. #else
  31141. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31142. #endif
  31143. #else
  31144. #ifndef NO_WOLFSSL_SERVER
  31145. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31146. #else
  31147. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31148. #endif
  31149. #endif
  31150. f = XFOPEN("./certs/ecc-key.der", "rb");
  31151. AssertTrue((f != XBADFILE));
  31152. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  31153. XFCLOSE(f);
  31154. server_key = buf;
  31155. pkey = NULL;
  31156. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  31157. AssertNull(pkey);
  31158. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  31159. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31160. EVP_PKEY_free(pkey);
  31161. pkey = NULL;
  31162. SSL_CTX_free(ctx);
  31163. }
  31164. #endif
  31165. res = TEST_RES_CHECK(1);
  31166. #ifndef NO_BIO
  31167. (void)bio;
  31168. #endif
  31169. (void)pkey;
  31170. (void)server_key;
  31171. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31172. return res;
  31173. }
  31174. static int test_wolfSSL_PEM_file_RSAKey(void)
  31175. {
  31176. int res = TEST_SKIPPED;
  31177. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31178. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31179. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31180. RSA* rsa = NULL;
  31181. XFILE fp;
  31182. AssertTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31183. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31184. XFCLOSE(fp);
  31185. AssertIntEQ(RSA_size(rsa), 256);
  31186. AssertIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31187. AssertIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31188. AssertIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31189. AssertIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31190. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31191. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31192. RSA_free(rsa);
  31193. res = TEST_RES_CHECK(1);
  31194. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31195. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31196. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31197. return res;
  31198. }
  31199. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31200. {
  31201. int res = TEST_SKIPPED;
  31202. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31203. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  31204. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31205. RSA* rsa = NULL;
  31206. XFILE f = NULL;
  31207. f = XFOPEN(svrKeyFile, "r");
  31208. AssertTrue((f != XBADFILE));
  31209. AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31210. AssertIntEQ(RSA_size(rsa), 256);
  31211. AssertIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31212. NULL), WOLFSSL_FAILURE);
  31213. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31214. NULL), WOLFSSL_FAILURE);
  31215. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31216. WOLFSSL_SUCCESS);
  31217. RSA_free(rsa);
  31218. XFCLOSE(f);
  31219. #ifdef HAVE_ECC
  31220. f = XFOPEN(eccKeyFile, "r");
  31221. AssertTrue((f != XBADFILE));
  31222. AssertNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31223. XFCLOSE(f);
  31224. #endif /* HAVE_ECC */
  31225. res = TEST_RES_CHECK(1);
  31226. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31227. return res;
  31228. }
  31229. #ifndef NO_BIO
  31230. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31231. {
  31232. int res = TEST_SKIPPED;
  31233. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31234. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31235. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31236. RSA* rsa = NULL;
  31237. BIO* bio = NULL;
  31238. /* PrivateKey */
  31239. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31240. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31241. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31242. AssertNotNull(rsa);
  31243. AssertIntEQ(RSA_size(rsa), 256);
  31244. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31245. NULL), WOLFSSL_FAILURE);
  31246. BIO_free(bio);
  31247. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31248. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31249. NULL), WOLFSSL_SUCCESS);
  31250. BIO_free(bio);
  31251. RSA_free(rsa);
  31252. /* PUBKEY */
  31253. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31254. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31255. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31256. AssertIntEQ(RSA_size(rsa), 256);
  31257. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31258. BIO_free(bio);
  31259. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31260. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31261. BIO_free(bio);
  31262. RSA_free(rsa);
  31263. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31264. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31265. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31266. BIO_free(bio);
  31267. RSA_free(rsa);
  31268. #ifdef HAVE_ECC
  31269. /* ensure that non-rsa keys do not work */
  31270. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31271. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31272. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31273. BIO_free(bio);
  31274. RSA_free(rsa);
  31275. #endif /* HAVE_ECC */
  31276. res = TEST_RES_CHECK(1);
  31277. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31278. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31279. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31280. return res;
  31281. }
  31282. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31283. {
  31284. int res = TEST_SKIPPED;
  31285. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31286. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31287. RSA* rsa = NULL;
  31288. RSA* rsa_dup = NULL;
  31289. BIO* bio = NULL;
  31290. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31291. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31292. AssertIntEQ(RSA_size(rsa), 256);
  31293. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  31294. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  31295. /* Test duplicating empty key. */
  31296. rsa_dup = RSA_new();
  31297. AssertNull(RSAPublicKey_dup(rsa_dup));
  31298. RSA_free(rsa_dup);
  31299. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31300. AssertPtrNE(rsa_dup, rsa);
  31301. #endif
  31302. /* test if valgrind complains about unreleased memory */
  31303. RSA_up_ref(rsa);
  31304. RSA_free(rsa);
  31305. BIO_free(bio);
  31306. RSA_free(rsa);
  31307. RSA_free(rsa_dup);
  31308. #ifdef HAVE_ECC
  31309. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31310. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31311. BIO_free(bio);
  31312. #endif /* HAVE_ECC */
  31313. res = TEST_RES_CHECK(1);
  31314. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31315. return res;
  31316. }
  31317. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31318. {
  31319. int res = TEST_SKIPPED;
  31320. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31321. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31322. XFILE file;
  31323. const char* fname = "./certs/client-keyPub.pem";
  31324. RSA *rsa;
  31325. AssertNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31326. file = XFOPEN(fname, "rb");
  31327. AssertTrue((file != XBADFILE));
  31328. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31329. AssertIntEQ(RSA_size(rsa), 256);
  31330. RSA_free(rsa);
  31331. XFCLOSE(file);
  31332. res = TEST_RES_CHECK(1);
  31333. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31334. return res;
  31335. }
  31336. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31337. {
  31338. int res = TEST_SKIPPED;
  31339. #ifndef HAVE_SELFTEST
  31340. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31341. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31342. DSA* dsa = NULL;
  31343. BIO* bio = NULL;
  31344. /* PrivateKey */
  31345. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31346. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31347. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31348. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  31349. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  31350. WOLFSSL_FAILURE);
  31351. BIO_free(bio);
  31352. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31353. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  31354. WOLFSSL_SUCCESS);
  31355. BIO_free(bio);
  31356. DSA_free(dsa);
  31357. /* PUBKEY */
  31358. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31359. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31360. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31361. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  31362. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31363. BIO_free(bio);
  31364. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31365. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31366. BIO_free(bio);
  31367. DSA_free(dsa);
  31368. #ifdef HAVE_ECC
  31369. /* ensure that non-dsa keys do not work */
  31370. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31371. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31372. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31373. BIO_free(bio);
  31374. DSA_free(dsa);
  31375. #endif /* HAVE_ECC */
  31376. res = TEST_RES_CHECK(1);
  31377. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31378. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31379. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31380. #endif /* HAVE_SELFTEST */
  31381. return res;
  31382. }
  31383. static int test_wolfSSL_PEM_bio_ECKey(void)
  31384. {
  31385. int res = TEST_SKIPPED;
  31386. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31387. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31388. EC_KEY* ec = NULL;
  31389. EC_KEY* ec2;
  31390. BIO* bio = NULL;
  31391. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31392. unsigned char* pem = NULL;
  31393. int pLen;
  31394. #endif
  31395. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31396. "MAA=\n"
  31397. "-----END PUBLIC KEY-----";
  31398. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31399. "MAA=\n"
  31400. "-----END EC PRIVATE KEY-----";
  31401. /* PrivateKey */
  31402. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31403. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31404. ec2 = NULL;
  31405. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31406. AssertIntEQ(ec == ec2, 1);
  31407. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31408. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31409. NULL), WOLFSSL_FAILURE);
  31410. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31411. NULL), WOLFSSL_FAILURE);
  31412. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31413. WOLFSSL_FAILURE);
  31414. BIO_free(bio);
  31415. /* Public key data - fail. */
  31416. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31417. AssertNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31418. BIO_free(bio);
  31419. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31420. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31421. NULL), WOLFSSL_SUCCESS);
  31422. BIO_free(bio);
  31423. AssertIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31424. NULL),WOLFSSL_FAILURE);
  31425. AssertIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31426. WOLFSSL_FAILURE);
  31427. AssertIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31428. WOLFSSL_FAILURE);
  31429. AssertIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31430. WOLFSSL_SUCCESS);
  31431. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31432. NULL), 0);
  31433. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31434. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31435. NULL), 0);
  31436. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31437. NULL), 0);
  31438. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31439. &pLen), 0);
  31440. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31441. &pLen), 0);
  31442. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31443. &pLen), 0);
  31444. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31445. NULL), 0);
  31446. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31447. &pLen), 1);
  31448. AssertIntGT(pLen, 0);
  31449. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31450. #endif
  31451. EC_KEY_free(ec);
  31452. /* PUBKEY */
  31453. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31454. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31455. ec2 = NULL;
  31456. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31457. AssertIntEQ(ec == ec2, 1);
  31458. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31459. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31460. BIO_free(bio);
  31461. /* Test 0x30, 0x00 fails. */
  31462. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31463. sizeof(ec_key_bad_1)));
  31464. AssertNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31465. BIO_free(bio);
  31466. /* Private key data - fail. */
  31467. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31468. AssertNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31469. BIO_free(bio);
  31470. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31471. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31472. BIO_free(bio);
  31473. /* Same test as above, but with a file pointer rather than a BIO. */
  31474. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31475. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31476. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31477. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31478. EC_KEY_free(ec);
  31479. #ifndef NO_RSA
  31480. /* ensure that non-ec keys do not work */
  31481. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31482. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31483. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31484. BIO_free(bio);
  31485. EC_KEY_free(ec);
  31486. #endif /* HAVE_ECC */
  31487. /* Test 0x30, 0x00 fails. */
  31488. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31489. sizeof(ec_priv_key_bad_1)));
  31490. AssertNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31491. BIO_free(bio);
  31492. res = TEST_RES_CHECK(1);
  31493. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31494. return res;
  31495. }
  31496. static int test_wolfSSL_PEM_PUBKEY(void)
  31497. {
  31498. int res = TEST_SKIPPED;
  31499. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31500. BIO* bio = NULL;
  31501. EVP_PKEY* pkey = NULL;
  31502. /* test creating new EVP_PKEY with bad arg */
  31503. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31504. /* test loading ECC key using BIO */
  31505. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31506. {
  31507. XFILE file;
  31508. const char* fname = "./certs/ecc-client-keyPub.pem";
  31509. size_t sz;
  31510. byte* buf;
  31511. EVP_PKEY* pkey2;
  31512. EC_KEY* ec_key;
  31513. file = XFOPEN(fname, "rb");
  31514. AssertTrue((file != XBADFILE));
  31515. AssertIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31516. sz = XFTELL(file);
  31517. AssertIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31518. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31519. if (buf)
  31520. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  31521. XFCLOSE(file);
  31522. /* Test using BIO new mem and loading PEM private key */
  31523. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31524. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31525. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31526. BIO_free(bio);
  31527. bio = NULL;
  31528. /* Qt unit test case*/
  31529. AssertNotNull(pkey2 = EVP_PKEY_new());
  31530. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31531. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31532. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31533. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31534. #else
  31535. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31536. #endif
  31537. EC_KEY_free(ec_key);
  31538. EVP_PKEY_free(pkey2);
  31539. EVP_PKEY_free(pkey);
  31540. pkey = NULL;
  31541. }
  31542. #endif
  31543. (void)bio;
  31544. (void)pkey;
  31545. res = TEST_RES_CHECK(1);
  31546. #endif
  31547. return res;
  31548. }
  31549. #endif /* !NO_BIO */
  31550. static int test_DSA_do_sign_verify(void)
  31551. {
  31552. int res = TEST_SKIPPED;
  31553. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31554. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31555. !defined(NO_DSA)
  31556. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31557. DSA_SIG* sig;
  31558. DSA* dsa;
  31559. word32 bytes;
  31560. byte sigBin[DSA_SIG_SIZE];
  31561. int dsacheck;
  31562. #ifdef USE_CERT_BUFFERS_1024
  31563. byte tmp[ONEK_BUF];
  31564. XMEMSET(tmp, 0, sizeof(tmp));
  31565. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31566. bytes = sizeof_dsa_key_der_1024;
  31567. #elif defined(USE_CERT_BUFFERS_2048)
  31568. byte tmp[TWOK_BUF];
  31569. XMEMSET(tmp, 0, sizeof(tmp));
  31570. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31571. bytes = sizeof_dsa_key_der_2048;
  31572. #else
  31573. byte tmp[TWOK_BUF];
  31574. XMEMSET(tmp, 0, sizeof(tmp));
  31575. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  31576. if (fp == XBADFILE) {
  31577. return WOLFSSL_BAD_FILE;
  31578. }
  31579. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  31580. XFCLOSE(fp);
  31581. #endif /* END USE_CERT_BUFFERS_1024 */
  31582. XMEMSET(digest, 202, sizeof(digest));
  31583. AssertNotNull(dsa = DSA_new());
  31584. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  31585. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31586. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31587. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31588. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31589. DSA_SIG_free(sig);
  31590. DSA_free(dsa);
  31591. res = TEST_RES_CHECK(1);
  31592. #endif
  31593. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31594. return res;
  31595. }
  31596. static int test_wolfSSL_tmp_dh(void)
  31597. {
  31598. int res = TEST_SKIPPED;
  31599. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31600. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31601. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31602. byte buff[6000];
  31603. char file[] = "./certs/dsaparams.pem";
  31604. XFILE f;
  31605. int bytes;
  31606. DSA* dsa;
  31607. DH* dh;
  31608. #if defined(WOLFSSL_DH_EXTRA) && \
  31609. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31610. DH* dh2;
  31611. #endif
  31612. BIO* bio;
  31613. SSL* ssl;
  31614. SSL_CTX* ctx;
  31615. #ifndef NO_WOLFSSL_SERVER
  31616. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31617. #else
  31618. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31619. #endif
  31620. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  31621. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  31622. AssertNotNull(ssl = SSL_new(ctx));
  31623. f = XFOPEN(file, "rb");
  31624. AssertTrue((f != XBADFILE));
  31625. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  31626. XFCLOSE(f);
  31627. bio = BIO_new_mem_buf((void*)buff, bytes);
  31628. AssertNotNull(bio);
  31629. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31630. AssertNotNull(dsa);
  31631. dh = wolfSSL_DSA_dup_DH(dsa);
  31632. AssertNotNull(dh);
  31633. #if defined(WOLFSSL_DH_EXTRA) && \
  31634. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31635. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  31636. #endif
  31637. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31638. #ifndef NO_WOLFSSL_SERVER
  31639. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31640. #else
  31641. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31642. #endif
  31643. BIO_free(bio);
  31644. DSA_free(dsa);
  31645. DH_free(dh);
  31646. #if defined(WOLFSSL_DH_EXTRA) && \
  31647. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31648. DH_free(dh2);
  31649. #endif
  31650. SSL_free(ssl);
  31651. SSL_CTX_free(ctx);
  31652. res = TEST_RES_CHECK(1);
  31653. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31654. #endif
  31655. return res;
  31656. }
  31657. static int test_wolfSSL_ctrl(void)
  31658. {
  31659. int res = TEST_SKIPPED;
  31660. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31661. byte buff[6000];
  31662. BIO* bio;
  31663. int bytes;
  31664. BUF_MEM* ptr = NULL;
  31665. XMEMSET(buff, 0, sizeof(buff));
  31666. bytes = sizeof(buff);
  31667. bio = BIO_new_mem_buf((void*)buff, bytes);
  31668. AssertNotNull(bio);
  31669. AssertNotNull(BIO_s_socket());
  31670. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31671. /* needs tested after stubs filled out @TODO
  31672. SSL_ctrl
  31673. SSL_CTX_ctrl
  31674. */
  31675. BIO_free(bio);
  31676. res = TEST_RES_CHECK(1);
  31677. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31678. return res;
  31679. }
  31680. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31681. {
  31682. int res = TEST_SKIPPED;
  31683. #ifdef OPENSSL_EXTRA
  31684. static const unsigned char pw[] = "password";
  31685. static const int pwSz = sizeof(pw) - 1;
  31686. size_t checkPwSz = 0;
  31687. const unsigned char* checkPw = NULL;
  31688. WOLFSSL_EVP_PKEY* key = NULL;
  31689. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31690. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31691. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  31692. if (key) {
  31693. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  31694. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  31695. AssertIntEQ(key->pkey_sz, pwSz);
  31696. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31697. }
  31698. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31699. AssertIntEQ((int)checkPwSz, pwSz);
  31700. if (checkPw) {
  31701. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31702. }
  31703. wolfSSL_EVP_PKEY_free(key);
  31704. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  31705. if (key) {
  31706. AssertIntEQ(key->pkey_sz, 0);
  31707. }
  31708. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31709. (void)checkPw;
  31710. AssertIntEQ((int)checkPwSz, 0);
  31711. wolfSSL_EVP_PKEY_free(key);
  31712. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  31713. if (key) {
  31714. AssertIntEQ(key->pkey_sz, 0);
  31715. }
  31716. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31717. (void)checkPw;
  31718. AssertIntEQ((int)checkPwSz, 0);
  31719. wolfSSL_EVP_PKEY_free(key);
  31720. res = TEST_RES_CHECK(1);
  31721. #endif /* OPENSSL_EXTRA */
  31722. return res;
  31723. }
  31724. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31725. {
  31726. int res = TEST_SKIPPED;
  31727. #ifdef OPENSSL_EXTRA
  31728. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31729. const char *priv = "ABCDEFGHIJKLMNOP";
  31730. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31731. WOLFSSL_EVP_PKEY* key = NULL;
  31732. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31733. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31734. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31735. NULL, (const unsigned char *)priv, 0, cipher));
  31736. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31737. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31738. AssertNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31739. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31740. wolfSSL_EVP_PKEY_free(key);
  31741. res = TEST_RES_CHECK(1);
  31742. #endif /* defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) */
  31743. #endif /* OPENSSL_EXTRA */
  31744. return res;
  31745. }
  31746. static int test_wolfSSL_EVP_Digest(void)
  31747. {
  31748. int res = TEST_SKIPPED;
  31749. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31750. const char* in = "abc";
  31751. int inLen = (int)XSTRLEN(in);
  31752. byte out[WC_SHA256_DIGEST_SIZE];
  31753. unsigned int outLen;
  31754. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31755. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31756. "\x15\xAD";
  31757. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  31758. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31759. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31760. res = TEST_RES_CHECK(1);
  31761. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31762. return res;
  31763. }
  31764. static int test_wolfSSL_EVP_Digest_all(void)
  31765. {
  31766. int res = TEST_SKIPPED;
  31767. #ifdef OPENSSL_EXTRA
  31768. const char* digests[] = {
  31769. #ifndef NO_MD5
  31770. "MD5",
  31771. #endif
  31772. #ifndef NO_SHA
  31773. "SHA",
  31774. #endif
  31775. #ifdef WOLFSSL_SHA224
  31776. "SHA224",
  31777. #endif
  31778. #ifndef NO_SHA256
  31779. "SHA256",
  31780. #endif
  31781. #ifdef WOLFSSL_SHA384
  31782. "SHA384",
  31783. #endif
  31784. #ifdef WOLFSSL_SHA512
  31785. "SHA512",
  31786. #endif
  31787. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31788. "SHA512_224",
  31789. #endif
  31790. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31791. "SHA512_256",
  31792. #endif
  31793. #ifdef WOLFSSL_SHA3
  31794. #ifndef WOLFSSL_NOSHA3_224
  31795. "SHA3_224",
  31796. #endif
  31797. #ifndef WOLFSSL_NOSHA3_256
  31798. "SHA3_256",
  31799. #endif
  31800. "SHA3_384",
  31801. #ifndef WOLFSSL_NOSHA3_512
  31802. "SHA3_512",
  31803. #endif
  31804. #endif /* WOLFSSL_SHA3 */
  31805. NULL
  31806. };
  31807. const char** d;
  31808. const unsigned char in[] = "abc";
  31809. int inLen = XSTR_SIZEOF(in);
  31810. byte out[WC_MAX_DIGEST_SIZE];
  31811. unsigned int outLen;
  31812. for (d = digests; *d != NULL; d++) {
  31813. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31814. AssertIntGT(outLen, 0);
  31815. AssertIntEQ(EVP_MD_size(*d), outLen);
  31816. }
  31817. res = TEST_RES_CHECK(1);
  31818. #endif
  31819. return res;
  31820. }
  31821. static int test_wolfSSL_EVP_MD_size(void)
  31822. {
  31823. int res = TEST_SKIPPED;
  31824. #ifdef OPENSSL_EXTRA
  31825. WOLFSSL_EVP_MD_CTX mdCtx;
  31826. #ifdef WOLFSSL_SHA3
  31827. #ifndef WOLFSSL_NOSHA3_224
  31828. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31829. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31830. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31831. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31832. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31833. #endif
  31834. #ifndef WOLFSSL_NOSHA3_256
  31835. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31836. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31837. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31838. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31839. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31840. #endif
  31841. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31842. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31843. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31844. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31845. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31846. #ifndef WOLFSSL_NOSHA3_512
  31847. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31848. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31849. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31850. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31851. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31852. #endif
  31853. #endif /* WOLFSSL_SHA3 */
  31854. #ifndef NO_SHA256
  31855. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31856. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31857. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  31858. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  31859. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31860. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31861. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31862. #endif
  31863. #ifndef NO_MD5
  31864. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31865. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31866. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  31867. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  31868. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31869. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31870. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31871. #endif
  31872. #ifdef WOLFSSL_SHA224
  31873. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31874. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31875. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  31876. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  31877. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31878. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31879. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31880. #endif
  31881. #ifdef WOLFSSL_SHA384
  31882. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31883. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31884. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  31885. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  31886. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31887. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31888. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31889. #endif
  31890. #ifdef WOLFSSL_SHA512
  31891. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31892. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31893. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  31894. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  31895. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  31896. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  31897. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31898. #endif
  31899. #ifndef NO_SHA
  31900. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31901. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  31902. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  31903. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  31904. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31905. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31906. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31907. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31908. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  31909. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  31910. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  31911. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31912. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31913. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31914. #endif
  31915. /* error case */
  31916. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31917. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  31918. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  31919. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  31920. /* Cleanup is valid on uninit'ed struct */
  31921. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31922. res = TEST_RES_CHECK(1);
  31923. #endif /* OPENSSL_EXTRA */
  31924. return res;
  31925. }
  31926. static int test_wolfSSL_EVP_MD_pkey_type(void)
  31927. {
  31928. int res = TEST_SKIPPED;
  31929. #ifdef OPENSSL_EXTRA
  31930. const WOLFSSL_EVP_MD* md;
  31931. #ifndef NO_MD5
  31932. AssertNotNull(md = EVP_md5());
  31933. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  31934. #endif
  31935. #ifndef NO_SHA
  31936. AssertNotNull(md = EVP_sha1());
  31937. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  31938. #endif
  31939. #ifdef WOLFSSL_SHA224
  31940. AssertNotNull(md = EVP_sha224());
  31941. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  31942. #endif
  31943. AssertNotNull(md = EVP_sha256());
  31944. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  31945. #ifdef WOLFSSL_SHA384
  31946. AssertNotNull(md = EVP_sha384());
  31947. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  31948. #endif
  31949. #ifdef WOLFSSL_SHA512
  31950. AssertNotNull(md = EVP_sha512());
  31951. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  31952. #endif
  31953. res = TEST_RES_CHECK(1);
  31954. #endif
  31955. return res;
  31956. }
  31957. #ifdef OPENSSL_EXTRA
  31958. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  31959. size_t testKeySz, const char* testData, size_t testDataSz,
  31960. const byte* testResult, size_t testResultSz)
  31961. {
  31962. unsigned char check[WC_MAX_DIGEST_SIZE];
  31963. size_t checkSz = -1;
  31964. WOLFSSL_EVP_PKEY* key;
  31965. WOLFSSL_EVP_MD_CTX mdCtx;
  31966. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  31967. testKey, (int)testKeySz));
  31968. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31969. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31970. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31971. (unsigned int)testDataSz), 1);
  31972. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31973. AssertIntEQ((int)checkSz, (int)testResultSz);
  31974. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31975. AssertIntEQ((int)checkSz,(int)testResultSz);
  31976. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31977. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31978. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31979. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31980. (unsigned int)testDataSz), 1);
  31981. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31982. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31983. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31984. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31985. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31986. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31987. AssertIntEQ((int)checkSz, (int)testResultSz);
  31988. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31989. AssertIntEQ((int)checkSz,(int)testResultSz);
  31990. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31991. (unsigned int)testDataSz - 4), 1);
  31992. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31993. AssertIntEQ((int)checkSz,(int)testResultSz);
  31994. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31995. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31996. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31997. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31998. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31999. (unsigned int)testDataSz - 4), 1);
  32000. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32001. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32002. wolfSSL_EVP_PKEY_free(key);
  32003. }
  32004. #endif
  32005. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  32006. {
  32007. int res = TEST_SKIPPED;
  32008. #ifdef OPENSSL_EXTRA
  32009. static const unsigned char testKey[] =
  32010. {
  32011. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32012. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32013. 0x0b, 0x0b, 0x0b, 0x0b
  32014. };
  32015. static const char testData[] = "Hi There";
  32016. #ifdef WOLFSSL_SHA224
  32017. static const unsigned char testResultSha224[] =
  32018. {
  32019. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  32020. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  32021. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  32022. 0x53, 0x68, 0x4b, 0x22
  32023. };
  32024. #endif
  32025. #ifndef NO_SHA256
  32026. static const unsigned char testResultSha256[] =
  32027. {
  32028. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  32029. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  32030. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  32031. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  32032. };
  32033. #endif
  32034. #ifdef WOLFSSL_SHA384
  32035. static const unsigned char testResultSha384[] =
  32036. {
  32037. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32038. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32039. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32040. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32041. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32042. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32043. };
  32044. #endif
  32045. #ifdef WOLFSSL_SHA512
  32046. static const unsigned char testResultSha512[] =
  32047. {
  32048. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32049. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32050. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32051. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32052. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32053. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32054. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32055. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32056. };
  32057. #endif
  32058. #ifdef WOLFSSL_SHA3
  32059. #ifndef WOLFSSL_NOSHA3_224
  32060. static const unsigned char testResultSha3_224[] =
  32061. {
  32062. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32063. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32064. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32065. 0xf3, 0xc8, 0x60, 0xf7
  32066. };
  32067. #endif
  32068. #ifndef WOLFSSL_NOSHA3_256
  32069. static const unsigned char testResultSha3_256[] =
  32070. {
  32071. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32072. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32073. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32074. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32075. };
  32076. #endif
  32077. #ifndef WOLFSSL_NOSHA3_384
  32078. static const unsigned char testResultSha3_384[] =
  32079. {
  32080. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32081. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32082. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32083. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32084. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32085. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32086. };
  32087. #endif
  32088. #ifndef WOLFSSL_NOSHA3_512
  32089. static const unsigned char testResultSha3_512[] =
  32090. {
  32091. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32092. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32093. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32094. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32095. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32096. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32097. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32098. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32099. };
  32100. #endif
  32101. #endif
  32102. #ifndef NO_SHA256
  32103. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  32104. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  32105. #endif
  32106. #ifdef WOLFSSL_SHA224
  32107. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  32108. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  32109. #endif
  32110. #ifdef WOLFSSL_SHA384
  32111. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  32112. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  32113. #endif
  32114. #ifdef WOLFSSL_SHA512
  32115. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  32116. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  32117. #endif
  32118. #ifdef WOLFSSL_SHA3
  32119. #ifndef WOLFSSL_NOSHA3_224
  32120. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  32121. testData, XSTRLEN(testData), testResultSha3_224,
  32122. sizeof(testResultSha3_224));
  32123. #endif
  32124. #ifndef WOLFSSL_NOSHA3_256
  32125. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  32126. testData, XSTRLEN(testData), testResultSha3_256,
  32127. sizeof(testResultSha3_256));
  32128. #endif
  32129. #ifndef WOLFSSL_NOSHA3_384
  32130. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  32131. testData, XSTRLEN(testData), testResultSha3_384,
  32132. sizeof(testResultSha3_384));
  32133. #endif
  32134. #ifndef WOLFSSL_NOSHA3_512
  32135. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  32136. testData, XSTRLEN(testData), testResultSha3_512,
  32137. sizeof(testResultSha3_512));
  32138. #endif
  32139. #endif
  32140. res = TEST_RES_CHECK(1);
  32141. #endif /* OPENSSL_EXTRA */
  32142. return res;
  32143. }
  32144. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32145. {
  32146. int res = TEST_SKIPPED;
  32147. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  32148. defined(USE_CERT_BUFFERS_2048)
  32149. WOLFSSL_EVP_PKEY* privKey;
  32150. WOLFSSL_EVP_PKEY* pubKey;
  32151. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  32152. const char testData[] = "Hi There";
  32153. WOLFSSL_EVP_MD_CTX mdCtx;
  32154. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32155. size_t checkSz = -1;
  32156. int sz = 2048 / 8;
  32157. const unsigned char* cp;
  32158. const unsigned char* p;
  32159. unsigned char check[2048/8];
  32160. size_t i;
  32161. int paddings[] = {
  32162. RSA_PKCS1_PADDING,
  32163. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32164. RSA_PKCS1_PSS_PADDING,
  32165. #endif
  32166. };
  32167. cp = client_key_der_2048;
  32168. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32169. sizeof_client_key_der_2048)));
  32170. p = client_keypub_der_2048;
  32171. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32172. sizeof_client_keypub_der_2048)));
  32173. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32174. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32175. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32176. NULL, privKey), 1);
  32177. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32178. (unsigned int)XSTRLEN(testData)), 1);
  32179. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32180. AssertIntEQ((int)checkSz, sz);
  32181. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32182. AssertIntEQ((int)checkSz,sz);
  32183. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32184. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32185. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy), 1);
  32186. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32187. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32188. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32189. NULL, pubKey), 1);
  32190. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32191. (unsigned int)XSTRLEN(testData)),
  32192. 1);
  32193. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32194. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32195. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32196. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32197. NULL, privKey), 1);
  32198. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32199. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32200. AssertIntEQ((int)checkSz, sz);
  32201. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32202. AssertIntEQ((int)checkSz, sz);
  32203. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32204. (unsigned int)XSTRLEN(testData) - 4), 1);
  32205. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32206. AssertIntEQ((int)checkSz, sz);
  32207. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32208. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32209. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32210. NULL, pubKey), 1);
  32211. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32212. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32213. (unsigned int)XSTRLEN(testData) - 4),
  32214. 1);
  32215. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32216. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32217. /* Check all signing padding types */
  32218. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32219. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32220. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32221. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32222. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32223. paddings[i]), 1);
  32224. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32225. (unsigned int)XSTRLEN(testData)), 1);
  32226. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32227. AssertIntEQ((int)checkSz, sz);
  32228. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32229. AssertIntEQ((int)checkSz,sz);
  32230. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32231. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32232. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32233. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32234. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32235. paddings[i]), 1);
  32236. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32237. (unsigned int)XSTRLEN(testData)), 1);
  32238. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32239. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32240. }
  32241. wolfSSL_EVP_PKEY_free(pubKey);
  32242. wolfSSL_EVP_PKEY_free(privKey);
  32243. res = TEST_RES_CHECK(1);
  32244. #endif
  32245. return res;
  32246. }
  32247. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32248. {
  32249. int res = TEST_SKIPPED;
  32250. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32251. WOLFSSL_EVP_PKEY* privKey;
  32252. WOLFSSL_EVP_PKEY* pubKey;
  32253. const char testData[] = "Hi There";
  32254. WOLFSSL_EVP_MD_CTX mdCtx;
  32255. size_t checkSz = -1;
  32256. const unsigned char* cp;
  32257. const unsigned char* p;
  32258. unsigned char check[2048/8];
  32259. cp = ecc_clikey_der_256;
  32260. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32261. sizeof_ecc_clikey_der_256);
  32262. AssertNotNull(privKey);
  32263. p = ecc_clikeypub_der_256;
  32264. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32265. sizeof_ecc_clikeypub_der_256)));
  32266. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32267. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32268. NULL, privKey), 1);
  32269. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32270. (unsigned int)XSTRLEN(testData)), 1);
  32271. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32272. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32273. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32274. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32275. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32276. NULL, pubKey), 1);
  32277. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32278. (unsigned int)XSTRLEN(testData)),
  32279. 1);
  32280. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32281. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32282. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32283. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32284. NULL, privKey), 1);
  32285. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32286. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32287. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32288. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32289. (unsigned int)XSTRLEN(testData) - 4), 1);
  32290. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32291. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32292. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32293. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32294. NULL, pubKey), 1);
  32295. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32296. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32297. (unsigned int)XSTRLEN(testData) - 4),
  32298. 1);
  32299. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32300. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32301. wolfSSL_EVP_PKEY_free(pubKey);
  32302. wolfSSL_EVP_PKEY_free(privKey);
  32303. res = TEST_RES_CHECK(1);
  32304. #endif
  32305. return res;
  32306. }
  32307. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32308. {
  32309. int res = TEST_SKIPPED;
  32310. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32311. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32312. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32313. char caFile[] = "./certs/client-ca.pem";
  32314. char clientFile[] = "./certs/client-cert.pem";
  32315. SSL_CTX* ctx;
  32316. X509* x509;
  32317. BIO *bio = NULL;
  32318. X509 *cert = NULL;
  32319. X509 *ca;
  32320. STACK_OF(X509) *chain = NULL;
  32321. STACK_OF(X509) *chain2 = NULL;
  32322. #ifndef NO_WOLFSSL_SERVER
  32323. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32324. #else
  32325. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32326. #endif
  32327. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  32328. AssertNotNull(x509);
  32329. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32330. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  32331. AssertNotNull(x509);
  32332. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  32333. /* additional test of getting EVP_PKEY key size from X509
  32334. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32335. * allowed with user RSA */
  32336. {
  32337. EVP_PKEY* pkey;
  32338. #if defined(HAVE_ECC)
  32339. X509* ecX509;
  32340. #endif /* HAVE_ECC */
  32341. AssertNotNull(pkey = X509_get_pubkey(x509));
  32342. /* current RSA key is 2048 bit (256 bytes) */
  32343. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  32344. EVP_PKEY_free(pkey);
  32345. #if defined(HAVE_ECC)
  32346. #if defined(USE_CERT_BUFFERS_256)
  32347. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32348. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32349. SSL_FILETYPE_ASN1));
  32350. #else
  32351. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  32352. SSL_FILETYPE_PEM));
  32353. #endif
  32354. pkey = X509_get_pubkey(ecX509);
  32355. AssertNotNull(pkey);
  32356. /* current ECC key is 256 bit (32 bytes) */
  32357. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  32358. X509_free(ecX509);
  32359. EVP_PKEY_free(pkey);
  32360. #endif /* HAVE_ECC */
  32361. }
  32362. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  32363. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32364. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32365. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  32366. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32367. #endif
  32368. SSL_CTX_free(ctx);
  32369. #ifndef NO_WOLFSSL_SERVER
  32370. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32371. #else
  32372. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32373. #endif
  32374. /* Test haproxy use case */
  32375. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32376. /* Read Certificate */
  32377. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32378. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32379. AssertNotNull(chain = sk_X509_new_null());
  32380. AssertIntEQ(sk_X509_push(chain, ca), 1);
  32381. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  32382. AssertNotNull(ca = sk_X509_shift(chain2));
  32383. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32384. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32385. BIO_free(bio);
  32386. X509_free(cert);
  32387. sk_X509_pop_free(chain, X509_free);
  32388. sk_X509_pop_free(chain2, X509_free);
  32389. SSL_CTX_free(ctx);
  32390. res = TEST_RES_CHECK(1);
  32391. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32392. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32393. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32394. return res;
  32395. }
  32396. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32397. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32398. {
  32399. int res = TEST_SKIPPED;
  32400. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32401. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32402. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32403. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32404. tcp_ready ready;
  32405. func_args client_args;
  32406. func_args server_args;
  32407. #ifndef SINGLE_THREADED
  32408. THREAD_TYPE serverThread;
  32409. #endif
  32410. callback_functions client_cb;
  32411. callback_functions server_cb;
  32412. int line = 0;
  32413. int flag = ERR_TXT_STRING;
  32414. const char* file = NULL;
  32415. const char* data = NULL;
  32416. /* create a failed connection and inspect the error */
  32417. #ifdef WOLFSSL_TIRTOS
  32418. fdOpenSession(Task_self());
  32419. #endif
  32420. XMEMSET(&client_args, 0, sizeof(func_args));
  32421. XMEMSET(&server_args, 0, sizeof(func_args));
  32422. StartTCP();
  32423. InitTcpReady(&ready);
  32424. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32425. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32426. client_cb.method = wolfTLSv1_1_client_method;
  32427. server_cb.method = wolfTLSv1_2_server_method;
  32428. server_args.signal = &ready;
  32429. server_args.callbacks = &server_cb;
  32430. client_args.signal = &ready;
  32431. client_args.callbacks = &client_cb;
  32432. #ifndef SINGLE_THREADED
  32433. start_thread(test_server_nofail, &server_args, &serverThread);
  32434. wait_tcp_ready(&server_args);
  32435. test_client_nofail(&client_args, NULL);
  32436. join_thread(serverThread);
  32437. #endif
  32438. FreeTcpReady(&ready);
  32439. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32440. AssertNotNull(data);
  32441. /* check clearing error state */
  32442. ERR_remove_state(0);
  32443. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32444. ERR_peek_last_error_line(NULL, &line);
  32445. AssertIntEQ(line, 0);
  32446. ERR_peek_last_error_line(&file, NULL);
  32447. AssertNull(file);
  32448. /* retry connection to fill error queue */
  32449. XMEMSET(&client_args, 0, sizeof(func_args));
  32450. XMEMSET(&server_args, 0, sizeof(func_args));
  32451. StartTCP();
  32452. InitTcpReady(&ready);
  32453. client_cb.method = wolfTLSv1_1_client_method;
  32454. server_cb.method = wolfTLSv1_2_server_method;
  32455. server_args.signal = &ready;
  32456. server_args.callbacks = &server_cb;
  32457. client_args.signal = &ready;
  32458. client_args.callbacks = &client_cb;
  32459. start_thread(test_server_nofail, &server_args, &serverThread);
  32460. wait_tcp_ready(&server_args);
  32461. test_client_nofail(&client_args, NULL);
  32462. join_thread(serverThread);
  32463. FreeTcpReady(&ready);
  32464. /* check that error code was stored */
  32465. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32466. ERR_peek_last_error_line(NULL, &line);
  32467. AssertIntNE(line, 0);
  32468. ERR_peek_last_error_line(&file, NULL);
  32469. AssertNotNull(file);
  32470. #ifdef WOLFSSL_TIRTOS
  32471. fdOpenSession(Task_self());
  32472. #endif
  32473. fprintf(stderr, "\nTesting error print out\n");
  32474. ERR_print_errors_fp(stderr);
  32475. fprintf(stderr, "Done testing print out\n\n");
  32476. res = TEST_RES_CHECK(1);
  32477. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32478. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32479. return res;
  32480. }
  32481. #endif
  32482. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32483. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32484. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32485. {
  32486. (void) ok;
  32487. (void) ctx;
  32488. fprintf(stderr, "ENTER verify_cb\n");
  32489. return SSL_SUCCESS;
  32490. }
  32491. #endif
  32492. static int test_wolfSSL_X509_Name_canon(void)
  32493. {
  32494. int res = TEST_SKIPPED;
  32495. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32496. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32497. defined(WOLFSSL_CERT_GEN) && \
  32498. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32499. const long ex_hash1 = 0x0fdb2da4;
  32500. const long ex_hash2 = 0x9f3e8c9e;
  32501. X509_NAME *name = NULL;
  32502. X509 *x509 = NULL;
  32503. FILE* file = NULL;
  32504. unsigned long hash = 0;
  32505. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32506. byte *pbuf = NULL;
  32507. word32 len = 0;
  32508. (void) ex_hash2;
  32509. file = XFOPEN(caCertFile, "rb");
  32510. AssertNotNull(file);
  32511. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32512. AssertNotNull(name = X509_get_issuer_name(x509));
  32513. /* When output buffer is NULL, should return necessary output buffer
  32514. * length.*/
  32515. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32516. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32517. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32518. hash = (((unsigned long)digest[3] << 24) |
  32519. ((unsigned long)digest[2] << 16) |
  32520. ((unsigned long)digest[1] << 8) |
  32521. ((unsigned long)digest[0]));
  32522. AssertIntEQ(hash, ex_hash1);
  32523. XFCLOSE(file);
  32524. X509_free(x509);
  32525. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32526. pbuf = NULL;
  32527. file = XFOPEN(cliCertFile, "rb");
  32528. AssertNotNull(file);
  32529. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32530. AssertNotNull(name = X509_get_issuer_name(x509));
  32531. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32532. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32533. hash = (((unsigned long)digest[3] << 24) |
  32534. ((unsigned long)digest[2] << 16) |
  32535. ((unsigned long)digest[1] << 8) |
  32536. ((unsigned long)digest[0]));
  32537. AssertIntEQ(hash, ex_hash2);
  32538. XFCLOSE(file);
  32539. X509_free(x509);
  32540. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32541. res = TEST_RES_CHECK(1);
  32542. #endif
  32543. return res;
  32544. }
  32545. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32546. {
  32547. int res = TEST_SKIPPED;
  32548. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32549. const int MAX_DIR = 4;
  32550. const char paths[][32] = {
  32551. "./certs/ed25519",
  32552. "./certs/ecc",
  32553. "./certs/crl",
  32554. "./certs/",
  32555. };
  32556. char CertCrl_path[MAX_FILENAME_SZ];
  32557. char *p;
  32558. X509_STORE* str;
  32559. X509_LOOKUP* lookup;
  32560. WOLFSSL_STACK* sk = NULL;
  32561. int len, total_len, i;
  32562. (void) sk;
  32563. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32564. /* illegal string */
  32565. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32566. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32567. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32568. SSL_FILETYPE_PEM,NULL), 0);
  32569. /* free store */
  32570. X509_STORE_free(str);
  32571. /* short folder string */
  32572. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32573. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32574. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32575. SSL_FILETYPE_PEM,NULL), 1);
  32576. #if defined(WOLFSSL_INT_H)
  32577. /* only available when including internal.h */
  32578. AssertNotNull(sk = lookup->dirs->dir_entry);
  32579. #endif
  32580. /* free store */
  32581. X509_STORE_free(str);
  32582. /* typical function check */
  32583. p = &CertCrl_path[0];
  32584. total_len = 0;
  32585. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32586. len = (int)XSTRLEN((const char*)&paths[i]);
  32587. total_len += len;
  32588. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32589. p += len;
  32590. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32591. }
  32592. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32593. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32594. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  32595. SSL_FILETYPE_PEM,NULL), 1);
  32596. #if defined(WOLFSSL_INT_H)
  32597. /* only available when including internal.h */
  32598. AssertNotNull(sk = lookup->dirs->dir_entry);
  32599. #endif
  32600. X509_STORE_free(str);
  32601. res = TEST_RES_CHECK(1);
  32602. #endif
  32603. return res;
  32604. }
  32605. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32606. {
  32607. int res = TEST_SKIPPED;
  32608. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32609. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32610. defined(WOLFSSL_SIGNER_DER_CERT)
  32611. X509_STORE_CTX* ctx;
  32612. X509_STORE* str;
  32613. X509_LOOKUP* lookup;
  32614. X509* cert1;
  32615. X509* x509Ca;
  32616. X509* x509Svr;
  32617. X509* issuer;
  32618. WOLFSSL_STACK* sk = NULL;
  32619. X509_NAME* caName;
  32620. X509_NAME* issuerName;
  32621. FILE* file1 = NULL;
  32622. int i, cert_count, cmp;
  32623. char der[] = "certs/ca-cert.der";
  32624. #ifdef HAVE_CRL
  32625. char pem[][100] = {
  32626. "./certs/crl/crl.pem",
  32627. "./certs/crl/crl2.pem",
  32628. "./certs/crl/caEccCrl.pem",
  32629. "./certs/crl/eccCliCRL.pem",
  32630. "./certs/crl/eccSrvCRL.pem",
  32631. ""
  32632. };
  32633. #endif
  32634. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  32635. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32636. fclose(file1);
  32637. AssertNotNull(ctx = X509_STORE_CTX_new());
  32638. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32639. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32640. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32641. SSL_FILETYPE_PEM,NULL), 1);
  32642. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32643. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  32644. /* check if CA cert is loaded into the store */
  32645. for (i = 0; i < cert_count; i++) {
  32646. x509Ca = sk_X509_value(sk, i);
  32647. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32648. }
  32649. AssertNotNull((x509Svr =
  32650. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32651. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32652. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32653. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32654. AssertNotNull(issuer);
  32655. caName = X509_get_subject_name(x509Ca);
  32656. AssertNotNull(caName);
  32657. issuerName = X509_get_subject_name(issuer);
  32658. AssertNotNull(issuerName);
  32659. cmp = X509_NAME_cmp(caName, issuerName);
  32660. AssertIntEQ(cmp, 0);
  32661. /* load der format */
  32662. X509_free(issuer);
  32663. X509_STORE_CTX_free(ctx);
  32664. X509_STORE_free(str);
  32665. sk_X509_pop_free(sk, NULL);
  32666. X509_free(x509Svr);
  32667. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32668. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32669. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32670. SSL_FILETYPE_ASN1,NULL), 1);
  32671. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32672. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  32673. /* check if CA cert is loaded into the store */
  32674. for (i = 0; i < cert_count; i++) {
  32675. x509Ca = sk_X509_value(sk, i);
  32676. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32677. }
  32678. X509_STORE_free(str);
  32679. sk_X509_pop_free(sk, NULL);
  32680. X509_free(cert1);
  32681. #ifdef HAVE_CRL
  32682. AssertNotNull(str = wolfSSL_X509_STORE_new());
  32683. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32684. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32685. SSL_FILETYPE_PEM,NULL), 1);
  32686. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32687. "certs/server-revoked-cert.pem",
  32688. SSL_FILETYPE_PEM,NULL), 1);
  32689. if (str) {
  32690. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32691. WOLFSSL_FILETYPE_PEM), 1);
  32692. /* since store hasn't yet known the revoked cert*/
  32693. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32694. "certs/server-revoked-cert.pem",
  32695. WOLFSSL_FILETYPE_PEM), 1);
  32696. }
  32697. for (i = 0; pem[i][0] != '\0'; i++)
  32698. {
  32699. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32700. SSL_FILETYPE_PEM, NULL), 1);
  32701. }
  32702. if (str) {
  32703. /* since store knows crl list */
  32704. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32705. "certs/server-revoked-cert.pem",
  32706. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32707. }
  32708. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32709. X509_STORE_free(str);
  32710. #endif
  32711. res = TEST_RES_CHECK(1);
  32712. #endif
  32713. return res;
  32714. }
  32715. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32716. {
  32717. int res = TEST_SKIPPED;
  32718. #if defined(OPENSSL_EXTRA)
  32719. X509_STORE_CTX_cleanup(NULL);
  32720. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32721. AssertTrue(1); /* to confirm previous call gives no harm */
  32722. res = TEST_RES_CHECK(1);
  32723. #endif
  32724. return res;
  32725. }
  32726. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32727. {
  32728. int res = TEST_SKIPPED;
  32729. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32730. #ifdef WOLFSSL_SIGNER_DER_CERT
  32731. int cmp;
  32732. #endif
  32733. X509_STORE_CTX* ctx;
  32734. X509_STORE* str;
  32735. X509* x509Ca;
  32736. X509* x509Svr;
  32737. X509* issuer;
  32738. X509_NAME* caName;
  32739. X509_NAME* issuerName;
  32740. AssertNotNull(ctx = X509_STORE_CTX_new());
  32741. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32742. AssertNotNull((x509Ca =
  32743. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32744. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32745. AssertNotNull((x509Svr =
  32746. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32747. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32748. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32749. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32750. AssertNotNull(issuer);
  32751. caName = X509_get_subject_name(x509Ca);
  32752. AssertNotNull(caName);
  32753. issuerName = X509_get_subject_name(issuer);
  32754. AssertNotNull(issuerName);
  32755. #ifdef WOLFSSL_SIGNER_DER_CERT
  32756. cmp = X509_NAME_cmp(caName, issuerName);
  32757. AssertIntEQ(cmp, 0);
  32758. #endif
  32759. X509_free(issuer);
  32760. X509_STORE_CTX_free(ctx);
  32761. X509_free(x509Svr);
  32762. X509_STORE_free(str);
  32763. X509_free(x509Ca);
  32764. res = TEST_RES_CHECK(1);
  32765. #endif
  32766. return res;
  32767. }
  32768. static int test_wolfSSL_PKCS7_certs(void)
  32769. {
  32770. int res = TEST_SKIPPED;
  32771. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32772. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32773. STACK_OF(X509)* sk = NULL;
  32774. STACK_OF(X509_INFO)* info_sk = NULL;
  32775. PKCS7 *p7 = NULL;
  32776. BIO* bio;
  32777. const byte* p = NULL;
  32778. int buflen = 0;
  32779. int i;
  32780. /* Test twice. Once with d2i and once without to test
  32781. * that everything is free'd correctly. */
  32782. for (i = 0; i < 2; i++) {
  32783. AssertNotNull(p7 = PKCS7_new());
  32784. p7->version = 1;
  32785. #ifdef NO_SHA
  32786. p7->hashOID = SHA256h;
  32787. #else
  32788. p7->hashOID = SHAh;
  32789. #endif
  32790. AssertNotNull(bio = BIO_new(BIO_s_file()));
  32791. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32792. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32793. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  32794. AssertNotNull(sk = sk_X509_new_null());
  32795. while (sk_X509_INFO_num(info_sk)) {
  32796. X509_INFO* info;
  32797. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  32798. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  32799. info->x509 = NULL;
  32800. X509_INFO_free(info);
  32801. }
  32802. sk_X509_INFO_free(info_sk);
  32803. BIO_free(bio);
  32804. bio = BIO_new(BIO_s_mem());
  32805. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32806. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32807. if (i == 0) {
  32808. PKCS7_free(p7);
  32809. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  32810. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  32811. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32812. /* PKCS7_free free's the certs */
  32813. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  32814. }
  32815. BIO_free(bio);
  32816. PKCS7_free(p7);
  32817. }
  32818. res = TEST_RES_CHECK(1);
  32819. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32820. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32821. return res;
  32822. }
  32823. static int test_wolfSSL_X509_STORE_CTX(void)
  32824. {
  32825. int res = TEST_SKIPPED;
  32826. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32827. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32828. X509_STORE_CTX* ctx;
  32829. X509_STORE* str;
  32830. X509* x509;
  32831. #ifdef OPENSSL_ALL
  32832. X509* x5092;
  32833. STACK_OF(X509) *sk, *sk2, *sk3;
  32834. #endif
  32835. AssertNotNull(ctx = X509_STORE_CTX_new());
  32836. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32837. AssertNotNull((x509 =
  32838. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32839. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32840. #ifdef OPENSSL_ALL
  32841. /* sk_X509_new only in OPENSSL_ALL */
  32842. sk = sk_X509_new_null();
  32843. AssertNotNull(sk);
  32844. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32845. #else
  32846. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32847. #endif
  32848. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32849. X509_STORE_CTX_set_error(ctx, -5);
  32850. X509_STORE_CTX_set_error(NULL, -5);
  32851. X509_STORE_CTX_free(ctx);
  32852. #ifdef OPENSSL_ALL
  32853. sk_X509_pop_free(sk, NULL);
  32854. #endif
  32855. X509_STORE_free(str);
  32856. X509_free(x509);
  32857. AssertNotNull(ctx = X509_STORE_CTX_new());
  32858. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32859. X509_STORE_CTX_free(ctx);
  32860. #ifdef OPENSSL_ALL
  32861. /* test X509_STORE_CTX_get(1)_chain */
  32862. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32863. SSL_FILETYPE_PEM)));
  32864. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32865. SSL_FILETYPE_PEM)));
  32866. AssertNotNull((sk = sk_X509_new_null()));
  32867. AssertIntEQ(sk_X509_push(sk, x509), 1);
  32868. AssertNotNull((str = X509_STORE_new()));
  32869. AssertNotNull((ctx = X509_STORE_CTX_new()));
  32870. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32871. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32872. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32873. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  32874. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  32875. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  32876. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  32877. X509_STORE_CTX_free(ctx);
  32878. X509_STORE_free(str);
  32879. /* CTX certs not freed yet */
  32880. X509_free(x5092);
  32881. sk_X509_pop_free(sk, NULL);
  32882. /* sk3 is dup so free here */
  32883. sk_X509_pop_free(sk3, NULL);
  32884. #endif
  32885. /* test X509_STORE_CTX_get/set_ex_data */
  32886. {
  32887. int i = 0, tmpData = 5;
  32888. void* tmpDataRet;
  32889. AssertNotNull(ctx = X509_STORE_CTX_new());
  32890. #ifdef HAVE_EX_DATA
  32891. for (i = 0; i < MAX_EX_DATA; i++) {
  32892. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32893. WOLFSSL_SUCCESS);
  32894. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32895. AssertNotNull(tmpDataRet);
  32896. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  32897. }
  32898. #else
  32899. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32900. WOLFSSL_FAILURE);
  32901. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32902. AssertNull(tmpDataRet);
  32903. #endif
  32904. X509_STORE_CTX_free(ctx);
  32905. }
  32906. /* test X509_STORE_get/set_ex_data */
  32907. {
  32908. int i = 0, tmpData = 99;
  32909. void* tmpDataRet;
  32910. AssertNotNull(str = X509_STORE_new());
  32911. #ifdef HAVE_EX_DATA
  32912. for (i = 0; i < MAX_EX_DATA; i++) {
  32913. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32914. WOLFSSL_SUCCESS);
  32915. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32916. AssertNotNull(tmpDataRet);
  32917. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  32918. }
  32919. #else
  32920. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32921. WOLFSSL_FAILURE);
  32922. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32923. AssertNull(tmpDataRet);
  32924. #endif
  32925. X509_STORE_free(str);
  32926. }
  32927. res = TEST_RES_CHECK(1);
  32928. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32929. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32930. return res;
  32931. }
  32932. static int test_wolfSSL_X509_STORE_set_flags(void)
  32933. {
  32934. int res = TEST_SKIPPED;
  32935. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32936. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32937. X509_STORE* store;
  32938. X509* x509;
  32939. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  32940. AssertNotNull((x509 =
  32941. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  32942. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  32943. #ifdef HAVE_CRL
  32944. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  32945. #else
  32946. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  32947. NOT_COMPILED_IN);
  32948. #endif
  32949. wolfSSL_X509_free(x509);
  32950. wolfSSL_X509_STORE_free(store);
  32951. res = TEST_RES_CHECK(1);
  32952. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32953. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32954. return res;
  32955. }
  32956. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  32957. {
  32958. int res = TEST_SKIPPED;
  32959. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  32960. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32961. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  32962. WOLFSSL_X509_STORE* store;
  32963. WOLFSSL_X509_LOOKUP* lookup;
  32964. AssertNotNull(store = wolfSSL_X509_STORE_new());
  32965. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  32966. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  32967. X509_FILETYPE_PEM), 1);
  32968. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  32969. X509_FILETYPE_PEM), 1);
  32970. if (store) {
  32971. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  32972. WOLFSSL_FILETYPE_PEM), 1);
  32973. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  32974. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  32975. }
  32976. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  32977. X509_FILETYPE_PEM), 1);
  32978. if (store) {
  32979. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  32980. WOLFSSL_FILETYPE_PEM), 1);
  32981. }
  32982. wolfSSL_X509_STORE_free(store);
  32983. res = TEST_RES_CHECK(1);
  32984. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  32985. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32986. return res;
  32987. }
  32988. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  32989. {
  32990. int res = TEST_SKIPPED;
  32991. #if defined(OPENSSL_EXTRA)
  32992. WOLFSSL_X509_STORE_CTX* ctx;
  32993. time_t c_time;
  32994. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  32995. c_time = 365*24*60*60;
  32996. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  32997. AssertTrue(
  32998. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  32999. AssertTrue(ctx->param->check_time == c_time);
  33000. wolfSSL_X509_STORE_CTX_free(ctx);
  33001. res = TEST_RES_CHECK(1);
  33002. #endif /* OPENSSL_EXTRA */
  33003. return res;
  33004. }
  33005. static int test_wolfSSL_CTX_get0_set1_param(void)
  33006. {
  33007. int res = TEST_SKIPPED;
  33008. #if defined(OPENSSL_EXTRA)
  33009. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33010. int ret;
  33011. SSL_CTX* ctx;
  33012. WOLFSSL_X509_VERIFY_PARAM* pParam;
  33013. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  33014. char testIPv4[] = "127.0.0.1";
  33015. char testhostName[] = "foo.hoge.com";
  33016. #ifndef NO_WOLFSSL_SERVER
  33017. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33018. #else
  33019. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33020. #endif
  33021. AssertNull(SSL_CTX_get0_param(NULL));
  33022. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  33023. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33024. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  33025. AssertNotNull(pvpm);
  33026. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33027. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33028. (int)XSTRLEN(testhostName));
  33029. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  33030. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33031. ret = SSL_CTX_set1_param(ctx, pvpm);
  33032. AssertIntEQ(1, ret);
  33033. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33034. (int)XSTRLEN(testhostName)));
  33035. AssertIntEQ(0x01, pParam->hostFlags);
  33036. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33037. /* test for incorrect patameter */
  33038. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33039. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33040. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33041. SSL_CTX_free(ctx);
  33042. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33043. res = TEST_RES_CHECK(1);
  33044. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33045. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33046. return res;
  33047. }
  33048. static int test_wolfSSL_get0_param(void)
  33049. {
  33050. int res = TEST_SKIPPED;
  33051. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33052. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33053. SSL_CTX* ctx;
  33054. SSL* ssl;
  33055. WOLFSSL_X509_VERIFY_PARAM* pParam;
  33056. #ifndef NO_WOLFSSL_SERVER
  33057. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33058. #else
  33059. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33060. #endif
  33061. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33062. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33063. AssertNotNull(ssl = SSL_new(ctx));
  33064. pParam = SSL_get0_param(ssl);
  33065. (void)pParam;
  33066. SSL_free(ssl);
  33067. SSL_CTX_free(ctx);
  33068. res = TEST_RES_CHECK(1);
  33069. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33070. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33071. return res;
  33072. }
  33073. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33074. {
  33075. int res = TEST_SKIPPED;
  33076. #if defined(OPENSSL_EXTRA)
  33077. const char host[] = "www.example.com";
  33078. WOLFSSL_X509_VERIFY_PARAM* pParam;
  33079. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33080. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  33081. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33082. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33083. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33084. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33085. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33086. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33087. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33088. res = TEST_RES_CHECK(1);
  33089. #endif /* OPENSSL_EXTRA */
  33090. return res;
  33091. }
  33092. static int test_wolfSSL_set1_host(void)
  33093. {
  33094. int res = TEST_SKIPPED;
  33095. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33096. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33097. const char host[] = "www.test_wolfSSL_set1_host.com";
  33098. const char emptyStr[] = "";
  33099. SSL_CTX* ctx;
  33100. SSL* ssl;
  33101. WOLFSSL_X509_VERIFY_PARAM* pParam;
  33102. #ifndef NO_WOLFSSL_SERVER
  33103. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33104. #else
  33105. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33106. #endif
  33107. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33108. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33109. AssertNotNull(ssl = SSL_new(ctx));
  33110. pParam = SSL_get0_param(ssl);
  33111. /* we should get back host string */
  33112. SSL_set1_host(ssl, host);
  33113. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33114. /* we should get back empty string */
  33115. SSL_set1_host(ssl, emptyStr);
  33116. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33117. /* we should get back host string */
  33118. SSL_set1_host(ssl, host);
  33119. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33120. /* we should get back empty string */
  33121. SSL_set1_host(ssl, NULL);
  33122. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33123. SSL_free(ssl);
  33124. SSL_CTX_free(ctx);
  33125. res = TEST_RES_CHECK(1);
  33126. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33127. #endif /* OPENSSL_EXTRA */
  33128. return res;
  33129. }
  33130. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33131. {
  33132. int res = TEST_SKIPPED;
  33133. #if defined(OPENSSL_EXTRA)
  33134. unsigned char buf[16] = {0};
  33135. WOLFSSL_X509_VERIFY_PARAM* param;
  33136. AssertNotNull(param = X509_VERIFY_PARAM_new());
  33137. /* test 127.0.0.1 */
  33138. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33139. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33140. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33141. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33142. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33143. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33144. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33145. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33146. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33147. AssertIntEQ(XSTRNCMP(param->ipasc,
  33148. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33149. /* test 2001:db8:: */
  33150. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33151. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33152. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33153. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33154. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33155. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33156. /* test ::1234:5678 */
  33157. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33158. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33159. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33160. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33161. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33162. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33163. /* test 2001:db8::1234:5678 */
  33164. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33165. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33166. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33167. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33168. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33169. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33170. sizeof(param->ipasc)), 0);
  33171. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33172. /* 2001:db8:1::ab9:c0a8:102 */
  33173. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33174. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33175. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33176. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33177. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33178. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33179. sizeof(param->ipasc)), 0);
  33180. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33181. res = TEST_RES_CHECK(1);
  33182. #endif /* OPENSSL_EXTRA */
  33183. return res;
  33184. }
  33185. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33186. {
  33187. int res = TEST_SKIPPED;
  33188. #if defined(OPENSSL_EXTRA)
  33189. X509_STORE* store;
  33190. X509_STORE_CTX* ctx;
  33191. X509_STORE_CTX* ctx_no_init;
  33192. AssertNotNull((store = X509_STORE_new()));
  33193. AssertNotNull(ctx = X509_STORE_CTX_new());
  33194. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  33195. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33196. AssertNull(X509_STORE_CTX_get0_store(NULL));
  33197. /* should return NULL if ctx has not bee initialized */
  33198. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33199. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  33200. wolfSSL_X509_STORE_CTX_free(ctx);
  33201. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33202. X509_STORE_free(store);
  33203. res = TEST_RES_CHECK(1);
  33204. #endif /* OPENSSL_EXTRA */
  33205. return res;
  33206. }
  33207. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33208. {
  33209. int res = TEST_SKIPPED;
  33210. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33211. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33212. WOLFSSL_CTX* ctx;
  33213. WOLFSSL* ssl;
  33214. X509_NAME* name = NULL;
  33215. STACK_OF(X509_NAME)* names = NULL;
  33216. STACK_OF(X509_NAME)* ca_list = NULL;
  33217. int i, names_len;
  33218. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33219. /* Send two X501 names in cert request */
  33220. names = SSL_load_client_CA_file(cliCertFile);
  33221. AssertNotNull(names);
  33222. ca_list = SSL_load_client_CA_file(caCertFile);
  33223. AssertNotNull(ca_list);
  33224. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  33225. SSL_CTX_set_client_CA_list(ctx, names);
  33226. /* This should only free the stack structure */
  33227. sk_X509_NAME_free(ca_list);
  33228. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33229. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33230. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33231. for (i=0; i<names_len; i++) {
  33232. AssertNotNull(name = sk_X509_NAME_value(names, i));
  33233. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  33234. }
  33235. /* Needed to be able to create ssl object */
  33236. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33237. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33238. AssertNotNull(ssl = wolfSSL_new(ctx));
  33239. /* load again as old names are responsibility of ctx to free*/
  33240. names = SSL_load_client_CA_file(cliCertFile);
  33241. AssertNotNull(names);
  33242. SSL_set_client_CA_list(ssl, names);
  33243. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33244. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33245. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33246. for (i=0; i<names_len; i++) {
  33247. AssertNotNull(name = sk_X509_NAME_value(names, i));
  33248. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  33249. }
  33250. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33251. {
  33252. tcp_ready ready;
  33253. func_args server_args;
  33254. callback_functions server_cb;
  33255. THREAD_TYPE serverThread;
  33256. WOLFSSL* ssl_client;
  33257. WOLFSSL_CTX* ctx_client;
  33258. SOCKET_T sockfd = 0;
  33259. /* wolfSSL_get_client_CA_list() with handshake */
  33260. StartTCP();
  33261. InitTcpReady(&ready);
  33262. XMEMSET(&server_args, 0, sizeof(func_args));
  33263. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33264. server_args.signal = &ready;
  33265. server_args.callbacks = &server_cb;
  33266. /* we are responsible for free'ing WOLFSSL_CTX */
  33267. server_cb.ctx = ctx;
  33268. server_cb.isSharedCtx = 1;
  33269. AssertIntEQ(WOLFSSL_SUCCESS,
  33270. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33271. start_thread(test_server_nofail, &server_args, &serverThread);
  33272. wait_tcp_ready(&server_args);
  33273. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33274. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33275. AssertIntEQ(WOLFSSL_SUCCESS,
  33276. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  33277. AssertIntEQ(WOLFSSL_SUCCESS,
  33278. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33279. AssertIntEQ(WOLFSSL_SUCCESS,
  33280. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33281. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  33282. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33283. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33284. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33285. /* We are expecting two cert names to be sent */
  33286. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  33287. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33288. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33289. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  33290. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  33291. }
  33292. wolfSSL_shutdown(ssl_client);
  33293. wolfSSL_free(ssl_client);
  33294. wolfSSL_CTX_free(ctx_client);
  33295. join_thread(serverThread);
  33296. FreeTcpReady(&ready);
  33297. }
  33298. #endif
  33299. wolfSSL_free(ssl);
  33300. wolfSSL_CTX_free(ctx);
  33301. res = TEST_RES_CHECK(1);
  33302. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33303. * !NO_BIO */
  33304. return res;
  33305. }
  33306. static int test_wolfSSL_CTX_add_client_CA(void)
  33307. {
  33308. int res = TEST_SKIPPED;
  33309. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33310. !defined(NO_WOLFSSL_CLIENT)
  33311. WOLFSSL_CTX* ctx;
  33312. WOLFSSL_X509* x509;
  33313. WOLFSSL_X509* x509_a;
  33314. STACK_OF(X509_NAME)* ca_list;
  33315. int ret = 0;
  33316. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33317. /* Add client cert */
  33318. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  33319. AssertNotNull(x509);
  33320. ret = SSL_CTX_add_client_CA(ctx, x509);
  33321. AssertIntEQ(ret, SSL_SUCCESS);
  33322. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33323. /* Add another client cert */
  33324. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33325. SSL_FILETYPE_PEM));
  33326. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33327. /* test for incorrect parameter */
  33328. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33329. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33330. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33331. X509_free(x509);
  33332. X509_free(x509_a);
  33333. SSL_CTX_free(ctx);
  33334. res = TEST_RES_CHECK(1);
  33335. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33336. return res;
  33337. }
  33338. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  33339. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33340. {
  33341. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33342. WOLFSSL_CTX* ctx = callbacks->ctx;
  33343. WOLFSSL* ssl = NULL;
  33344. SOCKET_T sfd = 0;
  33345. SOCKET_T cfd = 0;
  33346. word16 port;
  33347. char input[1024];
  33348. int idx;
  33349. int ret, err = 0;
  33350. const char* privateName = "ech-private-name.com";
  33351. int privateNameLen = (int)XSTRLEN(privateName);
  33352. ((func_args*)args)->return_code = TEST_FAIL;
  33353. port = ((func_args*)args)->signal->port;
  33354. AssertIntEQ(WOLFSSL_SUCCESS,
  33355. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33356. AssertIntEQ(WOLFSSL_SUCCESS,
  33357. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33358. WOLFSSL_FILETYPE_PEM));
  33359. AssertIntEQ(WOLFSSL_SUCCESS,
  33360. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33361. WOLFSSL_FILETYPE_PEM));
  33362. if (callbacks->ctx_ready)
  33363. callbacks->ctx_ready(ctx);
  33364. ssl = wolfSSL_new(ctx);
  33365. /* set the sni for the server */
  33366. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33367. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33368. CloseSocket(sfd);
  33369. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33370. if (callbacks->ssl_ready)
  33371. callbacks->ssl_ready(ssl);
  33372. do {
  33373. err = 0; /* Reset error */
  33374. ret = wolfSSL_accept(ssl);
  33375. if (ret != WOLFSSL_SUCCESS) {
  33376. err = wolfSSL_get_error(ssl, 0);
  33377. }
  33378. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33379. if (ret != WOLFSSL_SUCCESS) {
  33380. char buff[WOLFSSL_MAX_ERROR_SZ];
  33381. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33382. }
  33383. else {
  33384. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33385. input[idx] = 0;
  33386. printf("Client message: %s\n", input);
  33387. }
  33388. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33389. privateNameLen));
  33390. ((func_args*)args)->return_code = TEST_SUCCESS;
  33391. }
  33392. if (callbacks->on_result)
  33393. callbacks->on_result(ssl);
  33394. wolfSSL_shutdown(ssl);
  33395. wolfSSL_free(ssl);
  33396. wolfSSL_CTX_free(ctx);
  33397. CloseSocket(cfd);
  33398. #ifdef FP_ECC
  33399. wc_ecc_fp_free();
  33400. #endif
  33401. return 0;
  33402. }
  33403. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33404. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33405. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  33406. {
  33407. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33408. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  33409. WOLFSSL* ssl = NULL;
  33410. SOCKET_T sfd = 0;
  33411. SOCKET_T cfd = 0;
  33412. word16 port;
  33413. char msg[] = "I hear you fa shizzle!";
  33414. int len = (int) XSTRLEN(msg);
  33415. char input[1024];
  33416. int idx;
  33417. int ret, err = 0;
  33418. #ifdef WOLFSSL_TIRTOS
  33419. fdOpenSession(Task_self());
  33420. #endif
  33421. ((func_args*)args)->return_code = TEST_FAIL;
  33422. port = ((func_args*)args)->signal->port;
  33423. AssertIntEQ(WOLFSSL_SUCCESS,
  33424. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33425. AssertIntEQ(WOLFSSL_SUCCESS,
  33426. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33427. WOLFSSL_FILETYPE_PEM));
  33428. AssertIntEQ(WOLFSSL_SUCCESS,
  33429. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33430. WOLFSSL_FILETYPE_PEM));
  33431. if (callbacks->ctx_ready)
  33432. callbacks->ctx_ready(ctx);
  33433. ssl = wolfSSL_new(ctx);
  33434. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33435. CloseSocket(sfd);
  33436. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33437. if (callbacks->ssl_ready)
  33438. callbacks->ssl_ready(ssl);
  33439. do {
  33440. err = 0; /* Reset error */
  33441. ret = wolfSSL_accept(ssl);
  33442. if (ret != WOLFSSL_SUCCESS) {
  33443. err = wolfSSL_get_error(ssl, 0);
  33444. }
  33445. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33446. if (ret != WOLFSSL_SUCCESS) {
  33447. char buff[WOLFSSL_MAX_ERROR_SZ];
  33448. fprintf(stderr, "error = %d, %s\n", err,
  33449. wolfSSL_ERR_error_string(err, buff));
  33450. }
  33451. else {
  33452. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33453. input[idx] = 0;
  33454. fprintf(stderr, "Client message: %s\n", input);
  33455. }
  33456. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  33457. #ifdef WOLFSSL_TIRTOS
  33458. Task_yield();
  33459. #endif
  33460. ((func_args*)args)->return_code = TEST_SUCCESS;
  33461. }
  33462. if (callbacks->on_result)
  33463. callbacks->on_result(ssl);
  33464. wolfSSL_shutdown(ssl);
  33465. wolfSSL_free(ssl);
  33466. wolfSSL_CTX_free(ctx);
  33467. CloseSocket(cfd);
  33468. #ifdef WOLFSSL_TIRTOS
  33469. fdCloseSession(Task_self());
  33470. #endif
  33471. #ifndef WOLFSSL_TIRTOS
  33472. return 0;
  33473. #endif
  33474. }
  33475. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33476. {
  33477. AssertNotNull(ssl);
  33478. AssertNotNull(line);
  33479. XFILE fp;
  33480. const byte lf = '\n';
  33481. fp = XFOPEN("./MyKeyLog.txt", "a");
  33482. XFWRITE( line, 1, strlen(line),fp);
  33483. XFWRITE( (void*)&lf,1,1,fp);
  33484. XFCLOSE(fp);
  33485. }
  33486. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33487. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33488. {
  33489. int res = TEST_SKIPPED;
  33490. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33491. !defined(NO_WOLFSSL_CLIENT)
  33492. SSL_CTX* ctx;
  33493. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33494. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33495. SSL_CTX_free(ctx);
  33496. SSL_CTX_set_keylog_callback(NULL, NULL);
  33497. res = TEST_RES_CHECK(1);
  33498. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33499. return res;
  33500. }
  33501. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33502. {
  33503. int res = TEST_SKIPPED;
  33504. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33505. !defined(NO_WOLFSSL_CLIENT)
  33506. SSL_CTX* ctx;
  33507. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33508. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33509. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33510. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33511. SSL_CTX_set_keylog_callback(ctx, NULL );
  33512. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33513. SSL_CTX_free(ctx);
  33514. res = TEST_RES_CHECK(1);
  33515. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33516. return res;
  33517. }
  33518. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33519. {
  33520. int res = TEST_SKIPPED;
  33521. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33522. /* This test is intended for checking whether keylog callback is called
  33523. * in client during TLS handshake between the client and a server.
  33524. */
  33525. tcp_ready ready;
  33526. func_args client_args;
  33527. func_args server_args;
  33528. THREAD_TYPE serverThread;
  33529. callback_functions server_cbf;
  33530. callback_functions client_cbf;
  33531. SOCKET_T sockfd = 0;
  33532. WOLFSSL_CTX* ctx;
  33533. WOLFSSL* ssl;
  33534. XFILE fp;
  33535. char msg[64] = "hello wolfssl!";
  33536. char reply[1024];
  33537. int msgSz = (int)XSTRLEN(msg);
  33538. #ifdef WOLFSSL_TIRTOS
  33539. fdOpenSession(Task_self());
  33540. #endif
  33541. InitTcpReady(&ready);
  33542. ready.port = 22222;
  33543. XMEMSET(&client_args, 0, sizeof(func_args));
  33544. XMEMSET(&server_args, 0, sizeof(func_args));
  33545. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33546. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33547. server_cbf.method = wolfTLSv1_2_server_method;
  33548. server_args.callbacks = &server_cbf;
  33549. server_args.signal = &ready;
  33550. /* clean up keylog file */
  33551. fp = XFOPEN("./MyKeyLog.txt", "w");
  33552. XFCLOSE(fp);
  33553. /* start server task */
  33554. start_thread(server_task, &server_args, &serverThread);
  33555. wait_tcp_ready(&server_args);
  33556. /* run as a TLS1.2 client */
  33557. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33558. AssertIntEQ(WOLFSSL_SUCCESS,
  33559. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33560. AssertIntEQ(WOLFSSL_SUCCESS,
  33561. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33562. AssertIntEQ(WOLFSSL_SUCCESS,
  33563. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33564. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33565. /* set keylog callback */
  33566. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  33567. /* get connected the server task */
  33568. AssertNotNull(ssl = wolfSSL_new(ctx));
  33569. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33570. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33571. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  33572. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  33573. wolfSSL_shutdown(ssl);
  33574. wolfSSL_free(ssl);
  33575. wolfSSL_CTX_free(ctx);
  33576. CloseSocket(sockfd);
  33577. join_thread(serverThread);
  33578. FreeTcpReady(&ready);
  33579. #ifdef WOLFSSL_TIRTOS
  33580. fdOpenSession(Task_self());
  33581. #endif
  33582. /* check if the keylog file exists */
  33583. char buff[300] = {0};
  33584. int found = 0;
  33585. fp = XFOPEN("./MyKeyLog.txt", "r");
  33586. AssertNotNull(fp);
  33587. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  33588. if (0 == strncmp(buff,"CLIENT_RANDOM ",
  33589. sizeof("CLIENT_RANDOM ")-1)) {
  33590. found = 1;
  33591. break;
  33592. }
  33593. }
  33594. XFCLOSE(fp);
  33595. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33596. AssertNotNull( found );
  33597. res = TEST_RES_CHECK(1);
  33598. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33599. return res;
  33600. }
  33601. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33602. {
  33603. int res = TEST_SKIPPED;
  33604. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33605. defined(HAVE_SECRET_CALLBACK)
  33606. /* This test is intended for checking whether keylog callback is called
  33607. * in client during TLS handshake between the client and a server.
  33608. */
  33609. tcp_ready ready;
  33610. func_args client_args;
  33611. func_args server_args;
  33612. THREAD_TYPE serverThread;
  33613. callback_functions server_cbf;
  33614. callback_functions client_cbf;
  33615. SOCKET_T sockfd = 0;
  33616. WOLFSSL_CTX* ctx;
  33617. WOLFSSL* ssl;
  33618. XFILE fp;
  33619. char msg[64] = "hello wolfssl!";
  33620. char reply[1024];
  33621. int msgSz = (int)XSTRLEN(msg);
  33622. #ifdef WOLFSSL_TIRTOS
  33623. fdOpenSession(Task_self());
  33624. #endif
  33625. InitTcpReady(&ready);
  33626. ready.port = 22222;
  33627. XMEMSET(&client_args, 0, sizeof(func_args));
  33628. XMEMSET(&server_args, 0, sizeof(func_args));
  33629. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33630. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33631. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33632. server_args.callbacks = &server_cbf;
  33633. server_args.signal = &ready;
  33634. /* clean up keylog file */
  33635. fp = XFOPEN("./MyKeyLog.txt", "w");
  33636. XFCLOSE(fp);
  33637. /* start server task */
  33638. start_thread(server_task, &server_args, &serverThread);
  33639. wait_tcp_ready(&server_args);
  33640. /* run as a TLS1.3 client */
  33641. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33642. AssertIntEQ(WOLFSSL_SUCCESS,
  33643. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33644. AssertIntEQ(WOLFSSL_SUCCESS,
  33645. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33646. AssertIntEQ(WOLFSSL_SUCCESS,
  33647. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33648. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33649. /* set keylog callback */
  33650. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  33651. /* get connected the server task */
  33652. AssertNotNull(ssl = wolfSSL_new(ctx));
  33653. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33654. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33655. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  33656. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  33657. wolfSSL_free(ssl);
  33658. wolfSSL_CTX_free(ctx);
  33659. join_thread(serverThread);
  33660. FreeTcpReady(&ready);
  33661. #ifdef WOLFSSL_TIRTOS
  33662. fdOpenSession(Task_self());
  33663. #endif
  33664. /* check if the keylog file exists */
  33665. {
  33666. char buff[300] = {0};
  33667. int found[4] = {0};
  33668. int numfnd = 0;
  33669. int i;
  33670. fp = XFOPEN("./MyKeyLog.txt", "r");
  33671. AssertNotNull(fp);
  33672. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  33673. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33674. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33675. found[0] = 1;
  33676. continue;
  33677. }
  33678. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33679. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33680. found[1] = 1;
  33681. continue;
  33682. }
  33683. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  33684. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33685. found[2] = 1;
  33686. continue;
  33687. }
  33688. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  33689. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33690. found[3] = 1;
  33691. continue;
  33692. }
  33693. }
  33694. XFCLOSE(fp);
  33695. for (i = 0; i < 4; i++) {
  33696. if (found[i] != 0)
  33697. numfnd++;
  33698. }
  33699. AssertIntEQ(numfnd, 4);
  33700. }
  33701. res = TEST_RES_CHECK(1);
  33702. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33703. return res;
  33704. }
  33705. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  33706. static int test_wolfSSL_Tls13_ECH_params(void)
  33707. {
  33708. #if !defined(NO_WOLFSSL_CLIENT)
  33709. word32 outputLen = 0;
  33710. byte testBuf[72];
  33711. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33712. WOLFSSL *ssl = wolfSSL_new(ctx);
  33713. AssertNotNull(ctx);
  33714. AssertNotNull(ssl);
  33715. /* invalid ctx */
  33716. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33717. "ech-public-name.com", 0, 0, 0));
  33718. /* invalid public name */
  33719. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33720. 0, 0));
  33721. /* invalid algorithms */
  33722. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33723. "ech-public-name.com", 1000, 1000, 1000));
  33724. /* invalid ctx */
  33725. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33726. &outputLen));
  33727. /* invalid output len */
  33728. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33729. /* invalid ssl */
  33730. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33731. (char*)testBuf, sizeof(testBuf)));
  33732. /* invalid configs64 */
  33733. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33734. sizeof(testBuf)));
  33735. /* invalid size */
  33736. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33737. (char*)testBuf, 0));
  33738. /* invalid ssl */
  33739. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33740. sizeof(testBuf)));
  33741. /* invalid configs */
  33742. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33743. sizeof(testBuf)));
  33744. /* invalid size */
  33745. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33746. /* invalid ssl */
  33747. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33748. /* invalid size */
  33749. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33750. wolfSSL_free(ssl);
  33751. wolfSSL_CTX_free(ctx);
  33752. #endif /* !NO_WOLFSSL_CLIENT */
  33753. return TEST_SUCCESS;
  33754. }
  33755. static int test_wolfSSL_Tls13_ECH(void)
  33756. {
  33757. tcp_ready ready;
  33758. func_args client_args;
  33759. func_args server_args;
  33760. THREAD_TYPE serverThread;
  33761. callback_functions server_cbf;
  33762. callback_functions client_cbf;
  33763. SOCKET_T sockfd = 0;
  33764. WOLFSSL_CTX* ctx;
  33765. WOLFSSL* ssl;
  33766. const char* publicName = "ech-public-name.com";
  33767. const char* privateName = "ech-private-name.com";
  33768. int privateNameLen = 20;
  33769. char reply[1024];
  33770. int replyLen = 0;
  33771. byte rawEchConfig[128];
  33772. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33773. InitTcpReady(&ready);
  33774. ready.port = 22222;
  33775. XMEMSET(&client_args, 0, sizeof(func_args));
  33776. XMEMSET(&server_args, 0, sizeof(func_args));
  33777. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33778. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33779. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33780. /* create the server context here so we can get the ech config */
  33781. AssertNotNull(server_cbf.ctx =
  33782. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33783. /* generate ech config */
  33784. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33785. publicName, 0, 0, 0));
  33786. /* get the config for the client to use */
  33787. AssertIntEQ(WOLFSSL_SUCCESS,
  33788. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33789. &rawEchConfigLen));
  33790. server_args.callbacks = &server_cbf;
  33791. server_args.signal = &ready;
  33792. /* start server task */
  33793. start_thread(server_task_ech, &server_args, &serverThread);
  33794. wait_tcp_ready(&server_args);
  33795. /* run as a TLS1.3 client */
  33796. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33797. AssertIntEQ(WOLFSSL_SUCCESS,
  33798. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33799. AssertIntEQ(WOLFSSL_SUCCESS,
  33800. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33801. AssertIntEQ(WOLFSSL_SUCCESS,
  33802. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33803. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33804. /* get connected the server task */
  33805. AssertNotNull(ssl = wolfSSL_new(ctx));
  33806. /* set the ech configs for the client */
  33807. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33808. rawEchConfigLen));
  33809. /* set the sni for the client */
  33810. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33811. privateName, privateNameLen));
  33812. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33813. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33814. AssertIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33815. privateNameLen);
  33816. AssertIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33817. /* add th null terminator for string compare */
  33818. reply[replyLen] = 0;
  33819. /* check that the server replied with the private name */
  33820. AssertStrEQ(privateName, reply);
  33821. wolfSSL_free(ssl);
  33822. wolfSSL_CTX_free(ctx);
  33823. join_thread(serverThread);
  33824. FreeTcpReady(&ready);
  33825. return TEST_SUCCESS;
  33826. }
  33827. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33828. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33829. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33830. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33831. static void post_auth_version_cb(WOLFSSL* ssl)
  33832. {
  33833. /* do handshake and then test version error */
  33834. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33835. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33836. }
  33837. static void post_auth_version_client_cb(WOLFSSL* ssl)
  33838. {
  33839. /* do handshake and then test version error */
  33840. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33841. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33842. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33843. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33844. /* check was added to error queue */
  33845. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33846. /* check the string matches expected string */
  33847. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33848. "WRONG_SSL_VERSION");
  33849. #endif
  33850. }
  33851. static void post_auth_cb(WOLFSSL* ssl)
  33852. {
  33853. WOLFSSL_X509* x509;
  33854. /* do handshake and then test version error */
  33855. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33856. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33857. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33858. wolfSSL_X509_free(x509);
  33859. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33860. }
  33861. static void set_post_auth_cb(WOLFSSL* ssl)
  33862. {
  33863. if (!wolfSSL_is_server(ssl)) {
  33864. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33865. }
  33866. else {
  33867. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33868. }
  33869. }
  33870. #endif
  33871. static int test_wolfSSL_Tls13_postauth(void)
  33872. {
  33873. int res = TEST_SKIPPED;
  33874. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33875. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33876. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33877. tcp_ready ready;
  33878. func_args client_args;
  33879. func_args server_args;
  33880. callback_functions server_cbf;
  33881. callback_functions client_cbf;
  33882. THREAD_TYPE serverThread;
  33883. XMEMSET(&client_args, 0, sizeof(func_args));
  33884. XMEMSET(&server_args, 0, sizeof(func_args));
  33885. StartTCP();
  33886. InitTcpReady(&ready);
  33887. #if defined(USE_WINDOWS_API)
  33888. /* use RNG to get random port if using windows */
  33889. ready.port = GetRandomPort();
  33890. #endif
  33891. server_args.signal = &ready;
  33892. client_args.signal = &ready;
  33893. /* test version failure doing post auth with TLS 1.2 connection */
  33894. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33895. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33896. server_cbf.method = wolfTLSv1_2_server_method;
  33897. server_cbf.ssl_ready = set_post_auth_cb;
  33898. server_cbf.on_result = post_auth_version_cb;
  33899. client_cbf.ssl_ready = set_post_auth_cb;
  33900. client_cbf.on_result = post_auth_version_client_cb;
  33901. server_args.callbacks = &server_cbf;
  33902. client_args.callbacks = &client_cbf;
  33903. start_thread(test_server_nofail, &server_args, &serverThread);
  33904. wait_tcp_ready(&server_args);
  33905. test_client_nofail(&client_args, NULL);
  33906. join_thread(serverThread);
  33907. /* tests on post auth with TLS 1.3 */
  33908. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33909. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33910. server_cbf.method = wolfTLSv1_3_server_method;
  33911. server_cbf.ssl_ready = set_post_auth_cb;
  33912. client_cbf.ssl_ready = set_post_auth_cb;
  33913. server_cbf.on_result = post_auth_cb;
  33914. client_cbf.on_result = NULL;
  33915. server_args.callbacks = &server_cbf;
  33916. client_args.callbacks = &client_cbf;
  33917. start_thread(test_server_nofail, &server_args, &serverThread);
  33918. wait_tcp_ready(&server_args);
  33919. test_client_nofail(&client_args, NULL);
  33920. join_thread(serverThread);
  33921. FreeTcpReady(&ready);
  33922. res = TEST_RES_CHECK(1);
  33923. #endif
  33924. return res;
  33925. }
  33926. static int test_wolfSSL_X509_NID(void)
  33927. {
  33928. int res = TEST_SKIPPED;
  33929. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  33930. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  33931. int sigType;
  33932. int nameSz;
  33933. X509* cert;
  33934. EVP_PKEY* pubKeyTmp;
  33935. X509_NAME* name;
  33936. char commonName[80];
  33937. char countryName[80];
  33938. char localityName[80];
  33939. char stateName[80];
  33940. char orgName[80];
  33941. char orgUnit[80];
  33942. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  33943. /* convert cert from DER to internal WOLFSSL_X509 struct */
  33944. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  33945. sizeof_client_cert_der_2048));
  33946. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  33947. /* extract PUBLIC KEY from cert */
  33948. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  33949. /* extract signatureType */
  33950. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  33951. /* extract subjectName info */
  33952. AssertNotNull(name = X509_get_subject_name(cert));
  33953. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  33954. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33955. NULL, 0)), 0);
  33956. AssertIntEQ(nameSz, 15);
  33957. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33958. commonName, sizeof(commonName))), 0);
  33959. AssertIntEQ(nameSz, 15);
  33960. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  33961. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33962. commonName, 9)), 0);
  33963. AssertIntEQ(nameSz, 8);
  33964. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  33965. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  33966. countryName, sizeof(countryName))), 0);
  33967. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  33968. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  33969. localityName, sizeof(localityName))), 0);
  33970. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  33971. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  33972. stateName, sizeof(stateName))), 0);
  33973. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  33974. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  33975. orgName, sizeof(orgName))), 0);
  33976. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  33977. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  33978. orgUnit, sizeof(orgUnit))), 0);
  33979. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  33980. EVP_PKEY_free(pubKeyTmp);
  33981. X509_free(cert);
  33982. res = TEST_RES_CHECK(1);
  33983. #endif
  33984. return res;
  33985. }
  33986. static int test_wolfSSL_CTX_set_srp_username(void)
  33987. {
  33988. int res = TEST_SKIPPED;
  33989. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  33990. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33991. WOLFSSL_CTX* ctx;
  33992. WOLFSSL* ssl;
  33993. const char *username = "TESTUSER";
  33994. const char *password = "TESTPASSWORD";
  33995. int r;
  33996. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  33997. AssertNotNull(ctx);
  33998. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  33999. AssertIntEQ(r,SSL_SUCCESS);
  34000. wolfSSL_CTX_free(ctx);
  34001. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  34002. AssertNotNull(ctx);
  34003. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  34004. AssertIntEQ(r,SSL_SUCCESS);
  34005. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  34006. AssertIntEQ(r,SSL_SUCCESS);
  34007. AssertNotNull(ssl = SSL_new(ctx));
  34008. AssertNotNull(SSL_get_srp_username(ssl));
  34009. AssertStrEQ(SSL_get_srp_username(ssl), username);
  34010. wolfSSL_free(ssl);
  34011. wolfSSL_CTX_free(ctx);
  34012. res = TEST_RES_CHECK(1);
  34013. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34014. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34015. return res;
  34016. }
  34017. static int test_wolfSSL_CTX_set_srp_password(void)
  34018. {
  34019. int res = TEST_SKIPPED;
  34020. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34021. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34022. WOLFSSL_CTX* ctx;
  34023. const char *username = "TESTUSER";
  34024. const char *password = "TESTPASSWORD";
  34025. int r;
  34026. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  34027. AssertNotNull(ctx);
  34028. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  34029. AssertIntEQ(r,SSL_SUCCESS);
  34030. wolfSSL_CTX_free(ctx);
  34031. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  34032. AssertNotNull(ctx);
  34033. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  34034. AssertIntEQ(r,SSL_SUCCESS);
  34035. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  34036. AssertIntEQ(r,SSL_SUCCESS);
  34037. wolfSSL_CTX_free(ctx);
  34038. res = TEST_RES_CHECK(1);
  34039. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34040. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34041. return res;
  34042. }
  34043. static int test_wolfSSL_X509_STORE(void)
  34044. {
  34045. int res = TEST_SKIPPED;
  34046. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34047. X509_STORE *store;
  34048. #ifdef HAVE_CRL
  34049. X509_STORE_CTX *storeCtx;
  34050. X509_CRL *crl;
  34051. X509 *ca, *cert;
  34052. const char crlPem[] = "./certs/crl/crl.revoked";
  34053. const char srvCert[] = "./certs/server-revoked-cert.pem";
  34054. const char caCert[] = "./certs/ca-cert.pem";
  34055. XFILE fp;
  34056. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34057. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34058. SSL_FILETYPE_PEM)));
  34059. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34060. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34061. SSL_FILETYPE_PEM)));
  34062. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  34063. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34064. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34065. X509_STORE_free(store);
  34066. X509_STORE_CTX_free(storeCtx);
  34067. X509_free(cert);
  34068. X509_free(ca);
  34069. /* should fail to verify now after adding in CRL */
  34070. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34071. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34072. SSL_FILETYPE_PEM)));
  34073. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34074. fp = XFOPEN(crlPem, "rb");
  34075. AssertTrue((fp != XBADFILE));
  34076. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34077. NULL, NULL));
  34078. XFCLOSE(fp);
  34079. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34080. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34081. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  34082. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34083. SSL_FILETYPE_PEM)));
  34084. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34085. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34086. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  34087. X509_CRL_free(crl);
  34088. X509_STORE_free(store);
  34089. X509_STORE_CTX_free(storeCtx);
  34090. X509_free(cert);
  34091. X509_free(ca);
  34092. #endif /* HAVE_CRL */
  34093. #ifndef WOLFCRYPT_ONLY
  34094. {
  34095. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34096. SSL_CTX* ctx;
  34097. SSL* ssl;
  34098. int i;
  34099. for (i = 0; i < 2; i++) {
  34100. #ifndef NO_WOLFSSL_SERVER
  34101. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34102. #else
  34103. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34104. #endif
  34105. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34106. SSL_CTX_set_cert_store(ctx, store);
  34107. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34108. SSL_CTX_set_cert_store(ctx, store);
  34109. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34110. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34111. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34112. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34113. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34114. AssertNotNull(ssl = SSL_new(ctx));
  34115. if (i == 0) {
  34116. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  34117. }
  34118. else {
  34119. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34120. X509_STORE_free(store);
  34121. }
  34122. SSL_free(ssl);
  34123. SSL_CTX_free(ctx);
  34124. }
  34125. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34126. }
  34127. #endif
  34128. res = TEST_RES_CHECK(1);
  34129. #endif
  34130. return res;
  34131. }
  34132. static int test_wolfSSL_X509_STORE_load_locations(void)
  34133. {
  34134. int res = TEST_SKIPPED;
  34135. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34136. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34137. SSL_CTX *ctx;
  34138. X509_STORE *store;
  34139. const char ca_file[] = "./certs/ca-cert.pem";
  34140. const char client_pem_file[] = "./certs/client-cert.pem";
  34141. const char client_der_file[] = "./certs/client-cert.der";
  34142. const char ecc_file[] = "./certs/ecc-key.pem";
  34143. const char certs_path[] = "./certs/";
  34144. const char bad_path[] = "./bad-path/";
  34145. #ifdef HAVE_CRL
  34146. const char crl_path[] = "./certs/crl/";
  34147. const char crl_file[] = "./certs/crl/crl.pem";
  34148. #endif
  34149. #ifndef NO_WOLFSSL_SERVER
  34150. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34151. #else
  34152. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34153. #endif
  34154. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  34155. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  34156. /* Test bad arguments */
  34157. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  34158. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34159. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  34160. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  34161. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  34162. #ifdef HAVE_CRL
  34163. /* Test with CRL */
  34164. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  34165. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  34166. #endif
  34167. /* Test with CA */
  34168. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  34169. /* Test with client_cert and certs path */
  34170. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  34171. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  34172. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34173. /* Clear nodes */
  34174. ERR_clear_error();
  34175. #endif
  34176. SSL_CTX_free(ctx);
  34177. res = TEST_RES_CHECK(1);
  34178. #endif
  34179. return res;
  34180. }
  34181. static int test_X509_STORE_get0_objects(void)
  34182. {
  34183. int res = TEST_SKIPPED;
  34184. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34185. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34186. X509_STORE *store;
  34187. X509_STORE *store_cpy;
  34188. SSL_CTX *ctx;
  34189. X509_OBJECT *obj;
  34190. STACK_OF(X509_OBJECT) *objs;
  34191. int i;
  34192. /* Setup store */
  34193. #ifndef NO_WOLFSSL_SERVER
  34194. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34195. #else
  34196. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34197. #endif
  34198. AssertNotNull(store_cpy = X509_STORE_new());
  34199. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  34200. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  34201. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  34202. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  34203. #ifdef HAVE_CRL
  34204. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  34205. #endif
  34206. /* Store ready */
  34207. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34208. AssertNotNull(objs = X509_STORE_get0_objects(store));
  34209. #ifdef HAVE_CRL
  34210. #ifdef WOLFSSL_SIGNER_DER_CERT
  34211. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  34212. #else
  34213. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  34214. #endif
  34215. #else
  34216. #ifdef WOLFSSL_SIGNER_DER_CERT
  34217. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  34218. #else
  34219. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  34220. #endif
  34221. #endif
  34222. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34223. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34224. switch (X509_OBJECT_get_type(obj)) {
  34225. case X509_LU_X509:
  34226. AssertNotNull(X509_OBJECT_get0_X509(obj));
  34227. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  34228. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  34229. break;
  34230. case X509_LU_CRL:
  34231. #ifdef HAVE_CRL
  34232. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  34233. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  34234. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  34235. break;
  34236. #endif
  34237. case X509_LU_NONE:
  34238. default:
  34239. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34240. "(when built with crl support)"),
  34241. ("Unrecognized X509_OBJECT type or none"));
  34242. }
  34243. }
  34244. X509_STORE_free(store_cpy);
  34245. SSL_CTX_free(ctx);
  34246. res = TEST_RES_CHECK(1);
  34247. #endif
  34248. return res;
  34249. }
  34250. static int test_wolfSSL_BN_CTX(void)
  34251. {
  34252. int res = TEST_SKIPPED;
  34253. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34254. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34255. WOLFSSL_BN_CTX* bn_ctx;
  34256. WOLFSSL_BIGNUM* t;
  34257. AssertNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34258. /* No implementation. */
  34259. BN_CTX_init(NULL);
  34260. AssertNotNull(t = BN_CTX_get(NULL));
  34261. BN_free(t);
  34262. AssertNotNull(t = BN_CTX_get(bn_ctx));
  34263. BN_free(t);
  34264. #ifndef NO_WOLFSSL_STUB
  34265. /* No implementation. */
  34266. BN_CTX_start(NULL);
  34267. BN_CTX_start(bn_ctx);
  34268. #endif
  34269. BN_CTX_free(NULL);
  34270. BN_CTX_free(bn_ctx);
  34271. res = TEST_RES_CHECK(1);
  34272. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34273. return res;
  34274. }
  34275. static int test_wolfSSL_BN(void)
  34276. {
  34277. int res = TEST_SKIPPED;
  34278. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34279. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34280. BIGNUM* a;
  34281. BIGNUM* b;
  34282. BIGNUM* c;
  34283. BIGNUM* d;
  34284. BIGNUM emptyBN;
  34285. /* Setup */
  34286. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34287. /* internal not set emptyBN. */
  34288. AssertNotNull(a = BN_new());
  34289. AssertNotNull(b = BN_new());
  34290. AssertNotNull(c = BN_dup(b));
  34291. AssertNotNull(d = BN_new());
  34292. /* Invalid parameter testing. */
  34293. BN_free(NULL);
  34294. AssertNull(BN_dup(NULL));
  34295. AssertNull(BN_dup(&emptyBN));
  34296. AssertNull(BN_copy(NULL, NULL));
  34297. AssertNull(BN_copy(b, NULL));
  34298. AssertNull(BN_copy(NULL, c));
  34299. AssertNull(BN_copy(b, &emptyBN));
  34300. AssertNull(BN_copy(&emptyBN, c));
  34301. BN_clear(NULL);
  34302. BN_clear(&emptyBN);
  34303. AssertIntEQ(BN_num_bytes(NULL), 0);
  34304. AssertIntEQ(BN_num_bytes(&emptyBN), 0);
  34305. AssertIntEQ(BN_num_bits(NULL), 0);
  34306. AssertIntEQ(BN_num_bits(&emptyBN), 0);
  34307. AssertIntEQ(BN_is_negative(NULL), 0);
  34308. AssertIntEQ(BN_is_negative(&emptyBN), 0);
  34309. /* END Invalid Parameters */
  34310. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34311. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34312. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34313. AssertIntEQ(BN_num_bits(a), 2);
  34314. AssertIntEQ(BN_num_bytes(a), 1);
  34315. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34316. defined(WOLFSSL_SP_INT_NEGATIVE))
  34317. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34318. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34319. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34320. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34321. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34322. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34323. {
  34324. /* Do additional tests on negative BN conversions. */
  34325. char * ret;
  34326. ASN1_INTEGER * asn1;
  34327. BIGNUM * tmp;
  34328. /* Sanity check we have a negative BN. */
  34329. AssertIntEQ(BN_is_negative(c), 1);
  34330. AssertNotNull(ret = BN_bn2dec(c));
  34331. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34332. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34333. /* Convert to ASN1_INTEGER and back to BN. */
  34334. AssertNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34335. AssertNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34336. /* After converting back BN should be negative and correct. */
  34337. AssertIntEQ(BN_is_negative(tmp), 1);
  34338. AssertNotNull(ret = BN_bn2dec(tmp));
  34339. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34340. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34341. ASN1_INTEGER_free(asn1);
  34342. BN_free(tmp);
  34343. }
  34344. #endif
  34345. AssertIntEQ(BN_get_word(c), 4);
  34346. #endif
  34347. AssertIntEQ(BN_set_word(a, 3), 1);
  34348. AssertIntEQ(BN_set_word(b, 3), 1);
  34349. AssertIntEQ(BN_set_word(c, 4), 1);
  34350. /* NULL == NULL, NULL < num, num > NULL */
  34351. AssertIntEQ(BN_cmp(NULL, NULL), 0);
  34352. AssertIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34353. AssertIntLT(BN_cmp(NULL, b), 0);
  34354. AssertIntLT(BN_cmp(&emptyBN, b), 0);
  34355. AssertIntGT(BN_cmp(a, NULL), 0);
  34356. AssertIntGT(BN_cmp(a, &emptyBN), 0);
  34357. AssertIntEQ(BN_cmp(a, b), 0);
  34358. AssertIntLT(BN_cmp(a, c), 0);
  34359. AssertIntGT(BN_cmp(c, b), 0);
  34360. AssertIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34361. AssertIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34362. AssertIntEQ(BN_print_fp(stderr, NULL), 0);
  34363. AssertIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34364. AssertIntEQ(BN_print_fp(XBADFILE, a), 0);
  34365. AssertIntEQ(BN_print_fp(stderr, a), 1);
  34366. BN_clear(a);
  34367. BN_free(a);
  34368. BN_free(b);
  34369. BN_free(c);
  34370. BN_clear_free(d);
  34371. res = TEST_RES_CHECK(1);
  34372. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34373. return res;
  34374. }
  34375. static int test_wolfSSL_BN_init(void)
  34376. {
  34377. int res = TEST_SKIPPED;
  34378. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34379. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34380. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34381. BIGNUM* ap;
  34382. BIGNUM bv;
  34383. BIGNUM cv;
  34384. BIGNUM dv;
  34385. AssertNotNull(ap = BN_new());
  34386. BN_init(NULL);
  34387. XMEMSET(&bv, 0, sizeof(bv));
  34388. AssertNull(BN_dup(&bv));
  34389. BN_init(&bv);
  34390. BN_init(&cv);
  34391. BN_init(&dv);
  34392. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34393. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34394. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34395. /* a^b mod c = */
  34396. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34397. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34398. /* check result 3^2 mod 5 */
  34399. AssertIntEQ(BN_get_word(&dv), 4);
  34400. /* a*b mod c = */
  34401. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34402. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34403. /* check result 3*2 mod 5 */
  34404. AssertIntEQ(BN_get_word(&dv), 1);
  34405. BN_free(ap);
  34406. res = TEST_RES_CHECK(1);
  34407. #endif
  34408. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34409. return res;
  34410. }
  34411. static int test_wolfSSL_BN_enc_dec(void)
  34412. {
  34413. int res = TEST_SKIPPED;
  34414. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34415. BIGNUM* a;
  34416. BIGNUM* b;
  34417. BIGNUM* c = NULL;
  34418. BIGNUM emptyBN;
  34419. char* str;
  34420. const char* emptyStr = "";
  34421. const char* numberStr = "12345";
  34422. const char* badStr = "g12345";
  34423. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34424. const char* twoStr = "2";
  34425. #endif
  34426. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34427. unsigned char outNum[5];
  34428. /* Setup */
  34429. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34430. AssertNotNull(a = BN_new());
  34431. AssertNotNull(b = BN_new());
  34432. AssertIntEQ(BN_set_word(a, 2), 1);
  34433. /* Invalid parameters */
  34434. AssertIntEQ(BN_bn2bin(NULL, NULL), -1);
  34435. AssertIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34436. AssertIntEQ(BN_bn2bin(NULL, outNum), -1);
  34437. AssertIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34438. AssertNull(BN_bn2hex(NULL));
  34439. AssertNull(BN_bn2hex(&emptyBN));
  34440. AssertNull(BN_bn2dec(NULL));
  34441. AssertNull(BN_bn2dec(&emptyBN));
  34442. AssertNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34443. AssertNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34444. AssertNull(BN_bin2bn(binNum, -1, a));
  34445. AssertNull(BN_bin2bn(binNum, -1, NULL));
  34446. AssertNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34447. AssertIntEQ(BN_hex2bn(NULL, NULL), 0);
  34448. AssertIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34449. AssertIntEQ(BN_hex2bn(&a, NULL), 0);
  34450. AssertIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34451. AssertIntEQ(BN_hex2bn(&a, badStr), 0);
  34452. AssertIntEQ(BN_hex2bn(&c, badStr), 0);
  34453. AssertIntEQ(BN_dec2bn(NULL, NULL), 0);
  34454. AssertIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34455. AssertIntEQ(BN_dec2bn(&a, NULL), 0);
  34456. AssertIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34457. AssertIntEQ(BN_dec2bn(&a, badStr), 0);
  34458. AssertIntEQ(BN_dec2bn(&c, badStr), 0);
  34459. AssertIntEQ(BN_set_word(a, 2), 1);
  34460. AssertIntEQ(BN_bn2bin(a, NULL), 1);
  34461. AssertIntEQ(BN_bn2bin(a, outNum), 1);
  34462. AssertNotNull(BN_bin2bn(outNum, 1, b));
  34463. AssertIntEQ(BN_cmp(a, b), 0);
  34464. AssertNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34465. AssertIntEQ(BN_cmp(a, b), -1);
  34466. AssertNotNull(str = BN_bn2hex(a));
  34467. AssertNotNull(BN_hex2bn(&b, str));
  34468. AssertIntEQ(BN_cmp(a, b), 0);
  34469. AssertNotNull(BN_hex2bn(&b, numberStr));
  34470. AssertIntEQ(BN_cmp(a, b), -1);
  34471. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34472. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34473. AssertNotNull(str = BN_bn2dec(a));
  34474. AssertStrEQ(str, twoStr);
  34475. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34476. #ifndef NO_RSA
  34477. AssertNotNull(str = BN_bn2dec(a));
  34478. AssertNotNull(BN_dec2bn(&b, str));
  34479. AssertIntEQ(BN_cmp(a, b), 0);
  34480. AssertNotNull(BN_dec2bn(&b, numberStr));
  34481. AssertIntEQ(BN_cmp(a, b), -1);
  34482. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34483. #else
  34484. /* No implementation - fail with good parameters. */
  34485. AssertIntEQ(BN_dec2bn(&a, numberStr), 0);
  34486. #endif
  34487. #endif
  34488. BN_free(b);
  34489. BN_free(a);
  34490. res = TEST_RES_CHECK(1);
  34491. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34492. return res;
  34493. }
  34494. static int test_wolfSSL_BN_word(void)
  34495. {
  34496. int res = TEST_SKIPPED;
  34497. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34498. BIGNUM* a;
  34499. BIGNUM* b;
  34500. BIGNUM* c;
  34501. BIGNUM av;
  34502. AssertNotNull(a = BN_new());
  34503. AssertNotNull(b = BN_new());
  34504. AssertNotNull(c = BN_new());
  34505. XMEMSET(&av, 0, sizeof(av));
  34506. /* Invalid parameter. */
  34507. AssertIntEQ(BN_add_word(NULL, 3), 0);
  34508. AssertIntEQ(BN_add_word(&av, 3), 0);
  34509. AssertIntEQ(BN_sub_word(NULL, 3), 0);
  34510. AssertIntEQ(BN_sub_word(&av, 3), 0);
  34511. AssertIntEQ(BN_set_word(NULL, 3), 0);
  34512. AssertIntEQ(BN_set_word(&av, 3), 0);
  34513. AssertIntEQ(BN_get_word(NULL), 0);
  34514. AssertIntEQ(BN_get_word(&av), 0);
  34515. AssertIntEQ(BN_is_word(NULL, 3), 0);
  34516. AssertIntEQ(BN_is_word(&av, 3), 0);
  34517. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34518. !defined(NO_DSA))
  34519. AssertIntEQ(BN_mod_word(NULL, 3), -1);
  34520. AssertIntEQ(BN_mod_word(&av, 3), -1);
  34521. #endif
  34522. AssertIntEQ(BN_one(NULL), 0);
  34523. AssertIntEQ(BN_one(&av), 0);
  34524. BN_zero(NULL);
  34525. BN_zero(&av);
  34526. AssertIntEQ(BN_is_one(NULL), 0);
  34527. AssertIntEQ(BN_is_one(&av), 0);
  34528. AssertIntEQ(BN_is_zero(NULL), 0);
  34529. AssertIntEQ(BN_is_zero(&av), 0);
  34530. AssertIntEQ(BN_set_word(a, 3), 1);
  34531. AssertIntEQ(BN_set_word(b, 2), 1);
  34532. AssertIntEQ(BN_set_word(c, 5), 1);
  34533. /* a + 3 = */
  34534. AssertIntEQ(BN_add_word(a, 3), 1);
  34535. /* check result 3 + 3*/
  34536. AssertIntEQ(BN_get_word(a), 6);
  34537. AssertIntEQ(BN_is_word(a, 6), 1);
  34538. AssertIntEQ(BN_is_word(a, 5), 0);
  34539. /* set a back to 3 */
  34540. AssertIntEQ(BN_set_word(a, 3), 1);
  34541. /* a - 3 = */
  34542. AssertIntEQ(BN_sub_word(a, 3), 1);
  34543. /* check result 3 - 3*/
  34544. AssertIntEQ(BN_get_word(a), 0);
  34545. AssertIntEQ(BN_one(a), 1);
  34546. AssertIntEQ(BN_is_word(a, 1), 1);
  34547. AssertIntEQ(BN_is_word(a, 0), 0);
  34548. AssertIntEQ(BN_is_one(a), 1);
  34549. AssertIntEQ(BN_is_zero(a), 0);
  34550. BN_zero(a);
  34551. AssertIntEQ(BN_is_word(a, 0), 1);
  34552. AssertIntEQ(BN_is_word(a, 1), 0);
  34553. AssertIntEQ(BN_is_zero(a), 1);
  34554. AssertIntEQ(BN_is_one(a), 0);
  34555. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34556. !defined(NO_DSA))
  34557. AssertIntEQ(BN_set_word(a, 5), 1);
  34558. AssertIntEQ(BN_mod_word(a, 3), 2);
  34559. AssertIntEQ(BN_mod_word(a, 0), -1);
  34560. #endif
  34561. BN_free(c);
  34562. BN_free(b);
  34563. BN_free(a);
  34564. res = TEST_RES_CHECK(1);
  34565. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34566. return res;
  34567. }
  34568. static int test_wolfSSL_BN_bits(void)
  34569. {
  34570. int res = TEST_SKIPPED;
  34571. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34572. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34573. BIGNUM* a;
  34574. BIGNUM emptyBN;
  34575. /* Setup */
  34576. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34577. AssertNotNull(a = BN_new());
  34578. /* Invalid parameters. */
  34579. AssertIntEQ(BN_set_bit(NULL, 1), 0);
  34580. AssertIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34581. AssertIntEQ(BN_set_bit(a, -1), 0);
  34582. AssertIntEQ(BN_clear_bit(NULL, 1), 0);
  34583. AssertIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34584. AssertIntEQ(BN_clear_bit(a, -1), 0);
  34585. AssertIntEQ(BN_is_bit_set(NULL, 1), 0);
  34586. AssertIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34587. AssertIntEQ(BN_is_bit_set(a, -1), 0);
  34588. AssertIntEQ(BN_is_odd(NULL), 0);
  34589. AssertIntEQ(BN_is_odd(&emptyBN), 0);
  34590. AssertIntEQ(BN_set_word(a, 0), 1);
  34591. AssertIntEQ(BN_is_zero(a), 1);
  34592. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  34593. AssertIntEQ(BN_is_zero(a), 0);
  34594. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  34595. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  34596. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  34597. AssertIntEQ(BN_is_zero(a), 1);
  34598. AssertIntEQ(BN_set_bit(a, 0), 1);
  34599. AssertIntEQ(BN_is_odd(a), 1);
  34600. AssertIntEQ(BN_clear_bit(a, 0), 1);
  34601. AssertIntEQ(BN_is_odd(a), 0);
  34602. AssertIntEQ(BN_set_bit(a, 1), 1);
  34603. AssertIntEQ(BN_is_odd(a), 0);
  34604. AssertIntEQ(BN_set_bit(a, 129), 1);
  34605. AssertIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34606. #ifndef NO_WOLFSSL_STUB
  34607. AssertIntEQ(BN_mask_bits(a, 1), 0);
  34608. #endif
  34609. BN_free(a);
  34610. res = TEST_RES_CHECK(1);
  34611. #endif
  34612. return res;
  34613. }
  34614. static int test_wolfSSL_BN_shift(void)
  34615. {
  34616. int res = TEST_SKIPPED;
  34617. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34618. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34619. BIGNUM* a;
  34620. BIGNUM* b;
  34621. BIGNUM emptyBN;
  34622. /* Setup */
  34623. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34624. AssertNotNull(a = BN_new());
  34625. AssertNotNull(b = BN_new());
  34626. /* Invalid parameters. */
  34627. AssertIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34628. AssertIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34629. AssertIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34630. AssertIntEQ(BN_lshift(b, NULL, 1), 0);
  34631. AssertIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34632. AssertIntEQ(BN_lshift(NULL, a, 1), 0);
  34633. AssertIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34634. AssertIntEQ(BN_lshift(b, a, -1), 0);
  34635. AssertIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34636. AssertIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34637. AssertIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34638. AssertIntEQ(BN_rshift(b, NULL, 1), 0);
  34639. AssertIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34640. AssertIntEQ(BN_rshift(NULL, a, 1), 0);
  34641. AssertIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34642. AssertIntEQ(BN_rshift(b, a, -1), 0);
  34643. AssertIntEQ(BN_set_word(a, 1), 1);
  34644. AssertIntEQ(BN_lshift(b, a, 1), 1);
  34645. AssertIntEQ(BN_is_word(b, 2), 1);
  34646. AssertIntEQ(BN_lshift(a, a, 1), 1);
  34647. AssertIntEQ(BN_is_word(a, 2), 1);
  34648. AssertIntEQ(BN_rshift(b, a, 1), 1);
  34649. AssertIntEQ(BN_is_word(b, 1), 1);
  34650. AssertIntEQ(BN_rshift(a, a, 1), 1);
  34651. AssertIntEQ(BN_is_word(a, 1), 1);
  34652. BN_free(b);
  34653. BN_free(a);
  34654. res = TEST_RES_CHECK(1);
  34655. #endif
  34656. return res;
  34657. }
  34658. static int test_wolfSSL_BN_math(void)
  34659. {
  34660. int res = TEST_SKIPPED;
  34661. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34662. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34663. BIGNUM* a;
  34664. BIGNUM* b;
  34665. BIGNUM* r;
  34666. BIGNUM* rem;
  34667. BIGNUM emptyBN;
  34668. BN_ULONG val1;
  34669. BN_ULONG val2;
  34670. /* Setup */
  34671. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34672. AssertNotNull(a = BN_new());
  34673. AssertNotNull(b = BN_new());
  34674. AssertNotNull(r = BN_new());
  34675. AssertNotNull(rem = BN_new());
  34676. /* Invalid parameters. */
  34677. AssertIntEQ(BN_add(NULL, NULL, NULL), 0);
  34678. AssertIntEQ(BN_add(r, NULL, NULL), 0);
  34679. AssertIntEQ(BN_add(NULL, a, NULL), 0);
  34680. AssertIntEQ(BN_add(NULL, NULL, b), 0);
  34681. AssertIntEQ(BN_add(r, a, NULL), 0);
  34682. AssertIntEQ(BN_add(r, NULL, b), 0);
  34683. AssertIntEQ(BN_add(NULL, a, b), 0);
  34684. AssertIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34685. AssertIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34686. AssertIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34687. AssertIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34688. AssertIntEQ(BN_add(r, a, &emptyBN), 0);
  34689. AssertIntEQ(BN_add(r, &emptyBN, b), 0);
  34690. AssertIntEQ(BN_add(&emptyBN, a, b), 0);
  34691. AssertIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34692. AssertIntEQ(BN_sub(r, NULL, NULL), 0);
  34693. AssertIntEQ(BN_sub(NULL, a, NULL), 0);
  34694. AssertIntEQ(BN_sub(NULL, NULL, b), 0);
  34695. AssertIntEQ(BN_sub(r, a, NULL), 0);
  34696. AssertIntEQ(BN_sub(r, NULL, b), 0);
  34697. AssertIntEQ(BN_sub(NULL, a, b), 0);
  34698. AssertIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34699. AssertIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34700. AssertIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34701. AssertIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34702. AssertIntEQ(BN_sub(r, a, &emptyBN), 0);
  34703. AssertIntEQ(BN_sub(r, &emptyBN, b), 0);
  34704. AssertIntEQ(BN_sub(&emptyBN, a, b), 0);
  34705. AssertIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34706. AssertIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34707. AssertIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34708. AssertIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34709. AssertIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34710. AssertIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34711. AssertIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34712. AssertIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34713. AssertIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34714. AssertIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34715. AssertIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34716. AssertIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34717. AssertIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34718. AssertIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34719. AssertIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34720. AssertIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34721. AssertIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34722. AssertIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34723. AssertIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34724. AssertIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34725. AssertIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34726. AssertIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34727. AssertIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34728. AssertIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34729. AssertIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34730. AssertIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34731. AssertIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34732. AssertIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34733. AssertIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34734. AssertIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34735. AssertIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34736. AssertIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34737. AssertIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34738. AssertIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34739. AssertIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34740. AssertIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34741. AssertIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34742. AssertIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34743. AssertIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34744. AssertIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34745. AssertIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34746. AssertIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34747. AssertIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34748. AssertIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34749. AssertIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34750. AssertIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34751. /* END Invalid parameters. */
  34752. val1 = 8;
  34753. val2 = 3;
  34754. AssertIntEQ(BN_set_word(a, val1), 1);
  34755. AssertIntEQ(BN_set_word(b, val2), 1);
  34756. AssertIntEQ(BN_add(r, a, b), 1);
  34757. AssertIntEQ(BN_is_word(r, val1 + val2), 1);
  34758. AssertIntEQ(BN_sub(r, a, b), 1);
  34759. AssertIntEQ(BN_is_word(r, val1 - val2), 1);
  34760. AssertIntEQ(BN_mul(r, a, b, NULL), 1);
  34761. AssertIntEQ(BN_is_word(r, val1 * val2), 1);
  34762. AssertIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34763. AssertIntEQ(BN_is_word(r, val1 / val2), 1);
  34764. AssertIntEQ(BN_is_word(rem, val1 % val2), 1);
  34765. AssertIntEQ(BN_mod(r, a, b, NULL), 1);
  34766. AssertIntEQ(BN_is_word(r, val1 % val2), 1);
  34767. BN_free(rem);
  34768. BN_free(r);
  34769. BN_free(b);
  34770. BN_free(a);
  34771. res = TEST_RES_CHECK(1);
  34772. #endif
  34773. return res;
  34774. }
  34775. static int test_wolfSSL_BN_math_mod(void)
  34776. {
  34777. int res = TEST_SKIPPED;
  34778. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34779. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34780. BIGNUM* a;
  34781. BIGNUM* b;
  34782. BIGNUM* m;
  34783. BIGNUM* r;
  34784. BIGNUM* t;
  34785. BIGNUM emptyBN;
  34786. BN_ULONG val1;
  34787. BN_ULONG val2;
  34788. BN_ULONG val3;
  34789. /* Setup */
  34790. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34791. AssertNotNull(a = BN_new());
  34792. AssertNotNull(b = BN_new());
  34793. AssertNotNull(m = BN_new());
  34794. AssertNotNull(r = BN_new());
  34795. /* Invalid parameters. */
  34796. AssertIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34797. AssertIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34798. AssertIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34799. AssertIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34800. AssertIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34801. AssertIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34802. AssertIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34803. AssertIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34804. AssertIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34805. AssertIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34806. AssertIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34807. AssertIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34808. AssertIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34809. AssertIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34810. AssertIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34811. AssertIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34812. AssertIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34813. AssertIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34814. AssertIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34815. AssertIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34816. AssertIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34817. AssertIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34818. AssertIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34819. AssertIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34820. AssertIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34821. AssertIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34822. AssertIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34823. AssertIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34824. AssertIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34825. AssertIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34826. AssertIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34827. AssertIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34828. AssertIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34829. AssertIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34830. AssertIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34831. AssertIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34832. AssertIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34833. AssertIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34834. AssertIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34835. AssertIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34836. AssertIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34837. AssertIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34838. AssertIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34839. AssertIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34840. AssertIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34841. AssertIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34842. AssertIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34843. AssertIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34844. AssertIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34845. AssertIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34846. AssertIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34847. AssertIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34848. AssertIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34849. AssertIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34850. AssertNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34851. AssertNull(BN_mod_inverse(r, a, NULL, NULL));
  34852. AssertNull(BN_mod_inverse(r, NULL, m, NULL));
  34853. AssertNull(BN_mod_inverse(r, NULL, m, NULL));
  34854. AssertNull(BN_mod_inverse(r, a, NULL, NULL));
  34855. AssertNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34856. AssertNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34857. AssertNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34858. AssertNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34859. AssertNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34860. AssertNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34861. AssertNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34862. /* END Invalid parameters. */
  34863. val1 = 9;
  34864. val2 = 13;
  34865. val3 = 5;
  34866. AssertIntEQ(BN_set_word(a, val1), 1);
  34867. AssertIntEQ(BN_set_word(b, val2), 1);
  34868. AssertIntEQ(BN_set_word(m, val3), 1);
  34869. AssertIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34870. AssertIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34871. AssertIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34872. AssertIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34873. AssertIntEQ(BN_set_word(a, 2), 1);
  34874. AssertIntEQ(BN_set_word(b, 3), 1);
  34875. AssertIntEQ(BN_set_word(m, 5), 1);
  34876. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34877. AssertIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34878. AssertIntEQ(BN_is_word(r, 3), 1);
  34879. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34880. AssertNotNull(BN_mod_inverse(r, a, m, NULL));
  34881. AssertIntEQ(BN_is_word(r, 3), 1);
  34882. AssertNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34883. AssertIntEQ(BN_is_word(t, 3), 1);
  34884. BN_free(t);
  34885. /* No inverse case. No inverse when a divides b. */
  34886. AssertIntEQ(BN_set_word(a, 3), 1);
  34887. AssertIntEQ(BN_set_word(m, 9), 1);
  34888. AssertNull(BN_mod_inverse(r, a, m, NULL));
  34889. BN_free(r);
  34890. BN_free(m);
  34891. BN_free(b);
  34892. BN_free(a);
  34893. res = TEST_RES_CHECK(1);
  34894. #endif
  34895. return res;
  34896. }
  34897. static int test_wolfSSL_BN_math_other(void)
  34898. {
  34899. int res = TEST_SKIPPED;
  34900. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34901. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34902. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  34903. BIGNUM* a;
  34904. BIGNUM* b;
  34905. BIGNUM* r;
  34906. BIGNUM emptyBN;
  34907. /* Setup */
  34908. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34909. AssertNotNull(a = BN_new());
  34910. AssertNotNull(b = BN_new());
  34911. AssertNotNull(r = BN_new());
  34912. /* Invalid parameters. */
  34913. AssertIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  34914. AssertIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  34915. AssertIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  34916. AssertIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  34917. AssertIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  34918. AssertIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  34919. AssertIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  34920. AssertIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34921. AssertIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  34922. AssertIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  34923. AssertIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  34924. AssertIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  34925. AssertIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  34926. AssertIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  34927. /* END Invalid parameters. */
  34928. /* No comman factors between 2 and 3. */
  34929. AssertIntEQ(BN_set_word(a, 2), 1);
  34930. AssertIntEQ(BN_set_word(b, 3), 1);
  34931. AssertIntEQ(BN_gcd(r, a, b, NULL), 1);
  34932. AssertIntEQ(BN_is_word(r, 1), 1);
  34933. /* 3 is largest value that divides both 6 and 9. */
  34934. AssertIntEQ(BN_set_word(a, 6), 1);
  34935. AssertIntEQ(BN_set_word(b, 9), 1);
  34936. AssertIntEQ(BN_gcd(r, a, b, NULL), 1);
  34937. AssertIntEQ(BN_is_word(r, 3), 1);
  34938. /* GCD of 0 and 0 is undefined. */
  34939. AssertIntEQ(BN_set_word(a, 0), 1);
  34940. AssertIntEQ(BN_set_word(b, 0), 1);
  34941. AssertIntEQ(BN_gcd(r, a, b, NULL), 0);
  34942. /* Teardown */
  34943. BN_free(r);
  34944. BN_free(b);
  34945. BN_free(a);
  34946. res = TEST_RES_CHECK(1);
  34947. #endif
  34948. #endif
  34949. return res;
  34950. }
  34951. static int test_wolfSSL_BN_rand(void)
  34952. {
  34953. int res = TEST_SKIPPED;
  34954. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  34955. BIGNUM* bn;
  34956. BIGNUM* range;
  34957. BIGNUM emptyBN;
  34958. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34959. AssertNotNull(bn = BN_new());
  34960. AssertNotNull(range = BN_new());
  34961. /* Invalid parameters. */
  34962. AssertIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  34963. AssertIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34964. AssertIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  34965. AssertIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  34966. AssertIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34967. AssertIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  34968. AssertIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  34969. AssertIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34970. AssertIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  34971. AssertIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  34972. AssertIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34973. AssertIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  34974. AssertIntEQ(BN_rand_range(NULL, NULL), 0);
  34975. AssertIntEQ(BN_rand_range(bn, NULL), 0);
  34976. AssertIntEQ(BN_rand_range(NULL, range), 0);
  34977. AssertIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  34978. AssertIntEQ(BN_rand_range(bn, &emptyBN), 0);
  34979. AssertIntEQ(BN_rand_range(&emptyBN, range), 0);
  34980. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  34981. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34982. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34983. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34984. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34985. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34986. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34987. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34988. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34989. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34990. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34991. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34992. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34993. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34994. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34995. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34996. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34997. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34998. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34999. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35000. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35001. /* 1 bit random value must have no more than one top bit set. */
  35002. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35003. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35004. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35005. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35006. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35007. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35008. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35009. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35010. /* END Invalid parameters. */
  35011. /* 0 bit random: 0. */
  35012. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35013. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35014. AssertIntEQ(BN_is_zero(bn), 1);
  35015. AssertIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  35016. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35017. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35018. AssertIntEQ(BN_is_zero(bn), 1);
  35019. /* 1 bit random: 0 or 1. */
  35020. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35021. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35022. AssertIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35023. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35024. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35025. AssertIntEQ(BN_get_word(bn), 1);
  35026. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35027. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35028. AssertIntEQ(BN_get_word(bn), 1);
  35029. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35030. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35031. AssertIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35032. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35033. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35034. AssertIntEQ(BN_get_word(bn), 1);
  35035. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35036. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35037. AssertIntEQ(BN_get_word(bn), 1);
  35038. AssertIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35039. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35040. AssertIntEQ(BN_num_bits(bn), 8);
  35041. AssertIntEQ(BN_is_bit_set(bn, 7), 1);
  35042. AssertIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35043. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35044. AssertIntEQ(BN_num_bits(bn), 8);
  35045. AssertIntEQ(BN_is_bit_set(bn, 7), 1);
  35046. AssertIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35047. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35048. AssertIntEQ(BN_is_bit_set(bn, 7), 1);
  35049. AssertIntEQ(BN_is_bit_set(bn, 6), 1);
  35050. AssertIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35051. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35052. AssertIntEQ(BN_is_bit_set(bn, 7), 1);
  35053. AssertIntEQ(BN_is_bit_set(bn, 6), 1);
  35054. AssertIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35055. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35056. AssertIntEQ(BN_is_bit_set(bn, 0), 1);
  35057. AssertIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35058. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35059. AssertIntEQ(BN_is_bit_set(bn, 0), 1);
  35060. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35061. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35062. * case, requesting a 13-bit random number would actually return a 16-bit
  35063. * random number. */
  35064. AssertIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35065. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35066. AssertIntEQ(BN_num_bits(bn), 13);
  35067. AssertIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35068. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35069. AssertIntEQ(BN_rand_range(bn, range), 1);
  35070. AssertIntEQ(BN_set_word(range, 0), 1);
  35071. AssertIntEQ(BN_rand_range(bn, range), 1);
  35072. AssertIntEQ(BN_set_word(range, 1), 1);
  35073. AssertIntEQ(BN_rand_range(bn, range), 1);
  35074. BN_free(bn);
  35075. BN_free(range);
  35076. res = TEST_RES_CHECK(1);
  35077. #endif
  35078. return res;
  35079. }
  35080. static int test_wolfSSL_BN_prime(void)
  35081. {
  35082. int res = TEST_SKIPPED;
  35083. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35084. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35085. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35086. BIGNUM* a;
  35087. BIGNUM* add;
  35088. BIGNUM* rem;
  35089. BIGNUM emptyBN;
  35090. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35091. AssertNotNull(a = BN_new());
  35092. AssertNotNull(add = BN_new());
  35093. AssertNotNull(rem = BN_new());
  35094. /* Invalid parameters. */
  35095. /* BN_generate_prime_ex()
  35096. * prime - must have valid BIGNUM
  35097. * bits - Greater then 0
  35098. * safe - not supported, must be 0
  35099. * add - not supported, must be NULL
  35100. * rem - not supported, must be NULL
  35101. * cb - anything
  35102. */
  35103. AssertIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35104. AssertIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35105. AssertIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35106. AssertIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35107. AssertIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35108. AssertIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35109. AssertIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35110. AssertIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35111. AssertIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35112. AssertIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35113. AssertIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35114. AssertIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35115. AssertIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35116. AssertIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35117. AssertIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35118. AssertIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35119. AssertIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35120. AssertIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35121. AssertIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35122. AssertIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35123. AssertIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35124. AssertIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35125. AssertIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35126. AssertIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35127. /* END Invalid parameters. */
  35128. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35129. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35130. AssertIntEQ(BN_clear_bit(a, 0), 1);
  35131. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35132. BN_free(rem);
  35133. BN_free(add);
  35134. BN_free(a);
  35135. res = TEST_RES_CHECK(1);
  35136. #endif
  35137. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35138. return res;
  35139. }
  35140. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35141. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35142. #define TEST_ARG 0x1234
  35143. static void msg_cb(int write_p, int version, int content_type,
  35144. const void *buf, size_t len, SSL *ssl, void *arg)
  35145. {
  35146. (void)write_p;
  35147. (void)version;
  35148. (void)content_type;
  35149. (void)buf;
  35150. (void)len;
  35151. (void)ssl;
  35152. AssertTrue(arg == (void*)TEST_ARG);
  35153. }
  35154. #endif
  35155. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35156. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  35157. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  35158. !defined(NO_WOLFSSL_SERVER)
  35159. #ifndef SINGLE_THREADED
  35160. #if defined(SESSION_CERTS)
  35161. #include "wolfssl/internal.h"
  35162. #endif
  35163. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35164. {
  35165. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35166. STACK_OF(X509)* sk;
  35167. X509* x509;
  35168. int i, num;
  35169. BIO* bio;
  35170. #endif
  35171. (void) ctx;
  35172. fprintf(stderr, "\n===== msgcb called ====\n");
  35173. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35174. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35175. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35176. AssertNotNull(SSL_get0_verified_chain(ssl));
  35177. #else
  35178. (void) ssl;
  35179. #endif
  35180. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35181. bio = BIO_new(BIO_s_file());
  35182. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  35183. sk = SSL_get_peer_cert_chain(ssl);
  35184. AssertNotNull(sk);
  35185. if (!sk) {
  35186. BIO_free(bio);
  35187. return SSL_FAILURE;
  35188. }
  35189. num = sk_X509_num(sk);
  35190. AssertTrue(num > 0);
  35191. for (i = 0; i < num; i++) {
  35192. x509 = sk_X509_value(sk,i);
  35193. AssertNotNull(x509);
  35194. if (!x509)
  35195. break;
  35196. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35197. X509_print(bio,x509);
  35198. fprintf(stderr, "\n\n");
  35199. }
  35200. BIO_free(bio);
  35201. #endif
  35202. return SSL_SUCCESS;
  35203. }
  35204. #endif
  35205. #endif
  35206. static int test_wolfSSL_msgCb(void)
  35207. {
  35208. int res = TEST_SKIPPED;
  35209. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35210. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  35211. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  35212. !defined(NO_WOLFSSL_SERVER)
  35213. tcp_ready ready;
  35214. func_args client_args;
  35215. func_args server_args;
  35216. #ifndef SINGLE_THREADED
  35217. THREAD_TYPE serverThread;
  35218. #endif
  35219. callback_functions client_cb;
  35220. callback_functions server_cb;
  35221. /* create a failed connection and inspect the error */
  35222. #ifdef WOLFSSL_TIRTOS
  35223. fdOpenSession(Task_self());
  35224. #endif
  35225. XMEMSET(&client_args, 0, sizeof(func_args));
  35226. XMEMSET(&server_args, 0, sizeof(func_args));
  35227. StartTCP();
  35228. InitTcpReady(&ready);
  35229. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  35230. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  35231. #ifndef WOLFSSL_NO_TLS12
  35232. client_cb.method = wolfTLSv1_2_client_method;
  35233. server_cb.method = wolfTLSv1_2_server_method;
  35234. #else
  35235. client_cb.method = wolfTLSv1_3_client_method;
  35236. server_cb.method = wolfTLSv1_3_server_method;
  35237. #endif
  35238. server_args.signal = &ready;
  35239. server_args.callbacks = &server_cb;
  35240. client_args.signal = &ready;
  35241. client_args.callbacks = &client_cb;
  35242. client_args.return_code = TEST_FAIL;
  35243. #ifndef SINGLE_THREADED
  35244. start_thread(test_server_nofail, &server_args, &serverThread);
  35245. wait_tcp_ready(&server_args);
  35246. test_client_nofail(&client_args, msgCb);
  35247. join_thread(serverThread);
  35248. #endif
  35249. FreeTcpReady(&ready);
  35250. #ifndef SINGLE_THREADED
  35251. AssertTrue(client_args.return_code);
  35252. AssertTrue(server_args.return_code);
  35253. #endif
  35254. #ifdef WOLFSSL_TIRTOS
  35255. fdOpenSession(Task_self());
  35256. #endif
  35257. res = TEST_RES_CHECK(1);
  35258. #endif
  35259. return res;
  35260. }
  35261. static int test_wolfSSL_either_side(void)
  35262. {
  35263. int res = TEST_SKIPPED;
  35264. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35265. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35266. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  35267. tcp_ready ready;
  35268. func_args client_args;
  35269. func_args server_args;
  35270. #ifndef SINGLE_THREADED
  35271. THREAD_TYPE serverThread;
  35272. #endif
  35273. callback_functions client_cb;
  35274. callback_functions server_cb;
  35275. /* create a failed connection and inspect the error */
  35276. #ifdef WOLFSSL_TIRTOS
  35277. fdOpenSession(Task_self());
  35278. #endif
  35279. XMEMSET(&client_args, 0, sizeof(func_args));
  35280. XMEMSET(&server_args, 0, sizeof(func_args));
  35281. StartTCP();
  35282. InitTcpReady(&ready);
  35283. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  35284. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  35285. /* Use different CTX for client and server */
  35286. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35287. AssertNotNull(client_cb.ctx);
  35288. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35289. AssertNotNull(server_cb.ctx);
  35290. /* we are responsible for free'ing WOLFSSL_CTX */
  35291. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35292. server_args.signal = &ready;
  35293. server_args.callbacks = &server_cb;
  35294. client_args.signal = &ready;
  35295. client_args.callbacks = &client_cb;
  35296. client_args.return_code = TEST_FAIL;
  35297. #ifndef SINGLE_THREADED
  35298. start_thread(test_server_nofail, &server_args, &serverThread);
  35299. wait_tcp_ready(&server_args);
  35300. test_client_nofail(&client_args, NULL);
  35301. join_thread(serverThread);
  35302. #endif
  35303. wolfSSL_CTX_free(client_cb.ctx);
  35304. wolfSSL_CTX_free(server_cb.ctx);
  35305. FreeTcpReady(&ready);
  35306. #ifndef SINGLE_THREADED
  35307. AssertTrue(client_args.return_code);
  35308. AssertTrue(server_args.return_code);
  35309. #endif
  35310. #ifdef WOLFSSL_TIRTOS
  35311. fdOpenSession(Task_self());
  35312. #endif
  35313. res = TEST_RES_CHECK(1);
  35314. #endif
  35315. return res;
  35316. }
  35317. static int test_wolfSSL_DTLS_either_side(void)
  35318. {
  35319. int res = TEST_SKIPPED;
  35320. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35321. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35322. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  35323. defined(WOLFSSL_DTLS)
  35324. tcp_ready ready;
  35325. func_args client_args;
  35326. func_args server_args;
  35327. #ifndef SINGLE_THREADED
  35328. THREAD_TYPE serverThread;
  35329. #endif
  35330. callback_functions client_cb;
  35331. callback_functions server_cb;
  35332. #ifdef WOLFSSL_TIRTOS
  35333. fdOpenSession(Task_self());
  35334. #endif
  35335. XMEMSET(&client_args, 0, sizeof(func_args));
  35336. XMEMSET(&server_args, 0, sizeof(func_args));
  35337. StartTCP();
  35338. InitTcpReady(&ready);
  35339. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  35340. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  35341. /* Use different CTX for client and server */
  35342. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35343. AssertNotNull(client_cb.ctx);
  35344. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35345. AssertNotNull(server_cb.ctx);
  35346. /* we are responsible for free'ing WOLFSSL_CTX */
  35347. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35348. server_args.signal = &ready;
  35349. server_args.callbacks = &server_cb;
  35350. client_args.signal = &ready;
  35351. client_args.callbacks = &client_cb;
  35352. client_args.return_code = TEST_FAIL;
  35353. #ifndef SINGLE_THREADED
  35354. start_thread(test_server_nofail, &server_args, &serverThread);
  35355. wait_tcp_ready(&server_args);
  35356. test_client_nofail(&client_args, NULL);
  35357. join_thread(serverThread);
  35358. #endif
  35359. wolfSSL_CTX_free(client_cb.ctx);
  35360. wolfSSL_CTX_free(server_cb.ctx);
  35361. FreeTcpReady(&ready);
  35362. #ifndef SINGLE_THREADED
  35363. AssertTrue(client_args.return_code);
  35364. AssertTrue(server_args.return_code);
  35365. #endif
  35366. #ifdef WOLFSSL_TIRTOS
  35367. fdOpenSession(Task_self());
  35368. #endif
  35369. res = TEST_RES_CHECK(1);
  35370. #endif
  35371. return res;
  35372. }
  35373. static int test_generate_cookie(void)
  35374. {
  35375. int res = TEST_SKIPPED;
  35376. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35377. SSL_CTX* ctx;
  35378. SSL* ssl;
  35379. byte buf[FOURK_BUF] = {0};
  35380. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35381. AssertNotNull(ssl = SSL_new(ctx));
  35382. /* Test unconnected */
  35383. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35384. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35385. wolfSSL_SetCookieCtx(ssl, ctx);
  35386. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  35387. AssertNull(wolfSSL_GetCookieCtx(NULL));
  35388. SSL_free(ssl);
  35389. SSL_CTX_free(ctx);
  35390. res = TEST_RES_CHECK(1);
  35391. #endif
  35392. return res;
  35393. }
  35394. static int test_wolfSSL_set_options(void)
  35395. {
  35396. int res = TEST_SKIPPED;
  35397. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35398. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35399. WOLFSSL* ssl;
  35400. WOLFSSL_CTX* ctx;
  35401. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35402. char appData[] = "extra msg";
  35403. #endif
  35404. #ifdef OPENSSL_EXTRA
  35405. unsigned char protos[] = {
  35406. 7, 't', 'l', 's', '/', '1', '.', '2',
  35407. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35408. };
  35409. unsigned int len = sizeof(protos);
  35410. void *arg = (void *)TEST_ARG;
  35411. #endif
  35412. #ifndef NO_WOLFSSL_SERVER
  35413. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35414. #else
  35415. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35416. #endif
  35417. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35418. WOLFSSL_FILETYPE_PEM));
  35419. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35420. WOLFSSL_FILETYPE_PEM));
  35421. AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35422. == WOLFSSL_OP_NO_TLSv1);
  35423. AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35424. AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35425. WOLFSSL_OP_NO_SSLv2)), 0);
  35426. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35427. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35428. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35429. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35430. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35431. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35432. AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35433. WOLFSSL_OP_NO_COMPRESSION));
  35434. wolfSSL_CTX_free(ctx);
  35435. #ifndef NO_WOLFSSL_SERVER
  35436. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  35437. AssertNotNull(ctx);
  35438. #else
  35439. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  35440. AssertNotNull(ctx);
  35441. #endif
  35442. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35443. WOLFSSL_FILETYPE_PEM));
  35444. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35445. WOLFSSL_FILETYPE_PEM));
  35446. #ifdef OPENSSL_EXTRA
  35447. AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35448. #endif
  35449. AssertNotNull(ssl = wolfSSL_new(ctx));
  35450. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35451. #ifdef HAVE_EX_DATA
  35452. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35453. AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35454. if (ssl) {
  35455. AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35456. appData, sizeof(appData)), 0);
  35457. }
  35458. #else
  35459. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35460. AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35461. #endif
  35462. #endif
  35463. AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35464. WOLFSSL_OP_NO_TLSv1);
  35465. AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35466. AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35467. WOLFSSL_OP_NO_SSLv2)), 0);
  35468. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35469. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35470. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35471. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35472. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35473. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35474. #ifdef OPENSSL_EXTRA
  35475. AssertFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35476. WOLFSSL_OP_NO_COMPRESSION));
  35477. #endif
  35478. #ifdef OPENSSL_EXTRA
  35479. AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35480. wolfSSL_set_msg_callback_arg(ssl, arg);
  35481. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35482. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35483. #else
  35484. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35485. #endif
  35486. #endif
  35487. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35488. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35489. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35490. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35491. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35492. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35493. #else
  35494. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35495. #endif
  35496. #endif /* HAVE_ALPN && !NO_BIO */
  35497. #endif
  35498. wolfSSL_free(ssl);
  35499. wolfSSL_CTX_free(ctx);
  35500. res = TEST_RES_CHECK(1);
  35501. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35502. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35503. return res;
  35504. }
  35505. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35506. {
  35507. int res = TEST_SKIPPED;
  35508. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35509. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35510. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35511. SSL* ssl;
  35512. SSL_CTX* ctx;
  35513. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  35514. #ifndef NO_WOLFSSL_SERVER
  35515. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35516. #else
  35517. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35518. #endif
  35519. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35520. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35521. AssertNotNull(ssl = SSL_new(ctx));
  35522. AssertNotNull(sk = SSL_get_ciphers(ssl));
  35523. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35524. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  35525. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35526. /* error case because connection has not been established yet */
  35527. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35528. sk_SSL_CIPHER_free(dupSk);
  35529. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35530. SSL_free(ssl);
  35531. SSL_CTX_free(ctx);
  35532. res = TEST_RES_CHECK(1);
  35533. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35534. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35535. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35536. return res;
  35537. }
  35538. static int test_wolfSSL_set1_curves_list(void)
  35539. {
  35540. int res = TEST_SKIPPED;
  35541. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35542. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35543. SSL* ssl = NULL;
  35544. SSL_CTX* ctx = NULL;
  35545. #ifndef NO_WOLFSSL_SERVER
  35546. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35547. #else
  35548. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35549. #endif
  35550. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35551. SSL_FILETYPE_PEM));
  35552. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35553. AssertNotNull(ssl = SSL_new(ctx));
  35554. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35555. #ifdef HAVE_ECC
  35556. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35557. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35558. #endif
  35559. #ifdef HAVE_CURVE25519
  35560. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35561. #else
  35562. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35563. #endif
  35564. #ifdef HAVE_CURVE448
  35565. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35566. #else
  35567. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35568. #endif
  35569. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35570. #ifdef HAVE_ECC
  35571. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35572. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35573. #endif
  35574. #ifdef HAVE_CURVE25519
  35575. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35576. #else
  35577. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35578. #endif
  35579. #ifdef HAVE_CURVE448
  35580. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35581. #else
  35582. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35583. #endif
  35584. SSL_free(ssl);
  35585. SSL_CTX_free(ctx);
  35586. res = TEST_RES_CHECK(1);
  35587. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35588. #endif
  35589. return res;
  35590. }
  35591. static int test_wolfSSL_set1_sigalgs_list(void)
  35592. {
  35593. int res = TEST_SKIPPED;
  35594. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35595. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35596. SSL* ssl;
  35597. SSL_CTX* ctx;
  35598. #ifndef NO_WOLFSSL_SERVER
  35599. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35600. #else
  35601. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35602. #endif
  35603. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35604. SSL_FILETYPE_PEM));
  35605. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35606. AssertNotNull(ssl = SSL_new(ctx));
  35607. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35608. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35609. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35610. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35611. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35612. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35613. #ifndef NO_RSA
  35614. #ifndef NO_SHA256
  35615. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35616. WOLFSSL_FAILURE);
  35617. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35618. WOLFSSL_FAILURE);
  35619. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35620. WOLFSSL_SUCCESS);
  35621. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35622. WOLFSSL_SUCCESS);
  35623. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35624. WOLFSSL_FAILURE);
  35625. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35626. WOLFSSL_FAILURE);
  35627. #ifdef WC_RSA_PSS
  35628. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35629. WOLFSSL_SUCCESS);
  35630. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35631. WOLFSSL_SUCCESS);
  35632. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35633. WOLFSSL_SUCCESS);
  35634. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35635. WOLFSSL_SUCCESS);
  35636. #endif
  35637. #ifdef WOLFSSL_SHA512
  35638. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35639. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35640. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35641. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35642. #elif defined(WOLFSSL_SHA384)
  35643. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35644. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35645. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35646. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35647. #endif
  35648. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35649. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35650. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35651. WOLFSSL_FAILURE);
  35652. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35653. WOLFSSL_FAILURE);
  35654. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35655. WOLFSSL_FAILURE);
  35656. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35657. WOLFSSL_FAILURE);
  35658. #endif
  35659. #endif
  35660. #ifdef HAVE_ECC
  35661. #ifndef NO_SHA256
  35662. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35663. WOLFSSL_SUCCESS);
  35664. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  35665. #ifdef WOLFSSL_SHA512
  35666. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35667. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35668. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35669. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35670. #elif defined(WOLFSSL_SHA384)
  35671. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35672. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35673. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35674. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35675. #endif
  35676. #endif
  35677. #endif
  35678. #ifdef HAVE_ED25519
  35679. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35680. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35681. #endif
  35682. #ifdef HAVE_ED448
  35683. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35684. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35685. #endif
  35686. #ifndef NO_DSA
  35687. #ifndef NO_SHA256
  35688. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35689. WOLFSSL_SUCCESS);
  35690. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35691. WOLFSSL_SUCCESS);
  35692. #endif
  35693. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35694. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35695. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35696. WOLFSSL_SUCCESS);
  35697. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35698. WOLFSSL_SUCCESS);
  35699. #endif
  35700. #endif
  35701. SSL_free(ssl);
  35702. SSL_CTX_free(ctx);
  35703. res = TEST_RES_CHECK(1);
  35704. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35705. #endif
  35706. return res;
  35707. }
  35708. /* Testing wolfSSL_set_tlsext_status_type function.
  35709. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35710. */
  35711. static int test_wolfSSL_set_tlsext_status_type(void)
  35712. {
  35713. int res = TEST_SKIPPED;
  35714. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35715. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35716. SSL* ssl;
  35717. SSL_CTX* ctx;
  35718. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35719. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35720. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35721. AssertNotNull(ssl = SSL_new(ctx));
  35722. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35723. SSL_SUCCESS);
  35724. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35725. SSL_free(ssl);
  35726. SSL_CTX_free(ctx);
  35727. res = TEST_RES_CHECK(1);
  35728. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35729. return res;
  35730. }
  35731. #ifndef NO_BIO
  35732. static int test_wolfSSL_PEM_read_bio(void)
  35733. {
  35734. int res = TEST_SKIPPED;
  35735. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35736. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35737. byte buff[6000];
  35738. XFILE f;
  35739. int bytes;
  35740. X509* x509;
  35741. BIO* bio = NULL;
  35742. BUF_MEM* buf;
  35743. f = XFOPEN(cliCertFile, "rb");
  35744. AssertTrue((f != XBADFILE));
  35745. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  35746. XFCLOSE(f);
  35747. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35748. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35749. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35750. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35751. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35752. /* BIO should return the set EOF value */
  35753. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35754. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35755. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35756. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35757. BIO_free(bio);
  35758. BUF_MEM_free(buf);
  35759. X509_free(x509);
  35760. res = TEST_RES_CHECK(1);
  35761. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35762. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35763. return res;
  35764. }
  35765. #if defined(OPENSSL_EXTRA)
  35766. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35767. long argl, long ret)
  35768. {
  35769. (void)bio;
  35770. (void)cmd;
  35771. (void)argp;
  35772. (void)argi;
  35773. (void)argl;
  35774. return ret;
  35775. }
  35776. #endif
  35777. static int test_wolfSSL_BIO(void)
  35778. {
  35779. int res = TEST_SKIPPED;
  35780. #if defined(OPENSSL_EXTRA)
  35781. const unsigned char* p;
  35782. byte buff[20];
  35783. BIO* bio1;
  35784. BIO* bio2;
  35785. BIO* bio3;
  35786. char* bufPt;
  35787. int i;
  35788. for (i = 0; i < 20; i++) {
  35789. buff[i] = i;
  35790. }
  35791. /* test BIO_free with NULL */
  35792. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35793. /* Creating and testing type BIO_s_bio */
  35794. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  35795. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  35796. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  35797. /* read/write before set up */
  35798. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35799. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35800. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  35801. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35802. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35803. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35804. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35805. XMEMCPY(bufPt, buff, 10);
  35806. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35807. /* write buffer full */
  35808. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35809. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35810. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35811. /* write the other direction with pair */
  35812. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35813. XMEMCPY(bufPt, buff, 8);
  35814. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35815. /* try read */
  35816. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35817. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35818. /* try read using ctrl function */
  35819. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35820. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35821. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35822. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35823. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35824. for (i = 0; i < 20; i++) {
  35825. AssertIntEQ((int)bufPt[i], i);
  35826. }
  35827. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  35828. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35829. for (i = 0; i < 8; i++) {
  35830. AssertIntEQ((int)bufPt[i], i);
  35831. }
  35832. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  35833. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35834. /* new pair */
  35835. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35836. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35837. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35838. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35839. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  35840. /* test wrap around... */
  35841. AssertIntEQ(BIO_reset(bio1), 0);
  35842. AssertIntEQ(BIO_reset(bio3), 0);
  35843. /* fill write buffer, read only small amount then write again */
  35844. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35845. XMEMCPY(bufPt, buff, 20);
  35846. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35847. for (i = 0; i < 4; i++) {
  35848. AssertIntEQ(bufPt[i], i);
  35849. }
  35850. /* try writing over read index */
  35851. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35852. XMEMSET(bufPt, 0, 4);
  35853. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35854. /* read and write 0 bytes */
  35855. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35856. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35857. /* should read only to end of write buffer then need to read again */
  35858. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35859. for (i = 0; i < 16; i++) {
  35860. AssertIntEQ(bufPt[i], buff[4 + i]);
  35861. }
  35862. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35863. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35864. for (i = 0; i < 4; i++) {
  35865. AssertIntEQ(bufPt[i], 0);
  35866. }
  35867. /* read index should not have advanced with nread0 */
  35868. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35869. for (i = 0; i < 4; i++) {
  35870. AssertIntEQ(bufPt[i], 0);
  35871. }
  35872. /* write and fill up buffer checking reset of index state */
  35873. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35874. XMEMCPY(bufPt, buff, 20);
  35875. /* test reset on data in bio1 write buffer */
  35876. AssertIntEQ(BIO_reset(bio1), 0);
  35877. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35878. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  35879. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35880. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35881. AssertNotNull(p);
  35882. XMEMCPY(bufPt, buff, 20);
  35883. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35884. for (i = 0; i < 6; i++) {
  35885. AssertIntEQ(bufPt[i], i);
  35886. }
  35887. /* test case of writing twice with offset read index */
  35888. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35889. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35890. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35891. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35892. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35893. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35894. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35895. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35896. BIO_free(bio1);
  35897. BIO_free(bio3);
  35898. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35899. {
  35900. BIO* bioA = NULL;
  35901. BIO* bioB = NULL;
  35902. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35903. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35904. BIO_free(bioA);
  35905. bioA = NULL;
  35906. BIO_free(bioB);
  35907. bioB = NULL;
  35908. }
  35909. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35910. /* BIOs with file pointers */
  35911. #if !defined(NO_FILESYSTEM)
  35912. {
  35913. XFILE f1;
  35914. XFILE f2;
  35915. BIO* f_bio1;
  35916. BIO* f_bio2;
  35917. unsigned char cert[300];
  35918. char testFile[] = "tests/bio_write_test.txt";
  35919. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35920. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35921. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35922. /* Failure due to wrong BIO type */
  35923. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35924. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35925. f1 = XFOPEN(svrCertFile, "rwb");
  35926. AssertTrue((f1 != XBADFILE));
  35927. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35928. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  35929. WOLFSSL_SUCCESS);
  35930. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35931. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  35932. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  35933. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  35934. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  35935. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  35936. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  35937. AssertIntEQ(BIO_reset(f_bio2), 0);
  35938. AssertIntEQ(BIO_tell(NULL),-1);
  35939. AssertIntEQ(BIO_tell(f_bio2),0);
  35940. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  35941. AssertIntEQ(BIO_tell(f_bio2),4);
  35942. BIO_free(f_bio1);
  35943. BIO_free(f_bio2);
  35944. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  35945. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35946. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35947. BIO_free(f_bio1);
  35948. }
  35949. #endif /* !defined(NO_FILESYSTEM) */
  35950. /* BIO info callback */
  35951. {
  35952. const char* testArg = "test";
  35953. BIO* cb_bio;
  35954. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  35955. BIO_set_callback(cb_bio, bioCallback);
  35956. AssertNotNull(BIO_get_callback(cb_bio));
  35957. BIO_set_callback(cb_bio, NULL);
  35958. AssertNull(BIO_get_callback(cb_bio));
  35959. BIO_set_callback_arg(cb_bio, (char*)testArg);
  35960. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  35961. AssertNull(BIO_get_callback_arg(NULL));
  35962. BIO_free(cb_bio);
  35963. }
  35964. /* BIO_vfree */
  35965. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  35966. BIO_vfree(NULL);
  35967. BIO_vfree(bio1);
  35968. res = TEST_RES_CHECK(1);
  35969. #endif
  35970. return res;
  35971. }
  35972. #endif /* !NO_BIO */
  35973. static int test_wolfSSL_a2i_IPADDRESS(void)
  35974. {
  35975. int res = TEST_SKIPPED;
  35976. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  35977. const unsigned char* data;
  35978. int dataSz = 0;
  35979. ASN1_OCTET_STRING *st;
  35980. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  35981. const unsigned char ipv6_exp[] = {
  35982. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35983. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  35984. };
  35985. const unsigned char ipv6_home[] = {
  35986. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35987. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  35988. };
  35989. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  35990. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  35991. data = ASN1_STRING_get0_data(st);
  35992. dataSz = ASN1_STRING_length(st);
  35993. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  35994. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  35995. ASN1_STRING_free(st);
  35996. AssertNotNull(st = a2i_IPADDRESS("::1"));
  35997. data = ASN1_STRING_get0_data(st);
  35998. dataSz = ASN1_STRING_length(st);
  35999. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  36000. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  36001. ASN1_STRING_free(st);
  36002. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  36003. data = ASN1_STRING_get0_data(st);
  36004. dataSz = ASN1_STRING_length(st);
  36005. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  36006. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  36007. ASN1_STRING_free(st);
  36008. res = TEST_RES_CHECK(1);
  36009. #endif
  36010. return res;
  36011. }
  36012. static int test_wolfSSL_DES_ecb_encrypt(void)
  36013. {
  36014. int res = TEST_SKIPPED;
  36015. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  36016. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  36017. WOLFSSL_DES_key_schedule key;
  36018. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  36019. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  36020. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  36021. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  36022. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  36023. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  36024. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  36025. /* Encrypt messages */
  36026. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  36027. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  36028. {
  36029. /* Decrypt messages */
  36030. int ret1 = 0;
  36031. int ret2 = 0;
  36032. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  36033. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  36034. AssertIntEQ(ret1,0);
  36035. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  36036. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  36037. AssertIntEQ(ret2,0);
  36038. }
  36039. res = TEST_RES_CHECK(1);
  36040. #endif
  36041. return res;
  36042. }
  36043. static int test_wolfSSL_X509_cmp_time(void)
  36044. {
  36045. int res = TEST_SKIPPED;
  36046. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  36047. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  36048. WOLFSSL_ASN1_TIME asn_time;
  36049. time_t t;
  36050. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  36051. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  36052. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  36053. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  36054. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  36055. res = TEST_RES_CHECK(1);
  36056. #endif
  36057. return res;
  36058. }
  36059. static int test_wolfSSL_X509_time_adj(void)
  36060. {
  36061. int res = TEST_SKIPPED;
  36062. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  36063. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  36064. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  36065. !defined(NO_ASN_TIME)
  36066. X509* x509;
  36067. time_t t, not_before, not_after;
  36068. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36069. client_cert_der_2048, sizeof_client_cert_der_2048,
  36070. WOLFSSL_FILETYPE_ASN1));
  36071. t = 0;
  36072. not_before = wc_Time(0);
  36073. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  36074. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  36075. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  36076. /* Check X509_gmtime_adj, too. */
  36077. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  36078. X509_free(x509);
  36079. res = TEST_RES_CHECK(1);
  36080. #endif
  36081. return res;
  36082. }
  36083. static int test_wolfSSL_X509(void)
  36084. {
  36085. int res = TEST_SKIPPED;
  36086. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  36087. && !defined(NO_RSA)
  36088. X509* x509;
  36089. #ifndef NO_BIO
  36090. BIO* bio;
  36091. X509_STORE_CTX* ctx;
  36092. X509_STORE* store;
  36093. #endif
  36094. char der[] = "certs/ca-cert.der";
  36095. XFILE fp;
  36096. AssertNotNull(x509 = X509_new());
  36097. X509_free(x509);
  36098. #ifndef NO_BIO
  36099. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  36100. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36101. #ifdef WOLFSSL_CERT_GEN
  36102. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  36103. #endif
  36104. AssertNotNull(ctx = X509_STORE_CTX_new());
  36105. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  36106. AssertNotNull(store = X509_STORE_new());
  36107. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  36108. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  36109. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  36110. X509_STORE_CTX_free(ctx);
  36111. X509_STORE_free(store);
  36112. X509_free(x509);
  36113. BIO_free(bio);
  36114. #endif
  36115. /** d2i_X509_fp test **/
  36116. fp = XFOPEN(der, "rb");
  36117. AssertTrue((fp != XBADFILE));
  36118. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  36119. AssertNotNull(x509);
  36120. X509_free(x509);
  36121. XFCLOSE(fp);
  36122. fp = XFOPEN(der, "rb");
  36123. AssertTrue((fp != XBADFILE));
  36124. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  36125. AssertNotNull(x509);
  36126. X509_free(x509);
  36127. XFCLOSE(fp);
  36128. /* X509_up_ref test */
  36129. AssertIntEQ(X509_up_ref(NULL), 0);
  36130. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  36131. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  36132. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  36133. X509_free(x509); /* refCount = 2 */
  36134. X509_free(x509); /* refCount = 1 */
  36135. X509_free(x509); /* refCount = 0, free */
  36136. res = TEST_RES_CHECK(1);
  36137. #endif
  36138. return res;
  36139. }
  36140. static int test_wolfSSL_X509_get_ext_count(void)
  36141. {
  36142. int res = TEST_SKIPPED;
  36143. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36144. !defined(NO_RSA)
  36145. int ret = 0;
  36146. WOLFSSL_X509* x509;
  36147. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36148. FILE* f;
  36149. /* NULL parameter check */
  36150. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36151. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36152. SSL_FILETYPE_PEM));
  36153. AssertIntEQ(X509_get_ext_count(x509), 5);
  36154. wolfSSL_X509_free(x509);
  36155. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36156. SSL_FILETYPE_PEM));
  36157. AssertIntEQ(X509_get_ext_count(x509), 5);
  36158. wolfSSL_X509_free(x509);
  36159. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  36160. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36161. fclose(f);
  36162. /* wolfSSL_X509_get_ext_count() valid input */
  36163. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36164. /* wolfSSL_X509_get_ext_count() NULL argument */
  36165. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36166. wolfSSL_X509_free(x509);
  36167. res = TEST_RES_CHECK(1);
  36168. #endif
  36169. return res;
  36170. }
  36171. static int test_wolfSSL_X509_sign2(void)
  36172. {
  36173. int res = TEST_SKIPPED;
  36174. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36175. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36176. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36177. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36178. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36179. WOLFSSL_X509 *x509, *ca;
  36180. const unsigned char *der;
  36181. const unsigned char *pt;
  36182. WOLFSSL_EVP_PKEY *priv;
  36183. WOLFSSL_X509_NAME *name;
  36184. int derSz;
  36185. #ifndef NO_ASN_TIME
  36186. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  36187. const int year = 365*24*60*60;
  36188. const int day = 24*60*60;
  36189. const int hour = 60*60;
  36190. const int mini = 60;
  36191. time_t t;
  36192. #endif
  36193. const unsigned char expected[] = {
  36194. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36195. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  36196. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  36197. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36198. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36199. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36200. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36201. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36202. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36203. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36204. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36205. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36206. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36207. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36208. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36209. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36210. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36211. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36212. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36213. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36214. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36215. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36216. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36217. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36218. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36219. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36220. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36221. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36222. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36223. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36224. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36225. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36226. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36227. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36228. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36229. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36230. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36231. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36232. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36233. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36234. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36235. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36236. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36237. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36238. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36239. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36240. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36241. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36242. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36243. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36244. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36245. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36246. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36247. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36248. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36249. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36250. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36251. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36252. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36253. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36254. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36255. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36256. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36257. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36258. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36259. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36260. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36261. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36262. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36263. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36264. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36265. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36266. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36267. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36268. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36269. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36270. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36271. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36272. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36273. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36274. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36275. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  36276. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  36277. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36278. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36279. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36280. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36281. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  36282. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  36283. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  36284. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  36285. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  36286. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  36287. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  36288. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  36289. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  36290. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  36291. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  36292. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  36293. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  36294. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  36295. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  36296. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  36297. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  36298. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  36299. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  36300. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  36301. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  36302. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  36303. };
  36304. pt = ca_key_der_2048;
  36305. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36306. sizeof_ca_key_der_2048));
  36307. pt = client_cert_der_2048;
  36308. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36309. sizeof_client_cert_der_2048));
  36310. pt = ca_cert_der_2048;
  36311. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36312. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36313. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36314. #ifndef NO_ASN_TIME
  36315. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36316. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36317. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36318. AssertIntEQ(notAfter->length, 13);
  36319. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36320. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36321. #endif
  36322. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  36323. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36324. AssertIntEQ(derSz, sizeof(expected));
  36325. #ifndef NO_ASN_TIME
  36326. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  36327. #endif
  36328. wolfSSL_X509_free(ca);
  36329. wolfSSL_X509_free(x509);
  36330. wolfSSL_EVP_PKEY_free(priv);
  36331. #ifndef NO_ASN_TIME
  36332. wolfSSL_ASN1_TIME_free(notBefore);
  36333. wolfSSL_ASN1_TIME_free(notAfter);
  36334. #endif
  36335. res = TEST_RES_CHECK(1);
  36336. #endif
  36337. return res;
  36338. }
  36339. static int test_wolfSSL_X509_sign(void)
  36340. {
  36341. int res = TEST_SKIPPED;
  36342. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36343. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36344. int ret;
  36345. char *cn;
  36346. word32 cnSz;
  36347. X509_NAME *name;
  36348. X509 *x509, *ca;
  36349. DecodedCert dCert;
  36350. EVP_PKEY *pub;
  36351. EVP_PKEY *priv;
  36352. EVP_MD_CTX *mctx;
  36353. #if defined(USE_CERT_BUFFERS_1024)
  36354. const unsigned char* rsaPriv = client_key_der_1024;
  36355. const unsigned char* rsaPub = client_keypub_der_1024;
  36356. const unsigned char* certIssuer = client_cert_der_1024;
  36357. long clientKeySz = (long)sizeof_client_key_der_1024;
  36358. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  36359. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  36360. #elif defined(USE_CERT_BUFFERS_2048)
  36361. const unsigned char* rsaPriv = client_key_der_2048;
  36362. const unsigned char* rsaPub = client_keypub_der_2048;
  36363. const unsigned char* certIssuer = client_cert_der_2048;
  36364. long clientKeySz = (long)sizeof_client_key_der_2048;
  36365. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  36366. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  36367. #endif
  36368. byte sn[16];
  36369. int snSz = sizeof(sn);
  36370. /* Set X509_NAME fields */
  36371. AssertNotNull(name = X509_NAME_new());
  36372. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  36373. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  36374. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  36375. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  36376. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  36377. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  36378. /* Get private and public keys */
  36379. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  36380. clientKeySz));
  36381. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  36382. AssertNotNull(x509 = X509_new());
  36383. /* Set version 3 */
  36384. AssertIntNE(X509_set_version(x509, 2L), 0);
  36385. /* Set subject name, add pubkey, and sign certificate */
  36386. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  36387. X509_NAME_free(name);
  36388. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  36389. #ifdef WOLFSSL_ALT_NAMES
  36390. /* Add some subject alt names */
  36391. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  36392. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  36393. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  36394. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  36395. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  36396. "sphygmomanometer",
  36397. ASN_DNS_TYPE), SSL_SUCCESS);
  36398. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  36399. "supercalifragilisticexpialidocious",
  36400. ASN_DNS_TYPE), SSL_SUCCESS);
  36401. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  36402. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  36403. ASN_DNS_TYPE), SSL_SUCCESS);
  36404. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36405. {
  36406. unsigned char ip4_type[] = {127,128,0,255};
  36407. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  36408. 0xff, 0xee, 0x99, 0x88,
  36409. 0x77, 0x66, 0x55, 0x44,
  36410. 0x00, 0x33, 0x22, 0x11};
  36411. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  36412. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  36413. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  36414. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  36415. }
  36416. #endif
  36417. #endif /* WOLFSSL_ALT_NAMES */
  36418. /* test valid sign case */
  36419. AssertIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  36420. /* test valid X509_sign_ctx case */
  36421. AssertNotNull(mctx = EVP_MD_CTX_new());
  36422. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  36423. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  36424. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  36425. AssertIntEQ(X509_get_ext_count(x509), 1);
  36426. #endif
  36427. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36428. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  36429. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  36430. #endif
  36431. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  36432. WOLFSSL_SUCCESS);
  36433. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  36434. /* Variation in size depends on ASN.1 encoding when MSB is set.
  36435. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  36436. * with the MSB set. See GenerateInteger in asn.c */
  36437. #ifndef USE_CERT_BUFFERS_1024
  36438. #ifndef WOLFSSL_ALT_NAMES
  36439. /* Valid case - size should be 781-786 with 16 byte serial number */
  36440. AssertTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  36441. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36442. /* Valid case - size should be 955-960 with 16 byte serial number */
  36443. AssertTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  36444. #else
  36445. /* Valid case - size should be 926-931 with 16 byte serial number */
  36446. AssertTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  36447. #endif
  36448. #else
  36449. #ifndef WOLFSSL_ALT_NAMES
  36450. /* Valid case - size should be 537-542 with 16 byte serial number */
  36451. AssertTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  36452. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36453. /* Valid case - size should be 695-670 with 16 byte serial number */
  36454. AssertTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  36455. #else
  36456. /* Valid case - size should be 666-671 with 16 byte serial number */
  36457. AssertTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  36458. #endif
  36459. #endif
  36460. /* check that issuer name is as expected after signature */
  36461. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  36462. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  36463. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  36464. AssertNotNull(name = X509_get_subject_name(ca));
  36465. cnSz = X509_NAME_get_sz(name);
  36466. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36467. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36468. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  36469. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36470. #ifdef WOLFSSL_MULTI_ATTRIB
  36471. /* test adding multiple OU's to the signer */
  36472. AssertNotNull(name = X509_get_subject_name(ca));
  36473. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36474. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  36475. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36476. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  36477. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  36478. #endif
  36479. AssertNotNull(name = X509_get_subject_name(ca));
  36480. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  36481. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36482. AssertNotNull(name = X509_get_issuer_name(x509));
  36483. cnSz = X509_NAME_get_sz(name);
  36484. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36485. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36486. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  36487. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  36488. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36489. FreeDecodedCert(&dCert);
  36490. /* Test invalid parameters */
  36491. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  36492. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  36493. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  36494. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  36495. EVP_MD_CTX_free(mctx);
  36496. AssertNotNull(mctx = EVP_MD_CTX_new());
  36497. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  36498. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  36499. /* test invalid version number */
  36500. #if defined(OPENSSL_ALL)
  36501. AssertIntNE(X509_set_version(x509, 6L), 0);
  36502. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36503. /* uses ParseCert which fails on bad version number */
  36504. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  36505. #endif
  36506. EVP_MD_CTX_free(mctx);
  36507. EVP_PKEY_free(priv);
  36508. EVP_PKEY_free(pub);
  36509. X509_free(x509);
  36510. X509_free(ca);
  36511. res = TEST_RES_CHECK(1);
  36512. #endif
  36513. return res;
  36514. }
  36515. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  36516. {
  36517. int res = TEST_SKIPPED;
  36518. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36519. X509* x509 = NULL;
  36520. const X509_ALGOR* alg;
  36521. AssertNotNull(x509 = X509_new());
  36522. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  36523. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  36524. X509_free(x509);
  36525. res = TEST_RES_CHECK(1);
  36526. #endif
  36527. return res;
  36528. }
  36529. static int test_wolfSSL_X509_ALGOR_get0(void)
  36530. {
  36531. int res = TEST_SKIPPED;
  36532. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36533. !defined(NO_SHA256) && !defined(NO_RSA)
  36534. X509* x509 = NULL;
  36535. const ASN1_OBJECT* obj = NULL;
  36536. const X509_ALGOR* alg;
  36537. int pptype = 0;
  36538. const void *ppval = NULL;
  36539. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36540. SSL_FILETYPE_PEM));
  36541. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  36542. /* Invalid case */
  36543. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  36544. AssertNull(obj);
  36545. /* Valid case */
  36546. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  36547. AssertNotNull(obj);
  36548. AssertNull(ppval);
  36549. AssertIntNE(pptype, 0);
  36550. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  36551. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  36552. X509_free(x509);
  36553. res = TEST_RES_CHECK(1);
  36554. #endif
  36555. return res;
  36556. }
  36557. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  36558. {
  36559. int res = TEST_SKIPPED;
  36560. #if defined(OPENSSL_EXTRA)
  36561. X509_VERIFY_PARAM *paramTo;
  36562. X509_VERIFY_PARAM *paramFrom;
  36563. int ret;
  36564. char testIPv4[] = "127.0.0.1";
  36565. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  36566. char testhostName1[] = "foo.hoge.com";
  36567. char testhostName2[] = "foobar.hoge.com";
  36568. paramTo = X509_VERIFY_PARAM_new();
  36569. AssertNotNull(paramTo);
  36570. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  36571. paramFrom = X509_VERIFY_PARAM_new();
  36572. AssertNotNull(paramFrom);
  36573. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  36574. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  36575. (int)XSTRLEN(testhostName1));
  36576. AssertIntEQ(1, ret);
  36577. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  36578. (int)XSTRLEN(testhostName1)));
  36579. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  36580. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  36581. AssertIntEQ(0x01, paramFrom->hostFlags);
  36582. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  36583. AssertIntEQ(0, ret);
  36584. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  36585. AssertIntEQ(1, ret);
  36586. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36587. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  36588. AssertIntEQ(1, ret);
  36589. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  36590. AssertIntEQ(1, ret);
  36591. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36592. /* null pointer */
  36593. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  36594. AssertIntEQ(WOLFSSL_FAILURE, ret);
  36595. /* in the case of "from" null, returns success */
  36596. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  36597. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  36598. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  36599. AssertIntEQ(WOLFSSL_FAILURE, ret);
  36600. /* inherit flags test : VPARAM_DEFAULT */
  36601. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  36602. AssertIntEQ(1, ret);
  36603. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36604. (int)XSTRLEN(testhostName1)));
  36605. AssertIntEQ(0x01, paramTo->hostFlags);
  36606. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36607. /* inherit flags test : VPARAM OVERWRITE */
  36608. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36609. (int)XSTRLEN(testhostName2));
  36610. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  36611. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36612. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  36613. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  36614. AssertIntEQ(1, ret);
  36615. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36616. (int)XSTRLEN(testhostName1)));
  36617. AssertIntEQ(0x01, paramTo->hostFlags);
  36618. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36619. /* inherit flags test : VPARAM_RESET_FLAGS */
  36620. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36621. (int)XSTRLEN(testhostName2));
  36622. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  36623. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  36624. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  36625. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  36626. AssertIntEQ(1, ret);
  36627. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36628. (int)XSTRLEN(testhostName1)));
  36629. AssertIntEQ(0x01, paramTo->hostFlags);
  36630. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36631. /* inherit flags test : VPARAM_LOCKED */
  36632. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36633. (int)XSTRLEN(testhostName2));
  36634. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  36635. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36636. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  36637. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  36638. AssertIntEQ(1, ret);
  36639. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  36640. (int)XSTRLEN(testhostName2)));
  36641. AssertIntEQ(0x00, paramTo->hostFlags);
  36642. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36643. /* test for incorrect parameters */
  36644. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  36645. AssertIntEQ(0, ret);
  36646. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  36647. AssertIntEQ(0, ret);
  36648. /* inherit flags test : VPARAM_ONCE, not testable yet */
  36649. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  36650. AssertIntEQ(1, ret);
  36651. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  36652. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  36653. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  36654. AssertIntEQ(1, ret);
  36655. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  36656. AssertIntEQ(0, ret);
  36657. X509_VERIFY_PARAM_free(paramTo);
  36658. X509_VERIFY_PARAM_free(paramFrom);
  36659. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  36660. res = TEST_RES_CHECK(1);
  36661. #endif
  36662. return res;
  36663. }
  36664. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  36665. static int test_wolfSSL_check_domain_verify_count = 0;
  36666. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  36667. WOLFSSL_X509_STORE_CTX* store)
  36668. {
  36669. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  36670. AssertIntEQ(preverify, 1);
  36671. test_wolfSSL_check_domain_verify_count++;
  36672. return 1;
  36673. }
  36674. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  36675. {
  36676. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  36677. /* Domain check should only be done on the leaf cert */
  36678. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  36679. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  36680. "wolfSSL Server Chain", 0), 1);
  36681. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  36682. test_wolfSSL_check_domain_verify_cb);
  36683. }
  36684. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  36685. {
  36686. /* Use a cert with different domains in chain */
  36687. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  36688. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  36689. }
  36690. static int test_wolfSSL_check_domain(void)
  36691. {
  36692. tcp_ready ready;
  36693. func_args client_args;
  36694. func_args server_args;
  36695. THREAD_TYPE serverThread;
  36696. callback_functions func_cb_client;
  36697. callback_functions func_cb_server;
  36698. XMEMSET(&client_args, 0, sizeof(func_args));
  36699. XMEMSET(&server_args, 0, sizeof(func_args));
  36700. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  36701. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  36702. #ifdef WOLFSSL_TIRTOS
  36703. fdOpenSession(Task_self());
  36704. #endif
  36705. StartTCP();
  36706. InitTcpReady(&ready);
  36707. #if defined(USE_WINDOWS_API)
  36708. /* use RNG to get random port if using windows */
  36709. ready.port = GetRandomPort();
  36710. #endif
  36711. server_args.signal = &ready;
  36712. client_args.signal = &ready;
  36713. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  36714. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  36715. client_args.callbacks = &func_cb_client;
  36716. server_args.callbacks = &func_cb_server;
  36717. start_thread(test_server_nofail, &server_args, &serverThread);
  36718. wait_tcp_ready(&server_args);
  36719. test_client_nofail(&client_args, NULL);
  36720. join_thread(serverThread);
  36721. AssertTrue(client_args.return_code);
  36722. AssertTrue(server_args.return_code);
  36723. FreeTcpReady(&ready);
  36724. /* Should have been called once for each cert in sent chain */
  36725. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  36726. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  36727. #else
  36728. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  36729. #endif
  36730. return TEST_RES_CHECK(1);
  36731. }
  36732. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  36733. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  36734. {
  36735. int res = TEST_SKIPPED;
  36736. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36737. X509* x509 = NULL;
  36738. X509_PUBKEY* pubKey;
  36739. AssertNotNull(x509 = X509_new());
  36740. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  36741. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  36742. X509_free(x509);
  36743. res = TEST_RES_CHECK(1);
  36744. #endif
  36745. return res;
  36746. }
  36747. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  36748. {
  36749. int res = TEST_SKIPPED;
  36750. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36751. !defined(NO_SHA256) && !defined(NO_RSA)
  36752. X509* x509 = NULL;
  36753. ASN1_OBJECT* obj = NULL;
  36754. const ASN1_OBJECT* pa_oid = NULL;
  36755. X509_PUBKEY* pubKey;
  36756. X509_PUBKEY* pubKey2;
  36757. EVP_PKEY* evpKey;
  36758. const unsigned char *pk;
  36759. int ppklen, pptype;
  36760. X509_ALGOR *pa;
  36761. const void *pval;
  36762. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  36763. SSL_FILETYPE_PEM));
  36764. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36765. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36766. AssertNotNull(pk);
  36767. AssertNotNull(pa);
  36768. AssertNotNull(pubKey);
  36769. AssertIntGT(ppklen, 0);
  36770. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  36771. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36772. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  36773. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36774. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36775. AssertNotNull(pk);
  36776. AssertNotNull(pa);
  36777. AssertIntGT(ppklen, 0);
  36778. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36779. AssertNotNull(pa_oid);
  36780. AssertNull(pval);
  36781. AssertIntEQ(pptype, V_ASN1_NULL);
  36782. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  36783. X509_PUBKEY_free(pubKey2);
  36784. X509_free(x509);
  36785. EVP_PKEY_free(evpKey);
  36786. res = TEST_RES_CHECK(1);
  36787. #endif
  36788. return res;
  36789. }
  36790. static int test_wolfSSL_X509_PUBKEY_EC(void)
  36791. {
  36792. int res = TEST_SKIPPED;
  36793. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36794. X509* x509 = NULL;
  36795. ASN1_OBJECT* obj = NULL;
  36796. ASN1_OBJECT* poid;
  36797. const ASN1_OBJECT* pa_oid = NULL;
  36798. X509_PUBKEY* pubKey;
  36799. X509_PUBKEY* pubKey2;
  36800. EVP_PKEY* evpKey;
  36801. const unsigned char *pk;
  36802. int ppklen, pptype;
  36803. X509_ALGOR *pa;
  36804. const void *pval;
  36805. char buf[50];
  36806. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36807. SSL_FILETYPE_PEM));
  36808. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36809. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36810. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  36811. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36812. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36813. AssertNotNull(pk);
  36814. AssertNotNull(pa);
  36815. AssertIntGT(ppklen, 0);
  36816. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36817. AssertNotNull(pa_oid);
  36818. AssertNotNull(pval);
  36819. AssertIntEQ(pptype, V_ASN1_OBJECT);
  36820. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36821. poid = (ASN1_OBJECT *)pval;
  36822. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36823. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36824. X509_PUBKEY_free(pubKey2);
  36825. X509_free(x509);
  36826. EVP_PKEY_free(evpKey);
  36827. res = TEST_RES_CHECK(1);
  36828. #endif
  36829. return res;
  36830. }
  36831. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36832. {
  36833. int res = TEST_SKIPPED;
  36834. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36835. word32 bytes;
  36836. #ifdef USE_CERT_BUFFERS_1024
  36837. byte tmp[ONEK_BUF];
  36838. #elif defined(USE_CERT_BUFFERS_2048)
  36839. byte tmp[TWOK_BUF];
  36840. #else
  36841. byte tmp[TWOK_BUF];
  36842. #endif /* END USE_CERT_BUFFERS_1024 */
  36843. const unsigned char* dsaKeyDer = tmp;
  36844. ASN1_OBJECT* obj = NULL;
  36845. ASN1_STRING* str;
  36846. const ASN1_OBJECT* pa_oid = NULL;
  36847. X509_PUBKEY* pubKey = NULL;
  36848. EVP_PKEY* evpKey = NULL;
  36849. const unsigned char *pk;
  36850. int ppklen, pptype;
  36851. X509_ALGOR *pa;
  36852. const void *pval;
  36853. #ifdef USE_CERT_BUFFERS_1024
  36854. XMEMSET(tmp, 0, sizeof(tmp));
  36855. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36856. bytes = sizeof_dsa_key_der_1024;
  36857. #elif defined(USE_CERT_BUFFERS_2048)
  36858. XMEMSET(tmp, 0, sizeof(tmp));
  36859. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36860. bytes = sizeof_dsa_key_der_2048;
  36861. #else
  36862. {
  36863. XFILE fp;
  36864. XMEMSET(tmp, 0, sizeof(tmp));
  36865. fp = XFOPEN("./certs/dsa2048.der", "rb");
  36866. if (fp == XBADFILE) {
  36867. return WOLFSSL_BAD_FILE;
  36868. }
  36869. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  36870. XFCLOSE(fp);
  36871. }
  36872. #endif
  36873. /* Initialize pkey with der format dsa key */
  36874. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  36875. AssertNotNull(pubKey = X509_PUBKEY_new());
  36876. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  36877. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36878. AssertNotNull(pk);
  36879. AssertNotNull(pa);
  36880. AssertIntGT(ppklen, 0);
  36881. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36882. AssertNotNull(pa_oid);
  36883. AssertNotNull(pval);
  36884. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  36885. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  36886. str = (ASN1_STRING *)pval;
  36887. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  36888. #ifdef USE_CERT_BUFFERS_1024
  36889. AssertIntEQ(ASN1_STRING_length(str), 291);
  36890. #else
  36891. AssertIntEQ(ASN1_STRING_length(str), 549);
  36892. #endif /* END USE_CERT_BUFFERS_1024 */
  36893. X509_PUBKEY_free(pubKey);
  36894. EVP_PKEY_free(evpKey);
  36895. res = TEST_RES_CHECK(1);
  36896. #endif
  36897. return res;
  36898. }
  36899. static int test_wolfSSL_RAND(void)
  36900. {
  36901. int res = TEST_SKIPPED;
  36902. #if defined(OPENSSL_EXTRA)
  36903. byte seed[16];
  36904. XMEMSET(seed, 0, sizeof(seed));
  36905. RAND_seed(seed, sizeof(seed));
  36906. AssertIntEQ(RAND_poll(), 1);
  36907. RAND_cleanup();
  36908. AssertIntEQ(RAND_egd(NULL), -1);
  36909. #ifndef NO_FILESYSTEM
  36910. {
  36911. char fname[100];
  36912. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  36913. AssertIntEQ(RAND_write_file(NULL), 0);
  36914. }
  36915. #endif
  36916. res = TEST_RES_CHECK(1);
  36917. #endif
  36918. return res;
  36919. }
  36920. static int test_wolfSSL_BUF(void)
  36921. {
  36922. int res = TEST_SKIPPED;
  36923. #if defined(OPENSSL_EXTRA)
  36924. BUF_MEM* buf;
  36925. AssertNotNull(buf = BUF_MEM_new());
  36926. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  36927. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  36928. BUF_MEM_free(buf);
  36929. res = TEST_RES_CHECK(1);
  36930. #endif
  36931. return res;
  36932. }
  36933. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36934. static int stub_rand_seed(const void *buf, int num)
  36935. {
  36936. (void)buf;
  36937. (void)num;
  36938. return 123;
  36939. }
  36940. static int stub_rand_bytes(unsigned char *buf, int num)
  36941. {
  36942. (void)buf;
  36943. (void)num;
  36944. return 456;
  36945. }
  36946. static byte* was_stub_rand_cleanup_called(void)
  36947. {
  36948. static byte was_called = 0;
  36949. return &was_called;
  36950. }
  36951. static void stub_rand_cleanup(void)
  36952. {
  36953. byte* was_called = was_stub_rand_cleanup_called();
  36954. *was_called = 1;
  36955. return;
  36956. }
  36957. static byte* was_stub_rand_add_called(void)
  36958. {
  36959. static byte was_called = 0;
  36960. return &was_called;
  36961. }
  36962. static int stub_rand_add(const void *buf, int num, double entropy)
  36963. {
  36964. byte* was_called = was_stub_rand_add_called();
  36965. (void)buf;
  36966. (void)num;
  36967. (void)entropy;
  36968. *was_called = 1;
  36969. return 0;
  36970. }
  36971. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  36972. {
  36973. (void)buf;
  36974. (void)num;
  36975. return 9876;
  36976. }
  36977. static int stub_rand_status(void)
  36978. {
  36979. return 5432;
  36980. }
  36981. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36982. static int test_wolfSSL_RAND_set_rand_method(void)
  36983. {
  36984. int res = TEST_SKIPPED;
  36985. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36986. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  36987. unsigned char* buf = NULL;
  36988. int num = 0;
  36989. double entropy = 0;
  36990. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  36991. byte* was_add_called = was_stub_rand_add_called();
  36992. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  36993. DYNAMIC_TYPE_TMP_BUFFER);
  36994. AssertIntNE(wolfSSL_RAND_status(), 5432);
  36995. AssertIntEQ(*was_cleanup_called, 0);
  36996. RAND_cleanup();
  36997. AssertIntEQ(*was_cleanup_called, 0);
  36998. rand_methods.seed = &stub_rand_seed;
  36999. rand_methods.bytes = &stub_rand_bytes;
  37000. rand_methods.cleanup = &stub_rand_cleanup;
  37001. rand_methods.add = &stub_rand_add;
  37002. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  37003. rand_methods.status = &stub_rand_status;
  37004. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  37005. AssertIntEQ(RAND_seed(buf, num), 123);
  37006. AssertIntEQ(RAND_bytes(buf, num), 456);
  37007. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  37008. AssertIntEQ(RAND_status(), 5432);
  37009. AssertIntEQ(*was_add_called, 0);
  37010. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  37011. RAND_add(buf, num, entropy);
  37012. AssertIntEQ(*was_add_called, 1);
  37013. was_add_called = 0;
  37014. AssertIntEQ(*was_cleanup_called, 0);
  37015. RAND_cleanup();
  37016. AssertIntEQ(*was_cleanup_called, 1);
  37017. *was_cleanup_called = 0;
  37018. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  37019. AssertIntNE(RAND_status(), 5432);
  37020. AssertIntEQ(*was_cleanup_called, 0);
  37021. RAND_cleanup();
  37022. AssertIntEQ(*was_cleanup_called, 0);
  37023. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37024. res = TEST_RES_CHECK(1);
  37025. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37026. return res;
  37027. }
  37028. static int test_wolfSSL_RAND_bytes(void)
  37029. {
  37030. int res = TEST_SKIPPED;
  37031. #if defined(OPENSSL_EXTRA)
  37032. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  37033. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  37034. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  37035. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  37036. int max_bufsize;
  37037. byte *my_buf;
  37038. /* sanity check */
  37039. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  37040. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  37041. max_bufsize = size4;
  37042. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  37043. DYNAMIC_TYPE_TMP_BUFFER);
  37044. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  37045. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  37046. AssertNotNull(my_buf);
  37047. XMEMSET(my_buf, 0, max_bufsize);
  37048. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  37049. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  37050. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  37051. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  37052. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37053. res = TEST_RES_CHECK(1);
  37054. #endif
  37055. return res;
  37056. }
  37057. static int test_wolfSSL_PKCS8_Compat(void)
  37058. {
  37059. int res = TEST_SKIPPED;
  37060. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  37061. #ifndef NO_BIO
  37062. PKCS8_PRIV_KEY_INFO* pt;
  37063. BIO* bio;
  37064. XFILE f;
  37065. int bytes;
  37066. char pkcs8_buffer[512];
  37067. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37068. EVP_PKEY *pkey = NULL;
  37069. #endif
  37070. /* file from wolfssl/certs/ directory */
  37071. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  37072. AssertTrue(f != XBADFILE);
  37073. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  37074. XFCLOSE(f);
  37075. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37076. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  37077. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37078. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  37079. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  37080. /* gets PKCS8 pointer to pkey */
  37081. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  37082. EVP_PKEY_free(pkey);
  37083. #endif
  37084. BIO_free(bio);
  37085. PKCS8_PRIV_KEY_INFO_free(pt);
  37086. res = TEST_RES_CHECK(1);
  37087. #endif
  37088. #endif
  37089. return res;
  37090. }
  37091. static int test_wolfSSL_PKCS8_d2i(void)
  37092. {
  37093. int res = TEST_SKIPPED;
  37094. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  37095. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  37096. * requires a 12 byte password in FIPS mode. This test ends up
  37097. * trying to use an 8 byte password. */
  37098. #ifndef NO_FILESYSTEM
  37099. unsigned char pkcs8_buffer[2048];
  37100. const unsigned char* p;
  37101. int bytes;
  37102. XFILE file;
  37103. WOLFSSL_EVP_PKEY* pkey = NULL;
  37104. #ifndef NO_BIO
  37105. BIO* bio;
  37106. #if defined(OPENSSL_ALL) && \
  37107. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  37108. defined(HAVE_ECC)) && \
  37109. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37110. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  37111. #endif
  37112. #endif
  37113. #ifndef NO_RSA
  37114. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  37115. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  37116. #ifndef NO_DES3
  37117. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  37118. #endif
  37119. #endif /* NO_RSA */
  37120. #ifdef HAVE_ECC
  37121. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  37122. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  37123. #ifndef NO_DES3
  37124. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  37125. #endif
  37126. #endif /* HAVE_ECC */
  37127. #endif /* !NO_FILESYSTEM */
  37128. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  37129. #ifndef NO_RSA
  37130. #ifdef USE_CERT_BUFFERS_1024
  37131. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  37132. int rsaSz = sizeof_server_key_der_1024;
  37133. #else
  37134. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  37135. int rsaSz = sizeof_server_key_der_2048;
  37136. #endif
  37137. #endif
  37138. #ifdef HAVE_ECC
  37139. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  37140. int ecSz = sizeof_ecc_key_der_256;
  37141. #endif
  37142. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  37143. #ifndef NO_FILESYSTEM
  37144. (void)pkcs8_buffer;
  37145. (void)p;
  37146. (void)bytes;
  37147. (void)file;
  37148. #ifndef NO_BIO
  37149. (void)bio;
  37150. #endif
  37151. #endif
  37152. #ifdef OPENSSL_ALL
  37153. #ifndef NO_RSA
  37154. /* Try to auto-detect normal RSA private key */
  37155. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  37156. EVP_PKEY_free(pkey);
  37157. #endif
  37158. #ifdef HAVE_ECC
  37159. /* Try to auto-detect normal EC private key */
  37160. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  37161. EVP_PKEY_free(pkey);
  37162. #endif
  37163. #endif /* OPENSSL_ALL */
  37164. #ifndef NO_FILESYSTEM
  37165. #ifndef NO_RSA
  37166. /* Get DER encoded RSA PKCS#8 data. */
  37167. file = XFOPEN(rsaDerPkcs8File, "rb");
  37168. AssertTrue(file != XBADFILE);
  37169. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37170. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37171. file)), 0);
  37172. XFCLOSE(file);
  37173. p = pkcs8_buffer;
  37174. #ifdef OPENSSL_ALL
  37175. /* Try to decode - auto-detect key type. */
  37176. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37177. #else
  37178. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37179. #endif
  37180. /* Get PEM encoded RSA PKCS#8 data. */
  37181. file = XFOPEN(rsaPemPkcs8File, "rb");
  37182. AssertTrue(file != XBADFILE);
  37183. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37184. file)), 0);
  37185. XFCLOSE(file);
  37186. #if defined(OPENSSL_ALL) && \
  37187. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37188. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37189. /* Write PKCS#8 PEM to BIO. */
  37190. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37191. NULL), bytes);
  37192. /* Compare file and written data */
  37193. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37194. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37195. BIO_free(bio);
  37196. #if !defined(NO_DES3) && !defined(NO_SHA)
  37197. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37198. /* Write Encrypted PKCS#8 PEM to BIO. */
  37199. bytes = 1834;
  37200. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37201. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37202. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37203. (void*)"yassl123"));
  37204. EVP_PKEY_free(evpPkey);
  37205. BIO_free(bio);
  37206. #endif /* !NO_DES3 && !NO_SHA */
  37207. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37208. EVP_PKEY_free(pkey);
  37209. /* PKCS#8 encrypted RSA key */
  37210. #ifndef NO_DES3
  37211. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  37212. AssertTrue(file != XBADFILE);
  37213. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37214. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37215. file)), 0);
  37216. XFCLOSE(file);
  37217. #if defined(OPENSSL_ALL) && \
  37218. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37219. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37220. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37221. (void*)"yassl123"));
  37222. EVP_PKEY_free(pkey);
  37223. BIO_free(bio);
  37224. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37225. #endif /* !NO_DES3 */
  37226. #endif /* NO_RSA */
  37227. #ifdef HAVE_ECC
  37228. /* PKCS#8 encode EC key */
  37229. file = XFOPEN(ecDerPkcs8File, "rb");
  37230. AssertTrue(file != XBADFILE);
  37231. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37232. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37233. file)), 0);
  37234. XFCLOSE(file);
  37235. p = pkcs8_buffer;
  37236. #ifdef OPENSSL_ALL
  37237. /* Try to decode - auto-detect key type. */
  37238. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37239. #else
  37240. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37241. #endif
  37242. /* Get PEM encoded RSA PKCS#8 data. */
  37243. file = XFOPEN(ecPemPkcs8File, "rb");
  37244. AssertTrue(file != XBADFILE);
  37245. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37246. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37247. file)), 0);
  37248. XFCLOSE(file);
  37249. #if defined(OPENSSL_ALL) && \
  37250. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37251. defined(HAVE_AES_CBC)
  37252. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37253. /* Write PKCS#8 PEM to BIO. */
  37254. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37255. NULL), bytes);
  37256. /* Compare file and written data */
  37257. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37258. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37259. BIO_free(bio);
  37260. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37261. /* Write Encrypted PKCS#8 PEM to BIO. */
  37262. bytes = 379;
  37263. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37264. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37265. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37266. (void*)"yassl123"));
  37267. EVP_PKEY_free(evpPkey);
  37268. BIO_free(bio);
  37269. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37270. EVP_PKEY_free(pkey);
  37271. /* PKCS#8 encrypted EC key */
  37272. #ifndef NO_DES3
  37273. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  37274. AssertTrue(file != XBADFILE);
  37275. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37276. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37277. file)), 0);
  37278. XFCLOSE(file);
  37279. #if defined(OPENSSL_ALL) && \
  37280. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37281. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37282. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37283. (void*)"yassl123"));
  37284. EVP_PKEY_free(pkey);
  37285. BIO_free(bio);
  37286. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37287. #endif /* !NO_DES3 */
  37288. #endif /* HAVE_ECC */
  37289. #endif /* !NO_FILESYSTEM */
  37290. res = TEST_RES_CHECK(1);
  37291. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37292. return res;
  37293. }
  37294. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37295. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37296. #define LOGGING_THREADS 5
  37297. #define ERROR_COUNT 10
  37298. /* copied from logging.c since this is not exposed otherwise */
  37299. #ifndef ERROR_QUEUE_MAX
  37300. #ifdef ERROR_QUEUE_PER_THREAD
  37301. #define ERROR_QUEUE_MAX 16
  37302. #else
  37303. /* this breaks from compat of unlimited error queue size */
  37304. #define ERROR_QUEUE_MAX 100
  37305. #endif
  37306. #endif
  37307. static volatile int loggingThreadsReady;
  37308. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  37309. {
  37310. const char* file;
  37311. int line;
  37312. unsigned long err;
  37313. int errorCount = 0;
  37314. int i;
  37315. (void)args;
  37316. while (!loggingThreadsReady);
  37317. for (i = 0; i < ERROR_COUNT; i++)
  37318. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37319. while ((err = ERR_get_error_line(&file, &line))) {
  37320. AssertIntEQ(err, 990 + errorCount);
  37321. errorCount++;
  37322. }
  37323. AssertIntEQ(errorCount, ERROR_COUNT);
  37324. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  37325. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  37326. errorCount = 0;
  37327. for (i = 0; i < ERROR_QUEUE_MAX + 3; 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. /* test that the 3 errors over the max were dropped */
  37334. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  37335. return 0;
  37336. }
  37337. #endif
  37338. static int test_error_queue_per_thread(void)
  37339. {
  37340. int res = TEST_SKIPPED;
  37341. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37342. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37343. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  37344. int i;
  37345. ERR_clear_error(); /* clear out any error nodes */
  37346. loggingThreadsReady = 0;
  37347. for (i = 0; i < LOGGING_THREADS; i++)
  37348. start_thread(test_logging, NULL, &loggingThreads[i]);
  37349. loggingThreadsReady = 1;
  37350. for (i = 0; i < LOGGING_THREADS; i++)
  37351. join_thread(loggingThreads[i]);
  37352. res = TEST_RES_CHECK(1);
  37353. #endif
  37354. return res;
  37355. }
  37356. static int test_wolfSSL_ERR_put_error(void)
  37357. {
  37358. int res = TEST_SKIPPED;
  37359. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37360. defined(DEBUG_WOLFSSL)
  37361. const char* file;
  37362. int line;
  37363. ERR_clear_error(); /* clear out any error nodes */
  37364. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37365. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  37366. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  37367. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  37368. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  37369. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  37370. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  37371. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  37372. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  37373. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  37374. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  37375. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  37376. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  37377. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  37378. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  37379. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  37380. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  37381. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  37382. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  37383. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  37384. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  37385. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  37386. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  37387. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  37388. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  37389. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  37390. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  37391. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  37392. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  37393. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  37394. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  37395. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  37396. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  37397. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  37398. "this file", 100);
  37399. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  37400. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37401. AssertIntEQ(line, 100);
  37402. AssertIntEQ(wolfSSL_ERR_peek_error(),
  37403. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37404. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  37405. #endif
  37406. /* try reading past end of error queue */
  37407. file = NULL;
  37408. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  37409. AssertNull(file);
  37410. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  37411. PEMerr(4,4);
  37412. AssertIntEQ(ERR_get_error(), 4);
  37413. /* Empty and free up all error nodes */
  37414. ERR_clear_error();
  37415. /* Verify all nodes are cleared */
  37416. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37417. ERR_clear_error();
  37418. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  37419. res = TEST_RES_CHECK(1);
  37420. #endif
  37421. return res;
  37422. }
  37423. /*
  37424. * This is a regression test for a bug where the peek/get error functions were
  37425. * drawing from the end of the queue rather than the front.
  37426. */
  37427. static int test_wolfSSL_ERR_get_error_order(void)
  37428. {
  37429. int res = TEST_SKIPPED;
  37430. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  37431. /* Empty the queue. */
  37432. wolfSSL_ERR_clear_error();
  37433. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  37434. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  37435. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  37436. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  37437. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  37438. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  37439. res = TEST_RES_CHECK(1);
  37440. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  37441. return res;
  37442. }
  37443. #ifndef NO_BIO
  37444. static int test_wolfSSL_ERR_print_errors(void)
  37445. {
  37446. int res = TEST_SKIPPED;
  37447. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37448. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  37449. BIO* bio;
  37450. char buf[1024];
  37451. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37452. ERR_clear_error(); /* clear out any error nodes */
  37453. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37454. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  37455. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  37456. ERR_print_errors(bio);
  37457. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  37458. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  37459. buf, 55), 0);
  37460. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  37461. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  37462. buf, 56), 0);
  37463. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  37464. AssertIntEQ(buf[0], '\0');
  37465. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  37466. BIO_free(bio);
  37467. res = TEST_RES_CHECK(1);
  37468. #endif
  37469. return res;
  37470. }
  37471. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37472. defined(DEBUG_WOLFSSL)
  37473. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  37474. {
  37475. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  37476. return 0;
  37477. }
  37478. #endif
  37479. static int test_wolfSSL_ERR_print_errors_cb(void)
  37480. {
  37481. int res = TEST_SKIPPED;
  37482. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37483. defined(DEBUG_WOLFSSL)
  37484. BIO* bio;
  37485. char buf[1024];
  37486. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37487. ERR_clear_error(); /* clear out any error nodes */
  37488. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37489. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  37490. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  37491. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  37492. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  37493. buf, 53), 0);
  37494. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  37495. buf + 53, 55), 0);
  37496. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  37497. BIO_free(bio);
  37498. res = TEST_RES_CHECK(1);
  37499. #endif
  37500. return res;
  37501. }
  37502. /*
  37503. * Testing WOLFSSL_ERROR_MSG
  37504. */
  37505. static int test_WOLFSSL_ERROR_MSG(void)
  37506. {
  37507. int res = TEST_SKIPPED;
  37508. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  37509. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  37510. const char* msg = TEST_STRING;
  37511. WOLFSSL_ERROR_MSG(msg);
  37512. res = TEST_RES_CHECK(1);
  37513. #endif
  37514. return res;
  37515. }/*End test_WOLFSSL_ERROR_MSG*/
  37516. /*
  37517. * Testing wc_ERR_remove_state
  37518. */
  37519. static int test_wc_ERR_remove_state(void)
  37520. {
  37521. int res = TEST_SKIPPED;
  37522. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  37523. wc_ERR_remove_state();
  37524. res = TEST_RES_CHECK(1);
  37525. #endif
  37526. return res;
  37527. }/*End test_wc_ERR_remove_state*/
  37528. /*
  37529. * Testing wc_ERR_print_errors_fp
  37530. */
  37531. static int test_wc_ERR_print_errors_fp(void)
  37532. {
  37533. int res = TEST_SKIPPED;
  37534. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  37535. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  37536. long sz;
  37537. XFILE fp;
  37538. int ret = 0;
  37539. WOLFSSL_ERROR(BAD_FUNC_ARG);
  37540. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  37541. wc_ERR_print_errors_fp(fp);
  37542. #if defined(DEBUG_WOLFSSL)
  37543. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  37544. sz = XFTELL(fp);
  37545. #ifdef NO_ERROR_QUEUE
  37546. /* File should be empty when NO_ERROR_QUEUE is defined */
  37547. if (sz != 0) {
  37548. ret = BAD_FUNC_ARG;
  37549. }
  37550. #else
  37551. if (sz == 0) {
  37552. ret = BAD_FUNC_ARG;
  37553. }
  37554. #endif
  37555. #endif
  37556. XFCLOSE(fp);
  37557. (void)sz;
  37558. res = TEST_RES_CHECK(ret == 0);
  37559. #endif
  37560. return res;
  37561. }/*End test_wc_ERR_print_errors_fp*/
  37562. #ifdef DEBUG_WOLFSSL
  37563. static void Logging_cb(const int logLevel, const char *const logMessage)
  37564. {
  37565. (void)logLevel;
  37566. (void)logMessage;
  37567. }
  37568. #endif
  37569. /*
  37570. * Testing wolfSSL_GetLoggingCb
  37571. */
  37572. static int test_wolfSSL_GetLoggingCb(void)
  37573. {
  37574. int ret = 0;
  37575. #ifdef DEBUG_WOLFSSL
  37576. /* Testing without wolfSSL_SetLoggingCb() */
  37577. if (ret == 0) {
  37578. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  37579. ret = 0;
  37580. }
  37581. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  37582. ret = -1;
  37583. }
  37584. }
  37585. /* Testing with wolfSSL_SetLoggingCb() */
  37586. if (ret == 0) {
  37587. ret = wolfSSL_SetLoggingCb(Logging_cb);
  37588. if (ret == 0) {
  37589. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  37590. ret = -1;
  37591. }
  37592. if (ret == 0) {
  37593. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  37594. ret = 0;
  37595. }
  37596. }
  37597. /* reset logging callback */
  37598. wolfSSL_SetLoggingCb(NULL);
  37599. }
  37600. }
  37601. #endif
  37602. if (ret == 0) {
  37603. if (wolfSSL_GetLoggingCb() != NULL) {
  37604. ret = -1;
  37605. }
  37606. }
  37607. return TEST_RES_CHECK(ret == 0);
  37608. }/*End test_wolfSSL_GetLoggingCb*/
  37609. #endif /* !NO_BIO */
  37610. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37611. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37612. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37613. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  37614. {
  37615. static const unsigned char key[] = "simple test key";
  37616. HMAC_CTX* hmac;
  37617. ENGINE* e = NULL;
  37618. unsigned char hash[WC_MAX_DIGEST_SIZE];
  37619. unsigned int len;
  37620. AssertNotNull(hmac = HMAC_CTX_new());
  37621. HMAC_CTX_init(hmac);
  37622. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  37623. SSL_SUCCESS);
  37624. /* re-using test key as data to hash */
  37625. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  37626. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  37627. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  37628. AssertIntEQ(len, md_len);
  37629. AssertIntEQ(HMAC_size(hmac), md_len);
  37630. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  37631. HMAC_cleanup(hmac);
  37632. HMAC_CTX_free(hmac);
  37633. len = 0;
  37634. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  37635. AssertIntEQ(len, md_len);
  37636. return 0;
  37637. }
  37638. #endif
  37639. static int test_wolfSSL_HMAC(void)
  37640. {
  37641. int res = TEST_SKIPPED;
  37642. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37643. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37644. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37645. #ifndef NO_SHA256
  37646. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  37647. #endif
  37648. #ifdef WOLFSSL_SHA224
  37649. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  37650. #endif
  37651. #ifdef WOLFSSL_SHA384
  37652. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  37653. #endif
  37654. #ifdef WOLFSSL_SHA512
  37655. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  37656. #endif
  37657. #ifdef WOLFSSL_SHA3
  37658. #ifndef WOLFSSL_NOSHA3_224
  37659. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  37660. #endif
  37661. #ifndef WOLFSSL_NOSHA3_256
  37662. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  37663. #endif
  37664. #ifndef WOLFSSL_NOSHA3_384
  37665. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  37666. #endif
  37667. #ifndef WOLFSSL_NOSHA3_512
  37668. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  37669. #endif
  37670. #endif
  37671. #ifndef NO_SHA
  37672. test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE);
  37673. #endif
  37674. res = TEST_RES_CHECK(1);
  37675. #endif
  37676. return res;
  37677. }
  37678. static int test_wolfSSL_CMAC(void)
  37679. {
  37680. int res = TEST_SKIPPED;
  37681. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  37682. defined(WOLFSSL_AES_DIRECT)
  37683. int i;
  37684. byte key[AES_128_KEY_SIZE];
  37685. CMAC_CTX* cmacCtx = NULL;
  37686. byte out[AES_BLOCK_SIZE];
  37687. size_t outLen = AES_BLOCK_SIZE;
  37688. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  37689. key[i] = i;
  37690. }
  37691. AssertNotNull(cmacCtx = CMAC_CTX_new());
  37692. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  37693. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37694. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37695. NULL), SSL_SUCCESS);
  37696. /* re-using test key as data to hash */
  37697. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  37698. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  37699. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  37700. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  37701. CMAC_CTX_free(cmacCtx);
  37702. /* give a key too small for the cipher, verify we get failure */
  37703. cmacCtx = NULL;
  37704. AssertNotNull(cmacCtx = CMAC_CTX_new());
  37705. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37706. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  37707. NULL), SSL_FAILURE);
  37708. CMAC_CTX_free(cmacCtx);
  37709. res = TEST_RES_CHECK(1);
  37710. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  37711. return res;
  37712. }
  37713. static int test_wolfSSL_OBJ(void)
  37714. {
  37715. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  37716. * mode
  37717. */
  37718. int res = TEST_SKIPPED;
  37719. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  37720. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  37721. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  37722. ASN1_OBJECT *obj = NULL;
  37723. ASN1_OBJECT *obj2 = NULL;
  37724. char buf[50];
  37725. XFILE fp;
  37726. X509 *x509 = NULL;
  37727. X509_NAME *x509Name;
  37728. X509_NAME_ENTRY *x509NameEntry;
  37729. ASN1_OBJECT *asn1Name = NULL;
  37730. int numNames;
  37731. BIO *bio = NULL;
  37732. int nid;
  37733. int i, j;
  37734. const char *f[] = {
  37735. #ifndef NO_RSA
  37736. "./certs/ca-cert.der",
  37737. #endif
  37738. #ifdef HAVE_ECC
  37739. "./certs/ca-ecc-cert.der",
  37740. "./certs/ca-ecc384-cert.der",
  37741. #endif
  37742. NULL};
  37743. ASN1_OBJECT *field_name_obj = NULL;
  37744. int lastpos = -1;
  37745. int tmp = -1;
  37746. ASN1_STRING *asn1 = NULL;
  37747. unsigned char *buf_dyn = NULL;
  37748. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  37749. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37750. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  37751. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  37752. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37753. ASN1_OBJECT_free(obj);
  37754. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  37755. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  37756. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  37757. #ifdef WOLFSSL_CERT_EXT
  37758. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  37759. #endif
  37760. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37761. AssertNotNull(obj2 = OBJ_dup(obj));
  37762. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  37763. ASN1_OBJECT_free(obj);
  37764. ASN1_OBJECT_free(obj2);
  37765. for (i = 0; f[i] != NULL; i++)
  37766. {
  37767. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  37768. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  37769. XFCLOSE(fp);
  37770. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  37771. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37772. /* Get the Common Name by using OBJ_txt2obj */
  37773. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  37774. do
  37775. {
  37776. lastpos = tmp;
  37777. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  37778. } while (tmp > -1);
  37779. AssertIntNE(lastpos, -1);
  37780. ASN1_OBJECT_free(field_name_obj);
  37781. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  37782. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  37783. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  37784. /*
  37785. * All Common Names should be www.wolfssl.com
  37786. * This makes testing easier as we can test for the expected value.
  37787. */
  37788. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  37789. OPENSSL_free(buf_dyn);
  37790. bio = BIO_new(BIO_s_mem());
  37791. AssertTrue(bio != NULL);
  37792. for (j = 0; j < numNames; j++)
  37793. {
  37794. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37795. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  37796. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37797. }
  37798. BIO_free(bio);
  37799. X509_free(x509);
  37800. }
  37801. #ifdef HAVE_PKCS12
  37802. {
  37803. PKCS12 *p12;
  37804. int boolRet;
  37805. EVP_PKEY *pkey = NULL;
  37806. const char *p12_f[] = {
  37807. #if !defined(NO_DES3) && !defined(NO_RSA)
  37808. "./certs/test-servercert.p12",
  37809. #endif
  37810. NULL};
  37811. for (i = 0; p12_f[i] != NULL; i++)
  37812. {
  37813. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  37814. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  37815. XFCLOSE(fp);
  37816. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  37817. &pkey, &x509, NULL)) > 0);
  37818. wc_PKCS12_free(p12);
  37819. EVP_PKEY_free(pkey);
  37820. x509Name = X509_get_issuer_name(x509);
  37821. AssertNotNull(x509Name);
  37822. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37823. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  37824. for (j = 0; j < numNames; j++)
  37825. {
  37826. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37827. AssertNotNull(asn1Name =
  37828. X509_NAME_ENTRY_get_object(x509NameEntry));
  37829. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37830. }
  37831. BIO_free(bio);
  37832. X509_free(x509);
  37833. }
  37834. }
  37835. #endif /* HAVE_PKCS12 */
  37836. res = TEST_RES_CHECK(1);
  37837. #endif
  37838. return res;
  37839. }
  37840. static int test_wolfSSL_OBJ_cmp(void)
  37841. {
  37842. int res = TEST_SKIPPED;
  37843. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37844. ASN1_OBJECT *obj = NULL;
  37845. ASN1_OBJECT *obj2 = NULL;
  37846. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37847. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  37848. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  37849. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  37850. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  37851. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  37852. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  37853. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  37854. ASN1_OBJECT_free(obj);
  37855. ASN1_OBJECT_free(obj2);
  37856. res = TEST_RES_CHECK(1);
  37857. #endif
  37858. return res;
  37859. }
  37860. static int test_wolfSSL_OBJ_txt2nid(void)
  37861. {
  37862. int res = TEST_SKIPPED;
  37863. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  37864. int i;
  37865. static const struct {
  37866. const char* sn;
  37867. const char* ln;
  37868. const char* oid;
  37869. int nid;
  37870. } testVals[] = {
  37871. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  37872. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  37873. NID_id_on_dnsSRV },
  37874. { "msUPN", "Microsoft User Principal Name",
  37875. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  37876. { NULL, NULL, NULL, NID_undef }
  37877. };
  37878. /* Invalid cases */
  37879. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  37880. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  37881. /* Valid cases */
  37882. for (i = 0; testVals[i].sn != NULL; i++) {
  37883. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  37884. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  37885. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  37886. }
  37887. res = TEST_RES_CHECK(1);
  37888. #endif
  37889. return res;
  37890. }
  37891. static int test_wolfSSL_OBJ_txt2obj(void)
  37892. {
  37893. int res = TEST_SKIPPED;
  37894. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  37895. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  37896. int i;
  37897. char buf[50];
  37898. ASN1_OBJECT* obj;
  37899. static const struct {
  37900. const char* oidStr;
  37901. const char* sn;
  37902. const char* ln;
  37903. } objs_list[] = {
  37904. #if defined(WOLFSSL_APACHE_HTTPD)
  37905. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  37906. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  37907. #endif
  37908. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  37909. { NULL, NULL, NULL }
  37910. };
  37911. static const struct {
  37912. const char* numeric;
  37913. const char* name;
  37914. } objs_named[] = {
  37915. /* In dictionary but not in normal list. */
  37916. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  37917. /* Made up OID. */
  37918. { "1.3.5.7", "1.3.5.7" },
  37919. { NULL, NULL }
  37920. };
  37921. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  37922. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  37923. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  37924. /* Test numerical value of oid (oidStr) */
  37925. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  37926. /* Convert object back to text to confirm oid is correct */
  37927. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37928. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37929. ASN1_OBJECT_free(obj);
  37930. XMEMSET(buf, 0, sizeof(buf));
  37931. /* Test short name (sn) */
  37932. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  37933. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  37934. /* Convert object back to text to confirm oid is correct */
  37935. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37936. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37937. ASN1_OBJECT_free(obj);
  37938. XMEMSET(buf, 0, sizeof(buf));
  37939. /* Test long name (ln) - should fail when no_name = 1 */
  37940. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  37941. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  37942. /* Convert object back to text to confirm oid is correct */
  37943. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37944. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37945. ASN1_OBJECT_free(obj);
  37946. XMEMSET(buf, 0, sizeof(buf));
  37947. }
  37948. for (i = 0; objs_named[i].numeric != NULL; i++) {
  37949. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  37950. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  37951. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  37952. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37953. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  37954. ASN1_OBJECT_free(obj);
  37955. }
  37956. res = TEST_RES_CHECK(1);
  37957. #endif
  37958. return res;
  37959. }
  37960. static int test_wolfSSL_PEM_write_bio_X509(void)
  37961. {
  37962. int res = TEST_SKIPPED;
  37963. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  37964. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  37965. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  37966. !defined(NO_FILESYSTEM)
  37967. /* This test contains the hard coded expected
  37968. * lengths. Update if necessary */
  37969. FILE* fp = NULL;
  37970. WOLFSSL_EVP_PKEY *priv = NULL;
  37971. BIO* input = NULL;
  37972. BIO* output = NULL;
  37973. X509* x509a = NULL;
  37974. X509* x509b = NULL;
  37975. ASN1_TIME* notBeforeA = NULL;
  37976. ASN1_TIME* notAfterA = NULL;
  37977. #ifndef NO_ASN_TIME
  37978. ASN1_TIME* notBeforeB = NULL;
  37979. ASN1_TIME* notAfterB = NULL;
  37980. #endif
  37981. int expectedLen;
  37982. fp = XFOPEN("certs/server-key.pem", "rb");
  37983. AssertNotNull(fp);
  37984. priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL);
  37985. XFCLOSE(fp);
  37986. fp = NULL;
  37987. AssertNotNull(priv);
  37988. AssertNotNull(input = BIO_new_file(
  37989. "certs/test/cert-ext-multiple.pem", "rb"));
  37990. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  37991. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  37992. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  37993. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  37994. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  37995. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  37996. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  37997. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  37998. /* compare length against expected */
  37999. expectedLen = 2000;
  38000. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38001. #ifndef NO_ASN_TIME
  38002. /* read exported X509 PEM back into struct, sanity check on export,
  38003. * make sure notBefore/notAfter are the same and certs are identical. */
  38004. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38005. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  38006. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  38007. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  38008. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  38009. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38010. X509_free(x509b);
  38011. #endif
  38012. /* Reset output buffer */
  38013. BIO_free(output);
  38014. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38015. /* Test forcing the AKID to be generated just from KeyIdentifier */
  38016. if (x509a->authKeyIdSrc != NULL) {
  38017. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  38018. x509a->authKeyId = x509a->authKeyIdSrc;
  38019. x509a->authKeyIdSrc = NULL;
  38020. x509a->authKeyIdSrcSz = 0;
  38021. }
  38022. /* Resign to re-generate the der */
  38023. AssertIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  38024. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38025. /* Check that we generate a smaller output since the AKID will
  38026. * only contain the KeyIdentifier without any additional
  38027. * information */
  38028. /* Here we copy the validity struct from the original */
  38029. expectedLen = 1688;
  38030. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38031. /* Reset buffers and x509 */
  38032. BIO_free(input);
  38033. BIO_free(output);
  38034. X509_free(x509a);
  38035. /* test CA and basicConstSet values are encoded when
  38036. * the cert is a CA */
  38037. AssertNotNull(input = BIO_new_file(
  38038. "certs/server-cert.pem", "rb"));
  38039. /* read PEM into X509 struct */
  38040. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38041. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38042. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38043. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38044. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  38045. * values are maintained and certs are identical.*/
  38046. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38047. AssertIntEQ(x509b->isCa, 1);
  38048. AssertIntEQ(x509b->basicConstSet, 1);
  38049. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38050. X509_free(x509a);
  38051. X509_free(x509b);
  38052. BIO_free(input);
  38053. BIO_free(output);
  38054. /* test CA and basicConstSet values are encoded when
  38055. * the cert is not CA */
  38056. AssertNotNull(input = BIO_new_file(
  38057. "certs/client-uri-cert.pem", "rb"));
  38058. /* read PEM into X509 struct */
  38059. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38060. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38061. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38062. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38063. /* read exported X509 PEM back into struct, ensure isCa and
  38064. * basicConstSet values are maintained and certs are identical */
  38065. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38066. AssertIntEQ(x509b->isCa, 0);
  38067. AssertIntEQ(x509b->basicConstSet, 1);
  38068. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38069. wolfSSL_EVP_PKEY_free(priv);
  38070. X509_free(x509a);
  38071. X509_free(x509b);
  38072. BIO_free(input);
  38073. BIO_free(output);
  38074. res = TEST_RES_CHECK(1);
  38075. #endif
  38076. return res;
  38077. }
  38078. static int test_wolfSSL_X509_NAME_ENTRY(void)
  38079. {
  38080. int res = TEST_SKIPPED;
  38081. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38082. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  38083. X509* x509;
  38084. #ifndef NO_BIO
  38085. BIO* bio;
  38086. #endif
  38087. X509_NAME* nm;
  38088. X509_NAME_ENTRY* entry;
  38089. unsigned char cn[] = "another name to add";
  38090. #ifdef OPENSSL_ALL
  38091. int i, names_len;
  38092. #endif
  38093. AssertNotNull(x509 =
  38094. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38095. #ifndef NO_BIO
  38096. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38097. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  38098. #endif
  38099. #ifdef WOLFSSL_CERT_REQ
  38100. {
  38101. X509_REQ* req;
  38102. #ifndef NO_BIO
  38103. BIO* bReq;
  38104. #endif
  38105. AssertNotNull(req =
  38106. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38107. #ifndef NO_BIO
  38108. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  38109. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  38110. BIO_free(bReq);
  38111. #endif
  38112. X509_free(req);
  38113. }
  38114. #endif
  38115. AssertNotNull(nm = X509_get_subject_name(x509));
  38116. /* Test add entry */
  38117. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  38118. 0x0c, cn, (int)sizeof(cn)));
  38119. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38120. #ifdef WOLFSSL_CERT_EXT
  38121. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  38122. (byte*)"support@wolfssl.com", 19, -1,
  38123. 1), WOLFSSL_SUCCESS);
  38124. #endif
  38125. X509_NAME_ENTRY_free(entry);
  38126. #ifdef WOLFSSL_CERT_REQ
  38127. {
  38128. unsigned char srv_pkcs9p[] = "Server";
  38129. unsigned char fvrtDrnk[] = "tequila";
  38130. unsigned char* der = NULL;
  38131. char* subject;
  38132. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  38133. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  38134. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  38135. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  38136. AssertIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  38137. AssertNotNull(der);
  38138. subject = X509_NAME_oneline(nm, 0, 0);
  38139. AssertNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  38140. #ifdef DEBUG_WOLFSSL
  38141. fprintf(stderr, "\n\t%s\n", subject);
  38142. #endif
  38143. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  38144. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  38145. }
  38146. #endif
  38147. /* Test add entry by text */
  38148. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  38149. 0x0c, cn, (int)sizeof(cn)));
  38150. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  38151. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  38152. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  38153. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  38154. #endif
  38155. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38156. X509_NAME_ENTRY_free(entry);
  38157. /* Test add entry by NID */
  38158. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  38159. cn, -1, -1, 0), SSL_SUCCESS);
  38160. #ifdef OPENSSL_ALL
  38161. /* stack of name entry */
  38162. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  38163. for (i=0; i<names_len; i++) {
  38164. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  38165. }
  38166. #endif
  38167. #ifndef NO_BIO
  38168. BIO_free(bio);
  38169. #endif
  38170. X509_free(x509); /* free's nm */
  38171. res = TEST_RES_CHECK(1);
  38172. #endif
  38173. return res;
  38174. }
  38175. /* Note the lack of wolfSSL_ prefix...this is a compatability layer test. */
  38176. static int test_GENERAL_NAME_set0_othername(void) {
  38177. int res = TEST_SKIPPED;
  38178. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38179. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38180. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38181. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM)
  38182. const char * cert_fname = "./certs/server-cert.der";
  38183. const char * key_fname = "./certs/server-key.der";
  38184. X509* x509 = NULL;
  38185. GENERAL_NAME* gn = NULL;
  38186. GENERAL_NAMES* gns = NULL;
  38187. ASN1_OBJECT* upn_oid = NULL;
  38188. ASN1_UTF8STRING *utf8str = NULL;
  38189. ASN1_TYPE *value = NULL;
  38190. X509_EXTENSION * ext = NULL;
  38191. byte* pt = NULL;
  38192. byte der[4096];
  38193. int derSz = 0;
  38194. EVP_PKEY* priv = NULL;
  38195. FILE* f = NULL;
  38196. AssertNotNull(f = fopen(cert_fname, "rb"));
  38197. AssertNotNull(x509 = d2i_X509_fp(f, NULL));
  38198. fclose(f);
  38199. AssertNotNull(gn = GENERAL_NAME_new());
  38200. AssertNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38201. AssertNotNull(utf8str = ASN1_UTF8STRING_new());
  38202. AssertIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38203. AssertNotNull(value = ASN1_TYPE_new());
  38204. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38205. AssertIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38206. AssertNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38207. AssertIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38208. AssertNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38209. AssertIntEQ(X509_add_ext(x509, ext, -1), 1);
  38210. AssertNotNull(f = fopen(key_fname, "rb"));
  38211. AssertIntGT(derSz = (int)fread(der, 1, sizeof(der), f), 0);
  38212. fclose(f);
  38213. pt = der;
  38214. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38215. (const unsigned char**)&pt, derSz));
  38216. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38217. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38218. ASN1_OBJECT_free(upn_oid);
  38219. X509_EXTENSION_free(ext);
  38220. X509_free(x509);
  38221. EVP_PKEY_free(priv);
  38222. res = TEST_RES_CHECK(1);
  38223. #endif
  38224. return res;
  38225. }
  38226. /* Note the lack of wolfSSL_ prefix...this is a compatability layer test. */
  38227. static int test_othername_and_SID_ext(void) {
  38228. int res = TEST_SKIPPED;
  38229. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38230. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38231. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38232. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM)
  38233. const char* csr_fname = "./certs/csr.signed.der";
  38234. const char* key_fname = "./certs/server-key.der";
  38235. byte der[4096];
  38236. int derSz = 0;
  38237. X509_REQ* x509 = NULL;
  38238. STACK_OF(X509_EXTENSION) *exts = NULL;
  38239. X509_EXTENSION * san_ext = NULL;
  38240. GENERAL_NAME* gn = NULL;
  38241. GENERAL_NAMES* gns = NULL;
  38242. ASN1_OBJECT* upn_oid = NULL;
  38243. ASN1_UTF8STRING *utf8str = NULL;
  38244. ASN1_TYPE *value = NULL;
  38245. /* SID extension. SID data format explained here:
  38246. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  38247. */
  38248. uint8_t SidExtension[] = {
  38249. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  38250. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  38251. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  38252. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  38253. X509_EXTENSION *sid_ext = NULL;
  38254. ASN1_OBJECT* sid_oid = NULL;
  38255. ASN1_OCTET_STRING *sid_data = NULL;
  38256. EVP_PKEY* priv = NULL;
  38257. FILE* f = NULL;
  38258. byte* pt = NULL;
  38259. AssertNotNull(f = fopen(csr_fname, "rb"));
  38260. AssertNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  38261. fclose(f);
  38262. AssertIntEQ(X509_REQ_set_version(x509, 2), 1);
  38263. AssertNotNull(gn = GENERAL_NAME_new());
  38264. AssertNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38265. AssertNotNull(utf8str = ASN1_UTF8STRING_new());
  38266. AssertIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38267. AssertNotNull(value = ASN1_TYPE_new());
  38268. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38269. AssertIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38270. AssertNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38271. AssertIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38272. AssertNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38273. AssertNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  38274. AssertNotNull(sid_data = ASN1_OCTET_STRING_new());
  38275. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  38276. AssertNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  38277. sid_data));
  38278. AssertNotNull(exts = sk_X509_EXTENSION_new_null());
  38279. /* Ensure an empty stack doesn't raise an error. */
  38280. AssertIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38281. AssertIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  38282. AssertIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  38283. AssertIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38284. AssertNotNull(f = fopen(key_fname, "rb"));
  38285. AssertIntGT(derSz = (int)fread(der, 1, sizeof(der), f), 0);
  38286. fclose(f);
  38287. pt = der;
  38288. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38289. (const unsigned char**)&pt, derSz));
  38290. AssertIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  38291. pt = der;
  38292. AssertIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  38293. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38294. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38295. ASN1_OBJECT_free(upn_oid);
  38296. ASN1_OBJECT_free(sid_oid);
  38297. ASN1_OCTET_STRING_free(sid_data);
  38298. X509_REQ_free(x509);
  38299. EVP_PKEY_free(priv);
  38300. res = TEST_RES_CHECK(1);
  38301. #endif
  38302. return res;
  38303. }
  38304. static int test_wolfSSL_X509_set_name(void)
  38305. {
  38306. int res = TEST_SKIPPED;
  38307. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38308. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38309. X509* x509;
  38310. X509_NAME* name;
  38311. AssertNotNull(name = X509_NAME_new());
  38312. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  38313. (byte*)"wolfssl.com", 11, 0, 1),
  38314. WOLFSSL_SUCCESS);
  38315. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  38316. (byte*)"support@wolfssl.com", 19, -1,
  38317. 1), WOLFSSL_SUCCESS);
  38318. AssertNotNull(x509 = X509_new());
  38319. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  38320. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  38321. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  38322. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  38323. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  38324. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  38325. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  38326. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  38327. X509_free(x509);
  38328. X509_NAME_free(name);
  38329. res = TEST_RES_CHECK(1);
  38330. #endif /* OPENSSL_ALL && !NO_CERTS */
  38331. return res;
  38332. }
  38333. static int test_wolfSSL_X509_set_notAfter(void)
  38334. {
  38335. int res = TEST_SKIPPED;
  38336. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38337. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38338. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38339. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  38340. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  38341. /* Generalized time will overflow time_t if not long */
  38342. X509* x;
  38343. BIO* bio;
  38344. ASN1_TIME *asn_time, *time_check;
  38345. const int year = 365*24*60*60;
  38346. const int day = 24*60*60;
  38347. const int hour = 60*60;
  38348. const int mini = 60;
  38349. int offset_day;
  38350. unsigned char buf[25];
  38351. time_t t;
  38352. /*
  38353. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38354. */
  38355. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  38356. offset_day = 7;
  38357. /*
  38358. * Free these.
  38359. */
  38360. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38361. AssertNotNull(asn_time);
  38362. AssertNotNull(x = X509_new());
  38363. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38364. /*
  38365. * Tests
  38366. */
  38367. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  38368. /* time_check is simply (ANS1_TIME*)x->notAfter */
  38369. AssertNotNull(time_check = X509_get_notAfter(x));
  38370. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38371. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38372. /* Convert to human readable format and compare to intended date */
  38373. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38374. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38375. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  38376. /*
  38377. * Cleanup
  38378. */
  38379. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  38380. X509_free(x);
  38381. BIO_free(bio);
  38382. res = TEST_RES_CHECK(1);
  38383. #endif
  38384. return res;
  38385. }
  38386. static int test_wolfSSL_X509_set_notBefore(void)
  38387. {
  38388. int res = TEST_SKIPPED;
  38389. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38390. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38391. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38392. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  38393. X509* x;
  38394. BIO* bio;
  38395. ASN1_TIME *asn_time, *time_check;
  38396. const int year = 365*24*60*60;
  38397. const int day = 24*60*60;
  38398. const int hour = 60*60;
  38399. const int mini = 60;
  38400. int offset_day;
  38401. unsigned char buf[25];
  38402. time_t t;
  38403. /*
  38404. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38405. */
  38406. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  38407. offset_day = 7;
  38408. /*
  38409. * Free these.
  38410. */
  38411. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38412. AssertNotNull(asn_time);
  38413. AssertNotNull(x = X509_new());
  38414. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38415. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  38416. /*
  38417. * Main Tests
  38418. */
  38419. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  38420. /* time_check == (ANS1_TIME*)x->notBefore */
  38421. AssertNotNull(time_check = X509_get_notBefore(x));
  38422. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38423. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38424. /* Convert to human readable format and compare to intended date */
  38425. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38426. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38427. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  38428. /*
  38429. * Cleanup
  38430. */
  38431. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  38432. X509_free(x);
  38433. BIO_free(bio);
  38434. res = TEST_RES_CHECK(1);
  38435. #endif
  38436. return res;
  38437. }
  38438. static int test_wolfSSL_X509_set_version(void)
  38439. {
  38440. int res = TEST_SKIPPED;
  38441. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38442. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38443. X509* x509;
  38444. long v = 2L;
  38445. long maxInt = INT_MAX;
  38446. AssertNotNull(x509 = X509_new());
  38447. /* These should pass. */
  38448. AssertTrue(wolfSSL_X509_set_version(x509, v));
  38449. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  38450. /* Fail Case: When v(long) is greater than x509->version(int). */
  38451. v = maxInt+1;
  38452. AssertFalse(wolfSSL_X509_set_version(x509, v));
  38453. /* Cleanup */
  38454. X509_free(x509);
  38455. res = TEST_RES_CHECK(1);
  38456. #endif
  38457. return res;
  38458. }
  38459. #ifndef NO_BIO
  38460. static int test_wolfSSL_BIO_gets(void)
  38461. {
  38462. int res = TEST_SKIPPED;
  38463. #if defined(OPENSSL_EXTRA)
  38464. BIO* bio;
  38465. BIO* bio2;
  38466. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  38467. char emp[] = "";
  38468. char bio_buffer[20];
  38469. int bufferSz = 20;
  38470. /* try with bad args */
  38471. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  38472. /* try with real msg */
  38473. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  38474. XMEMSET(bio_buffer, 0, bufferSz);
  38475. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  38476. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  38477. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  38478. AssertFalse(bio2 != BIO_next(bio));
  38479. /* make buffer filled with no terminating characters */
  38480. XMEMSET(bio_buffer, 1, bufferSz);
  38481. /* BIO_gets reads a line of data */
  38482. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38483. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38484. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38485. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  38486. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38487. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38488. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38489. /* check not null terminated string */
  38490. BIO_free(bio);
  38491. msg[0] = 0x33;
  38492. msg[1] = 0x33;
  38493. msg[2] = 0x33;
  38494. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38495. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  38496. AssertIntEQ(bio_buffer[0], msg[0]);
  38497. AssertIntEQ(bio_buffer[1], msg[1]);
  38498. AssertIntNE(bio_buffer[2], msg[2]);
  38499. BIO_free(bio);
  38500. msg[3] = 0x33;
  38501. bio_buffer[3] = 0x33;
  38502. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38503. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  38504. AssertIntEQ(bio_buffer[0], msg[0]);
  38505. AssertIntEQ(bio_buffer[1], msg[1]);
  38506. AssertIntEQ(bio_buffer[2], msg[2]);
  38507. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  38508. /* check reading an empty string */
  38509. BIO_free(bio);
  38510. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  38511. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  38512. AssertStrEQ(emp, bio_buffer);
  38513. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38514. /* check error cases */
  38515. BIO_free(bio);
  38516. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  38517. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38518. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38519. #if !defined(NO_FILESYSTEM)
  38520. {
  38521. BIO* f_bio;
  38522. XFILE f;
  38523. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  38524. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38525. f = XFOPEN(svrCertFile, "rb");
  38526. AssertTrue((f != XBADFILE));
  38527. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  38528. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38529. BIO_free(f_bio);
  38530. }
  38531. #endif /* NO_FILESYSTEM */
  38532. BIO_free(bio);
  38533. BIO_free(bio2);
  38534. /* try with type BIO */
  38535. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  38536. sizeof(msg));
  38537. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  38538. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38539. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  38540. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  38541. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  38542. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  38543. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  38544. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38545. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38546. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38547. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  38548. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38549. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38550. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38551. BIO_free(bio);
  38552. BIO_free(bio2);
  38553. /* check reading an empty string */
  38554. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  38555. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  38556. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38557. AssertStrEQ(emp, bio_buffer);
  38558. BIO_free(bio);
  38559. res = TEST_RES_CHECK(1);
  38560. #endif
  38561. return res;
  38562. }
  38563. static int test_wolfSSL_BIO_puts(void)
  38564. {
  38565. int res = TEST_SKIPPED;
  38566. #if defined(OPENSSL_EXTRA)
  38567. BIO* bio;
  38568. char input[] = "hello\0world\n.....ok\n\0";
  38569. char output[128];
  38570. XMEMSET(output, 0, sizeof(output));
  38571. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38572. AssertIntEQ(BIO_puts(bio, input), 5);
  38573. AssertIntEQ(BIO_pending(bio), 5);
  38574. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  38575. AssertIntEQ(BIO_pending(bio), 19);
  38576. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  38577. AssertStrEQ(output, "helloworld\n");
  38578. AssertIntEQ(BIO_pending(bio), 8);
  38579. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  38580. AssertStrEQ(output, ".....ok\n");
  38581. AssertIntEQ(BIO_pending(bio), 0);
  38582. AssertIntEQ(BIO_puts(bio, ""), -1);
  38583. BIO_free(bio);
  38584. res = TEST_RES_CHECK(1);
  38585. #endif
  38586. return res;
  38587. }
  38588. static int test_wolfSSL_BIO_dump(void)
  38589. {
  38590. int res = TEST_SKIPPED;
  38591. #if defined(OPENSSL_EXTRA)
  38592. BIO* bio;
  38593. static const unsigned char data[] = {
  38594. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  38595. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  38596. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  38597. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  38598. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  38599. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  38600. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  38601. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  38602. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  38603. 0xB4
  38604. };
  38605. /* Generated with OpenSSL. */
  38606. static const char expected[] =
  38607. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  38608. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  38609. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  38610. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  38611. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  38612. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  38613. static const char expectedAll[] =
  38614. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  38615. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  38616. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  38617. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  38618. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  38619. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  38620. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  38621. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  38622. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  38623. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  38624. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  38625. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  38626. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  38627. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  38628. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  38629. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  38630. char output[16 * 80];
  38631. int i;
  38632. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38633. /* Example key dumped. */
  38634. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  38635. sizeof(expected) - 1);
  38636. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  38637. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  38638. /* Try every possible value for a character. */
  38639. for (i = 0; i < 256; i++)
  38640. output[i] = i;
  38641. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  38642. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  38643. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  38644. BIO_free(bio);
  38645. res = TEST_RES_CHECK(1);
  38646. #endif
  38647. return res;
  38648. }
  38649. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38650. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38651. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38652. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  38653. {
  38654. (void)ssl;
  38655. (void)buf;
  38656. (void)sz;
  38657. (void)ctx;
  38658. return WOLFSSL_CBIO_ERR_WANT_READ;
  38659. }
  38660. #endif
  38661. static int test_wolfSSL_BIO_should_retry(void)
  38662. {
  38663. int res = TEST_SKIPPED;
  38664. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38665. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38666. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38667. tcp_ready ready;
  38668. func_args server_args;
  38669. THREAD_TYPE serverThread;
  38670. SOCKET_T sockfd = 0;
  38671. WOLFSSL_CTX* ctx;
  38672. WOLFSSL* ssl;
  38673. char msg[64] = "hello wolfssl!";
  38674. char reply[1024];
  38675. int msgSz = (int)XSTRLEN(msg);
  38676. int ret;
  38677. BIO* bio;
  38678. XMEMSET(&server_args, 0, sizeof(func_args));
  38679. #ifdef WOLFSSL_TIRTOS
  38680. fdOpenSession(Task_self());
  38681. #endif
  38682. StartTCP();
  38683. InitTcpReady(&ready);
  38684. #if defined(USE_WINDOWS_API)
  38685. /* use RNG to get random port if using windows */
  38686. ready.port = GetRandomPort();
  38687. #endif
  38688. server_args.signal = &ready;
  38689. start_thread(test_server_nofail, &server_args, &serverThread);
  38690. wait_tcp_ready(&server_args);
  38691. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38692. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  38693. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  38694. #endif
  38695. AssertIntEQ(WOLFSSL_SUCCESS,
  38696. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38697. AssertIntEQ(WOLFSSL_SUCCESS,
  38698. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38699. AssertIntEQ(WOLFSSL_SUCCESS,
  38700. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38701. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  38702. /* force retry */
  38703. ssl = wolfSSL_new(ctx);
  38704. AssertNotNull(ssl);
  38705. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  38706. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  38707. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  38708. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  38709. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  38710. AssertIntNE(BIO_should_retry(bio), 0);
  38711. /* now perform successful connection */
  38712. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  38713. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  38714. BIO_read(bio, reply, sizeof(reply));
  38715. ret = wolfSSL_get_error(ssl, -1);
  38716. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  38717. AssertIntNE(BIO_should_retry(bio), 0);
  38718. }
  38719. else {
  38720. AssertIntEQ(BIO_should_retry(bio), 0);
  38721. }
  38722. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  38723. XSTRLEN("I hear you fa shizzle!")), 0);
  38724. BIO_free(bio);
  38725. wolfSSL_CTX_free(ctx);
  38726. join_thread(serverThread);
  38727. FreeTcpReady(&ready);
  38728. #ifdef WOLFSSL_TIRTOS
  38729. fdOpenSession(Task_self());
  38730. #endif
  38731. res = TEST_RES_CHECK(1);
  38732. #endif
  38733. return res;
  38734. }
  38735. static int test_wolfSSL_BIO_connect(void)
  38736. {
  38737. int res = TEST_SKIPPED;
  38738. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  38739. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  38740. tcp_ready ready;
  38741. func_args server_args;
  38742. THREAD_TYPE serverThread;
  38743. BIO *tcpBio;
  38744. BIO *sslBio;
  38745. SSL_CTX* ctx;
  38746. SSL *ssl;
  38747. SSL *sslPtr;
  38748. char msg[] = "hello wolfssl!";
  38749. char reply[30];
  38750. char buff[10] = {0};
  38751. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38752. AssertIntEQ(WOLFSSL_SUCCESS,
  38753. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38754. AssertIntEQ(WOLFSSL_SUCCESS,
  38755. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38756. AssertIntEQ(WOLFSSL_SUCCESS,
  38757. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38758. /* Setup server */
  38759. XMEMSET(&server_args, 0, sizeof(func_args));
  38760. StartTCP();
  38761. InitTcpReady(&ready);
  38762. #if defined(USE_WINDOWS_API)
  38763. /* use RNG to get random port if using windows */
  38764. ready.port = GetRandomPort();
  38765. #endif
  38766. server_args.signal = &ready;
  38767. start_thread(test_server_nofail, &server_args, &serverThread);
  38768. wait_tcp_ready(&server_args);
  38769. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  38770. /* Start the test proper */
  38771. /* Setup the TCP BIO */
  38772. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  38773. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  38774. /* Setup the SSL object */
  38775. AssertNotNull(ssl = SSL_new(ctx));
  38776. SSL_set_connect_state(ssl);
  38777. /* Setup the SSL BIO */
  38778. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  38779. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  38780. /* Verify that BIO_get_ssl works. */
  38781. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  38782. AssertPtrEq(ssl, sslPtr);
  38783. /* Link BIO's so that sslBio uses tcpBio for IO */
  38784. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  38785. /* Do TCP connect */
  38786. AssertIntEQ(BIO_do_connect(sslBio), 1);
  38787. /* Do TLS handshake */
  38788. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  38789. /* Test writing */
  38790. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  38791. /* Expect length of default wolfSSL reply */
  38792. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  38793. /* Clean it all up */
  38794. BIO_free_all(sslBio);
  38795. /* Server clean up */
  38796. join_thread(serverThread);
  38797. FreeTcpReady(&ready);
  38798. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  38799. * after. */
  38800. XMEMSET(&server_args, 0, sizeof(func_args));
  38801. StartTCP();
  38802. InitTcpReady(&ready);
  38803. #if defined(USE_WINDOWS_API)
  38804. /* use RNG to get random port if using windows */
  38805. ready.port = GetRandomPort();
  38806. #endif
  38807. server_args.signal = &ready;
  38808. start_thread(test_server_nofail, &server_args, &serverThread);
  38809. wait_tcp_ready(&server_args);
  38810. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  38811. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  38812. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  38813. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  38814. AssertIntEQ(BIO_do_connect(sslBio), 1);
  38815. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  38816. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  38817. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  38818. /* Attempt to close the TLS connection gracefully. */
  38819. BIO_ssl_shutdown(sslBio);
  38820. BIO_free_all(sslBio);
  38821. join_thread(serverThread);
  38822. FreeTcpReady(&ready);
  38823. SSL_CTX_free(ctx);
  38824. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38825. wc_ecc_fp_free(); /* free per thread cache */
  38826. #endif
  38827. res = TEST_RES_CHECK(1);
  38828. #endif
  38829. return res;
  38830. }
  38831. static int test_wolfSSL_BIO_tls(void)
  38832. {
  38833. int res = TEST_SKIPPED;
  38834. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  38835. SSL_CTX* ctx;
  38836. SSL *ssl;
  38837. BIO *readBio;
  38838. BIO *writeBio;
  38839. int ret, err = 0;
  38840. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38841. AssertNotNull(ssl = SSL_new(ctx));
  38842. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  38843. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  38844. /* Qt reads data from write-bio,
  38845. * then writes the read data into plain packet.
  38846. * Qt reads data from plain packet,
  38847. * then writes the read data into read-bio.
  38848. */
  38849. SSL_set_bio(ssl, readBio, writeBio);
  38850. do {
  38851. #ifdef WOLFSSL_ASYNC_CRYPT
  38852. if (err == WC_PENDING_E) {
  38853. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  38854. if (ret < 0) { break; } else if (ret == 0) { continue; }
  38855. }
  38856. #endif
  38857. ret = SSL_connect(ssl);
  38858. err = SSL_get_error(ssl, 0);
  38859. } while (err == WC_PENDING_E);
  38860. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  38861. /* in this use case, should return WANT READ
  38862. * so that Qt will read the data from plain packet for next state.
  38863. */
  38864. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  38865. SSL_free(ssl);
  38866. SSL_CTX_free(ctx);
  38867. res = TEST_RES_CHECK(1);
  38868. #endif
  38869. return res;
  38870. }
  38871. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  38872. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  38873. {
  38874. BIO* clientBio;
  38875. SSL* sslClient;
  38876. SSL_CTX* ctx;
  38877. char connectAddr[20]; /* IP + port */;
  38878. (void)args;
  38879. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  38880. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  38881. AssertIntEQ(BIO_do_connect(clientBio), 1);
  38882. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38883. AssertNotNull(sslClient = SSL_new(ctx));
  38884. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  38885. SSL_set_bio(sslClient, clientBio, clientBio);
  38886. AssertIntEQ(SSL_connect(sslClient), 1);
  38887. SSL_free(sslClient);
  38888. SSL_CTX_free(ctx);
  38889. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38890. wc_ecc_fp_free(); /* free per thread cache */
  38891. #endif
  38892. return 0;
  38893. }
  38894. #endif
  38895. static int test_wolfSSL_BIO_accept(void)
  38896. {
  38897. int res = TEST_SKIPPED;
  38898. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  38899. BIO* serverBindBio;
  38900. BIO* serverAcceptBio;
  38901. SSL* sslServer;
  38902. SSL_CTX* ctx;
  38903. func_args args;
  38904. THREAD_TYPE thread;
  38905. char port[10]; /* 10 bytes should be enough to store the string
  38906. * representation of the port */
  38907. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  38908. AssertNotNull(serverBindBio = BIO_new_accept(port));
  38909. /* First BIO_do_accept binds the port */
  38910. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  38911. XMEMSET(&args, 0, sizeof(func_args));
  38912. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  38913. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  38914. /* Let's plug it into SSL to test */
  38915. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38916. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  38917. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  38918. AssertNotNull(sslServer = SSL_new(ctx));
  38919. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  38920. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  38921. AssertIntEQ(SSL_accept(sslServer), 1);
  38922. join_thread(thread);
  38923. BIO_free(serverBindBio);
  38924. SSL_free(sslServer);
  38925. SSL_CTX_free(ctx);
  38926. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38927. wc_ecc_fp_free(); /* free per thread cache */
  38928. #endif
  38929. res = TEST_RES_CHECK(1);
  38930. #endif
  38931. return res;
  38932. }
  38933. static int test_wolfSSL_BIO_write(void)
  38934. {
  38935. int res = TEST_SKIPPED;
  38936. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  38937. BIO* bio;
  38938. BIO* bio64;
  38939. BIO* ptr;
  38940. int sz;
  38941. char msg[] = "conversion test";
  38942. char out[40];
  38943. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  38944. void* bufPtr = NULL;
  38945. BUF_MEM* buf = NULL;
  38946. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  38947. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  38948. /* now should convert to base64 then write to memory */
  38949. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  38950. BIO_flush(bio);
  38951. /* test BIO chain */
  38952. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  38953. AssertNotNull(buf);
  38954. AssertIntEQ(buf->length, 25);
  38955. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  38956. AssertPtrEq(buf->data, bufPtr);
  38957. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  38958. sz = sizeof(out);
  38959. XMEMSET(out, 0, sz);
  38960. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  38961. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  38962. /* write then read should return the same message */
  38963. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  38964. sz = sizeof(out);
  38965. XMEMSET(out, 0, sz);
  38966. AssertIntEQ(BIO_read(bio, out, sz), 16);
  38967. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  38968. /* now try encoding with no line ending */
  38969. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  38970. #ifdef HAVE_EX_DATA
  38971. BIO_set_ex_data(bio64, 0, (void*) "data");
  38972. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  38973. #endif
  38974. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  38975. BIO_flush(bio);
  38976. sz = sizeof(out);
  38977. XMEMSET(out, 0, sz);
  38978. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  38979. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  38980. BIO_free_all(bio); /* frees bio64 also */
  38981. /* test with more than one bio64 in list */
  38982. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  38983. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  38984. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  38985. /* now should convert to base64 when stored and then decode with read */
  38986. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  38987. BIO_flush(bio);
  38988. sz = sizeof(out);
  38989. XMEMSET(out, 0, sz);
  38990. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  38991. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  38992. BIO_clear_flags(bio64, ~0);
  38993. BIO_set_retry_read(bio);
  38994. BIO_free_all(bio); /* frees bio64s also */
  38995. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  38996. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  38997. BIO_free(bio);
  38998. res = TEST_RES_CHECK(1);
  38999. #endif
  39000. return res;
  39001. }
  39002. static int test_wolfSSL_BIO_printf(void)
  39003. {
  39004. int res = TEST_SKIPPED;
  39005. #if defined(OPENSSL_ALL)
  39006. BIO* bio;
  39007. int sz = 7;
  39008. char msg[] = "TLS 1.3 for the world";
  39009. char out[60];
  39010. char expected[] = "TLS 1.3 for the world : sz = 7";
  39011. XMEMSET(out, 0, sizeof(out));
  39012. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39013. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  39014. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  39015. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  39016. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  39017. BIO_free(bio);
  39018. res = TEST_RES_CHECK(1);
  39019. #endif
  39020. return res;
  39021. }
  39022. static int test_wolfSSL_BIO_f_md(void)
  39023. {
  39024. int res = TEST_SKIPPED;
  39025. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  39026. BIO *bio, *mem;
  39027. char msg[] = "message to hash";
  39028. char out[60];
  39029. EVP_MD_CTX* ctx;
  39030. const unsigned char testKey[] =
  39031. {
  39032. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39033. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39034. 0x0b, 0x0b, 0x0b, 0x0b
  39035. };
  39036. const char testData[] = "Hi There";
  39037. const unsigned char testResult[] =
  39038. {
  39039. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  39040. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  39041. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  39042. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  39043. };
  39044. const unsigned char expectedHash[] =
  39045. {
  39046. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  39047. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  39048. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  39049. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  39050. };
  39051. const unsigned char emptyHash[] =
  39052. {
  39053. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  39054. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  39055. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  39056. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  39057. };
  39058. unsigned char check[sizeof(testResult) + 1];
  39059. size_t checkSz = -1;
  39060. EVP_PKEY* key;
  39061. XMEMSET(out, 0, sizeof(out));
  39062. AssertNotNull(bio = BIO_new(BIO_f_md()));
  39063. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  39064. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  39065. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  39066. /* should not be able to write/read yet since just digest wrapper and no
  39067. * data is passing through the bio */
  39068. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  39069. AssertIntEQ(BIO_pending(bio), 0);
  39070. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  39071. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  39072. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39073. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  39074. BIO_reset(bio);
  39075. /* append BIO mem to bio in order to read/write */
  39076. AssertNotNull(bio = BIO_push(bio, mem));
  39077. XMEMSET(out, 0, sizeof(out));
  39078. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  39079. AssertIntEQ(BIO_pending(bio), 16);
  39080. /* this just reads the message and does not hash it (gets calls final) */
  39081. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  39082. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39083. /* create a message digest using BIO */
  39084. XMEMSET(out, 0, sizeof(out));
  39085. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  39086. AssertIntEQ(BIO_pending(mem), 16);
  39087. AssertIntEQ(BIO_pending(bio), 16);
  39088. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39089. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  39090. BIO_free(bio);
  39091. BIO_free(mem);
  39092. /* test with HMAC */
  39093. XMEMSET(out, 0, sizeof(out));
  39094. AssertNotNull(bio = BIO_new(BIO_f_md()));
  39095. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  39096. BIO_get_md_ctx(bio, &ctx);
  39097. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  39098. testKey, (int)sizeof(testKey)));
  39099. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  39100. AssertNotNull(bio = BIO_push(bio, mem));
  39101. BIO_write(bio, testData, (int)strlen(testData));
  39102. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  39103. EVP_DigestSignFinal(ctx, check, &checkSz);
  39104. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  39105. EVP_PKEY_free(key);
  39106. BIO_free(bio);
  39107. BIO_free(mem);
  39108. res = TEST_RES_CHECK(1);
  39109. #endif
  39110. return res;
  39111. }
  39112. static int test_wolfSSL_BIO_up_ref(void)
  39113. {
  39114. int res = TEST_SKIPPED;
  39115. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39116. BIO* bio;
  39117. AssertNotNull(bio = BIO_new(BIO_f_md()));
  39118. AssertIntEQ(BIO_up_ref(NULL), 0);
  39119. AssertIntEQ(BIO_up_ref(bio), 1);
  39120. BIO_free(bio);
  39121. AssertIntEQ(BIO_up_ref(bio), 1);
  39122. BIO_free(bio);
  39123. BIO_free(bio);
  39124. res = TEST_RES_CHECK(1);
  39125. #endif
  39126. return res;
  39127. }
  39128. static int test_wolfSSL_BIO_reset(void)
  39129. {
  39130. int res = TEST_SKIPPED;
  39131. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39132. BIO* bio;
  39133. byte buf[16];
  39134. AssertNotNull(bio = BIO_new_mem_buf("secure your data",
  39135. (word32)XSTRLEN("secure your data")));
  39136. AssertIntEQ(BIO_read(bio, buf, 6), 6);
  39137. AssertIntEQ(XMEMCMP(buf, "secure", 6), 0);
  39138. XMEMSET(buf, 0, 16);
  39139. AssertIntEQ(BIO_read(bio, buf, 16), 10);
  39140. AssertIntEQ(XMEMCMP(buf, " your data", 10), 0);
  39141. /* You cannot write to MEM BIO with read-only mode. */
  39142. AssertIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  39143. AssertIntEQ(BIO_read(bio, buf, 16), -1);
  39144. XMEMSET(buf, 0, 16);
  39145. AssertIntEQ(BIO_reset(bio), 0);
  39146. AssertIntEQ(BIO_read(bio, buf, 16), 16);
  39147. AssertIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  39148. BIO_free(bio);
  39149. res = TEST_RES_CHECK(1);
  39150. #endif
  39151. return res;
  39152. }
  39153. #endif /* !NO_BIO */
  39154. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  39155. /* test that the callback arg is correct */
  39156. static int certCbArg = 0;
  39157. static int clientCertCb(WOLFSSL* ssl, void* arg)
  39158. {
  39159. if (ssl == NULL || arg != &certCbArg)
  39160. return 0;
  39161. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  39162. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39163. return 0;
  39164. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  39165. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39166. return 0;
  39167. return 1;
  39168. }
  39169. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  39170. {
  39171. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  39172. }
  39173. /**
  39174. * This is only done because test_client_nofail has no way to stop
  39175. * certificate and key loading
  39176. */
  39177. static void clientCertClearCb(WOLFSSL* ssl)
  39178. {
  39179. /* Clear the loaded certs to force the callbacks to set them up */
  39180. SSL_certs_clear(ssl);
  39181. }
  39182. static int serverCertCb(WOLFSSL* ssl, void* arg)
  39183. {
  39184. if (ssl == NULL || arg != &certCbArg)
  39185. return 0;
  39186. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  39187. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39188. return 0;
  39189. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  39190. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39191. return 0;
  39192. return 1;
  39193. }
  39194. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  39195. {
  39196. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  39197. }
  39198. /**
  39199. * This is only done because test_server_nofail has no way to stop
  39200. * certificate and key loading
  39201. */
  39202. static void serverCertClearCb(WOLFSSL* ssl)
  39203. {
  39204. /* Clear the loaded certs to force the callbacks to set them up */
  39205. SSL_certs_clear(ssl);
  39206. }
  39207. #endif
  39208. static int test_wolfSSL_cert_cb(void)
  39209. {
  39210. int res = TEST_SKIPPED;
  39211. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  39212. callback_functions func_cb_client;
  39213. callback_functions func_cb_server;
  39214. tcp_ready ready;
  39215. func_args client_args;
  39216. func_args server_args;
  39217. THREAD_TYPE serverThread;
  39218. XMEMSET(&client_args, 0, sizeof(func_args));
  39219. XMEMSET(&server_args, 0, sizeof(func_args));
  39220. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  39221. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  39222. #ifdef WOLFSSL_TIRTOS
  39223. fdOpenSession(Task_self());
  39224. #endif
  39225. StartTCP();
  39226. InitTcpReady(&ready);
  39227. #if defined(USE_WINDOWS_API)
  39228. /* use RNG to get random port if using windows */
  39229. ready.port = GetRandomPort();
  39230. #endif
  39231. server_args.signal = &ready;
  39232. client_args.signal = &ready;
  39233. client_args.callbacks = &func_cb_client;
  39234. server_args.callbacks = &func_cb_server;
  39235. func_cb_client.ctx_ready = clientCertSetupCb;
  39236. func_cb_client.ssl_ready = clientCertClearCb;
  39237. func_cb_server.ctx_ready = serverCertSetupCb;
  39238. func_cb_server.ssl_ready = serverCertClearCb;
  39239. start_thread(test_server_nofail, &server_args, &serverThread);
  39240. wait_tcp_ready(&server_args);
  39241. test_client_nofail(&client_args, NULL);
  39242. join_thread(serverThread);
  39243. AssertTrue(client_args.return_code);
  39244. AssertTrue(server_args.return_code);
  39245. FreeTcpReady(&ready);
  39246. #ifdef WOLFSSL_TIRTOS
  39247. fdOpenSession(Task_self());
  39248. #endif
  39249. res = TEST_RES_CHECK(1);
  39250. #endif
  39251. return res;
  39252. }
  39253. static int test_wolfSSL_SESSION(void)
  39254. {
  39255. int res = TEST_SKIPPED;
  39256. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39257. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39258. !defined(NO_SESSION_CACHE)
  39259. WOLFSSL* ssl;
  39260. WOLFSSL_CTX* ctx;
  39261. WOLFSSL_SESSION* sess;
  39262. WOLFSSL_SESSION* sess_copy;
  39263. #ifdef OPENSSL_EXTRA
  39264. #ifdef HAVE_EXT_CACHE
  39265. unsigned char* sessDer = NULL;
  39266. unsigned char* ptr = NULL;
  39267. int sz;
  39268. #endif
  39269. const unsigned char context[] = "user app context";
  39270. unsigned int contextSz = (unsigned int)sizeof(context);
  39271. #endif
  39272. int ret, err;
  39273. SOCKET_T sockfd;
  39274. tcp_ready ready;
  39275. func_args server_args;
  39276. THREAD_TYPE serverThread;
  39277. char msg[80];
  39278. const char* sendGET = "GET";
  39279. /* TLS v1.3 requires session tickets */
  39280. /* CHACHA and POLY1305 required for myTicketEncCb */
  39281. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  39282. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  39283. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  39284. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  39285. #else
  39286. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39287. #endif
  39288. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  39289. WOLFSSL_FILETYPE_PEM));
  39290. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  39291. WOLFSSL_FILETYPE_PEM));
  39292. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  39293. WOLFSSL_SUCCESS);
  39294. #ifdef WOLFSSL_ENCRYPTED_KEYS
  39295. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  39296. #endif
  39297. #ifdef HAVE_SESSION_TICKET
  39298. /* Use session tickets, for ticket tests below */
  39299. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  39300. #endif
  39301. XMEMSET(&server_args, 0, sizeof(func_args));
  39302. #ifdef WOLFSSL_TIRTOS
  39303. fdOpenSession(Task_self());
  39304. #endif
  39305. StartTCP();
  39306. InitTcpReady(&ready);
  39307. #if defined(USE_WINDOWS_API)
  39308. /* use RNG to get random port if using windows */
  39309. ready.port = GetRandomPort();
  39310. #endif
  39311. server_args.signal = &ready;
  39312. start_thread(test_server_nofail, &server_args, &serverThread);
  39313. wait_tcp_ready(&server_args);
  39314. /* client connection */
  39315. ssl = wolfSSL_new(ctx);
  39316. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  39317. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39318. #ifdef WOLFSSL_ASYNC_CRYPT
  39319. err = 0; /* Reset error */
  39320. #endif
  39321. do {
  39322. #ifdef WOLFSSL_ASYNC_CRYPT
  39323. if (err == WC_PENDING_E) {
  39324. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39325. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39326. }
  39327. #endif
  39328. ret = wolfSSL_connect(ssl);
  39329. err = wolfSSL_get_error(ssl, 0);
  39330. } while (err == WC_PENDING_E);
  39331. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  39332. #ifdef WOLFSSL_ASYNC_CRYPT
  39333. err = 0; /* Reset error */
  39334. #endif
  39335. do {
  39336. #ifdef WOLFSSL_ASYNC_CRYPT
  39337. if (err == WC_PENDING_E) {
  39338. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39339. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39340. }
  39341. #endif
  39342. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  39343. err = wolfSSL_get_error(ssl, 0);
  39344. } while (err == WC_PENDING_E);
  39345. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  39346. #ifdef WOLFSSL_ASYNC_CRYPT
  39347. err = 0; /* Reset error */
  39348. #endif
  39349. do {
  39350. #ifdef WOLFSSL_ASYNC_CRYPT
  39351. if (err == WC_PENDING_E) {
  39352. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39353. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39354. }
  39355. #endif
  39356. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  39357. err = wolfSSL_get_error(ssl, 0);
  39358. } while (err == WC_PENDING_E);
  39359. AssertIntEQ(ret, 23);
  39360. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  39361. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  39362. #ifdef HAVE_EXT_CACHE
  39363. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  39364. * HAVE_EXT_CACHE we get new objects each time */
  39365. #endif
  39366. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  39367. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  39368. sess = wolfSSL_get_session(ssl);
  39369. #ifdef OPENSSL_EXTRA
  39370. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  39371. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  39372. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  39373. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  39374. #ifdef HAVE_SESSION_TICKET
  39375. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  39376. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  39377. SESSION_TICKET_HINT_DEFAULT);
  39378. #else
  39379. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  39380. #endif
  39381. #else
  39382. (void)sess;
  39383. #endif /* OPENSSL_EXTRA */
  39384. /* Retain copy of the session for later testing */
  39385. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  39386. wolfSSL_shutdown(ssl);
  39387. wolfSSL_free(ssl);
  39388. join_thread(serverThread);
  39389. FreeTcpReady(&ready);
  39390. #ifdef WOLFSSL_TIRTOS
  39391. fdOpenSession(Task_self());
  39392. #endif
  39393. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  39394. {
  39395. X509 *x509;
  39396. char buf[30];
  39397. int bufSz;
  39398. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  39399. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  39400. X509_get_subject_name(x509), NID_organizationalUnitName,
  39401. buf, sizeof(buf))), 0);
  39402. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  39403. if (bufSz == 7) {
  39404. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  39405. }
  39406. if (bufSz == 16) {
  39407. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  39408. }
  39409. }
  39410. #endif
  39411. #ifdef HAVE_EXT_CACHE
  39412. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  39413. wolfSSL_SESSION_free(sess_copy);
  39414. sess_copy = NULL;
  39415. #endif
  39416. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  39417. /* get session from DER and update the timeout */
  39418. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  39419. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  39420. wolfSSL_SESSION_free(sess);
  39421. sess = NULL;
  39422. ptr = sessDer;
  39423. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  39424. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  39425. (const unsigned char**)&ptr, sz));
  39426. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  39427. sessDer = NULL;
  39428. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  39429. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39430. #endif
  39431. /* successful set session test */
  39432. AssertNotNull(ssl = wolfSSL_new(ctx));
  39433. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  39434. #ifdef HAVE_SESSION_TICKET
  39435. /* Test set/get session ticket */
  39436. {
  39437. const char* ticket = "This is a session ticket";
  39438. char buf[64] = {0};
  39439. word32 bufSz = (word32)sizeof(buf);
  39440. AssertIntEQ(SSL_SUCCESS,
  39441. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  39442. (word32)XSTRLEN(ticket)));
  39443. AssertIntEQ(SSL_SUCCESS,
  39444. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  39445. AssertStrEQ(ticket, buf);
  39446. }
  39447. #endif
  39448. #ifdef OPENSSL_EXTRA
  39449. /* session timeout case */
  39450. /* make the session to be expired */
  39451. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  39452. XSLEEP_MS(1200);
  39453. /* SSL_set_session should reject specified session but return success
  39454. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  39455. */
  39456. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39457. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  39458. #else
  39459. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  39460. #endif
  39461. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39462. /* fail case with miss match session context IDs (use compatibility API) */
  39463. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  39464. SSL_SUCCESS);
  39465. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39466. wolfSSL_free(ssl);
  39467. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  39468. SSL_FAILURE);
  39469. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  39470. SSL_SUCCESS);
  39471. AssertNotNull(ssl = wolfSSL_new(ctx));
  39472. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39473. #endif /* OPENSSL_EXTRA */
  39474. wolfSSL_free(ssl);
  39475. wolfSSL_SESSION_free(sess);
  39476. wolfSSL_CTX_free(ctx);
  39477. res = TEST_RES_CHECK(1);
  39478. #endif
  39479. return res;
  39480. }
  39481. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39482. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  39483. static int clientSessRemCountMalloc = 0;
  39484. static int serverSessRemCountMalloc = 0;
  39485. static int clientSessRemCountFree = 0;
  39486. static int serverSessRemCountFree = 0;
  39487. static WOLFSSL_CTX* serverSessCtx = NULL;
  39488. static WOLFSSL_SESSION* serverSess = NULL;
  39489. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39490. !defined(NO_SESSION_CACHE_REF)
  39491. static WOLFSSL_CTX* clientSessCtx = NULL;
  39492. static WOLFSSL_SESSION* clientSess = NULL;
  39493. #endif
  39494. static int serverSessRemIdx = 3;
  39495. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  39496. {
  39497. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  39498. (void)ctx;
  39499. AssertNotNull(mallocedData);
  39500. if (!*mallocedData)
  39501. clientSessRemCountFree++;
  39502. else
  39503. serverSessRemCountFree++;
  39504. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  39505. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  39506. }
  39507. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  39508. {
  39509. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  39510. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  39511. !defined(NO_SESSION_CACHE_REF)
  39512. /* Allow downgrade, set min version, and disable TLS 1.3.
  39513. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  39514. * reference to the session cache. But with WOLFSSL_TLS13 and without
  39515. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  39516. * session in the cache. In this case we need to downgrade to previous
  39517. * versions to just use the legacy session ID field. */
  39518. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  39519. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  39520. #endif
  39521. }
  39522. static void SessRemSslSetupCb(WOLFSSL* ssl)
  39523. {
  39524. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  39525. AssertNotNull(mallocedData);
  39526. *mallocedData = SSL_is_server(ssl);
  39527. if (!*mallocedData) {
  39528. clientSessRemCountMalloc++;
  39529. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39530. !defined(NO_SESSION_CACHE_REF)
  39531. AssertNotNull(clientSess = SSL_get1_session(ssl));
  39532. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  39533. SSL_SUCCESS);
  39534. #endif
  39535. }
  39536. else {
  39537. serverSessRemCountMalloc++;
  39538. AssertNotNull(serverSess = SSL_get1_session(ssl));
  39539. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  39540. SSL_SUCCESS);
  39541. }
  39542. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  39543. mallocedData), SSL_SUCCESS);
  39544. }
  39545. #endif
  39546. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  39547. {
  39548. int res = TEST_SKIPPED;
  39549. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39550. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  39551. /* Check that the remove callback gets called for external data in a
  39552. * session object */
  39553. callback_functions func_cb;
  39554. tcp_ready ready;
  39555. func_args client_args;
  39556. func_args server_args;
  39557. THREAD_TYPE serverThread;
  39558. XMEMSET(&client_args, 0, sizeof(func_args));
  39559. XMEMSET(&server_args, 0, sizeof(func_args));
  39560. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  39561. #ifdef WOLFSSL_TIRTOS
  39562. fdOpenSession(Task_self());
  39563. #endif
  39564. StartTCP();
  39565. InitTcpReady(&ready);
  39566. #if defined(USE_WINDOWS_API)
  39567. /* use RNG to get random port if using windows */
  39568. ready.port = GetRandomPort();
  39569. #endif
  39570. server_args.signal = &ready;
  39571. client_args.signal = &ready;
  39572. client_args.callbacks = &func_cb;
  39573. server_args.callbacks = &func_cb;
  39574. func_cb.ctx_ready = SessRemCtxSetupCb;
  39575. func_cb.on_result = SessRemSslSetupCb;
  39576. start_thread(test_server_nofail, &server_args, &serverThread);
  39577. wait_tcp_ready(&server_args);
  39578. test_client_nofail(&client_args, NULL);
  39579. join_thread(serverThread);
  39580. AssertTrue(client_args.return_code);
  39581. AssertTrue(server_args.return_code);
  39582. FreeTcpReady(&ready);
  39583. #ifdef WOLFSSL_TIRTOS
  39584. fdOpenSession(Task_self());
  39585. #endif
  39586. /* Both should have been allocated */
  39587. AssertIntEQ(clientSessRemCountMalloc, 1);
  39588. AssertIntEQ(serverSessRemCountMalloc, 1);
  39589. /* This should not be called yet. Session wasn't evicted from cache yet. */
  39590. AssertIntEQ(clientSessRemCountFree, 0);
  39591. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39592. !defined(NO_SESSION_CACHE_REF)
  39593. /* Force a cache lookup */
  39594. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  39595. /* Force a cache update */
  39596. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  39597. /* This should set the timeout to 0 and call the remove callback from within
  39598. * the session cache. */
  39599. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  39600. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  39601. AssertIntEQ(clientSessRemCountFree, 1);
  39602. #endif
  39603. /* Server session is in the cache so ex_data isn't free'd with the SSL
  39604. * object */
  39605. AssertIntEQ(serverSessRemCountFree, 0);
  39606. /* Force a cache lookup */
  39607. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  39608. /* Force a cache update */
  39609. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  39610. /* This should set the timeout to 0 and call the remove callback from within
  39611. * the session cache. */
  39612. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  39613. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  39614. AssertIntEQ(serverSessRemCountFree, 1);
  39615. /* Need to free the references that we kept */
  39616. SSL_CTX_free(serverSessCtx);
  39617. SSL_SESSION_free(serverSess);
  39618. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39619. !defined(NO_SESSION_CACHE_REF)
  39620. SSL_CTX_free(clientSessCtx);
  39621. SSL_SESSION_free(clientSess);
  39622. #endif
  39623. res = TEST_RES_CHECK(1);
  39624. #endif
  39625. return res;
  39626. }
  39627. static int test_wolfSSL_ticket_keys(void)
  39628. {
  39629. int res = TEST_SKIPPED;
  39630. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  39631. !defined(NO_WOLFSSL_SERVER)
  39632. WOLFSSL_CTX* ctx;
  39633. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  39634. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39635. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  39636. WOLFSSL_FAILURE);
  39637. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  39638. WOLFSSL_FAILURE);
  39639. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  39640. WOLFSSL_FAILURE);
  39641. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  39642. WOLFSSL_FAILURE);
  39643. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  39644. WOLFSSL_FAILURE);
  39645. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  39646. WOLFSSL_FAILURE);
  39647. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  39648. WOLFSSL_FAILURE);
  39649. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  39650. WOLFSSL_FAILURE);
  39651. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  39652. WOLFSSL_FAILURE);
  39653. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  39654. WOLFSSL_FAILURE);
  39655. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  39656. WOLFSSL_FAILURE);
  39657. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  39658. WOLFSSL_FAILURE);
  39659. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  39660. WOLFSSL_FAILURE);
  39661. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  39662. WOLFSSL_FAILURE);
  39663. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  39664. WOLFSSL_SUCCESS);
  39665. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  39666. WOLFSSL_SUCCESS);
  39667. wolfSSL_CTX_free(ctx);
  39668. res = TEST_RES_CHECK(1);
  39669. #endif
  39670. return res;
  39671. }
  39672. #ifndef NO_BIO
  39673. static int test_wolfSSL_d2i_PUBKEY(void)
  39674. {
  39675. int res = TEST_SKIPPED;
  39676. #if defined(OPENSSL_EXTRA)
  39677. BIO* bio;
  39678. EVP_PKEY* pkey;
  39679. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39680. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  39681. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  39682. /* RSA PUBKEY test */
  39683. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  39684. sizeof_client_keypub_der_2048), 0);
  39685. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39686. EVP_PKEY_free(pkey);
  39687. #endif
  39688. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  39689. /* ECC PUBKEY test */
  39690. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  39691. sizeof_ecc_clikeypub_der_256), 0);
  39692. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39693. EVP_PKEY_free(pkey);
  39694. #endif
  39695. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  39696. /* DSA PUBKEY test */
  39697. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  39698. sizeof_dsa_pub_key_der_2048), 0);
  39699. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39700. EVP_PKEY_free(pkey);
  39701. #endif
  39702. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  39703. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  39704. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  39705. (HAVE_FIPS_VERSION > 2))
  39706. /* DH PUBKEY test */
  39707. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  39708. sizeof_dh_pub_key_der_2048), 0);
  39709. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39710. EVP_PKEY_free(pkey);
  39711. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  39712. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  39713. BIO_free(bio);
  39714. (void)pkey;
  39715. res = TEST_RES_CHECK(1);
  39716. #endif
  39717. return res;
  39718. }
  39719. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  39720. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  39721. {
  39722. BIO* bio = NULL;
  39723. EVP_PKEY* pkey = NULL;
  39724. #ifndef NO_RSA
  39725. #endif
  39726. WOLFSSL_CTX* ctx;
  39727. #if defined(WOLFSSL_KEY_GEN)
  39728. unsigned char buff[4096];
  39729. unsigned char* bufPtr = buff;
  39730. #endif
  39731. /* test creating new EVP_PKEY with bad arg */
  39732. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  39733. /* test loading RSA key using BIO */
  39734. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  39735. {
  39736. XFILE file;
  39737. const char* fname = "./certs/server-key.der";
  39738. size_t sz;
  39739. byte* buf;
  39740. file = XFOPEN(fname, "rb");
  39741. AssertTrue((file != XBADFILE));
  39742. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  39743. sz = XFTELL(file);
  39744. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  39745. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  39746. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  39747. XFCLOSE(file);
  39748. /* Test using BIO new mem and loading DER private key */
  39749. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39750. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  39751. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  39752. BIO_free(bio);
  39753. bio = NULL;
  39754. EVP_PKEY_free(pkey);
  39755. pkey = NULL;
  39756. }
  39757. #endif
  39758. /* test loading ECC key using BIO */
  39759. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  39760. {
  39761. XFILE file;
  39762. const char* fname = "./certs/ecc-key.der";
  39763. size_t sz;
  39764. byte* buf;
  39765. file = XFOPEN(fname, "rb");
  39766. AssertTrue((file != XBADFILE));
  39767. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  39768. sz = XFTELL(file);
  39769. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  39770. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  39771. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  39772. XFCLOSE(file);
  39773. /* Test using BIO new mem and loading DER private key */
  39774. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39775. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  39776. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  39777. BIO_free(bio);
  39778. bio = NULL;
  39779. EVP_PKEY_free(pkey);
  39780. pkey = NULL;
  39781. }
  39782. #endif
  39783. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39784. #ifndef NO_WOLFSSL_SERVER
  39785. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  39786. #else
  39787. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  39788. #endif
  39789. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  39790. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  39791. {
  39792. RSA* rsa = NULL;
  39793. /* Tests bad parameters */
  39794. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  39795. /* RSA not set yet, expecting to fail*/
  39796. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  39797. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  39798. /* set RSA using bio*/
  39799. AssertIntGT(BIO_write(bio, client_key_der_2048,
  39800. sizeof_client_key_der_2048), 0);
  39801. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  39802. AssertNotNull(rsa);
  39803. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  39804. /*i2d RSAprivate key tests */
  39805. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  39806. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  39807. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  39808. sizeof_client_key_der_2048);
  39809. bufPtr -= sizeof_client_key_der_2048;
  39810. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  39811. sizeof_client_key_der_2048), 0);
  39812. bufPtr = NULL;
  39813. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  39814. sizeof_client_key_der_2048);
  39815. AssertNotNull(bufPtr);
  39816. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  39817. sizeof_client_key_der_2048), 0);
  39818. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  39819. RSA_free(rsa);
  39820. rsa = RSA_new();
  39821. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  39822. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  39823. RSA_free(rsa);
  39824. }
  39825. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  39826. SSL_CTX_free(ctx);
  39827. ctx = NULL;
  39828. BIO_free(bio);
  39829. bio = NULL;
  39830. return TEST_RES_CHECK(1);
  39831. }
  39832. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  39833. #endif /* !NO_BIO */
  39834. static int test_wolfSSL_sk_GENERAL_NAME(void)
  39835. {
  39836. int res = TEST_SKIPPED;
  39837. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39838. !defined(NO_RSA)
  39839. X509* x509;
  39840. GENERAL_NAME* gn;
  39841. unsigned char buf[4096];
  39842. const unsigned char* bufPt;
  39843. int bytes, i;
  39844. int j;
  39845. XFILE f;
  39846. STACK_OF(GENERAL_NAME)* sk;
  39847. f = XFOPEN(cliCertDerFileExt, "rb");
  39848. AssertTrue((f != XBADFILE));
  39849. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  39850. XFCLOSE(f);
  39851. for (j = 0; j < 2; ++j) {
  39852. bufPt = buf;
  39853. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  39854. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  39855. NID_subject_alt_name, NULL, NULL));
  39856. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  39857. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  39858. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  39859. switch (gn->type) {
  39860. case GEN_DNS:
  39861. fprintf(stderr, "found type GEN_DNS\n");
  39862. break;
  39863. case GEN_EMAIL:
  39864. fprintf(stderr, "found type GEN_EMAIL\n");
  39865. break;
  39866. case GEN_URI:
  39867. fprintf(stderr, "found type GEN_URI\n");
  39868. break;
  39869. }
  39870. }
  39871. X509_free(x509);
  39872. if (j == 0) {
  39873. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  39874. }
  39875. else {
  39876. /*
  39877. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  39878. * it was supposed to. This is a regression test for that bug.
  39879. */
  39880. GENERAL_NAMES_free(sk);
  39881. }
  39882. }
  39883. res = TEST_RES_CHECK(1);
  39884. #endif
  39885. return res;
  39886. }
  39887. static int test_wolfSSL_GENERAL_NAME_print(void)
  39888. {
  39889. int res = TEST_SKIPPED;
  39890. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  39891. X509* x509;
  39892. GENERAL_NAME* gn;
  39893. unsigned char buf[4096];
  39894. const unsigned char* bufPt;
  39895. int bytes;
  39896. XFILE f;
  39897. STACK_OF(GENERAL_NAME)* sk;
  39898. BIO* out;
  39899. unsigned char outbuf[128];
  39900. X509_EXTENSION* ext;
  39901. AUTHORITY_INFO_ACCESS* aia;
  39902. ACCESS_DESCRIPTION* ad;
  39903. const unsigned char v4Addr[] = {192,168,53,1};
  39904. const unsigned char v6Addr[] =
  39905. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  39906. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  39907. const unsigned char email[] =
  39908. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  39909. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  39910. const char* dnsStr = "DNS:example.com";
  39911. const char* uriStr = "URI:http://127.0.0.1:22220";
  39912. const char* v4addStr = "IP Address:192.168.53.1";
  39913. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  39914. const char* emailStr = "email:info@wolfssl.com";
  39915. const char* othrStr = "othername:<unsupported>";
  39916. const char* x400Str = "X400Name:<unsupported>";
  39917. const char* ediStr = "EdiPartyName:<unsupported>";
  39918. /* BIO to output */
  39919. AssertNotNull(out = BIO_new(BIO_s_mem()));
  39920. /* test for NULL param */
  39921. gn = NULL;
  39922. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  39923. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  39924. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  39925. /* test for GEN_DNS */
  39926. f = XFOPEN(cliCertDerFileExt, "rb");
  39927. AssertTrue((f != XBADFILE));
  39928. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  39929. XFCLOSE(f);
  39930. bufPt = buf;
  39931. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  39932. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  39933. NID_subject_alt_name, NULL, NULL));
  39934. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  39935. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39936. XMEMSET(outbuf,0,sizeof(outbuf));
  39937. BIO_read(out, outbuf, sizeof(outbuf));
  39938. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  39939. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  39940. X509_free(x509);
  39941. /* test for GEN_URI */
  39942. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  39943. AssertTrue((f != XBADFILE));
  39944. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39945. XFCLOSE(f);
  39946. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  39947. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  39948. AssertNotNull(aia);
  39949. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  39950. gn = ad->location;
  39951. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39952. XMEMSET(outbuf,0,sizeof(outbuf));
  39953. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  39954. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  39955. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  39956. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  39957. AssertNotNull(aia);
  39958. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  39959. X509_free(x509);
  39960. /* test for GEN_IPADD */
  39961. /* ip v4 address */
  39962. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  39963. gn->type = GEN_IPADD;
  39964. gn->d.iPAddress->length = sizeof(v4Addr);
  39965. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  39966. sizeof(v4Addr)), 1);
  39967. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39968. XMEMSET(outbuf,0,sizeof(outbuf));
  39969. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  39970. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  39971. GENERAL_NAME_free(gn);
  39972. /* ip v6 address */
  39973. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  39974. gn->type = GEN_IPADD;
  39975. gn->d.iPAddress->length = sizeof(v6Addr);
  39976. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  39977. sizeof(v6Addr)), 1);
  39978. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39979. XMEMSET(outbuf,0,sizeof(outbuf));
  39980. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  39981. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  39982. GENERAL_NAME_free(gn);
  39983. /* test for GEN_EMAIL */
  39984. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  39985. gn->type = GEN_EMAIL;
  39986. gn->d.rfc822Name->length = sizeof(email);
  39987. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  39988. sizeof(email)), 1);
  39989. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39990. XMEMSET(outbuf,0,sizeof(outbuf));
  39991. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  39992. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  39993. GENERAL_NAME_free(gn);
  39994. /* test for GEN_OTHERNAME */
  39995. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  39996. gn->type = GEN_OTHERNAME;
  39997. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39998. XMEMSET(outbuf,0,sizeof(outbuf));
  39999. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40000. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  40001. GENERAL_NAME_free(gn);
  40002. /* test for GEN_X400 */
  40003. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40004. gn->type = GEN_X400;
  40005. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40006. XMEMSET(outbuf,0,sizeof(outbuf));
  40007. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40008. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  40009. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40010. gn->type = GEN_IA5;
  40011. GENERAL_NAME_free(gn);
  40012. /* test for GEN_EDIPARTY */
  40013. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40014. gn->type = GEN_EDIPARTY;
  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, ediStr, XSTRLEN(ediStr)), 0);
  40019. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40020. gn->type = GEN_IA5;
  40021. GENERAL_NAME_free(gn);
  40022. BIO_free(out);
  40023. res = TEST_RES_CHECK(1);
  40024. #endif /* OPENSSL_ALL */
  40025. return res;
  40026. }
  40027. static int test_wolfSSL_sk_DIST_POINT(void)
  40028. {
  40029. int res = TEST_SKIPPED;
  40030. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40031. !defined(NO_RSA)
  40032. X509* x509;
  40033. unsigned char buf[4096];
  40034. const unsigned char* bufPt;
  40035. int bytes, i, j;
  40036. XFILE f;
  40037. DIST_POINT* dp;
  40038. DIST_POINT_NAME* dpn;
  40039. GENERAL_NAME* gn;
  40040. ASN1_IA5STRING* uri;
  40041. STACK_OF(DIST_POINT)* dps;
  40042. STACK_OF(GENERAL_NAME)* gns;
  40043. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  40044. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  40045. AssertTrue((f != XBADFILE));
  40046. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40047. XFCLOSE(f);
  40048. bufPt = buf;
  40049. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40050. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  40051. NID_crl_distribution_points, NULL, NULL));
  40052. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  40053. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  40054. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  40055. AssertNotNull(dpn = dp->distpoint);
  40056. /* this should be type 0, fullname */
  40057. AssertIntEQ(dpn->type, 0);
  40058. gns = dp->distpoint->name.fullname;
  40059. AssertNotNull(gns);
  40060. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40061. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  40062. gn = sk_GENERAL_NAME_value(gns, j);
  40063. AssertIntEQ(gn->type, GEN_URI);
  40064. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  40065. AssertNotNull(uri->data);
  40066. AssertIntGT(uri->length, 0);
  40067. }
  40068. }
  40069. X509_free(x509);
  40070. CRL_DIST_POINTS_free(dps);
  40071. res = TEST_RES_CHECK(1);
  40072. #endif
  40073. return res;
  40074. }
  40075. static int test_wolfSSL_MD4(void)
  40076. {
  40077. int res = TEST_SKIPPED;
  40078. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  40079. MD4_CTX md4;
  40080. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  40081. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  40082. "789012345678901234567890";
  40083. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  40084. "\xcc\x05\x36";
  40085. int msgSz = (int)XSTRLEN(msg);
  40086. XMEMSET(out, 0, sizeof(out));
  40087. MD4_Init(&md4);
  40088. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  40089. MD4_Final(out, &md4);
  40090. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  40091. res = TEST_RES_CHECK(1);
  40092. #endif
  40093. return res;
  40094. }
  40095. static int test_wolfSSL_verify_mode(void)
  40096. {
  40097. int res = TEST_SKIPPED;
  40098. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40099. WOLFSSL* ssl;
  40100. WOLFSSL_CTX* ctx;
  40101. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40102. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40103. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40104. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  40105. AssertNotNull(ssl = SSL_new(ctx));
  40106. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40107. SSL_free(ssl);
  40108. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  40109. AssertNotNull(ssl = SSL_new(ctx));
  40110. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40111. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40112. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40113. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  40114. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40115. SSL_free(ssl);
  40116. wolfSSL_CTX_set_verify(ctx,
  40117. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40118. AssertNotNull(ssl = SSL_new(ctx));
  40119. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40120. AssertIntEQ(SSL_get_verify_mode(ssl),
  40121. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40122. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  40123. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  40124. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40125. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40126. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40127. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40128. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40129. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40130. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  40131. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  40132. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  40133. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  40134. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  40135. #endif
  40136. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  40137. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40138. SSL_free(ssl);
  40139. SSL_CTX_free(ctx);
  40140. res = TEST_RES_CHECK(1);
  40141. #endif
  40142. return res;
  40143. }
  40144. static int test_wolfSSL_verify_depth(void)
  40145. {
  40146. int res = TEST_SKIPPED;
  40147. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40148. WOLFSSL* ssl;
  40149. WOLFSSL_CTX* ctx;
  40150. long depth;
  40151. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40152. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40153. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40154. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  40155. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  40156. AssertNotNull(ssl = SSL_new(ctx));
  40157. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  40158. SSL_free(ssl);
  40159. SSL_CTX_set_verify_depth(ctx, -1);
  40160. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  40161. SSL_CTX_set_verify_depth(ctx, 2);
  40162. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  40163. AssertNotNull(ssl = SSL_new(ctx));
  40164. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  40165. SSL_free(ssl);
  40166. SSL_CTX_free(ctx);
  40167. res = TEST_RES_CHECK(1);
  40168. #endif
  40169. return res;
  40170. }
  40171. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  40172. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  40173. * buffer of 64 bytes.
  40174. *
  40175. * returns the size of the digest buffer on success and a negative value on
  40176. * failure.
  40177. */
  40178. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  40179. {
  40180. HMAC_CTX ctx1;
  40181. HMAC_CTX ctx2;
  40182. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  40183. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  40184. unsigned char long_key[] =
  40185. "0123456789012345678901234567890123456789"
  40186. "0123456789012345678901234567890123456789"
  40187. "0123456789012345678901234567890123456789"
  40188. "0123456789012345678901234567890123456789";
  40189. unsigned char msg[] = "message to hash";
  40190. unsigned int digestSz = 64;
  40191. int keySz = sizeof(key);
  40192. int long_keySz = sizeof(long_key);
  40193. int msgSz = sizeof(msg);
  40194. unsigned char digest2[64];
  40195. unsigned int digestSz2 = 64;
  40196. HMAC_CTX_init(&ctx1);
  40197. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40198. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40199. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40200. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40201. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40202. HMAC_CTX_cleanup(&ctx1);
  40203. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40204. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  40205. HMAC_CTX_cleanup(&ctx2);
  40206. AssertIntEQ(digestSz, digestSz2);
  40207. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40208. /* test HMAC_Init with NULL key */
  40209. /* init after copy */
  40210. HMAC_CTX_init(&ctx1);
  40211. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40212. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40213. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40214. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40215. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40216. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40217. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40218. HMAC_CTX_cleanup(&ctx1);
  40219. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  40220. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40221. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40222. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40223. HMAC_CTX_cleanup(&ctx2);
  40224. AssertIntEQ(digestSz, digestSz2);
  40225. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40226. /* long key */
  40227. HMAC_CTX_init(&ctx1);
  40228. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  40229. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40230. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40231. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40232. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40233. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40234. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40235. HMAC_CTX_cleanup(&ctx1);
  40236. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  40237. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40238. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40239. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40240. HMAC_CTX_cleanup(&ctx2);
  40241. AssertIntEQ(digestSz, digestSz2);
  40242. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40243. /* init before copy */
  40244. HMAC_CTX_init(&ctx1);
  40245. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40246. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40247. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40248. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40249. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40250. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40251. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40252. HMAC_CTX_cleanup(&ctx1);
  40253. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40254. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40255. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40256. HMAC_CTX_cleanup(&ctx2);
  40257. AssertIntEQ(digestSz, digestSz2);
  40258. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40259. return digestSz;
  40260. }
  40261. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  40262. static int test_wolfSSL_HMAC_CTX(void)
  40263. {
  40264. int res = TEST_SKIPPED;
  40265. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  40266. unsigned char digest[64];
  40267. int digestSz;
  40268. #ifndef NO_SHA
  40269. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  40270. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  40271. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  40272. #endif /* !NO_SHA */
  40273. #ifdef WOLFSSL_SHA224
  40274. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  40275. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  40276. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  40277. "\x02\x0E", digest, digestSz), 0);
  40278. #endif /* WOLFSSL_SHA224 */
  40279. #ifndef NO_SHA256
  40280. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  40281. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  40282. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  40283. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  40284. #endif /* !NO_SHA256 */
  40285. #ifdef WOLFSSL_SHA384
  40286. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  40287. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  40288. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  40289. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  40290. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  40291. digest, digestSz), 0);
  40292. #endif /* WOLFSSL_SHA384 */
  40293. #ifdef WOLFSSL_SHA512
  40294. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  40295. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  40296. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  40297. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  40298. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  40299. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  40300. digest, digestSz), 0);
  40301. #endif /* WOLFSSL_SHA512 */
  40302. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  40303. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  40304. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  40305. "\xE4\x98\xDD", digest, digestSz), 0);
  40306. #endif /* !NO_MD5 */
  40307. res = TEST_RES_CHECK(1);
  40308. #endif
  40309. return res;
  40310. }
  40311. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40312. static void sslMsgCb(int w, int version, int type, const void* buf,
  40313. size_t sz, SSL* ssl, void* arg)
  40314. {
  40315. int i;
  40316. unsigned char* pt = (unsigned char*)buf;
  40317. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  40318. (w)?"Writing":"Reading", (int)sz, version, type);
  40319. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  40320. fprintf(stderr, "\n");
  40321. (void)ssl;
  40322. (void)arg;
  40323. }
  40324. #endif /* OPENSSL_EXTRA */
  40325. static int test_wolfSSL_msg_callback(void)
  40326. {
  40327. int res = TEST_SKIPPED;
  40328. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40329. WOLFSSL* ssl;
  40330. WOLFSSL_CTX* ctx;
  40331. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40332. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  40333. SSL_FILETYPE_PEM));
  40334. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  40335. SSL_FILETYPE_PEM));
  40336. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  40337. SSL_SUCCESS);
  40338. AssertNotNull(ssl = SSL_new(ctx));
  40339. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  40340. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  40341. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  40342. SSL_free(ssl);
  40343. SSL_CTX_free(ctx);
  40344. res = TEST_RES_CHECK(1);
  40345. #endif
  40346. return res;
  40347. }
  40348. static int test_wolfSSL_SHA(void)
  40349. {
  40350. int res = TEST_SKIPPED;
  40351. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  40352. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  40353. (!defined(HAVE_FIPS) || \
  40354. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  40355. {
  40356. const unsigned char in[] = "abc";
  40357. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  40358. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  40359. unsigned char out[WC_SHA_DIGEST_SIZE];
  40360. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  40361. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  40362. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  40363. /* SHA interface test */
  40364. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  40365. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  40366. AssertNotNull(SHA(in, 0, out));
  40367. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  40368. AssertNotNull(SHA(NULL, 0, out));
  40369. AssertNotNull(SHA(NULL, 0, NULL));
  40370. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  40371. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  40372. }
  40373. #endif
  40374. #if !defined(NO_SHA256)
  40375. {
  40376. const unsigned char in[] = "abc";
  40377. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  40378. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  40379. "\x15\xAD";
  40380. unsigned char out[WC_SHA256_DIGEST_SIZE];
  40381. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  40382. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  40383. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  40384. #else
  40385. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  40386. #endif
  40387. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  40388. }
  40389. #endif
  40390. #if defined(WOLFSSL_SHA384)
  40391. {
  40392. const unsigned char in[] = "abc";
  40393. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  40394. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  40395. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  40396. "\xc8\x25\xa7";
  40397. unsigned char out[WC_SHA384_DIGEST_SIZE];
  40398. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  40399. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  40400. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  40401. #else
  40402. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  40403. #endif
  40404. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  40405. }
  40406. #endif
  40407. #if defined(WOLFSSL_SHA512)
  40408. {
  40409. const unsigned char in[] = "abc";
  40410. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  40411. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  40412. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  40413. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  40414. "\xa5\x4c\xa4\x9f";
  40415. unsigned char out[WC_SHA512_DIGEST_SIZE];
  40416. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  40417. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  40418. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  40419. #else
  40420. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  40421. #endif
  40422. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  40423. }
  40424. #endif
  40425. res = TEST_RES_CHECK(1);
  40426. #endif
  40427. return res;
  40428. }
  40429. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  40430. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40431. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40432. static void binary_dump(void *ptr, int size)
  40433. {
  40434. #ifdef WOLFSSL_EVP_PRINT
  40435. int i = 0;
  40436. unsigned char *p = (unsigned char *) ptr;
  40437. fprintf(stderr, "{");
  40438. while ((p != NULL) && (i < size)) {
  40439. if ((i % 8) == 0) {
  40440. fprintf(stderr, "\n");
  40441. fprintf(stderr, " ");
  40442. }
  40443. fprintf(stderr, "0x%02x, ", p[i]);
  40444. i++;
  40445. }
  40446. fprintf(stderr, "\n};\n");
  40447. #else
  40448. (void) ptr;
  40449. (void) size;
  40450. #endif
  40451. }
  40452. static int last_val = 0x0f;
  40453. static int check_result(unsigned char *data, int len)
  40454. {
  40455. int i;
  40456. for ( ; len; ) {
  40457. last_val = (last_val + 1) % 16;
  40458. for (i = 0; i < 16; len--, i++, data++)
  40459. if (*data != last_val) {
  40460. return -1;
  40461. }
  40462. }
  40463. return 0;
  40464. }
  40465. static int r_offset;
  40466. static int w_offset;
  40467. static void init_offset(void)
  40468. {
  40469. r_offset = 0;
  40470. w_offset = 0;
  40471. }
  40472. static void get_record(unsigned char *data, unsigned char *buf, int len)
  40473. {
  40474. XMEMCPY(buf, data+r_offset, len);
  40475. r_offset += len;
  40476. }
  40477. static void set_record(unsigned char *data, unsigned char *buf, int len)
  40478. {
  40479. XMEMCPY(data+w_offset, buf, len);
  40480. w_offset += len;
  40481. }
  40482. static void set_plain(unsigned char *plain, int rec)
  40483. {
  40484. int i, j;
  40485. unsigned char *p = plain;
  40486. #define BLOCKSZ 16
  40487. for (i=0; i<(rec/BLOCKSZ); i++) {
  40488. for (j=0; j<BLOCKSZ; j++)
  40489. *p++ = (i % 16);
  40490. }
  40491. }
  40492. #endif
  40493. static int test_wolfSSL_EVP_Cipher_extra(void)
  40494. {
  40495. int res = TEST_SKIPPED;
  40496. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40497. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40498. /* aes128-cbc, keylen=16, ivlen=16 */
  40499. byte aes128_cbc_key[] = {
  40500. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40501. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40502. };
  40503. byte aes128_cbc_iv[] = {
  40504. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  40505. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  40506. };
  40507. /* teset data size table */
  40508. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  40509. int test_drive2[] = {8, 3, 8, 512, 0};
  40510. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  40511. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  40512. int test_drive_len[100];
  40513. int ret = 0;
  40514. EVP_CIPHER_CTX *evp = NULL;
  40515. int ilen = 0;
  40516. int klen = 0;
  40517. int i, j;
  40518. const EVP_CIPHER *type;
  40519. byte *iv;
  40520. byte *key;
  40521. int ivlen;
  40522. int keylen;
  40523. #define RECORDS 16
  40524. #define BUFFSZ 512
  40525. byte plain [BUFFSZ * RECORDS];
  40526. byte cipher[BUFFSZ * RECORDS];
  40527. byte inb[BUFFSZ];
  40528. byte outb[BUFFSZ+16];
  40529. int outl, inl;
  40530. iv = aes128_cbc_iv;
  40531. ivlen = sizeof(aes128_cbc_iv);
  40532. key = aes128_cbc_key;
  40533. keylen = sizeof(aes128_cbc_key);
  40534. type = EVP_aes_128_cbc();
  40535. set_plain(plain, BUFFSZ * RECORDS);
  40536. SSL_library_init();
  40537. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  40538. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40539. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40540. klen = EVP_CIPHER_CTX_key_length(evp);
  40541. if (klen > 0 && keylen != klen) {
  40542. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40543. }
  40544. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40545. if (ilen > 0 && ivlen != ilen) {
  40546. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40547. }
  40548. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40549. for (j = 0; j<RECORDS; j++)
  40550. {
  40551. inl = BUFFSZ;
  40552. get_record(plain, inb, inl);
  40553. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  40554. set_record(cipher, outb, outl);
  40555. }
  40556. for (i = 0; test_drive[i]; i++) {
  40557. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40558. init_offset();
  40559. test_drive_len[i] = 0;
  40560. for (j = 0; test_drive[i][j]; j++)
  40561. {
  40562. inl = test_drive[i][j];
  40563. test_drive_len[i] += inl;
  40564. get_record(plain, inb, inl);
  40565. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  40566. /* output to cipher buffer, so that following Dec test can detect
  40567. if any error */
  40568. set_record(cipher, outb, outl);
  40569. }
  40570. EVP_CipherFinal(evp, outb, &outl);
  40571. if (outl > 0)
  40572. set_record(cipher, outb, outl);
  40573. }
  40574. for (i = 0; test_drive[i]; i++) {
  40575. last_val = 0x0f;
  40576. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  40577. init_offset();
  40578. for (j = 0; test_drive[i][j]; j++) {
  40579. inl = test_drive[i][j];
  40580. get_record(cipher, inb, inl);
  40581. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  40582. binary_dump(outb, outl);
  40583. AssertIntEQ((ret = check_result(outb, outl)), 0);
  40584. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  40585. }
  40586. ret = EVP_CipherFinal(evp, outb, &outl);
  40587. binary_dump(outb, outl);
  40588. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  40589. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  40590. AssertTrue(ret);
  40591. }
  40592. EVP_CIPHER_CTX_free(evp);
  40593. /* Do an extra test to verify correct behavior with empty input. */
  40594. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  40595. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40596. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40597. klen = EVP_CIPHER_CTX_key_length(evp);
  40598. if (klen > 0 && keylen != klen) {
  40599. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40600. }
  40601. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40602. if (ilen > 0 && ivlen != ilen) {
  40603. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40604. }
  40605. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40606. /* outl should be set to 0 after passing NULL, 0 for input args. */
  40607. outl = -1;
  40608. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  40609. AssertIntEQ(outl, 0);
  40610. EVP_CIPHER_CTX_free(evp);
  40611. res = TEST_RES_CHECK(1);
  40612. #endif /* test_EVP_Cipher */
  40613. return res;
  40614. }
  40615. static int test_wolfSSL_PEM_read_DHparams(void)
  40616. {
  40617. int res = TEST_SKIPPED;
  40618. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  40619. !defined(NO_FILESYSTEM)
  40620. DH* dh;
  40621. XFILE fp;
  40622. unsigned char derOut[300];
  40623. unsigned char* derOutBuf = derOut;
  40624. int derOutSz = 0;
  40625. unsigned char derExpected[300];
  40626. int derExpectedSz = 0;
  40627. XMEMSET(derOut, 0, sizeof(derOut));
  40628. XMEMSET(derExpected, 0, sizeof(derExpected));
  40629. /* open DH param file, read into DH struct */
  40630. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  40631. /* bad args */
  40632. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  40633. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  40634. /* good args */
  40635. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  40636. XFCLOSE(fp);
  40637. /* read in certs/dh2048.der for comparison against exported params */
  40638. fp = XFOPEN("./certs/dh2048.der", "rb");
  40639. AssertTrue(fp != XBADFILE);
  40640. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  40641. XFCLOSE(fp);
  40642. /* export DH back to DER and compare */
  40643. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  40644. AssertIntEQ(derOutSz, derExpectedSz);
  40645. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  40646. DH_free(dh);
  40647. dh = NULL;
  40648. /* Test parsing with X9.42 header */
  40649. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  40650. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  40651. XFCLOSE(fp);
  40652. DH_free(dh);
  40653. res = TEST_RES_CHECK(1);
  40654. #endif
  40655. return res;
  40656. }
  40657. static int test_wolfSSL_AES_ecb_encrypt(void)
  40658. {
  40659. int res = TEST_SKIPPED;
  40660. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  40661. AES_KEY aes;
  40662. const byte msg[] =
  40663. {
  40664. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  40665. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  40666. };
  40667. const byte verify[] =
  40668. {
  40669. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  40670. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  40671. };
  40672. const byte key[] =
  40673. {
  40674. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  40675. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  40676. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  40677. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  40678. };
  40679. byte out[AES_BLOCK_SIZE];
  40680. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  40681. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40682. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  40683. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  40684. #ifdef HAVE_AES_DECRYPT
  40685. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  40686. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40687. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  40688. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  40689. #endif
  40690. /* test bad arguments */
  40691. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  40692. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  40693. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  40694. res = TEST_RES_CHECK(1);
  40695. #endif
  40696. return res;
  40697. }
  40698. static int test_wolfSSL_MD5(void)
  40699. {
  40700. int res = TEST_SKIPPED;
  40701. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  40702. byte input1[] = "";
  40703. byte input2[] = "message digest";
  40704. byte hash[WC_MD5_DIGEST_SIZE];
  40705. unsigned char output1[] =
  40706. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  40707. unsigned char output2[] =
  40708. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  40709. WOLFSSL_MD5_CTX md5;
  40710. XMEMSET(&md5, 0, sizeof(md5));
  40711. /* Test cases for illegal parameters */
  40712. AssertIntEQ(MD5_Init(NULL), 0);
  40713. AssertIntEQ(MD5_Init(&md5), 1);
  40714. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  40715. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  40716. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  40717. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  40718. AssertIntEQ(MD5_Final(hash, NULL), 0);
  40719. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  40720. /* Init MD5 CTX */
  40721. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  40722. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  40723. XSTRLEN((const char*)&input1)), 1);
  40724. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  40725. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  40726. /* Init MD5 CTX */
  40727. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  40728. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  40729. (int)XSTRLEN((const char*)input2)), 1);
  40730. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  40731. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  40732. #if !defined(NO_OLD_NAMES) && \
  40733. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  40734. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  40735. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  40736. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  40737. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  40738. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  40739. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  40740. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  40741. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  40742. {
  40743. byte data[] = "Data to be hashed.";
  40744. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  40745. AssertNotNull(MD5(data, sizeof(data), NULL));
  40746. AssertNotNull(MD5(data, sizeof(data), hash));
  40747. AssertNotNull(MD5(NULL, 0, hash));
  40748. AssertNull(MD5(NULL, sizeof(data), hash));
  40749. }
  40750. #endif
  40751. res = TEST_RES_CHECK(1);
  40752. #endif
  40753. return res;
  40754. }
  40755. static int test_wolfSSL_MD5_Transform(void)
  40756. {
  40757. int res = TEST_SKIPPED;
  40758. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  40759. byte input1[] = "";
  40760. byte input2[] = "abc";
  40761. byte local[WC_MD5_BLOCK_SIZE];
  40762. word32 sLen = 0;
  40763. #ifdef BIG_ENDIAN_ORDER
  40764. unsigned char output1[] =
  40765. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  40766. unsigned char output2[] =
  40767. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  40768. #else
  40769. unsigned char output1[] =
  40770. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  40771. unsigned char output2[] =
  40772. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  40773. #endif
  40774. union {
  40775. wc_Md5 native;
  40776. MD5_CTX compat;
  40777. } md5;
  40778. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  40779. XMEMSET(&local, 0, sizeof(local));
  40780. /* sanity check */
  40781. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  40782. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  40783. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  40784. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  40785. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40786. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  40787. /* Init MD5 CTX */
  40788. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  40789. /* Do Transform*/
  40790. sLen = (word32)XSTRLEN((char*)input1);
  40791. XMEMCPY(local, input1, sLen);
  40792. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  40793. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  40794. WC_MD5_DIGEST_SIZE), 0);
  40795. /* Init MD5 CTX */
  40796. AssertIntEQ(MD5_Init(&md5.compat), 1);
  40797. sLen = (word32)XSTRLEN((char*)input2);
  40798. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  40799. XMEMCPY(local, input2, sLen);
  40800. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  40801. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  40802. WC_MD5_DIGEST_SIZE), 0);
  40803. res = TEST_RES_CHECK(1);
  40804. #endif
  40805. return res;
  40806. }
  40807. static int test_wolfSSL_SHA224(void)
  40808. {
  40809. int res = TEST_SKIPPED;
  40810. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  40811. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40812. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  40813. unsigned char input[] =
  40814. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  40815. unsigned char output[] =
  40816. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  40817. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  40818. size_t inLen;
  40819. byte hash[WC_SHA224_DIGEST_SIZE];
  40820. inLen = XSTRLEN((char*)input);
  40821. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  40822. AssertNull(SHA224(NULL, inLen, hash));
  40823. AssertNotNull(SHA224(input, 0, hash));
  40824. AssertNotNull(SHA224(input, inLen, NULL));
  40825. AssertNotNull(SHA224(NULL, 0, hash));
  40826. AssertNotNull(SHA224(NULL, 0, NULL));
  40827. AssertNotNull(SHA224(input, inLen, hash));
  40828. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  40829. res = TEST_RES_CHECK(1);
  40830. #endif
  40831. return res;
  40832. }
  40833. static int test_wolfSSL_SHA_Transform(void)
  40834. {
  40835. int res = TEST_SKIPPED;
  40836. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  40837. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40838. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  40839. byte input1[] = "";
  40840. byte input2[] = "abc";
  40841. byte local[WC_SHA_BLOCK_SIZE];
  40842. word32 sLen = 0;
  40843. #ifdef BIG_ENDIAN_ORDER
  40844. unsigned char output1[] =
  40845. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  40846. "\x09\xf7\x3a\x93";
  40847. unsigned char output2[] =
  40848. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  40849. "\x7c\x6e\x08\xb8";
  40850. #else
  40851. unsigned char output1[] =
  40852. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  40853. "\x93\x3a\xf7\x09";
  40854. unsigned char output2[] =
  40855. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  40856. "\xb8\x08\x6e\x7c";
  40857. #endif
  40858. union {
  40859. wc_Sha native;
  40860. SHA_CTX compat;
  40861. } sha;
  40862. union {
  40863. wc_Sha native;
  40864. SHA_CTX compat;
  40865. } sha1;
  40866. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  40867. XMEMSET(&local, 0, sizeof(local));
  40868. /* sanity check */
  40869. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  40870. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  40871. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  40872. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  40873. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  40874. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  40875. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  40876. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40877. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  40878. /* Init SHA CTX */
  40879. AssertIntEQ(SHA_Init(&sha.compat), 1);
  40880. /* Do Transform*/
  40881. sLen = (word32)XSTRLEN((char*)input1);
  40882. XMEMCPY(local, input1, sLen);
  40883. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  40884. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  40885. WC_SHA_DIGEST_SIZE), 0);
  40886. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  40887. /* Init SHA CTX */
  40888. AssertIntEQ(SHA_Init(&sha.compat), 1);
  40889. sLen = (word32)XSTRLEN((char*)input2);
  40890. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40891. XMEMCPY(local, input2, sLen);
  40892. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  40893. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  40894. WC_SHA_DIGEST_SIZE), 0);
  40895. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  40896. /* SHA1 */
  40897. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40898. /* Init SHA CTX */
  40899. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  40900. /* Do Transform*/
  40901. sLen = (word32)XSTRLEN((char*)input1);
  40902. XMEMCPY(local, input1, sLen);
  40903. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  40904. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  40905. WC_SHA_DIGEST_SIZE), 0);
  40906. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  40907. /* Init SHA CTX */
  40908. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  40909. sLen = (word32)XSTRLEN((char*)input2);
  40910. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40911. XMEMCPY(local, input2, sLen);
  40912. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  40913. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  40914. WC_SHA_DIGEST_SIZE), 0);
  40915. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  40916. res = TEST_RES_CHECK(1);
  40917. #endif
  40918. #endif
  40919. return res;
  40920. }
  40921. static int test_wolfSSL_SHA256_Transform(void)
  40922. {
  40923. int res = TEST_SKIPPED;
  40924. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  40925. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40926. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40927. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  40928. !defined(WOLFSSL_KCAPI_HASH)
  40929. byte input1[] = "";
  40930. byte input2[] = "abc";
  40931. byte local[WC_SHA256_BLOCK_SIZE];
  40932. word32 sLen = 0;
  40933. #ifdef BIG_ENDIAN_ORDER
  40934. unsigned char output1[] =
  40935. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  40936. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  40937. unsigned char output2[] =
  40938. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  40939. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  40940. #else
  40941. unsigned char output1[] =
  40942. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  40943. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  40944. unsigned char output2[] =
  40945. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  40946. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  40947. #endif
  40948. union {
  40949. wc_Sha256 native;
  40950. SHA256_CTX compat;
  40951. } sha256;
  40952. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  40953. XMEMSET(&local, 0, sizeof(local));
  40954. /* sanity check */
  40955. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  40956. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  40957. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  40958. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  40959. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40960. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  40961. /* Init SHA256 CTX */
  40962. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  40963. /* Do Transform*/
  40964. sLen = (word32)XSTRLEN((char*)input1);
  40965. XMEMCPY(local, input1, sLen);
  40966. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  40967. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  40968. WC_SHA256_DIGEST_SIZE), 0);
  40969. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  40970. /* Init SHA256 CTX */
  40971. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  40972. sLen = (word32)XSTRLEN((char*)input2);
  40973. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  40974. XMEMCPY(local, input2, sLen);
  40975. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  40976. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  40977. WC_SHA256_DIGEST_SIZE), 0);
  40978. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  40979. res = TEST_RES_CHECK(1);
  40980. #endif
  40981. #endif
  40982. return res;
  40983. }
  40984. static int test_wolfSSL_SHA256(void)
  40985. {
  40986. int res = TEST_SKIPPED;
  40987. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  40988. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  40989. unsigned char input[] =
  40990. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  40991. unsigned char output[] =
  40992. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  40993. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  40994. "\x06\xC1";
  40995. size_t inLen;
  40996. byte hash[WC_SHA256_DIGEST_SIZE];
  40997. inLen = XSTRLEN((char*)input);
  40998. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  40999. AssertNotNull(SHA256(input, inLen, hash));
  41000. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  41001. res = TEST_RES_CHECK(1);
  41002. #endif
  41003. return res;
  41004. }
  41005. static int test_wolfSSL_SHA512_Transform(void)
  41006. {
  41007. int res = TEST_SKIPPED;
  41008. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  41009. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  41010. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  41011. !defined(WOLFSSL_KCAPI_HASH)
  41012. byte input1[] = "";
  41013. byte input2[] = "abc";
  41014. byte local[WC_SHA512_BLOCK_SIZE];
  41015. word32 sLen = 0;
  41016. #ifdef BIG_ENDIAN_ORDER
  41017. unsigned char output1[] =
  41018. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  41019. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  41020. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  41021. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  41022. unsigned char output2[] =
  41023. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  41024. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  41025. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  41026. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  41027. #else
  41028. unsigned char output1[] =
  41029. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  41030. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  41031. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  41032. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  41033. unsigned char output2[] =
  41034. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  41035. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  41036. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  41037. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  41038. #endif
  41039. union {
  41040. wc_Sha512 native;
  41041. SHA512_CTX compat;
  41042. } sha512;
  41043. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  41044. XMEMSET(&local, 0, sizeof(local));
  41045. /* sanity check */
  41046. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  41047. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  41048. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  41049. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  41050. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  41051. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  41052. /* Init SHA512 CTX */
  41053. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  41054. /* Do Transform*/
  41055. sLen = (word32)XSTRLEN((char*)input1);
  41056. XMEMCPY(local, input1, sLen);
  41057. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  41058. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  41059. WC_SHA512_DIGEST_SIZE), 0);
  41060. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  41061. /* Init SHA512 CTX */
  41062. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  41063. sLen = (word32)XSTRLEN((char*)input2);
  41064. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  41065. XMEMCPY(local, input2, sLen);
  41066. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  41067. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  41068. WC_SHA512_DIGEST_SIZE), 0);
  41069. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  41070. (void)input1;
  41071. res = TEST_RES_CHECK(1);
  41072. #endif
  41073. #endif
  41074. return res;
  41075. }
  41076. static int test_wolfSSL_X509_get_serialNumber(void)
  41077. {
  41078. int res = TEST_SKIPPED;
  41079. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  41080. ASN1_INTEGER* a;
  41081. BIGNUM* bn;
  41082. X509* x509;
  41083. char *serialHex;
  41084. byte serial[3];
  41085. int serialSz;
  41086. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41087. SSL_FILETYPE_PEM));
  41088. AssertNotNull(a = X509_get_serialNumber(x509));
  41089. /* check on value of ASN1 Integer */
  41090. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  41091. /* test setting serial number and then retrieving it */
  41092. AssertNotNull(a = ASN1_INTEGER_new());
  41093. ASN1_INTEGER_set(a, 3);
  41094. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41095. serialSz = sizeof(serial);
  41096. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41097. WOLFSSL_SUCCESS);
  41098. AssertIntEQ(serialSz, 1);
  41099. AssertIntEQ(serial[0], 3);
  41100. ASN1_INTEGER_free(a);
  41101. /* test setting serial number with 0's in it */
  41102. serial[0] = 0x01;
  41103. serial[1] = 0x00;
  41104. serial[2] = 0x02;
  41105. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  41106. a->data[0] = ASN_INTEGER;
  41107. a->data[1] = sizeof(serial);
  41108. XMEMCPY(&a->data[2], serial, sizeof(serial));
  41109. a->length = sizeof(serial) + 2;
  41110. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41111. XMEMSET(serial, 0, sizeof(serial));
  41112. serialSz = sizeof(serial);
  41113. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41114. WOLFSSL_SUCCESS);
  41115. AssertIntEQ(serialSz, 3);
  41116. AssertIntEQ(serial[0], 0x01);
  41117. AssertIntEQ(serial[1], 0x00);
  41118. AssertIntEQ(serial[2], 0x02);
  41119. ASN1_INTEGER_free(a);
  41120. X509_free(x509); /* free's a */
  41121. AssertNotNull(serialHex = BN_bn2hex(bn));
  41122. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  41123. AssertStrEQ(serialHex, "01");
  41124. #else
  41125. AssertStrEQ(serialHex, "1");
  41126. #endif
  41127. OPENSSL_free(serialHex);
  41128. AssertIntEQ(BN_get_word(bn), 1);
  41129. BN_free(bn);
  41130. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  41131. a = ASN1_INTEGER_new();
  41132. if (a) {
  41133. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  41134. DYNAMIC_TYPE_OPENSSL));
  41135. a->isDynamic = 1;
  41136. ASN1_INTEGER_free(a);
  41137. }
  41138. res = TEST_RES_CHECK(1);
  41139. #endif
  41140. return res;
  41141. }
  41142. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  41143. {
  41144. int res = TEST_SKIPPED;
  41145. #if defined(OPENSSL_EXTRA)
  41146. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  41147. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  41148. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  41149. res = TEST_RES_CHECK(1);
  41150. #endif
  41151. return res;
  41152. }
  41153. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  41154. {
  41155. int res = TEST_SKIPPED;
  41156. #if defined(OPENSSL_EXTRA)
  41157. #define MAX_HEXSTR_BUFSZ 9
  41158. #define NUM_CASES 5
  41159. struct Output {
  41160. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  41161. long ret;
  41162. };
  41163. int i;
  41164. int j;
  41165. const char* inputs[NUM_CASES] = {
  41166. "aabcd1357e",
  41167. "01:12:23:34:a5:b6:c7:d8:e9",
  41168. ":01:02",
  41169. "012",
  41170. ":ab:ac:d"
  41171. };
  41172. struct Output expectedOutputs[NUM_CASES] = {
  41173. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  41174. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  41175. {{0x01, 0x02}, 2},
  41176. {{0x00}, 0},
  41177. {{0x00}, 0}
  41178. };
  41179. long len = 0;
  41180. unsigned char* returnedBuf = NULL;
  41181. for (i = 0; i < NUM_CASES; ++i) {
  41182. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  41183. if (returnedBuf == NULL) {
  41184. AssertIntEQ(expectedOutputs[i].ret, 0);
  41185. continue;
  41186. }
  41187. AssertIntEQ(expectedOutputs[i].ret, len);
  41188. for (j = 0; j < len; ++j) {
  41189. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  41190. }
  41191. OPENSSL_free(returnedBuf);
  41192. }
  41193. res = TEST_RES_CHECK(1);
  41194. #endif
  41195. return res;
  41196. }
  41197. static int test_wolfSSL_X509_CA_num(void)
  41198. {
  41199. int res = TEST_SKIPPED;
  41200. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  41201. defined(HAVE_ECC) && !defined(NO_RSA)
  41202. WOLFSSL_X509_STORE *store;
  41203. WOLFSSL_X509 *x509_1, *x509_2;
  41204. int ca_num = 0;
  41205. store = wolfSSL_X509_STORE_new();
  41206. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  41207. wolfSSL_X509_STORE_add_cert(store, x509_1);
  41208. ca_num = wolfSSL_X509_CA_num(store);
  41209. AssertIntEQ(ca_num, 1);
  41210. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  41211. wolfSSL_X509_STORE_add_cert(store, x509_2);
  41212. ca_num = wolfSSL_X509_CA_num(store);
  41213. AssertIntEQ(ca_num, 2);
  41214. wolfSSL_X509_free(x509_1);
  41215. wolfSSL_X509_free(x509_2);
  41216. wolfSSL_X509_STORE_free(store);
  41217. res = TEST_RES_CHECK(1);
  41218. #endif
  41219. return res;
  41220. }
  41221. static int test_wolfSSL_X509_check_ca(void)
  41222. {
  41223. int res = TEST_SKIPPED;
  41224. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41225. WOLFSSL_X509 *x509;
  41226. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  41227. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  41228. wolfSSL_X509_free(x509);
  41229. res = TEST_RES_CHECK(1);
  41230. #endif
  41231. return res;
  41232. }
  41233. static int test_wolfSSL_X509_check_ip_asc(void)
  41234. {
  41235. int res = TEST_SKIPPED;
  41236. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41237. WOLFSSL_X509 *x509;
  41238. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  41239. #if 0
  41240. /* TODO: add cert gen for testing positive case */
  41241. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  41242. #endif
  41243. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  41244. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  41245. wolfSSL_X509_free(x509);
  41246. res = TEST_RES_CHECK(1);
  41247. #endif
  41248. return res;
  41249. }
  41250. static int test_wolfSSL_make_cert(void)
  41251. {
  41252. int res = TEST_SKIPPED;
  41253. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  41254. defined(WOLFSSL_CERT_EXT)
  41255. int ret;
  41256. Cert cert;
  41257. CertName name;
  41258. RsaKey key;
  41259. WC_RNG rng;
  41260. byte der[FOURK_BUF];
  41261. word32 idx;
  41262. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  41263. #ifdef OPENSSL_EXTRA
  41264. const unsigned char* pt;
  41265. int certSz;
  41266. X509* x509;
  41267. X509_NAME* x509name;
  41268. X509_NAME_ENTRY* entry;
  41269. ASN1_STRING* entryValue;
  41270. #endif
  41271. XMEMSET(&name, 0, sizeof(CertName));
  41272. /* set up cert name */
  41273. XMEMCPY(name.country, "US", sizeof("US"));
  41274. name.countryEnc = CTC_PRINTABLE;
  41275. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  41276. name.stateEnc = CTC_UTF8;
  41277. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  41278. name.localityEnc = CTC_UTF8;
  41279. XMEMCPY(name.sur, "Test", sizeof("Test"));
  41280. name.surEnc = CTC_UTF8;
  41281. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  41282. name.orgEnc = CTC_UTF8;
  41283. XMEMCPY(name.unit, "Development", sizeof("Development"));
  41284. name.unitEnc = CTC_UTF8;
  41285. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  41286. name.commonNameEnc = CTC_UTF8;
  41287. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  41288. name.serialDevEnc = CTC_PRINTABLE;
  41289. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  41290. name.userIdEnc = CTC_PRINTABLE;
  41291. #ifdef WOLFSSL_MULTI_ATTRIB
  41292. #if CTC_MAX_ATTRIB > 2
  41293. {
  41294. NameAttrib* n;
  41295. n = &name.name[0];
  41296. n->id = ASN_DOMAIN_COMPONENT;
  41297. n->type = CTC_UTF8;
  41298. n->sz = sizeof("com");
  41299. XMEMCPY(n->value, "com", sizeof("com"));
  41300. n = &name.name[1];
  41301. n->id = ASN_DOMAIN_COMPONENT;
  41302. n->type = CTC_UTF8;
  41303. n->sz = sizeof("wolfssl");
  41304. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  41305. }
  41306. #endif
  41307. #endif /* WOLFSSL_MULTI_ATTRIB */
  41308. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  41309. #ifndef HAVE_FIPS
  41310. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  41311. #else
  41312. AssertIntEQ(wc_InitRng(&rng), 0);
  41313. #endif
  41314. /* load test RSA key */
  41315. idx = 0;
  41316. #if defined(USE_CERT_BUFFERS_1024)
  41317. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  41318. sizeof_server_key_der_1024), 0);
  41319. #elif defined(USE_CERT_BUFFERS_2048)
  41320. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  41321. sizeof_server_key_der_2048), 0);
  41322. #else
  41323. /* error case, no RSA key loaded, happens later */
  41324. (void)idx;
  41325. #endif
  41326. XMEMSET(&cert, 0 , sizeof(Cert));
  41327. AssertIntEQ(wc_InitCert(&cert), 0);
  41328. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  41329. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  41330. cert.serialSz = (int)sizeof(mySerial);
  41331. cert.isCA = 1;
  41332. #ifndef NO_SHA256
  41333. cert.sigType = CTC_SHA256wRSA;
  41334. #else
  41335. cert.sigType = CTC_SHAwRSA;
  41336. #endif
  41337. /* add SKID from the Public Key */
  41338. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  41339. /* add AKID from the Public Key */
  41340. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  41341. ret = 0;
  41342. do {
  41343. #if defined(WOLFSSL_ASYNC_CRYPT)
  41344. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  41345. #endif
  41346. if (ret >= 0) {
  41347. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  41348. }
  41349. } while (ret == WC_PENDING_E);
  41350. AssertIntGT(ret, 0);
  41351. #ifdef OPENSSL_EXTRA
  41352. /* der holds a certificate with DC's now check X509 parsing of it */
  41353. certSz = ret;
  41354. pt = der;
  41355. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  41356. AssertNotNull(x509name = X509_get_subject_name(x509));
  41357. #ifdef WOLFSSL_MULTI_ATTRIB
  41358. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41359. -1)), 5);
  41360. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41361. idx)), 6);
  41362. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41363. idx)), -1);
  41364. #endif /* WOLFSSL_MULTI_ATTRIB */
  41365. /* compare DN at index 0 */
  41366. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  41367. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41368. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  41369. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  41370. #ifndef WOLFSSL_MULTI_ATTRIB
  41371. /* compare Serial Number */
  41372. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  41373. -1)), 7);
  41374. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41375. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41376. AssertIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  41377. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  41378. #endif
  41379. #ifdef WOLFSSL_MULTI_ATTRIB
  41380. /* get first and second DC and compare result */
  41381. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41382. -1)), 5);
  41383. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41384. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41385. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  41386. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41387. idx)), 6);
  41388. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41389. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41390. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  41391. #endif /* WOLFSSL_MULTI_ATTRIB */
  41392. /* try invalid index locations for regression test and sanity check */
  41393. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  41394. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  41395. X509_free(x509);
  41396. #endif /* OPENSSL_EXTRA */
  41397. wc_FreeRsaKey(&key);
  41398. wc_FreeRng(&rng);
  41399. res = TEST_RES_CHECK(1);
  41400. #endif
  41401. return res;
  41402. }
  41403. static int test_wolfSSL_X509_get_version(void)
  41404. {
  41405. int res = TEST_SKIPPED;
  41406. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41407. WOLFSSL_X509 *x509;
  41408. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  41409. AssertNotNull(x509);
  41410. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  41411. wolfSSL_X509_free(x509);
  41412. res = TEST_RES_CHECK(1);
  41413. #endif
  41414. return res;
  41415. }
  41416. static int test_wolfSSL_DES_ncbc(void)
  41417. {
  41418. int res = TEST_SKIPPED;
  41419. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  41420. const_DES_cblock myDes;
  41421. DES_cblock iv = {1};
  41422. DES_key_schedule key = {0};
  41423. unsigned char msg[] = "hello wolfssl";
  41424. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  41425. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  41426. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  41427. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  41428. /* partial block test */
  41429. DES_set_key(&key, &myDes);
  41430. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  41431. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  41432. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  41433. DES_set_key(&key, &myDes);
  41434. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  41435. *((byte*)&iv) = 1;
  41436. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  41437. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  41438. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  41439. /* full block test */
  41440. DES_set_key(&key, &myDes);
  41441. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  41442. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  41443. *((byte*)&iv) = 1;
  41444. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  41445. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  41446. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  41447. DES_set_key(&key, &myDes);
  41448. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  41449. *((byte*)&iv) = 1;
  41450. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  41451. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  41452. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  41453. res = TEST_RES_CHECK(1);
  41454. #endif
  41455. return res;
  41456. }
  41457. static int test_wolfSSL_AES_cbc_encrypt(void)
  41458. {
  41459. int res = TEST_SKIPPED;
  41460. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  41461. AES_KEY aes;
  41462. AES_KEY* aesN = NULL;
  41463. size_t len = 0;
  41464. size_t lenB = 0;
  41465. int keySz0 = 0;
  41466. int keySzN = -1;
  41467. byte out[AES_BLOCK_SIZE] = {0};
  41468. byte* outN = NULL;
  41469. /* Test vectors retrieved from:
  41470. * <begin URL>
  41471. * https://csrc.nist.gov/
  41472. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  41473. * documents/aes/KAT_AES.zip
  41474. * </end URL>
  41475. */
  41476. const byte* pt128N = NULL;
  41477. byte* key128N = NULL;
  41478. byte* iv128N = NULL;
  41479. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  41480. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41481. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  41482. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  41483. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  41484. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41485. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  41486. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  41487. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  41488. len = sizeof(pt128);
  41489. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  41490. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  41491. AssertIntNE(XMEMCMP(b, g, h), i)
  41492. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  41493. /* Stressing wolfSSL_AES_cbc_encrypt() */
  41494. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  41495. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  41496. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  41497. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41498. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  41499. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41500. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  41501. /* Stressing wolfSSL_AES_set_encrypt_key */
  41502. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  41503. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  41504. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  41505. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  41506. /* Stressing wolfSSL_AES_set_decrypt_key */
  41507. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  41508. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  41509. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  41510. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  41511. #ifdef WOLFSSL_AES_128
  41512. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  41513. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41514. RESET_IV(iv128tmp, iv128);
  41515. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  41516. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  41517. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41518. wc_AesFree((Aes*)&aes);
  41519. #ifdef HAVE_AES_DECRYPT
  41520. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  41521. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41522. RESET_IV(iv128tmp, iv128);
  41523. len = sizeof(ct128);
  41524. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  41525. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  41526. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  41527. wc_AesFree((Aes*)&aes);
  41528. #endif
  41529. #endif /* WOLFSSL_AES_128 */
  41530. #ifdef WOLFSSL_AES_192
  41531. {
  41532. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  41533. * Appendix F.2.3 */
  41534. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  41535. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41536. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  41537. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  41538. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  41539. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41540. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  41541. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  41542. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  41543. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  41544. len = sizeof(pt192);
  41545. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  41546. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41547. RESET_IV(iv192tmp, iv192);
  41548. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  41549. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  41550. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  41551. wc_AesFree((Aes*)&aes);
  41552. #ifdef HAVE_AES_DECRYPT
  41553. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  41554. len = sizeof(ct192);
  41555. RESET_IV(iv192tmp, iv192);
  41556. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41557. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  41558. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  41559. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  41560. wc_AesFree((Aes*)&aes);
  41561. #endif
  41562. }
  41563. #endif /* WOLFSSL_AES_192 */
  41564. #ifdef WOLFSSL_AES_256
  41565. {
  41566. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  41567. * Appendix F.2.5 */
  41568. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  41569. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41570. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  41571. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  41572. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  41573. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41574. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  41575. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  41576. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  41577. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  41578. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  41579. len = sizeof(pt256);
  41580. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  41581. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41582. RESET_IV(iv256tmp, iv256);
  41583. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41584. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  41585. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  41586. wc_AesFree((Aes*)&aes);
  41587. #ifdef HAVE_AES_DECRYPT
  41588. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  41589. len = sizeof(ct256);
  41590. RESET_IV(iv256tmp, iv256);
  41591. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41592. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41593. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  41594. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  41595. wc_AesFree((Aes*)&aes);
  41596. #endif
  41597. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  41598. {
  41599. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  41600. byte wrapPlain[sizeof(key256)] = { 0 };
  41601. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  41602. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  41603. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41604. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  41605. 15), WOLFSSL_FAILURE);
  41606. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  41607. sizeof(key256)), sizeof(wrapCipher));
  41608. wc_AesFree((Aes*)&aes);
  41609. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  41610. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41611. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  41612. 23), WOLFSSL_FAILURE);
  41613. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  41614. sizeof(wrapCipher)), sizeof(wrapPlain));
  41615. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  41616. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  41617. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  41618. wc_AesFree((Aes*)&aes);
  41619. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  41620. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41621. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  41622. sizeof(key256)), sizeof(wrapCipher));
  41623. wc_AesFree((Aes*)&aes);
  41624. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  41625. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41626. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  41627. sizeof(wrapCipher)), sizeof(wrapPlain));
  41628. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  41629. wc_AesFree((Aes*)&aes);
  41630. }
  41631. #endif /* HAVE_AES_KEYWRAP */
  41632. }
  41633. #endif /* WOLFSSL_AES_256 */
  41634. res = TEST_RES_CHECK(1);
  41635. #endif
  41636. return res;
  41637. }
  41638. static int test_wolfSSL_CRYPTO_cts128(void)
  41639. {
  41640. int res = TEST_SKIPPED;
  41641. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  41642. && defined(HAVE_CTS)
  41643. byte tmp[64]; /* Largest vector size */
  41644. /* Test vectors taken form RFC3962 Appendix B */
  41645. const testVector vects[] = {
  41646. {
  41647. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41648. "\x20",
  41649. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  41650. "\x97",
  41651. 17, 17
  41652. },
  41653. {
  41654. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41655. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  41656. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  41657. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  41658. 31, 31
  41659. },
  41660. {
  41661. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41662. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  41663. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  41664. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  41665. 32, 32
  41666. },
  41667. {
  41668. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41669. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41670. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  41671. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41672. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  41673. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  41674. 47, 47
  41675. },
  41676. {
  41677. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41678. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41679. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  41680. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41681. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  41682. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  41683. 48, 48
  41684. },
  41685. {
  41686. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41687. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41688. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  41689. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  41690. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41691. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  41692. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  41693. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  41694. 64, 64
  41695. }
  41696. };
  41697. byte keyBytes[AES_128_KEY_SIZE] = {
  41698. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  41699. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  41700. };
  41701. size_t i;
  41702. XMEMSET(tmp, 0, sizeof(tmp));
  41703. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  41704. AES_KEY encKey;
  41705. AES_KEY decKey;
  41706. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  41707. XMEMSET(iv, 0, sizeof(iv));
  41708. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  41709. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  41710. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  41711. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  41712. vects[i].outLen);
  41713. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  41714. XMEMSET(iv, 0, sizeof(iv));
  41715. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  41716. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  41717. vects[i].inLen);
  41718. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  41719. }
  41720. res = TEST_RES_CHECK(1);
  41721. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  41722. return res;
  41723. }
  41724. #if defined(OPENSSL_ALL)
  41725. static int test_wolfSSL_sk_CIPHER_description(void)
  41726. {
  41727. int res = TEST_SKIPPED;
  41728. #if !defined(NO_RSA)
  41729. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41730. int i,j,k;
  41731. int numCiphers = 0;
  41732. const SSL_METHOD *method = NULL;
  41733. const SSL_CIPHER *cipher = NULL;
  41734. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41735. SSL_CTX *ctx = NULL;
  41736. SSL *ssl = NULL;
  41737. char buf[256];
  41738. char test_str[9] = "0000000";
  41739. const char badStr[] = "unknown";
  41740. const char certPath[] = "./certs/client-cert.pem";
  41741. XMEMSET(buf, 0, sizeof(buf));
  41742. AssertNotNull(method = TLSv1_2_client_method());
  41743. AssertNotNull(ctx = SSL_CTX_new(method));
  41744. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41745. SSL_CTX_set_verify_depth(ctx, 4);
  41746. SSL_CTX_set_options(ctx, flags);
  41747. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41748. WOLFSSL_SUCCESS);
  41749. AssertNotNull(ssl = SSL_new(ctx));
  41750. /* SSL_get_ciphers returns a stack of all configured ciphers
  41751. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  41752. */
  41753. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41754. /* loop through the amount of supportedCiphers */
  41755. numCiphers = sk_num(supportedCiphers);
  41756. for (i = 0; i < numCiphers; ++i) {
  41757. /* sk_value increments "sk->data.cipher->cipherOffset".
  41758. * wolfSSL_sk_CIPHER_description sets the description for
  41759. * the cipher based on the provided offset.
  41760. */
  41761. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  41762. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  41763. }
  41764. /* Search cipher description string for "unknown" descriptor */
  41765. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  41766. k = 0;
  41767. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  41768. test_str[k] = badStr[k];
  41769. j++;
  41770. k++;
  41771. }
  41772. }
  41773. /* Fail if test_str == badStr == "unknown" */
  41774. AssertStrNE(test_str,badStr);
  41775. }
  41776. SSL_free(ssl);
  41777. SSL_CTX_free(ctx);
  41778. res = TEST_RES_CHECK(1);
  41779. #endif
  41780. return res;
  41781. }
  41782. static int test_wolfSSL_get_ciphers_compat(void)
  41783. {
  41784. int res = TEST_SKIPPED;
  41785. #if !defined(NO_RSA)
  41786. const SSL_METHOD *method = NULL;
  41787. const char certPath[] = "./certs/client-cert.pem";
  41788. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41789. SSL_CTX *ctx = NULL;
  41790. WOLFSSL *ssl = NULL;
  41791. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41792. method = SSLv23_client_method();
  41793. AssertNotNull(method);
  41794. ctx = SSL_CTX_new(method);
  41795. AssertNotNull(ctx);
  41796. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41797. SSL_CTX_set_verify_depth(ctx, 4);
  41798. SSL_CTX_set_options(ctx, flags);
  41799. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41800. WOLFSSL_SUCCESS);
  41801. AssertNotNull(ssl = SSL_new(ctx));
  41802. /* Test Bad NULL input */
  41803. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  41804. /* Test for Good input */
  41805. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41806. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  41807. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  41808. SSL_free(ssl);
  41809. SSL_CTX_free(ctx);
  41810. res = TEST_RES_CHECK(1);
  41811. #endif
  41812. return res;
  41813. }
  41814. static int test_wolfSSL_X509_PUBKEY_get(void)
  41815. {
  41816. WOLFSSL_X509_PUBKEY pubkey;
  41817. WOLFSSL_X509_PUBKEY* key;
  41818. WOLFSSL_EVP_PKEY evpkey ;
  41819. WOLFSSL_EVP_PKEY* evpPkey;
  41820. WOLFSSL_EVP_PKEY* retEvpPkey;
  41821. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  41822. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  41823. key = &pubkey;
  41824. evpPkey = &evpkey;
  41825. evpPkey->type = WOLFSSL_SUCCESS;
  41826. key->pkey = evpPkey;
  41827. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41828. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  41829. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  41830. key->pkey = NULL;
  41831. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41832. return TEST_RES_CHECK(retEvpPkey == NULL);
  41833. }
  41834. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  41835. {
  41836. int res = TEST_SKIPPED;
  41837. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41838. DSA *dsa = NULL;
  41839. DSA *setDsa = NULL;
  41840. EVP_PKEY *pkey = NULL;
  41841. EVP_PKEY *set1Pkey = NULL;
  41842. SHA_CTX sha;
  41843. byte signature[DSA_SIG_SIZE];
  41844. byte hash[WC_SHA_DIGEST_SIZE];
  41845. word32 bytes;
  41846. int answer;
  41847. #ifdef USE_CERT_BUFFERS_1024
  41848. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  41849. int dsaKeySz = sizeof_dsa_key_der_1024;
  41850. byte tmp[ONEK_BUF];
  41851. XMEMSET(tmp, 0, sizeof(tmp));
  41852. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41853. bytes = dsaKeySz;
  41854. #elif defined(USE_CERT_BUFFERS_2048)
  41855. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  41856. int dsaKeySz = sizeof_dsa_key_der_2048;
  41857. byte tmp[TWOK_BUF];
  41858. XMEMSET(tmp, 0, sizeof(tmp));
  41859. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41860. bytes = dsaKeySz;
  41861. #else
  41862. byte tmp[TWOK_BUF];
  41863. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  41864. int dsaKeySz;
  41865. XMEMSET(tmp, 0, sizeof(tmp));
  41866. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  41867. if (fp == XBADFILE) {
  41868. return WOLFSSL_BAD_FILE;
  41869. }
  41870. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  41871. XFCLOSE(fp);
  41872. #endif /* END USE_CERT_BUFFERS_1024 */
  41873. /* Create hash to later Sign and Verify */
  41874. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  41875. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  41876. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  41877. /* Initialize pkey with der format dsa key */
  41878. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  41879. (long)dsaKeySz));
  41880. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  41881. /* Should Fail: NULL argument */
  41882. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  41883. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  41884. /* Should Pass: Initialized pkey argument */
  41885. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  41886. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  41887. #ifdef USE_CERT_BUFFERS_1024
  41888. AssertIntEQ(DSA_bits(dsa), 1024);
  41889. #else
  41890. AssertIntEQ(DSA_bits(dsa), 2048);
  41891. #endif
  41892. /* Sign */
  41893. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  41894. /* Verify. */
  41895. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  41896. WOLFSSL_SUCCESS);
  41897. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  41898. /* Should Fail: set1Pkey not initialized */
  41899. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41900. /* Initialize set1Pkey */
  41901. set1Pkey = EVP_PKEY_new();
  41902. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  41903. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  41904. WOLFSSL_SUCCESS);
  41905. /* Should Pass: set dsa into set1Pkey */
  41906. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41907. DSA_free(dsa);
  41908. DSA_free(setDsa);
  41909. EVP_PKEY_free(pkey);
  41910. EVP_PKEY_free(set1Pkey);
  41911. res = TEST_RES_CHECK(1);
  41912. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41913. return res;
  41914. } /* END test_EVP_PKEY_set1_get1_DSA */
  41915. static int test_wolfSSL_DSA_SIG(void)
  41916. {
  41917. int res = TEST_SKIPPED;
  41918. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41919. !defined(HAVE_FIPS)
  41920. DSA *dsa = NULL;
  41921. DSA *dsa2 = NULL;
  41922. DSA_SIG *sig = NULL;
  41923. const BIGNUM *p = NULL;
  41924. const BIGNUM *q = NULL;
  41925. const BIGNUM *g = NULL;
  41926. const BIGNUM *pub = NULL;
  41927. const BIGNUM *priv = NULL;
  41928. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  41929. AssertNotNull(dsa = DSA_generate_parameters(2048,
  41930. NULL, 0, NULL, NULL, NULL, NULL));
  41931. DSA_free(dsa);
  41932. AssertNotNull(dsa = DSA_new());
  41933. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  41934. NULL, 0, NULL, NULL, NULL), 1);
  41935. AssertIntEQ(DSA_generate_key(dsa), 1);
  41936. DSA_get0_pqg(dsa, &p, &q, &g);
  41937. DSA_get0_key(dsa, &pub, &priv);
  41938. AssertNotNull(p = BN_dup(p));
  41939. AssertNotNull(q = BN_dup(q));
  41940. AssertNotNull(g = BN_dup(g));
  41941. AssertNotNull(pub = BN_dup(pub));
  41942. AssertNotNull(priv = BN_dup(priv));
  41943. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  41944. AssertNotNull(dsa2 = DSA_new());
  41945. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  41946. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  41947. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  41948. DSA_free(dsa);
  41949. DSA_free(dsa2);
  41950. DSA_SIG_free(sig);
  41951. res = TEST_RES_CHECK(1);
  41952. #endif
  41953. return res;
  41954. }
  41955. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  41956. {
  41957. int res = TEST_SKIPPED;
  41958. #ifdef HAVE_ECC
  41959. WOLFSSL_EC_KEY *ecKey = NULL;
  41960. WOLFSSL_EC_KEY *ecGet1 = NULL;
  41961. EVP_PKEY *pkey = NULL;
  41962. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  41963. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41964. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  41965. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  41966. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  41967. /* Should fail since ecKey is empty */
  41968. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  41969. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41970. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41971. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  41972. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  41973. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  41974. wolfSSL_EC_KEY_free(ecKey);
  41975. wolfSSL_EC_KEY_free(ecGet1);
  41976. EVP_PKEY_free(pkey);
  41977. res = TEST_RES_CHECK(1);
  41978. #endif /* HAVE_ECC */
  41979. return res;
  41980. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  41981. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  41982. {
  41983. int res = TEST_SKIPPED;
  41984. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41985. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41986. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  41987. DH *dh = NULL;
  41988. DH *setDh = NULL;
  41989. EVP_PKEY *pkey = NULL;
  41990. FILE* f = NULL;
  41991. unsigned char buf[4096];
  41992. const unsigned char* pt = buf;
  41993. const char* dh2048 = "./certs/dh2048.der";
  41994. long len = 0;
  41995. int code = -1;
  41996. XMEMSET(buf, 0, sizeof(buf));
  41997. f = XFOPEN(dh2048, "rb");
  41998. AssertTrue(f != XBADFILE);
  41999. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  42000. XFCLOSE(f);
  42001. /* Load dh2048.der into DH with internal format */
  42002. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42003. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  42004. AssertIntEQ(code, 0);
  42005. code = -1;
  42006. pkey = wolfSSL_EVP_PKEY_new();
  42007. /* Set DH into PKEY */
  42008. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  42009. /* Get DH from PKEY */
  42010. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  42011. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  42012. AssertIntEQ(code, 0);
  42013. EVP_PKEY_free(pkey);
  42014. DH_free(setDh);
  42015. DH_free(dh);
  42016. res = TEST_RES_CHECK(1);
  42017. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  42018. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  42019. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  42020. return res;
  42021. } /* END test_EVP_PKEY_set1_get1_DH */
  42022. static int test_wolfSSL_CTX_ctrl(void)
  42023. {
  42024. int res = TEST_SKIPPED;
  42025. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42026. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42027. char caFile[] = "./certs/client-ca.pem";
  42028. char clientFile[] = "./certs/client-cert.pem";
  42029. SSL_CTX* ctx;
  42030. X509* x509 = NULL;
  42031. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42032. byte buf[6000];
  42033. char file[] = "./certs/dsaparams.pem";
  42034. XFILE f;
  42035. int bytes;
  42036. BIO* bio;
  42037. DSA* dsa;
  42038. DH* dh;
  42039. #endif
  42040. #ifdef HAVE_ECC
  42041. WOLFSSL_EC_KEY* ecKey;
  42042. #endif
  42043. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42044. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  42045. AssertNotNull(x509);
  42046. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  42047. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  42048. AssertNotNull(x509);
  42049. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42050. /* Initialize DH */
  42051. f = XFOPEN(file, "rb");
  42052. AssertTrue((f != XBADFILE));
  42053. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  42054. XFCLOSE(f);
  42055. bio = BIO_new_mem_buf((void*)buf, bytes);
  42056. AssertNotNull(bio);
  42057. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  42058. AssertNotNull(dsa);
  42059. dh = wolfSSL_DSA_dup_DH(dsa);
  42060. AssertNotNull(dh);
  42061. #endif
  42062. #ifdef HAVE_ECC
  42063. /* Initialize WOLFSSL_EC_KEY */
  42064. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  42065. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  42066. #endif
  42067. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  42068. /* additional test of getting EVP_PKEY key size from X509
  42069. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  42070. * allowed with user RSA */
  42071. {
  42072. EVP_PKEY* pkey;
  42073. #if defined(HAVE_ECC)
  42074. X509* ecX509;
  42075. #endif /* HAVE_ECC */
  42076. AssertNotNull(pkey = X509_get_pubkey(x509));
  42077. /* current RSA key is 2048 bit (256 bytes) */
  42078. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  42079. EVP_PKEY_free(pkey);
  42080. #if defined(HAVE_ECC)
  42081. #if defined(USE_CERT_BUFFERS_256)
  42082. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  42083. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  42084. SSL_FILETYPE_ASN1));
  42085. #else
  42086. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  42087. cliEccCertFile, SSL_FILETYPE_PEM));
  42088. #endif
  42089. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  42090. /* current ECC key is 256 bit (32 bytes) */
  42091. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  42092. X509_free(ecX509);
  42093. EVP_PKEY_free(pkey);
  42094. #endif /* HAVE_ECC */
  42095. }
  42096. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  42097. /* Tests should fail with passed in NULL pointer */
  42098. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  42099. SSL_FAILURE);
  42100. #if !defined(NO_DH) && !defined(NO_DSA)
  42101. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  42102. SSL_FAILURE);
  42103. #endif
  42104. #ifdef HAVE_ECC
  42105. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  42106. SSL_FAILURE);
  42107. #endif
  42108. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  42109. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  42110. */
  42111. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  42112. SSL_SUCCESS);
  42113. /* Test with SSL_CTRL_OPTIONS
  42114. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  42115. */
  42116. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  42117. == SSL_OP_NO_TLSv1);
  42118. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  42119. /* Test with SSL_CTRL_SET_TMP_DH
  42120. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  42121. */
  42122. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42123. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  42124. SSL_SUCCESS);
  42125. #endif
  42126. /* Test with SSL_CTRL_SET_TMP_ECDH
  42127. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  42128. */
  42129. #ifdef HAVE_ECC
  42130. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  42131. SSL_SUCCESS);
  42132. #endif
  42133. #ifdef WOLFSSL_ENCRYPTED_KEYS
  42134. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  42135. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  42136. #endif
  42137. /* Test for min/max proto */
  42138. #ifndef WOLFSSL_NO_TLS12
  42139. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42140. 0, NULL), SSL_SUCCESS);
  42141. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42142. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42143. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  42144. #endif
  42145. #ifdef WOLFSSL_TLS13
  42146. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42147. 0, NULL), SSL_SUCCESS);
  42148. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42149. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  42150. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  42151. #ifndef WOLFSSL_NO_TLS12
  42152. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42153. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42154. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  42155. #endif
  42156. #endif
  42157. /* Cleanup and Pass */
  42158. #if !defined(NO_DH) && !defined(NO_DSA)
  42159. #ifndef NO_BIO
  42160. BIO_free(bio);
  42161. DSA_free(dsa);
  42162. DH_free(dh);
  42163. #endif
  42164. #endif
  42165. #ifdef HAVE_ECC
  42166. wolfSSL_EC_KEY_free(ecKey);
  42167. #endif
  42168. SSL_CTX_free(ctx);
  42169. res = TEST_RES_CHECK(1);
  42170. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42171. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  42172. return res;
  42173. }
  42174. static int test_wolfSSL_EVP_PKEY_assign(void)
  42175. {
  42176. int res = TEST_SKIPPED;
  42177. int type;
  42178. WOLFSSL_EVP_PKEY* pkey;
  42179. #ifndef NO_RSA
  42180. WOLFSSL_RSA* rsa;
  42181. #endif
  42182. #ifndef NO_DSA
  42183. WOLFSSL_DSA* dsa;
  42184. #endif
  42185. #ifdef HAVE_ECC
  42186. WOLFSSL_EC_KEY* ecKey;
  42187. #endif
  42188. (void)pkey;
  42189. #ifndef NO_RSA
  42190. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42191. type = EVP_PKEY_RSA;
  42192. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42193. AssertNotNull(rsa = wolfSSL_RSA_new());
  42194. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  42195. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  42196. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  42197. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  42198. wolfSSL_EVP_PKEY_free(pkey);
  42199. res = TEST_RES_CHECK(1);
  42200. }
  42201. #endif /* NO_RSA */
  42202. #ifndef NO_DSA
  42203. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42204. type = EVP_PKEY_DSA;
  42205. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42206. AssertNotNull(dsa = wolfSSL_DSA_new());
  42207. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  42208. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  42209. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  42210. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  42211. wolfSSL_EVP_PKEY_free(pkey);
  42212. res = TEST_RES_CHECK(1);
  42213. }
  42214. #endif /* NO_DSA */
  42215. #ifdef HAVE_ECC
  42216. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42217. type = EVP_PKEY_EC;
  42218. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42219. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  42220. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  42221. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  42222. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  42223. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  42224. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42225. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  42226. wolfSSL_EVP_PKEY_free(pkey);
  42227. res = TEST_RES_CHECK(1);
  42228. }
  42229. #endif /* HAVE_ECC */
  42230. (void)type;
  42231. return res;
  42232. }
  42233. static int test_wolfSSL_EVP_PKEY_base_id(void)
  42234. {
  42235. WOLFSSL_EVP_PKEY* pkey;
  42236. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42237. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  42238. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  42239. EVP_PKEY_free(pkey);
  42240. return TEST_RES_CHECK(1);
  42241. }
  42242. static int test_wolfSSL_EVP_PKEY_id(void)
  42243. {
  42244. WOLFSSL_EVP_PKEY* pkey;
  42245. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42246. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  42247. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  42248. EVP_PKEY_free(pkey);
  42249. return TEST_RES_CHECK(1);
  42250. }
  42251. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  42252. {
  42253. int res = TEST_SKIPPED;
  42254. #if defined(OPENSSL_ALL) && \
  42255. !defined(NO_ECC_SECP) && \
  42256. /* This last bit is taken from ecc.c. It is the condition that
  42257. * defines ECC256 */ \
  42258. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  42259. ECC_MIN_KEY_SZ <= 256)
  42260. EVP_PKEY_CTX* ctx;
  42261. EVP_PKEY* pkey = NULL;
  42262. /* Test error conditions. */
  42263. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  42264. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42265. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  42266. #ifndef NO_RSA
  42267. EVP_PKEY_CTX_free(ctx);
  42268. /* Parameter generation for RSA not supported yet. */
  42269. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  42270. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  42271. #endif
  42272. #ifdef HAVE_ECC
  42273. EVP_PKEY_CTX_free(ctx);
  42274. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42275. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  42276. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  42277. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  42278. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  42279. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  42280. WOLFSSL_SUCCESS);
  42281. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42282. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42283. #endif
  42284. EVP_PKEY_CTX_free(ctx);
  42285. EVP_PKEY_free(pkey);
  42286. res = TEST_RES_CHECK(1);
  42287. #endif
  42288. return res;
  42289. }
  42290. static int test_wolfSSL_EVP_PKEY_keygen(void)
  42291. {
  42292. WOLFSSL_EVP_PKEY* pkey = NULL;
  42293. EVP_PKEY_CTX* ctx = NULL;
  42294. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42295. WOLFSSL_EVP_PKEY* params = NULL;
  42296. DH* dh = NULL;
  42297. const BIGNUM* pubkey = NULL;
  42298. const BIGNUM* privkey = NULL;
  42299. ASN1_INTEGER* asn1int = NULL;
  42300. unsigned int length = 0;
  42301. byte* derBuffer = NULL;
  42302. #endif
  42303. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42304. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42305. /* Bad cases */
  42306. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  42307. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  42308. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  42309. /* Good case */
  42310. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  42311. EVP_PKEY_CTX_free(ctx);
  42312. EVP_PKEY_free(pkey);
  42313. pkey = NULL;
  42314. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42315. /* Test DH keygen */
  42316. {
  42317. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  42318. AssertNotNull(dh = DH_get_2048_256());
  42319. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42320. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  42321. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42322. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42323. DH_free(dh);
  42324. EVP_PKEY_CTX_free(ctx);
  42325. EVP_PKEY_free(params);
  42326. /* try exporting generated key to DER, to verify */
  42327. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  42328. DH_get0_key(dh, &pubkey, &privkey);
  42329. AssertNotNull(pubkey);
  42330. AssertNotNull(privkey);
  42331. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  42332. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  42333. ASN1_INTEGER_free(asn1int);
  42334. DH_free(dh);
  42335. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42336. EVP_PKEY_free(pkey);
  42337. }
  42338. #endif
  42339. return TEST_RES_CHECK(1);
  42340. }
  42341. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  42342. {
  42343. WOLFSSL_EVP_PKEY* pkey;
  42344. EVP_PKEY_CTX *ctx;
  42345. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42346. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42347. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42348. EVP_PKEY_CTX_free(ctx);
  42349. EVP_PKEY_free(pkey);
  42350. return TEST_RES_CHECK(1);
  42351. }
  42352. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  42353. {
  42354. int res = TEST_SKIPPED;
  42355. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  42356. WOLFSSL_EVP_PKEY* pkey;
  42357. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42358. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  42359. EVP_PKEY_free(pkey);
  42360. res = TEST_RES_CHECK(1);
  42361. #endif
  42362. return res;
  42363. }
  42364. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  42365. {
  42366. int res = TEST_SKIPPED;
  42367. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  42368. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  42369. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  42370. !defined(NO_FILESYSTEM)
  42371. WOLFSSL_EVP_PKEY* params = NULL;
  42372. WOLFSSL_EVP_PKEY* copy = NULL;
  42373. DH* dh = NULL;
  42374. BIGNUM* p1;
  42375. BIGNUM* g1;
  42376. BIGNUM* q1;
  42377. BIGNUM* p2;
  42378. BIGNUM* g2;
  42379. BIGNUM* q2;
  42380. /* create DH with DH_get_2048_256 params */
  42381. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  42382. AssertNotNull(dh = DH_get_2048_256());
  42383. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42384. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  42385. (const BIGNUM**)&q1,
  42386. (const BIGNUM**)&g1);
  42387. DH_free(dh);
  42388. /* create DH with random generated DH params */
  42389. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  42390. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  42391. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  42392. DH_free(dh);
  42393. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  42394. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  42395. AssertNotNull(dh->p);
  42396. AssertNotNull(dh->g);
  42397. AssertNotNull(dh->q);
  42398. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  42399. (const BIGNUM**)&q2,
  42400. (const BIGNUM**)&g2);
  42401. AssertIntEQ(BN_cmp(p1, p2), 0);
  42402. AssertIntEQ(BN_cmp(q1, q2), 0);
  42403. AssertIntEQ(BN_cmp(g1, g2), 0);
  42404. DH_free(dh);
  42405. EVP_PKEY_free(copy);
  42406. EVP_PKEY_free(params);
  42407. res = TEST_RES_CHECK(1);
  42408. #endif
  42409. return res;
  42410. }
  42411. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  42412. {
  42413. WOLFSSL_EVP_PKEY* pkey;
  42414. EVP_PKEY_CTX *ctx;
  42415. int bits = 2048;
  42416. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42417. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42418. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  42419. WOLFSSL_SUCCESS);
  42420. EVP_PKEY_CTX_free(ctx);
  42421. EVP_PKEY_free(pkey);
  42422. return TEST_RES_CHECK(1);
  42423. }
  42424. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  42425. {
  42426. /* This is large enough to be used for all key sizes */
  42427. byte key[AES_256_KEY_SIZE] = {0};
  42428. byte iv[AES_BLOCK_SIZE] = {0};
  42429. int i, enumlen;
  42430. EVP_CIPHER_CTX *ctx;
  42431. const EVP_CIPHER *init;
  42432. int enumArray[] = {
  42433. #ifdef HAVE_AES_CBC
  42434. NID_aes_128_cbc,
  42435. #endif
  42436. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42437. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42438. #ifdef HAVE_AESGCM
  42439. NID_aes_128_gcm,
  42440. #endif
  42441. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42442. #ifdef WOLFSSL_AES_COUNTER
  42443. NID_aes_128_ctr,
  42444. #endif
  42445. #ifndef NO_DES3
  42446. NID_des_cbc,
  42447. NID_des_ede3_cbc,
  42448. #endif
  42449. };
  42450. int iv_lengths[] = {
  42451. #ifdef HAVE_AES_CBC
  42452. AES_BLOCK_SIZE,
  42453. #endif
  42454. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42455. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42456. #ifdef HAVE_AESGCM
  42457. GCM_NONCE_MID_SZ,
  42458. #endif
  42459. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42460. #ifdef WOLFSSL_AES_COUNTER
  42461. AES_BLOCK_SIZE,
  42462. #endif
  42463. #ifndef NO_DES3
  42464. DES_BLOCK_SIZE,
  42465. DES_BLOCK_SIZE,
  42466. #endif
  42467. };
  42468. enumlen = (sizeof(enumArray)/sizeof(int));
  42469. for (i = 0; i < enumlen; i++) {
  42470. ctx = EVP_CIPHER_CTX_new();
  42471. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  42472. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42473. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42474. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  42475. EVP_CIPHER_CTX_free(ctx);
  42476. }
  42477. return TEST_RES_CHECK(1);
  42478. }
  42479. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  42480. {
  42481. int res = TEST_SKIPPED;
  42482. #if !defined(NO_DES3)
  42483. byte key[AES_256_KEY_SIZE] = {0};
  42484. byte iv[AES_BLOCK_SIZE] = {0};
  42485. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  42486. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  42487. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42488. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42489. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  42490. EVP_CIPHER_CTX_free(ctx);
  42491. res = TEST_RES_CHECK(1);
  42492. #endif
  42493. return res;
  42494. }
  42495. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  42496. {
  42497. int res = TEST_SKIPPED;
  42498. #if !defined(NO_DES3)
  42499. byte key[AES_256_KEY_SIZE] = {0};
  42500. byte iv[AES_BLOCK_SIZE] = {0};
  42501. int keylen;
  42502. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  42503. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  42504. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42505. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42506. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  42507. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  42508. WOLFSSL_SUCCESS);
  42509. EVP_CIPHER_CTX_free(ctx);
  42510. res = TEST_RES_CHECK(1);
  42511. #endif
  42512. return res;
  42513. }
  42514. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  42515. {
  42516. int res = TEST_SKIPPED;
  42517. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  42518. byte key[DES3_KEY_SIZE] = {0};
  42519. byte iv[DES_BLOCK_SIZE] = {0};
  42520. int ivLen, keyLen;
  42521. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  42522. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  42523. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42524. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42525. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  42526. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  42527. /* Bad cases */
  42528. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  42529. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  42530. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  42531. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  42532. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  42533. /* Good case */
  42534. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  42535. EVP_CIPHER_CTX_free(ctx);
  42536. res = TEST_RES_CHECK(1);
  42537. #endif
  42538. return res;
  42539. }
  42540. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  42541. {
  42542. WOLFSSL_ENGINE* e = NULL;
  42543. int id = 0;
  42544. EVP_PKEY_CTX *ctx;
  42545. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  42546. EVP_PKEY_CTX_free(ctx);
  42547. return TEST_RES_CHECK(1);
  42548. }
  42549. static int test_wolfSSL_EVP_rc4(void)
  42550. {
  42551. int res = TEST_SKIPPED;
  42552. #if !defined(NO_RC4)
  42553. res = TEST_RES_CHECK(wolfSSL_EVP_rc4() != NULL);
  42554. #endif
  42555. return res;
  42556. }
  42557. static int test_wolfSSL_EVP_enc_null(void)
  42558. {
  42559. return TEST_RES_CHECK(wolfSSL_EVP_enc_null() != NULL);
  42560. }
  42561. static int test_wolfSSL_EVP_rc2_cbc(void)
  42562. {
  42563. int res = TEST_SKIPPED;
  42564. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  42565. res = TEST_RES_CHECK(wolfSSL_EVP_rc2_cbc() == NULL);
  42566. #endif
  42567. return res;
  42568. }
  42569. static int test_wolfSSL_EVP_mdc2(void)
  42570. {
  42571. int res = TEST_SKIPPED;
  42572. #if !defined(NO_WOLFSSL_STUB)
  42573. res = TEST_RES_CHECK(wolfSSL_EVP_mdc2() == NULL);
  42574. #endif
  42575. return res;
  42576. }
  42577. static int test_wolfSSL_EVP_md4(void)
  42578. {
  42579. int res = TEST_SKIPPED;
  42580. #if !defined(NO_MD4)
  42581. res = TEST_RES_CHECK(wolfSSL_EVP_md4() != NULL);
  42582. #endif
  42583. return res;
  42584. }
  42585. static int test_wolfSSL_EVP_aes_256_gcm(void)
  42586. {
  42587. int res = TEST_SKIPPED;
  42588. #ifdef HAVE_AESGCM
  42589. res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_gcm() != NULL);
  42590. #endif
  42591. return res;
  42592. }
  42593. static int test_wolfSSL_EVP_aes_192_gcm(void)
  42594. {
  42595. int res = TEST_SKIPPED;
  42596. #ifdef HAVE_AESGCM
  42597. res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_gcm() != NULL);
  42598. #endif
  42599. return res;
  42600. }
  42601. static int test_wolfSSL_EVP_aes_256_ccm(void)
  42602. {
  42603. int res = TEST_SKIPPED;
  42604. #ifdef HAVE_AESCCM
  42605. res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_ccm() != NULL);
  42606. #endif
  42607. return res;
  42608. }
  42609. static int test_wolfSSL_EVP_aes_192_ccm(void)
  42610. {
  42611. int res = TEST_SKIPPED;
  42612. #ifdef HAVE_AESCCM
  42613. res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_ccm() != NULL);
  42614. #endif
  42615. return res;
  42616. }
  42617. static int test_wolfSSL_EVP_aes_128_ccm(void)
  42618. {
  42619. int res = TEST_SKIPPED;
  42620. #ifdef HAVE_AESCCM
  42621. res = TEST_RES_CHECK(wolfSSL_EVP_aes_128_ccm() != NULL);
  42622. #endif
  42623. return res;
  42624. }
  42625. static int test_wolfSSL_EVP_ripemd160(void)
  42626. {
  42627. int res = TEST_SKIPPED;
  42628. #if !defined(NO_WOLFSSL_STUB)
  42629. res = TEST_RES_CHECK(wolfSSL_EVP_ripemd160() == NULL);
  42630. #endif
  42631. return res;
  42632. }
  42633. static int test_wolfSSL_EVP_get_digestbynid(void)
  42634. {
  42635. #ifndef NO_MD5
  42636. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  42637. #endif
  42638. #ifndef NO_SHA
  42639. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  42640. #endif
  42641. #ifndef NO_SHA256
  42642. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  42643. #endif
  42644. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  42645. return TEST_RES_CHECK(1);
  42646. }
  42647. static int test_wolfSSL_EVP_MD_nid(void)
  42648. {
  42649. #ifndef NO_MD5
  42650. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  42651. #endif
  42652. #ifndef NO_SHA
  42653. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  42654. #endif
  42655. #ifndef NO_SHA256
  42656. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  42657. #endif
  42658. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  42659. return TEST_RES_CHECK(1);
  42660. }
  42661. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  42662. {
  42663. int res = TEST_SKIPPED;
  42664. #if defined(HAVE_ECC)
  42665. WOLFSSL_EVP_PKEY* pkey;
  42666. AssertNotNull(pkey = EVP_PKEY_new());
  42667. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  42668. EVP_PKEY_free(pkey);
  42669. res = TEST_RES_CHECK(1);
  42670. #endif
  42671. return res;
  42672. }
  42673. static int test_wolfSSL_EVP_X_STATE(void)
  42674. {
  42675. int res = TEST_SKIPPED;
  42676. #if !defined(NO_DES3) && !defined(NO_RC4)
  42677. byte key[DES3_KEY_SIZE] = {0};
  42678. byte iv[DES_IV_SIZE] = {0};
  42679. EVP_CIPHER_CTX *ctx;
  42680. const EVP_CIPHER *init;
  42681. /* Bad test cases */
  42682. ctx = EVP_CIPHER_CTX_new();
  42683. init = EVP_des_ede3_cbc();
  42684. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42685. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42686. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  42687. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  42688. EVP_CIPHER_CTX_free(ctx);
  42689. /* Good test case */
  42690. ctx = EVP_CIPHER_CTX_new();
  42691. init = wolfSSL_EVP_rc4();
  42692. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42693. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42694. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  42695. EVP_CIPHER_CTX_free(ctx);
  42696. res = TEST_RES_CHECK(1);
  42697. #endif
  42698. return res;
  42699. }
  42700. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  42701. {
  42702. int res = TEST_SKIPPED;
  42703. #if !defined(NO_DES3) && !defined(NO_RC4)
  42704. byte key[DES3_KEY_SIZE] = {0};
  42705. byte iv[DES_IV_SIZE] = {0};
  42706. EVP_CIPHER_CTX *ctx;
  42707. const EVP_CIPHER *init;
  42708. /* Bad test cases */
  42709. ctx = EVP_CIPHER_CTX_new();
  42710. init = EVP_des_ede3_cbc();
  42711. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42712. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42713. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  42714. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  42715. EVP_CIPHER_CTX_free(ctx);
  42716. /* Good test case */
  42717. ctx = EVP_CIPHER_CTX_new();
  42718. init = wolfSSL_EVP_rc4();
  42719. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42720. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42721. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  42722. EVP_CIPHER_CTX_free(ctx);
  42723. res = TEST_RES_CHECK(1);
  42724. #endif
  42725. return res;
  42726. }
  42727. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  42728. {
  42729. int res = TEST_SKIPPED;
  42730. #ifdef HAVE_AES_CBC
  42731. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42732. #ifdef WOLFSSL_AES_128
  42733. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  42734. #endif
  42735. #ifdef WOLFSSL_AES_192
  42736. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  42737. #endif
  42738. #ifdef WOLFSSL_AES_256
  42739. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  42740. #endif
  42741. res = TEST_RES_CHECK(1);
  42742. }
  42743. #endif
  42744. #ifdef HAVE_AESGCM
  42745. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42746. #ifdef WOLFSSL_AES_128
  42747. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  42748. #endif
  42749. #ifdef WOLFSSL_AES_192
  42750. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  42751. #endif
  42752. #ifdef WOLFSSL_AES_256
  42753. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  42754. #endif
  42755. res = TEST_RES_CHECK(1);
  42756. }
  42757. #endif
  42758. #ifdef HAVE_AESCCM
  42759. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42760. #ifdef WOLFSSL_AES_128
  42761. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  42762. #endif
  42763. #ifdef WOLFSSL_AES_192
  42764. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  42765. #endif
  42766. #ifdef WOLFSSL_AES_256
  42767. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  42768. #endif
  42769. res = TEST_RES_CHECK(1);
  42770. }
  42771. #endif
  42772. #ifdef WOLFSSL_AES_COUNTER
  42773. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42774. #ifdef WOLFSSL_AES_128
  42775. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  42776. #endif
  42777. #ifdef WOLFSSL_AES_192
  42778. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  42779. #endif
  42780. #ifdef WOLFSSL_AES_256
  42781. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  42782. #endif
  42783. res = TEST_RES_CHECK(1);
  42784. }
  42785. #endif
  42786. #ifdef HAVE_AES_ECB
  42787. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42788. #ifdef WOLFSSL_AES_128
  42789. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  42790. #endif
  42791. #ifdef WOLFSSL_AES_192
  42792. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  42793. #endif
  42794. #ifdef WOLFSSL_AES_256
  42795. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  42796. #endif
  42797. res = TEST_RES_CHECK(1);
  42798. }
  42799. #endif
  42800. #ifdef WOLFSSL_AES_OFB
  42801. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42802. #ifdef WOLFSSL_AES_128
  42803. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  42804. #endif
  42805. #ifdef WOLFSSL_AES_192
  42806. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  42807. #endif
  42808. #ifdef WOLFSSL_AES_256
  42809. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  42810. #endif
  42811. res = TEST_RES_CHECK(1);
  42812. }
  42813. #endif
  42814. #ifndef NO_RC4
  42815. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42816. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  42817. res = TEST_RES_CHECK(1);
  42818. }
  42819. #endif
  42820. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42821. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42822. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  42823. res = TEST_RES_CHECK(1);
  42824. }
  42825. #endif
  42826. return res;
  42827. }
  42828. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  42829. {
  42830. int i, enumlen;
  42831. int enumArray[] = {
  42832. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42833. #ifdef WOLFSSL_AES_128
  42834. NID_aes_128_cbc,
  42835. #endif
  42836. #ifdef WOLFSSL_AES_192
  42837. NID_aes_192_cbc,
  42838. #endif
  42839. #ifdef WOLFSSL_AES_256
  42840. NID_aes_256_cbc,
  42841. #endif
  42842. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42843. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42844. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42845. #ifdef HAVE_AESGCM
  42846. #ifdef WOLFSSL_AES_128
  42847. NID_aes_128_gcm,
  42848. #endif
  42849. #ifdef WOLFSSL_AES_192
  42850. NID_aes_192_gcm,
  42851. #endif
  42852. #ifdef WOLFSSL_AES_256
  42853. NID_aes_256_gcm,
  42854. #endif
  42855. #endif /* HAVE_AESGCM */
  42856. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42857. #ifdef WOLFSSL_AES_COUNTER
  42858. #ifdef WOLFSSL_AES_128
  42859. NID_aes_128_ctr,
  42860. #endif
  42861. #ifdef WOLFSSL_AES_192
  42862. NID_aes_192_ctr,
  42863. #endif
  42864. #ifdef WOLFSSL_AES_256
  42865. NID_aes_256_ctr,
  42866. #endif
  42867. #endif
  42868. #ifndef NO_DES3
  42869. NID_des_cbc,
  42870. NID_des_ede3_cbc,
  42871. #endif
  42872. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42873. NID_chacha20_poly1305,
  42874. #endif
  42875. };
  42876. int iv_lengths[] = {
  42877. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42878. #ifdef WOLFSSL_AES_128
  42879. AES_BLOCK_SIZE,
  42880. #endif
  42881. #ifdef WOLFSSL_AES_192
  42882. AES_BLOCK_SIZE,
  42883. #endif
  42884. #ifdef WOLFSSL_AES_256
  42885. AES_BLOCK_SIZE,
  42886. #endif
  42887. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42888. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42889. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42890. #ifdef HAVE_AESGCM
  42891. #ifdef WOLFSSL_AES_128
  42892. GCM_NONCE_MID_SZ,
  42893. #endif
  42894. #ifdef WOLFSSL_AES_192
  42895. GCM_NONCE_MID_SZ,
  42896. #endif
  42897. #ifdef WOLFSSL_AES_256
  42898. GCM_NONCE_MID_SZ,
  42899. #endif
  42900. #endif /* HAVE_AESGCM */
  42901. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42902. #ifdef WOLFSSL_AES_COUNTER
  42903. #ifdef WOLFSSL_AES_128
  42904. AES_BLOCK_SIZE,
  42905. #endif
  42906. #ifdef WOLFSSL_AES_192
  42907. AES_BLOCK_SIZE,
  42908. #endif
  42909. #ifdef WOLFSSL_AES_256
  42910. AES_BLOCK_SIZE,
  42911. #endif
  42912. #endif
  42913. #ifndef NO_DES3
  42914. DES_BLOCK_SIZE,
  42915. DES_BLOCK_SIZE,
  42916. #endif
  42917. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42918. CHACHA20_POLY1305_AEAD_IV_SIZE,
  42919. #endif
  42920. };
  42921. enumlen = (sizeof(enumArray)/sizeof(int));
  42922. for (i = 0; i < enumlen; i++) {
  42923. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  42924. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  42925. }
  42926. return TEST_RES_CHECK(1);
  42927. }
  42928. static int test_wolfSSL_EVP_SignInit_ex(void)
  42929. {
  42930. WOLFSSL_EVP_MD_CTX mdCtx;
  42931. WOLFSSL_ENGINE* e = 0;
  42932. const EVP_MD* md;
  42933. md = "SHA256";
  42934. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42935. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  42936. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42937. return TEST_RES_CHECK(1);
  42938. }
  42939. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  42940. {
  42941. int res = TEST_SKIPPED;
  42942. #if !defined(NO_SHA256)
  42943. WOLFSSL_EVP_MD_CTX mdCtx;
  42944. unsigned int s = 0;
  42945. unsigned char md[WC_SHA256_DIGEST_SIZE];
  42946. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  42947. /* Bad Case */
  42948. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42949. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42950. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  42951. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42952. #else
  42953. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42954. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  42955. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42956. #endif
  42957. /* Good Case */
  42958. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42959. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  42960. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  42961. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42962. res = TEST_RES_CHECK(1);
  42963. #endif
  42964. return res;
  42965. }
  42966. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  42967. {
  42968. int res = TEST_SKIPPED;
  42969. #if !defined(NO_DH) && \
  42970. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42971. FILE* f = NULL;
  42972. unsigned char buf[4096];
  42973. const unsigned char* pt = buf;
  42974. const char* params1 = "./certs/dh2048.der";
  42975. long len = 0;
  42976. WOLFSSL_DH* dh = NULL;
  42977. WOLFSSL_EVP_PKEY* pkey;
  42978. XMEMSET(buf, 0, sizeof(buf));
  42979. f = XFOPEN(params1, "rb");
  42980. AssertTrue(f != XBADFILE);
  42981. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  42982. XFCLOSE(f);
  42983. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42984. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  42985. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42986. /* Bad cases */
  42987. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  42988. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  42989. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  42990. /* Good case */
  42991. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  42992. EVP_PKEY_free(pkey);
  42993. res = TEST_RES_CHECK(1);
  42994. #endif
  42995. return res;
  42996. }
  42997. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  42998. {
  42999. int res = TEST_SKIPPED;
  43000. #if defined(OPENSSL_EXTRA)
  43001. EVP_PKEY* pkey;
  43002. EVP_PKEY_CTX* ctx;
  43003. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43004. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43005. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  43006. /* void */
  43007. EVP_PKEY_CTX_free(ctx);
  43008. AssertTrue(1);
  43009. #else
  43010. /* int */
  43011. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  43012. #endif
  43013. EVP_PKEY_free(pkey);
  43014. res = TEST_RES_CHECK(1);
  43015. #endif
  43016. return res;
  43017. }
  43018. static int test_wolfSSL_EVP_PKEY_param_check(void)
  43019. {
  43020. int res = TEST_SKIPPED;
  43021. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  43022. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43023. DH *dh = NULL;
  43024. DH *setDh = NULL;
  43025. EVP_PKEY *pkey = NULL;
  43026. EVP_PKEY_CTX* ctx = NULL;
  43027. FILE* f = NULL;
  43028. unsigned char buf[512];
  43029. const unsigned char* pt = buf;
  43030. const char* dh2048 = "./certs/dh2048.der";
  43031. long len = 0;
  43032. int code = -1;
  43033. XMEMSET(buf, 0, sizeof(buf));
  43034. f = XFOPEN(dh2048, "rb");
  43035. AssertTrue(f != XBADFILE);
  43036. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  43037. XFCLOSE(f);
  43038. /* Load dh2048.der into DH with internal format */
  43039. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  43040. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43041. AssertIntEQ(code, 0);
  43042. code = -1;
  43043. pkey = wolfSSL_EVP_PKEY_new();
  43044. /* Set DH into PKEY */
  43045. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43046. /* create ctx from pkey */
  43047. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43048. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  43049. /* */
  43050. /* TO DO invlaid case */
  43051. /* */
  43052. EVP_PKEY_CTX_free(ctx);
  43053. EVP_PKEY_free(pkey);
  43054. DH_free(setDh);
  43055. DH_free(dh);
  43056. res = TEST_RES_CHECK(1);
  43057. #endif
  43058. #endif
  43059. return res;
  43060. }
  43061. static int test_wolfSSL_EVP_BytesToKey(void)
  43062. {
  43063. int res = TEST_SKIPPED;
  43064. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43065. byte key[AES_BLOCK_SIZE] = {0};
  43066. byte iv[AES_BLOCK_SIZE] = {0};
  43067. int sz = 5;
  43068. int count = 0;
  43069. const EVP_MD* md = "SHA256";
  43070. const EVP_CIPHER *type;
  43071. const unsigned char *salt = (unsigned char *)"salt1234";
  43072. const byte data[] = {
  43073. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43074. 0x72,0x6c,0x64
  43075. };
  43076. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  43077. /* Bad cases */
  43078. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  43079. 0);
  43080. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  43081. 16);
  43082. md = "2";
  43083. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43084. WOLFSSL_FAILURE);
  43085. /* Good case */
  43086. md = "SHA256";
  43087. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43088. 16);
  43089. res = TEST_RES_CHECK(1);
  43090. #endif
  43091. return res;
  43092. }
  43093. static int test_evp_cipher_aes_gcm(void)
  43094. {
  43095. int res = TEST_SKIPPED;
  43096. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  43097. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  43098. (HAVE_FIPS_VERSION >= 2)))
  43099. /*
  43100. * This test checks data at various points in the encrypt/decrypt process
  43101. * against known values produced using the same test with OpenSSL. This
  43102. * interop testing is critical for verifying the correctness of our
  43103. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  43104. * a flow supported by OpenSSL that uses the control command
  43105. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  43106. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  43107. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  43108. * wolfSSL OpenSSH clients because there was a bug in this flow that
  43109. * happened to "cancel out" if both sides of the connection had the bug.
  43110. */
  43111. enum {
  43112. NUM_ENCRYPTIONS = 3,
  43113. AAD_SIZE = 4
  43114. };
  43115. byte plainText1[] = {
  43116. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  43117. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  43118. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  43119. };
  43120. byte plainText2[] = {
  43121. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  43122. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  43123. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  43124. };
  43125. byte plainText3[] = {
  43126. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  43127. 0x5b, 0x57, 0x10
  43128. };
  43129. byte* plainTexts[NUM_ENCRYPTIONS] = {
  43130. plainText1,
  43131. plainText2,
  43132. plainText3
  43133. };
  43134. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  43135. sizeof(plainText1),
  43136. sizeof(plainText2),
  43137. sizeof(plainText3)
  43138. };
  43139. byte aad1[AAD_SIZE] = {
  43140. 0x00, 0x00, 0x00, 0x01
  43141. };
  43142. byte aad2[AAD_SIZE] = {
  43143. 0x00, 0x00, 0x00, 0x10
  43144. };
  43145. byte aad3[AAD_SIZE] = {
  43146. 0x00, 0x00, 0x01, 0x00
  43147. };
  43148. byte* aads[NUM_ENCRYPTIONS] = {
  43149. aad1,
  43150. aad2,
  43151. aad3
  43152. };
  43153. const byte iv[GCM_NONCE_MID_SZ] = {
  43154. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  43155. };
  43156. byte currentIv[GCM_NONCE_MID_SZ];
  43157. const byte key[] = {
  43158. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  43159. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  43160. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  43161. };
  43162. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  43163. {
  43164. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43165. 0xEF
  43166. },
  43167. {
  43168. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43169. 0xF0
  43170. },
  43171. {
  43172. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43173. 0xF1
  43174. }
  43175. };
  43176. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  43177. {
  43178. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  43179. 0x3D, 0x32, 0x18, 0x34, 0xA9
  43180. },
  43181. {
  43182. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  43183. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  43184. },
  43185. {
  43186. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  43187. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  43188. }
  43189. };
  43190. const byte expCipherText1[] = {
  43191. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  43192. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  43193. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  43194. };
  43195. const byte expCipherText2[] = {
  43196. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  43197. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  43198. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  43199. };
  43200. const byte expCipherText3[] = {
  43201. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  43202. 0x80, 0x5E, 0x6B,
  43203. };
  43204. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  43205. expCipherText1,
  43206. expCipherText2,
  43207. expCipherText3
  43208. };
  43209. byte* cipherText;
  43210. byte* calcPlainText;
  43211. byte tag[AES_BLOCK_SIZE];
  43212. EVP_CIPHER_CTX* encCtx = NULL;
  43213. EVP_CIPHER_CTX* decCtx = NULL;
  43214. int i, j, outl;
  43215. /****************************************************/
  43216. for (i = 0; i < 3; ++i) {
  43217. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  43218. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  43219. /* First iteration, set key before IV. */
  43220. if (i == 0) {
  43221. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  43222. SSL_SUCCESS);
  43223. /*
  43224. * The call to EVP_CipherInit below (with NULL key) should clear the
  43225. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  43226. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  43227. * behavior.
  43228. */
  43229. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43230. (void*)iv), SSL_SUCCESS);
  43231. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  43232. SSL_SUCCESS);
  43233. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43234. currentIv), SSL_FAILURE);
  43235. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  43236. SSL_SUCCESS);
  43237. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  43238. SSL_SUCCESS);
  43239. }
  43240. /* Second iteration, IV before key. */
  43241. else {
  43242. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  43243. SSL_SUCCESS);
  43244. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  43245. SSL_SUCCESS);
  43246. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  43247. SSL_SUCCESS);
  43248. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  43249. SSL_SUCCESS);
  43250. }
  43251. /*
  43252. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  43253. * been issued first.
  43254. */
  43255. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43256. currentIv), SSL_FAILURE);
  43257. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43258. (void*)iv), SSL_SUCCESS);
  43259. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43260. (void*)iv), SSL_SUCCESS);
  43261. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  43262. /*************** Encrypt ***************/
  43263. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43264. currentIv), SSL_SUCCESS);
  43265. /* Check current IV against expected. */
  43266. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43267. /* Add AAD. */
  43268. if (i == 2) {
  43269. /* Test streaming API. */
  43270. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  43271. AAD_SIZE), SSL_SUCCESS);
  43272. }
  43273. else {
  43274. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  43275. AAD_SIZE);
  43276. }
  43277. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43278. DYNAMIC_TYPE_TMP_BUFFER));
  43279. /* Encrypt plaintext. */
  43280. if (i == 2) {
  43281. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  43282. plainTexts[j], plainTextSzs[j]),
  43283. SSL_SUCCESS);
  43284. }
  43285. else {
  43286. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  43287. plainTextSzs[j]), plainTextSzs[j]);
  43288. }
  43289. if (i == 2) {
  43290. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  43291. SSL_SUCCESS);
  43292. }
  43293. else {
  43294. /*
  43295. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  43296. * akin to calling EVP_CipherFinal.
  43297. */
  43298. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  43299. }
  43300. /* Check ciphertext against expected. */
  43301. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  43302. 0);
  43303. /* Get and check tag against expected. */
  43304. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  43305. sizeof(tag), tag), SSL_SUCCESS);
  43306. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  43307. /*************** Decrypt ***************/
  43308. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43309. currentIv), SSL_SUCCESS);
  43310. /* Check current IV against expected. */
  43311. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43312. /* Add AAD. */
  43313. if (i == 2) {
  43314. /* Test streaming API. */
  43315. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  43316. AAD_SIZE), SSL_SUCCESS);
  43317. }
  43318. else {
  43319. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  43320. AAD_SIZE);
  43321. }
  43322. /* Set expected tag. */
  43323. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  43324. sizeof(tag), tag), SSL_SUCCESS);
  43325. /* Decrypt ciphertext. */
  43326. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43327. DYNAMIC_TYPE_TMP_BUFFER));
  43328. if (i == 2) {
  43329. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  43330. cipherText, plainTextSzs[j]),
  43331. SSL_SUCCESS);
  43332. }
  43333. else {
  43334. /* This first EVP_Cipher call will check the tag, too. */
  43335. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  43336. plainTextSzs[j]), plainTextSzs[j]);
  43337. }
  43338. if (i == 2) {
  43339. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  43340. SSL_SUCCESS);
  43341. }
  43342. else {
  43343. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  43344. }
  43345. /* Check plaintext against expected. */
  43346. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  43347. 0);
  43348. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43349. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43350. }
  43351. EVP_CIPHER_CTX_free(encCtx);
  43352. EVP_CIPHER_CTX_free(decCtx);
  43353. }
  43354. res = TEST_RES_CHECK(1);
  43355. #endif
  43356. return res;
  43357. }
  43358. static int test_wolfSSL_OBJ_ln(void)
  43359. {
  43360. const int nid_set[] = {
  43361. NID_commonName,
  43362. NID_serialNumber,
  43363. NID_countryName,
  43364. NID_localityName,
  43365. NID_stateOrProvinceName,
  43366. NID_organizationName,
  43367. NID_organizationalUnitName,
  43368. NID_domainComponent,
  43369. NID_businessCategory,
  43370. NID_jurisdictionCountryName,
  43371. NID_jurisdictionStateOrProvinceName,
  43372. NID_emailAddress
  43373. };
  43374. const char* ln_set[] = {
  43375. "commonName",
  43376. "serialNumber",
  43377. "countryName",
  43378. "localityName",
  43379. "stateOrProvinceName",
  43380. "organizationName",
  43381. "organizationalUnitName",
  43382. "domainComponent",
  43383. "businessCategory",
  43384. "jurisdictionCountryName",
  43385. "jurisdictionStateOrProvinceName",
  43386. "emailAddress",
  43387. };
  43388. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  43389. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  43390. #ifdef HAVE_ECC
  43391. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43392. {
  43393. EC_builtin_curve r[27];
  43394. size_t nCurves = sizeof(r) / sizeof(r[0]);
  43395. nCurves = EC_get_builtin_curves(r,nCurves);
  43396. for (i = 0; i < nCurves; i++) {
  43397. /* skip ECC_CURVE_INVALID */
  43398. if (r[i].nid != ECC_CURVE_INVALID) {
  43399. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  43400. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  43401. }
  43402. }
  43403. }
  43404. #endif
  43405. #endif
  43406. for (i = 0; i < maxIdx; i++) {
  43407. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  43408. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  43409. }
  43410. return TEST_RES_CHECK(1);
  43411. }
  43412. static int test_wolfSSL_OBJ_sn(void)
  43413. {
  43414. int i = 0, maxIdx = 7;
  43415. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  43416. NID_stateOrProvinceName,NID_organizationName,
  43417. NID_organizationalUnitName,NID_emailAddress};
  43418. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  43419. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  43420. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  43421. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  43422. WOLFSSL_EMAIL_ADDR};
  43423. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  43424. for (i = 0; i < maxIdx; i++) {
  43425. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  43426. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  43427. }
  43428. return TEST_RES_CHECK(1);
  43429. }
  43430. #if !defined(NO_BIO)
  43431. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  43432. {
  43433. return lh_strhash(s[3]);
  43434. }
  43435. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  43436. {
  43437. return XSTRCMP(a[3], b[3]);
  43438. }
  43439. #endif
  43440. static int test_wolfSSL_TXT_DB(void)
  43441. {
  43442. int res = TEST_SKIPPED;
  43443. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43444. BIO *bio;
  43445. TXT_DB *db = NULL;
  43446. const int columns = 6;
  43447. const char *fields[6] = {
  43448. "V",
  43449. "320926161116Z",
  43450. "",
  43451. "12BD",
  43452. "unknown",
  43453. "/CN=rsa doe",
  43454. };
  43455. char** fields_copy;
  43456. /* Test read */
  43457. AssertNotNull(bio = BIO_new(BIO_s_file()));
  43458. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  43459. AssertNotNull(db = TXT_DB_read(bio, columns));
  43460. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  43461. DYNAMIC_TYPE_OPENSSL));
  43462. XMEMCPY(fields_copy, fields, sizeof(fields));
  43463. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  43464. BIO_free(bio);
  43465. /* Test write */
  43466. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  43467. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  43468. BIO_free(bio);
  43469. /* Test index */
  43470. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  43471. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  43472. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43473. fields[3] = "12DA";
  43474. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43475. fields[3] = "FFFF";
  43476. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43477. fields[3] = "";
  43478. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43479. TXT_DB_free(db);
  43480. res = TEST_RES_CHECK(1);
  43481. #endif
  43482. return res;
  43483. }
  43484. static int test_wolfSSL_NCONF(void)
  43485. {
  43486. int res = TEST_SKIPPED;
  43487. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43488. const char* confFile = "./tests/NCONF_test.cnf";
  43489. CONF* conf = NULL;
  43490. long eline = 0;
  43491. long num = 0;
  43492. AssertNotNull(conf = NCONF_new(NULL));
  43493. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  43494. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  43495. AssertIntEQ(num, 1234);
  43496. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  43497. AssertIntEQ(num, 4321);
  43498. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  43499. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  43500. "./test-dir/file1");
  43501. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  43502. "./test-dir/file1:./test-dir/file2:./section1:file2");
  43503. NCONF_free(conf);
  43504. res = TEST_RES_CHECK(1);
  43505. #endif
  43506. return res;
  43507. }
  43508. #endif /* OPENSSL_ALL */
  43509. static int test_wolfSSL_X509V3_EXT_get(void) {
  43510. int res = TEST_SKIPPED;
  43511. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43512. FILE* f;
  43513. int numOfExt =0;
  43514. int extNid = 0;
  43515. int i = 0;
  43516. WOLFSSL_X509* x509;
  43517. WOLFSSL_X509_EXTENSION* ext;
  43518. const WOLFSSL_v3_ext_method* method;
  43519. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  43520. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43521. fclose(f);
  43522. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  43523. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43524. for (i = 0; i < numOfExt; i++) {
  43525. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43526. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  43527. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43528. AssertIntEQ(method->ext_nid, extNid);
  43529. }
  43530. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  43531. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  43532. wolfSSL_X509_free(x509);
  43533. res = TEST_RES_CHECK(1);
  43534. #endif
  43535. return res;
  43536. }
  43537. static int test_wolfSSL_X509V3_EXT_nconf(void)
  43538. {
  43539. int res = TEST_SKIPPED;
  43540. #ifdef OPENSSL_ALL
  43541. const char *ext_names[] = {
  43542. "subjectKeyIdentifier",
  43543. "authorityKeyIdentifier",
  43544. "subjectAltName",
  43545. "keyUsage",
  43546. };
  43547. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  43548. int ext_nids[] = {
  43549. NID_subject_key_identifier,
  43550. NID_authority_key_identifier,
  43551. NID_subject_alt_name,
  43552. NID_key_usage,
  43553. };
  43554. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  43555. const char *ext_values[] = {
  43556. "hash",
  43557. "hash",
  43558. "DNS:example.com, IP:127.0.0.1",
  43559. "digitalSignature,keyEncipherment,dataEncipherment",
  43560. };
  43561. size_t i;
  43562. X509_EXTENSION* ext;
  43563. X509* x509 = X509_new();
  43564. for (i = 0; i < ext_names_count; i++) {
  43565. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  43566. AssertNotNull(ext);
  43567. X509_EXTENSION_free(ext);
  43568. }
  43569. for (i = 0; i < ext_nids_count; i++) {
  43570. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  43571. AssertNotNull(ext);
  43572. X509_EXTENSION_free(ext);
  43573. }
  43574. /* Test adding extension to X509 */
  43575. for (i = 0; i < ext_nids_count; i++) {
  43576. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  43577. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  43578. X509_EXTENSION_free(ext);
  43579. }
  43580. X509_free(x509);
  43581. res = TEST_RES_CHECK(1);
  43582. #endif
  43583. return res;
  43584. }
  43585. static int test_wolfSSL_X509V3_EXT(void) {
  43586. int res = TEST_SKIPPED;
  43587. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43588. FILE* f;
  43589. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  43590. char* str;
  43591. unsigned char* data;
  43592. const WOLFSSL_v3_ext_method* method;
  43593. WOLFSSL_X509* x509;
  43594. WOLFSSL_X509_EXTENSION* ext;
  43595. WOLFSSL_X509_EXTENSION* ext2;
  43596. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  43597. WOLFSSL_ASN1_STRING* asn1str;
  43598. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  43599. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  43600. WOLFSSL_BASIC_CONSTRAINTS* bc;
  43601. WOLFSSL_ACCESS_DESCRIPTION* ad;
  43602. WOLFSSL_GENERAL_NAME* gn;
  43603. /* Check NULL argument */
  43604. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  43605. /* Using OCSP cert with X509V3 extensions */
  43606. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  43607. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43608. fclose(f);
  43609. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43610. /* Basic Constraints */
  43611. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43612. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43613. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  43614. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  43615. AssertIntEQ(bc->ca, 1);
  43616. AssertNull(bc->pathlen);
  43617. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  43618. i++;
  43619. /* Subject Key Identifier */
  43620. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43621. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43622. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  43623. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43624. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  43625. asn1str));
  43626. X509_EXTENSION_free(ext2);
  43627. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43628. AssertNotNull(method->i2s);
  43629. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  43630. wolfSSL_ASN1_STRING_free(asn1str);
  43631. actual = strcmp(str,
  43632. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43633. AssertIntEQ(actual, 0);
  43634. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43635. i++;
  43636. /* Authority Key Identifier */
  43637. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43638. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43639. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  43640. AssertNotNull(aKeyId =
  43641. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  43642. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43643. AssertNotNull(asn1str = aKeyId->keyid);
  43644. AssertNotNull(str =
  43645. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  43646. actual = strcmp(str,
  43647. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43648. AssertIntEQ(actual, 0);
  43649. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43650. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  43651. i++;
  43652. /* Key Usage */
  43653. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43654. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43655. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  43656. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43657. #if defined(WOLFSSL_QT)
  43658. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  43659. #else
  43660. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  43661. #endif
  43662. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  43663. #ifdef BIG_ENDIAN_ORDER
  43664. actual = data[1];
  43665. #else
  43666. actual = data[0];
  43667. #endif
  43668. AssertIntEQ(actual, expected);
  43669. wolfSSL_ASN1_STRING_free(asn1str);
  43670. #if 1
  43671. i++;
  43672. /* Authority Info Access */
  43673. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43674. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43675. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  43676. AssertNotNull(aia =
  43677. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  43678. #if defined(WOLFSSL_QT)
  43679. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43680. #else
  43681. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43682. #endif
  43683. /* URI entry is an ACCESS_DESCRIPTION type */
  43684. #if defined(WOLFSSL_QT)
  43685. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  43686. #else
  43687. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  43688. #endif
  43689. AssertNotNull(adObj = ad->method);
  43690. /* Make sure nid is OCSP */
  43691. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  43692. /* GENERAL_NAME stores URI as an ASN1_STRING */
  43693. AssertNotNull(gn = ad->location);
  43694. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  43695. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  43696. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  43697. #if defined(WOLFSSL_QT)
  43698. str = (char*)ASN1_STRING_get0_data(asn1str);
  43699. #else
  43700. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  43701. #endif
  43702. actual = strcmp(str, "http://127.0.0.1:22220");
  43703. AssertIntEQ(actual, 0);
  43704. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  43705. #else
  43706. (void) aia; (void) ad; (void) adObj; (void) gn;
  43707. #endif
  43708. wolfSSL_X509_free(x509);
  43709. res = TEST_RES_CHECK(1);
  43710. #endif
  43711. return res;
  43712. }
  43713. static int test_wolfSSL_X509_get_extension_flags(void)
  43714. {
  43715. int res = TEST_SKIPPED;
  43716. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43717. XFILE f;
  43718. X509* x509;
  43719. unsigned int extFlags;
  43720. unsigned int keyUsageFlags;
  43721. unsigned int extKeyUsageFlags;
  43722. /* client-int-cert.pem has the following extension flags. */
  43723. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  43724. /* and the following key usage flags. */
  43725. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43726. | KU_NON_REPUDIATION
  43727. | KU_KEY_ENCIPHERMENT;
  43728. /* and the following extended key usage flags. */
  43729. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  43730. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  43731. AssertTrue(f != XBADFILE);
  43732. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43733. XFCLOSE(f);
  43734. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  43735. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43736. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  43737. X509_free(x509);
  43738. /* client-cert-ext.pem has the following extension flags. */
  43739. extFlags = EXFLAG_KUSAGE;
  43740. /* and the following key usage flags. */
  43741. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43742. | KU_KEY_CERT_SIGN
  43743. | KU_CRL_SIGN;
  43744. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  43745. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43746. XFCLOSE(f);
  43747. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  43748. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43749. X509_free(x509);
  43750. res = TEST_RES_CHECK(1);
  43751. #endif /* OPENSSL_ALL */
  43752. return res;
  43753. }
  43754. static int test_wolfSSL_X509_get_ext(void)
  43755. {
  43756. int res = TEST_SKIPPED;
  43757. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43758. int ret = 0;
  43759. FILE* f;
  43760. WOLFSSL_X509* x509;
  43761. WOLFSSL_X509_EXTENSION* foundExtension;
  43762. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  43763. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43764. fclose(f);
  43765. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  43766. /* wolfSSL_X509_get_ext() valid input */
  43767. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  43768. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  43769. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  43770. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  43771. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  43772. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  43773. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  43774. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  43775. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  43776. wolfSSL_X509_free(x509);
  43777. res = TEST_RES_CHECK(1);
  43778. #endif
  43779. return res;
  43780. }
  43781. static int test_wolfSSL_X509_get_ext_by_NID(void)
  43782. {
  43783. int res = TEST_SKIPPED;
  43784. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43785. int rc;
  43786. FILE* f;
  43787. WOLFSSL_X509* x509;
  43788. ASN1_OBJECT* obj = NULL;
  43789. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  43790. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43791. fclose(f);
  43792. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  43793. AssertIntGE(rc, 0);
  43794. /* Start search from last location (should fail) */
  43795. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  43796. AssertIntGE(rc, -1);
  43797. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  43798. AssertIntGE(rc, -1);
  43799. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  43800. AssertIntEQ(rc, -1);
  43801. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  43802. AssertIntEQ(rc, -1);
  43803. /* NID_ext_key_usage, check also its nid and oid */
  43804. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  43805. AssertIntGT(rc, -1);
  43806. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  43807. AssertIntEQ(obj->nid, NID_ext_key_usage);
  43808. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  43809. wolfSSL_X509_free(x509);
  43810. res = TEST_RES_CHECK(1);
  43811. #endif
  43812. return res;
  43813. }
  43814. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  43815. {
  43816. int res = TEST_SKIPPED;
  43817. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43818. int rc;
  43819. XFILE f;
  43820. WOLFSSL_X509* x509;
  43821. WOLFSSL_X509_EXTENSION* ext;
  43822. WOLFSSL_ASN1_STRING* sanString;
  43823. byte* sanDer;
  43824. const byte expectedDer[] = {
  43825. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  43826. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  43827. f = XFOPEN("./certs/server-cert.pem", "rb");
  43828. AssertTrue(f != XBADFILE);
  43829. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43830. fclose(f);
  43831. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  43832. AssertIntNE(rc, -1);
  43833. AssertNotNull(ext = X509_get_ext(x509, rc));
  43834. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  43835. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  43836. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  43837. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  43838. X509_free(x509);
  43839. res = TEST_RES_CHECK(1);
  43840. #endif
  43841. return res;
  43842. }
  43843. static int test_wolfSSL_X509_EXTENSION_new(void)
  43844. {
  43845. int res = TEST_SKIPPED;
  43846. #if defined (OPENSSL_ALL)
  43847. WOLFSSL_X509_EXTENSION* ext;
  43848. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  43849. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  43850. ext->obj->nid = WOLFSSL_SUCCESS;
  43851. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  43852. wolfSSL_X509_EXTENSION_free(ext);
  43853. res = TEST_RES_CHECK(1);
  43854. #endif
  43855. return res;
  43856. }
  43857. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  43858. {
  43859. int res = TEST_SKIPPED;
  43860. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43861. WOLFSSL_X509* x509;
  43862. WOLFSSL_X509_EXTENSION* ext;
  43863. WOLFSSL_ASN1_OBJECT* o;
  43864. FILE* file;
  43865. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  43866. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43867. fclose(file);
  43868. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  43869. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43870. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  43871. AssertIntEQ(o->nid, 128);
  43872. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  43873. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  43874. wolfSSL_X509_free(x509);
  43875. res = TEST_RES_CHECK(1);
  43876. #endif
  43877. return res;
  43878. }
  43879. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  43880. {
  43881. int res = TEST_SKIPPED;
  43882. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43883. WOLFSSL_X509* x509;
  43884. WOLFSSL_X509_EXTENSION* ext;
  43885. WOLFSSL_ASN1_STRING* str;
  43886. FILE* file;
  43887. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  43888. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43889. fclose(file);
  43890. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43891. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  43892. wolfSSL_X509_free(x509);
  43893. res = TEST_RES_CHECK(1);
  43894. #endif
  43895. return res;
  43896. }
  43897. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  43898. {
  43899. int res = TEST_SKIPPED;
  43900. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43901. WOLFSSL_X509* x509;
  43902. WOLFSSL_X509_EXTENSION* ext;
  43903. FILE* file;
  43904. int crit;
  43905. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  43906. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43907. fclose(file);
  43908. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43909. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  43910. AssertIntEQ(crit, 0);
  43911. wolfSSL_X509_free(x509);
  43912. res = TEST_RES_CHECK(1);
  43913. #endif
  43914. return res;
  43915. }
  43916. static int test_wolfSSL_X509V3_EXT_print(void)
  43917. {
  43918. int res = TEST_SKIPPED;
  43919. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  43920. !defined(NO_RSA)
  43921. {
  43922. FILE* f;
  43923. WOLFSSL_X509* x509;
  43924. X509_EXTENSION * ext = NULL;
  43925. int loc;
  43926. BIO *bio = NULL;
  43927. AssertNotNull(f = fopen(svrCertFile, "rb"));
  43928. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43929. fclose(f);
  43930. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  43931. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  43932. AssertIntGT(loc, -1);
  43933. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43934. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43935. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  43936. AssertIntGT(loc, -1);
  43937. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43938. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43939. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  43940. AssertIntGT(loc, -1);
  43941. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43942. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43943. wolfSSL_BIO_free(bio);
  43944. wolfSSL_X509_free(x509);
  43945. }
  43946. {
  43947. X509 *x509;
  43948. BIO *bio;
  43949. X509_EXTENSION *ext;
  43950. unsigned int i;
  43951. unsigned int idx;
  43952. /* Some NIDs to test with */
  43953. int nids[] = {
  43954. /* NID_key_usage, currently X509_get_ext returns this as a bit
  43955. * string, which messes up X509V3_EXT_print */
  43956. /* NID_ext_key_usage, */
  43957. NID_subject_alt_name,
  43958. };
  43959. int* n;
  43960. AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  43961. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  43962. WOLFSSL_FILETYPE_PEM));
  43963. fprintf(stderr, "\nPrinting extension values:\n");
  43964. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  43965. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  43966. * update the index */
  43967. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  43968. AssertNotNull(ext = X509_get_ext(x509, idx));
  43969. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  43970. fprintf(stderr, "\n");
  43971. }
  43972. BIO_free(bio);
  43973. X509_free(x509);
  43974. }
  43975. res = TEST_RES_CHECK(1);
  43976. #endif
  43977. return res;
  43978. }
  43979. static int test_wolfSSL_X509_cmp(void)
  43980. {
  43981. int res = TEST_SKIPPED;
  43982. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43983. FILE* file1;
  43984. FILE* file2;
  43985. WOLFSSL_X509* cert1;
  43986. WOLFSSL_X509* cert2;
  43987. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  43988. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  43989. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  43990. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  43991. fclose(file1);
  43992. fclose(file2);
  43993. /* wolfSSL_X509_cmp() testing matching certs */
  43994. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  43995. /* wolfSSL_X509_cmp() testing mismatched certs */
  43996. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  43997. /* wolfSSL_X509_cmp() testing NULL, valid args */
  43998. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  43999. /* wolfSSL_X509_cmp() testing valid, NULL args */
  44000. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  44001. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  44002. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  44003. wolfSSL_X509_free(cert1);
  44004. wolfSSL_X509_free(cert2);
  44005. res = TEST_RES_CHECK(1);
  44006. #endif
  44007. return res;
  44008. }
  44009. static int test_wolfSSL_PKEY_up_ref(void)
  44010. {
  44011. int res = TEST_SKIPPED;
  44012. #if defined(OPENSSL_ALL)
  44013. EVP_PKEY* pkey;
  44014. pkey = EVP_PKEY_new();
  44015. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  44016. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44017. EVP_PKEY_free(pkey);
  44018. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44019. EVP_PKEY_free(pkey);
  44020. EVP_PKEY_free(pkey);
  44021. res = TEST_RES_CHECK(1);
  44022. #endif
  44023. return res;
  44024. }
  44025. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  44026. {
  44027. int res = TEST_SKIPPED;
  44028. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44029. EVP_PKEY* pkey;
  44030. const unsigned char* p;
  44031. unsigned char *der = NULL, *tmp = NULL;
  44032. int derLen;
  44033. p = client_keypub_der_2048;
  44034. /* Check that key can be successfully decoded. */
  44035. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  44036. sizeof_client_keypub_der_2048));
  44037. /* Check that key can be successfully encoded. */
  44038. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44039. /* Ensure that the encoded version matches the original. */
  44040. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  44041. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44042. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44043. * is advanced. */
  44044. tmp = der;
  44045. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44046. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  44047. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44048. AssertTrue(der + derLen == tmp);
  44049. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44050. EVP_PKEY_free(pkey);
  44051. res = TEST_RES_CHECK(1);
  44052. #endif
  44053. return res;
  44054. }
  44055. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  44056. {
  44057. int res = TEST_SKIPPED;
  44058. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  44059. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44060. EVP_PKEY* pkey;
  44061. const unsigned char* p;
  44062. unsigned char *der = NULL, *tmp = NULL;
  44063. int derLen;
  44064. unsigned char pub_buf[65];
  44065. const int pub_len = 65;
  44066. BN_CTX * ctx;
  44067. EC_GROUP * curve;
  44068. EC_KEY * ephemeral_key;
  44069. const EC_POINT * h;
  44070. /* Generate an x963 key pair and get public part into pub_buf */
  44071. AssertNotNull(ctx = BN_CTX_new());
  44072. AssertNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  44073. AssertNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  44074. NID_X9_62_prime256v1));
  44075. AssertIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  44076. AssertNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  44077. AssertIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  44078. POINT_CONVERSION_UNCOMPRESSED,
  44079. pub_buf, pub_len, ctx));
  44080. /* Prepare the EVP_PKEY */
  44081. AssertNotNull(pkey = EVP_PKEY_new());
  44082. p = pub_buf;
  44083. /* Check that key can be successfully decoded. */
  44084. AssertNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  44085. pub_len));
  44086. /* Check that key can be successfully encoded. */
  44087. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44088. /* Ensure that the encoded version matches the original. */
  44089. AssertIntEQ(derLen, pub_len);
  44090. AssertIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44091. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44092. * is advanced. */
  44093. tmp = der;
  44094. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44095. AssertIntEQ(derLen, pub_len);
  44096. AssertIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44097. AssertTrue(der + derLen == tmp);
  44098. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44099. EVP_PKEY_free(pkey);
  44100. EC_KEY_free(ephemeral_key);
  44101. EC_GROUP_free(curve);
  44102. res = TEST_RES_CHECK(1);
  44103. #endif
  44104. return res;
  44105. }
  44106. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  44107. {
  44108. int res = TEST_SKIPPED;
  44109. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  44110. DSA* dsa;
  44111. char file[] = "./certs/dsaparams.der";
  44112. XFILE f;
  44113. int derInLen;
  44114. byte* derIn;
  44115. int derOutLen;
  44116. byte* derOut = NULL;
  44117. f = XFOPEN(file, "rb");
  44118. AssertTrue(f != XBADFILE);
  44119. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  44120. derInLen = (int)XFTELL(f);
  44121. AssertTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  44122. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  44123. DYNAMIC_TYPE_TMP_BUFFER));
  44124. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  44125. XFCLOSE(f);
  44126. /* Check that params can be successfully decoded. */
  44127. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  44128. /* Check that params can be successfully encoded. */
  44129. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  44130. /* Ensure that the encoded version matches the original. */
  44131. AssertIntEQ(derInLen, derOutLen);
  44132. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  44133. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44134. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44135. DSA_free(dsa);
  44136. res = TEST_RES_CHECK(1);
  44137. #endif
  44138. return res;
  44139. }
  44140. static int test_wolfSSL_i2d_PrivateKey(void)
  44141. {
  44142. int res = TEST_SKIPPED;
  44143. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  44144. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44145. {
  44146. EVP_PKEY* pkey;
  44147. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  44148. unsigned char buf[FOURK_BUF];
  44149. unsigned char* pt = NULL;
  44150. int bufSz;
  44151. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  44152. (long)sizeof_server_key_der_2048));
  44153. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  44154. pt = buf;
  44155. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  44156. AssertIntNE((pt - buf), 0);
  44157. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  44158. EVP_PKEY_free(pkey);
  44159. }
  44160. #endif
  44161. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44162. {
  44163. EVP_PKEY* pkey;
  44164. const unsigned char* client_key =
  44165. (const unsigned char*)ecc_clikey_der_256;
  44166. unsigned char buf[FOURK_BUF];
  44167. unsigned char* pt = NULL;
  44168. int bufSz;
  44169. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  44170. sizeof_ecc_clikey_der_256)));
  44171. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  44172. pt = buf;
  44173. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  44174. AssertIntNE((pt - buf), 0);
  44175. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  44176. EVP_PKEY_free(pkey);
  44177. }
  44178. #endif
  44179. res = TEST_RES_CHECK(1);
  44180. #endif
  44181. return res;
  44182. }
  44183. static int test_wolfSSL_OCSP_id_get0_info(void)
  44184. {
  44185. int res = TEST_SKIPPED;
  44186. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  44187. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44188. X509* cert;
  44189. X509* issuer;
  44190. OCSP_CERTID* id;
  44191. OCSP_CERTID* id2;
  44192. ASN1_STRING* name = NULL;
  44193. ASN1_OBJECT* pmd = NULL;
  44194. ASN1_STRING* keyHash = NULL;
  44195. ASN1_INTEGER* serial = NULL;
  44196. ASN1_INTEGER* x509Int;
  44197. AssertNotNull(cert =
  44198. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  44199. AssertNotNull(issuer =
  44200. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  44201. id = OCSP_cert_to_id(NULL, cert, issuer);
  44202. AssertNotNull(id);
  44203. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  44204. AssertNotNull(id2);
  44205. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  44206. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  44207. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  44208. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  44209. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  44210. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  44211. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  44212. AssertNotNull(serial);
  44213. /* compare serial number to one in cert, should be equal */
  44214. x509Int = X509_get_serialNumber(cert);
  44215. AssertNotNull(x509Int);
  44216. AssertIntEQ(x509Int->length, serial->length);
  44217. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  44218. /* test OCSP_id_cmp */
  44219. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  44220. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  44221. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  44222. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  44223. id->issuerHash[0] = ~id->issuerHash[0];
  44224. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  44225. OCSP_CERTID_free(id);
  44226. OCSP_CERTID_free(id2);
  44227. X509_free(cert); /* free's x509Int */
  44228. X509_free(issuer);
  44229. res = TEST_RES_CHECK(1);
  44230. #endif
  44231. return res;
  44232. }
  44233. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  44234. {
  44235. int res = TEST_SKIPPED;
  44236. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44237. WOLFSSL_OCSP_CERTID certId;
  44238. byte* targetBuffer;
  44239. byte* beginTargetBuffer;
  44240. /* OCSP CertID bytes taken from PCAP */
  44241. byte rawCertId[] = {
  44242. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44243. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44244. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44245. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44246. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44247. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44248. 0xcf, 0xbc, 0x91
  44249. };
  44250. int ret, i;
  44251. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  44252. certId.rawCertId = rawCertId;
  44253. certId.rawCertIdSize = sizeof(rawCertId);
  44254. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44255. beginTargetBuffer = targetBuffer;
  44256. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  44257. /* If target buffer is not null, function increments targetBuffer to point
  44258. just past the end of the encoded data. */
  44259. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  44260. /* Function returns the size of the encoded data. */
  44261. AssertIntEQ(ret, sizeof(rawCertId));
  44262. for (i = 0; i < ret; ++i)
  44263. {
  44264. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  44265. }
  44266. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44267. targetBuffer = NULL;
  44268. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  44269. /* If target buffer is null, function allocates memory for a buffer and
  44270. copies the encoded data into it. targetBuffer then points to the start of
  44271. this newly allocate buffer. */
  44272. AssertIntEQ(ret, sizeof(rawCertId));
  44273. for (i = 0; i < ret; ++i)
  44274. {
  44275. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  44276. }
  44277. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  44278. res = TEST_RES_CHECK(1);
  44279. #endif
  44280. return res;
  44281. }
  44282. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  44283. {
  44284. int res = TEST_SKIPPED;
  44285. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44286. WOLFSSL_OCSP_CERTID* certId;
  44287. WOLFSSL_OCSP_CERTID* certIdBad;
  44288. const unsigned char* rawCertIdPtr;
  44289. const unsigned char rawCertId[] = {
  44290. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44291. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44292. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44293. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44294. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44295. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44296. 0xcf, 0xbc, 0x91
  44297. };
  44298. rawCertIdPtr = &rawCertId[0];
  44299. /* If the cert ID is NULL the function should allocate it and copy the
  44300. * data to it. */
  44301. certId = NULL;
  44302. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  44303. AssertNotNull(certId);
  44304. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  44305. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  44306. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  44307. /* If the cert ID is not NULL the function will just copy the data to it. */
  44308. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  44309. DYNAMIC_TYPE_TMP_BUFFER);
  44310. AssertNotNull(certId);
  44311. XMEMSET(certId, 0, sizeof(*certId));
  44312. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  44313. rawCertIdPtr = &rawCertId[0];
  44314. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  44315. AssertNotNull(certId);
  44316. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  44317. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  44318. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44319. /* The below tests should fail when passed bad parameters. NULL should
  44320. * always be returned. */
  44321. certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr, sizeof(rawCertId));
  44322. AssertNull(certIdBad);
  44323. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL, sizeof(rawCertId));
  44324. AssertNull(certIdBad);
  44325. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0);
  44326. AssertNull(certIdBad);
  44327. res = TEST_RES_CHECK(1);
  44328. #endif
  44329. return res;
  44330. }
  44331. static int test_wolfSSL_OCSP_id_cmp(void)
  44332. {
  44333. int res = TEST_SKIPPED;
  44334. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44335. OCSP_CERTID id1;
  44336. OCSP_CERTID id2;
  44337. XMEMSET(&id1, 0, sizeof(id1));
  44338. XMEMSET(&id2, 0, sizeof(id2));
  44339. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  44340. res = TEST_RES_CHECK(1);
  44341. #endif
  44342. return res;
  44343. }
  44344. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  44345. {
  44346. int res = TEST_SKIPPED;
  44347. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44348. WOLFSSL_OCSP_SINGLERESP single;
  44349. const WOLFSSL_OCSP_CERTID* certId;
  44350. XMEMSET(&single, 0, sizeof(single));
  44351. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  44352. AssertPtrEq(&single, certId);
  44353. res = TEST_RES_CHECK(1);
  44354. #endif
  44355. return res;
  44356. }
  44357. static int test_wolfSSL_OCSP_single_get0_status(void)
  44358. {
  44359. int res = TEST_SKIPPED;
  44360. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44361. WOLFSSL_OCSP_SINGLERESP single;
  44362. CertStatus certStatus;
  44363. WOLFSSL_ASN1_TIME* thisDate;
  44364. WOLFSSL_ASN1_TIME* nextDate;
  44365. int ret, i;
  44366. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44367. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  44368. /* Fill the date fields with some dummy data. */
  44369. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  44370. certStatus.thisDateParsed.data[i] = i;
  44371. certStatus.nextDateParsed.data[i] = i;
  44372. }
  44373. certStatus.status = CERT_GOOD;
  44374. single.status = &certStatus;
  44375. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  44376. &nextDate);
  44377. AssertIntEQ(ret, CERT_GOOD);
  44378. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  44379. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  44380. res = TEST_RES_CHECK(1);
  44381. #endif
  44382. return res;
  44383. }
  44384. static int test_wolfSSL_OCSP_resp_count(void)
  44385. {
  44386. int res = TEST_SKIPPED;
  44387. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44388. WOLFSSL_OCSP_BASICRESP basicResp;
  44389. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  44390. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  44391. int count;
  44392. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  44393. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44394. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44395. count = wolfSSL_OCSP_resp_count(&basicResp);
  44396. AssertIntEQ(count, 0);
  44397. basicResp.single = &singleRespOne;
  44398. count = wolfSSL_OCSP_resp_count(&basicResp);
  44399. AssertIntEQ(count, 1);
  44400. singleRespOne.next = &singleRespTwo;
  44401. count = wolfSSL_OCSP_resp_count(&basicResp);
  44402. AssertIntEQ(count, 2);
  44403. res = TEST_RES_CHECK(1);
  44404. #endif
  44405. return res;
  44406. }
  44407. static int test_wolfSSL_OCSP_resp_get0(void)
  44408. {
  44409. int res = TEST_SKIPPED;
  44410. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44411. WOLFSSL_OCSP_BASICRESP basicResp;
  44412. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  44413. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  44414. WOLFSSL_OCSP_SINGLERESP* ret;
  44415. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  44416. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44417. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44418. basicResp.single = &singleRespOne;
  44419. singleRespOne.next = &singleRespTwo;
  44420. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  44421. AssertPtrEq(ret, &singleRespOne);
  44422. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  44423. AssertPtrEq(ret, &singleRespTwo);
  44424. res = TEST_RES_CHECK(1);
  44425. #endif
  44426. return res;
  44427. }
  44428. static int test_wolfSSL_EVP_PKEY_derive(void)
  44429. {
  44430. int res = TEST_SKIPPED;
  44431. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  44432. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  44433. EVP_PKEY_CTX *ctx;
  44434. unsigned char *skey;
  44435. size_t skeylen;
  44436. EVP_PKEY *pkey, *peerkey;
  44437. const unsigned char* key;
  44438. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  44439. /* DH */
  44440. key = dh_key_der_2048;
  44441. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  44442. sizeof_dh_key_der_2048)));
  44443. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  44444. key = dh_key_der_2048;
  44445. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  44446. sizeof_dh_key_der_2048)));
  44447. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  44448. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44449. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  44450. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  44451. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  44452. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  44453. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  44454. EVP_PKEY_CTX_free(ctx);
  44455. EVP_PKEY_free(peerkey);
  44456. EVP_PKEY_free(pkey);
  44457. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  44458. #endif
  44459. #ifdef HAVE_ECC
  44460. /* ECDH */
  44461. key = ecc_clikey_der_256;
  44462. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  44463. sizeof_ecc_clikey_der_256)));
  44464. key = ecc_clikeypub_der_256;
  44465. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  44466. sizeof_ecc_clikeypub_der_256)));
  44467. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44468. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  44469. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  44470. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  44471. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  44472. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  44473. EVP_PKEY_CTX_free(ctx);
  44474. EVP_PKEY_free(peerkey);
  44475. EVP_PKEY_free(pkey);
  44476. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  44477. #endif /* HAVE_ECC */
  44478. res = TEST_RES_CHECK(1);
  44479. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  44480. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  44481. return res;
  44482. }
  44483. static int test_wolfSSL_EVP_PBE_scrypt(void)
  44484. {
  44485. int res = TEST_SKIPPED;
  44486. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  44487. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  44488. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  44489. int ret;
  44490. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  44491. int pwdlen = sizeof(pwd);
  44492. const byte salt[] = {'N','a','C','l'};
  44493. int saltlen = sizeof(salt);
  44494. byte key[80];
  44495. word64 numOvr32 = (word64)INT32_MAX + 1;
  44496. /* expected derived key for N:16, r:1, p:1 */
  44497. const byte expectedKey[] = {
  44498. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  44499. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  44500. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  44501. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  44502. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  44503. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  44504. 0xE7, 0xE9, 0xC0, 0x9A};
  44505. /* N r p mx key keylen */
  44506. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  44507. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  44508. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  44509. AssertIntEQ(ret, 0); /* N must be power of 2 */
  44510. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  44511. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  44512. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  44513. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  44514. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  44515. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  44516. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  44517. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  44518. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  44519. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  44520. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  44521. AssertIntEQ(ret, 1); /* should succeed */
  44522. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  44523. key, 64);
  44524. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  44525. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  44526. key, 64);
  44527. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  44528. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  44529. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  44530. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  44531. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  44532. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  44533. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  44534. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  44535. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  44536. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  44537. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  44538. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  44539. AssertIntEQ(ret, 1);
  44540. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  44541. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  44542. res = TEST_RES_CHECK(1);
  44543. #endif /* !NO_PWDBASED && !NO_SHA256 */
  44544. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  44545. return res;
  44546. }
  44547. static int test_no_op_functions(void)
  44548. {
  44549. int res = TEST_SKIPPED;
  44550. #if defined(OPENSSL_EXTRA)
  44551. /* this makes sure wolfSSL can compile and run these no-op functions */
  44552. SSL_load_error_strings();
  44553. ENGINE_load_builtin_engines();
  44554. OpenSSL_add_all_ciphers();
  44555. AssertIntEQ(CRYPTO_malloc_init(), 0);
  44556. res = TEST_RES_CHECK(1);
  44557. #endif
  44558. return res;
  44559. }
  44560. static int test_wolfSSL_CRYPTO_memcmp(void)
  44561. {
  44562. int res = TEST_SKIPPED;
  44563. #ifdef OPENSSL_EXTRA
  44564. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44565. "implementation of TLS/SSL for embedded devices to the cloud.";
  44566. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44567. "implementation of TLS/SSL for embedded devices to the cloud.";
  44568. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44569. "implementation of TLS/SSL for embedded devices to the cloud!";
  44570. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  44571. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  44572. res = TEST_RES_CHECK(1);
  44573. #endif
  44574. return res;
  44575. }
  44576. /*----------------------------------------------------------------------------*
  44577. | wolfCrypt ASN
  44578. *----------------------------------------------------------------------------*/
  44579. static int test_wc_CreateEncryptedPKCS8Key(void)
  44580. {
  44581. int res = TEST_SKIPPED;
  44582. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  44583. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  44584. WC_RNG rng;
  44585. byte* encKey = NULL;
  44586. word32 encKeySz = 0;
  44587. word32 decKeySz = 0;
  44588. const char password[] = "Lorem ipsum dolor sit amet";
  44589. word32 passwordSz = (word32)XSTRLEN(password);
  44590. word32 tradIdx = 0;
  44591. AssertIntEQ(wc_InitRng(&rng), 0);
  44592. /* Call with NULL for out buffer to get necessary length. */
  44593. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44594. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  44595. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44596. LENGTH_ONLY_E);
  44597. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  44598. DYNAMIC_TYPE_TMP_BUFFER));
  44599. /* Call with the allocated out buffer. */
  44600. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44601. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  44602. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44603. 0);
  44604. /* Decrypt the encrypted PKCS8 key we just made. */
  44605. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  44606. passwordSz)), 0);
  44607. /* encKey now holds the decrypted key (decrypted in place). */
  44608. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  44609. /* Check that the decrypted key matches the key prior to encryption. */
  44610. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  44611. sizeof_server_key_der_2048), 0);
  44612. if (encKey != NULL)
  44613. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44614. wc_FreeRng(&rng);
  44615. res = TEST_RES_CHECK(1);
  44616. #endif
  44617. return res;
  44618. }
  44619. static int test_wc_GetPkcs8TraditionalOffset(void)
  44620. {
  44621. int res = TEST_SKIPPED;
  44622. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  44623. int length, derSz;
  44624. word32 inOutIdx;
  44625. const char* path = "./certs/server-keyPkcs8.der";
  44626. XFILE file;
  44627. byte der[2048];
  44628. file = XFOPEN(path, "rb");
  44629. AssertTrue(file != XBADFILE);
  44630. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  44631. XFCLOSE(file);
  44632. /* valid case */
  44633. inOutIdx = 0;
  44634. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  44635. AssertIntGT(length, 0);
  44636. /* inOutIdx > sz */
  44637. inOutIdx = 4000;
  44638. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  44639. AssertIntEQ(length, BAD_FUNC_ARG);
  44640. /* null input */
  44641. inOutIdx = 0;
  44642. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  44643. AssertIntEQ(length, BAD_FUNC_ARG);
  44644. /* invalid input, fill buffer with 1's */
  44645. XMEMSET(der, 1, sizeof(der));
  44646. inOutIdx = 0;
  44647. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  44648. AssertIntEQ(length, ASN_PARSE_E);
  44649. res = TEST_RES_CHECK(1);
  44650. #endif /* NO_ASN */
  44651. return res;
  44652. }
  44653. static int test_wc_SetSubjectRaw(void)
  44654. {
  44655. int res = TEST_SKIPPED;
  44656. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44657. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44658. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44659. WOLFSSL_X509* x509;
  44660. int peerCertSz;
  44661. const byte* peerCertBuf;
  44662. Cert forgedCert;
  44663. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  44664. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44665. AssertIntEQ(0, wc_InitCert(&forgedCert));
  44666. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  44667. wolfSSL_FreeX509(x509);
  44668. res = TEST_RES_CHECK(1);
  44669. #endif
  44670. return res;
  44671. }
  44672. static int test_wc_GetSubjectRaw(void)
  44673. {
  44674. int res = TEST_SKIPPED;
  44675. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44676. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  44677. Cert cert;
  44678. byte *subjectRaw;
  44679. AssertIntEQ(0, wc_InitCert(&cert));
  44680. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  44681. res = TEST_RES_CHECK(1);
  44682. #endif
  44683. return res;
  44684. }
  44685. static int test_wc_SetIssuerRaw(void)
  44686. {
  44687. int res = TEST_SKIPPED;
  44688. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44689. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44690. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44691. WOLFSSL_X509* x509;
  44692. int peerCertSz;
  44693. const byte* peerCertBuf;
  44694. Cert forgedCert;
  44695. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  44696. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44697. AssertIntEQ(0, wc_InitCert(&forgedCert));
  44698. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  44699. wolfSSL_FreeX509(x509);
  44700. res = TEST_RES_CHECK(1);
  44701. #endif
  44702. return res;
  44703. }
  44704. static int test_wc_SetIssueBuffer(void)
  44705. {
  44706. int res = TEST_SKIPPED;
  44707. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44708. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44709. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44710. WOLFSSL_X509* x509;
  44711. int peerCertSz;
  44712. const byte* peerCertBuf;
  44713. Cert forgedCert;
  44714. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  44715. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44716. AssertIntEQ(0, wc_InitCert(&forgedCert));
  44717. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  44718. wolfSSL_FreeX509(x509);
  44719. res = TEST_RES_CHECK(1);
  44720. #endif
  44721. return res;
  44722. }
  44723. /*
  44724. * Testing wc_SetSubjectKeyId
  44725. */
  44726. static int test_wc_SetSubjectKeyId(void)
  44727. {
  44728. int res = TEST_SKIPPED;
  44729. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44730. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44731. Cert cert;
  44732. const char* file = "certs/ecc-client-keyPub.pem";
  44733. AssertIntEQ(0, wc_InitCert(&cert));
  44734. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  44735. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  44736. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  44737. res = TEST_RES_CHECK(1);
  44738. #endif
  44739. return res;
  44740. } /* END test_wc_SetSubjectKeyId */
  44741. /*
  44742. * Testing wc_SetSubject
  44743. */
  44744. static int test_wc_SetSubject(void)
  44745. {
  44746. int res = TEST_SKIPPED;
  44747. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44748. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44749. Cert cert;
  44750. const char* file = "./certs/ca-ecc-cert.pem";
  44751. AssertIntEQ(0, wc_InitCert(&cert));
  44752. AssertIntEQ(0, wc_SetSubject(&cert, file));
  44753. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  44754. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  44755. res = TEST_RES_CHECK(1);
  44756. #endif
  44757. return res;
  44758. } /* END test_wc_SetSubject */
  44759. static int test_CheckCertSignature(void)
  44760. {
  44761. int res = TEST_SKIPPED;
  44762. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  44763. WOLFSSL_CERT_MANAGER* cm = NULL;
  44764. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44765. FILE* fp;
  44766. byte cert[4096];
  44767. int certSz;
  44768. #endif
  44769. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  44770. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44771. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  44772. #ifndef NO_RSA
  44773. #ifdef USE_CERT_BUFFERS_1024
  44774. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  44775. sizeof_server_cert_der_1024, NULL, cm));
  44776. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44777. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  44778. WOLFSSL_FILETYPE_ASN1));
  44779. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  44780. sizeof_server_cert_der_1024, NULL, cm));
  44781. #elif defined(USE_CERT_BUFFERS_2048)
  44782. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  44783. sizeof_server_cert_der_2048, NULL, cm));
  44784. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44785. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  44786. WOLFSSL_FILETYPE_ASN1));
  44787. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  44788. sizeof_server_cert_der_2048, NULL, cm));
  44789. #endif
  44790. #endif
  44791. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44792. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  44793. sizeof_serv_ecc_der_256, NULL, cm));
  44794. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44795. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  44796. WOLFSSL_FILETYPE_ASN1));
  44797. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  44798. NULL, cm));
  44799. #endif
  44800. #if !defined(NO_FILESYSTEM)
  44801. wolfSSL_CertManagerFree(cm);
  44802. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44803. #ifndef NO_RSA
  44804. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  44805. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44806. XFCLOSE(fp);
  44807. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44808. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44809. "./certs/ca-cert.pem", NULL));
  44810. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44811. #endif
  44812. #ifdef HAVE_ECC
  44813. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  44814. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44815. XFCLOSE(fp);
  44816. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44817. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44818. "./certs/ca-ecc-cert.pem", NULL));
  44819. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44820. #endif
  44821. #endif
  44822. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44823. (void)fp;
  44824. (void)cert;
  44825. (void)certSz;
  44826. #endif
  44827. wolfSSL_CertManagerFree(cm);
  44828. res = TEST_RES_CHECK(1);
  44829. #endif
  44830. return res;
  44831. }
  44832. static int test_wc_ParseCert(void)
  44833. {
  44834. int res = TEST_SKIPPED;
  44835. #if !defined(NO_CERTS) && !defined(NO_RSA)
  44836. DecodedCert decodedCert;
  44837. const byte* rawCert = client_cert_der_2048;
  44838. const int rawCertSize = sizeof_client_cert_der_2048;
  44839. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  44840. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44841. #ifndef IGNORE_NAME_CONSTRAINTS
  44842. /* check that the subjects emailAddress was not put in the alt name list */
  44843. AssertNotNull(decodedCert.subjectEmail);
  44844. AssertNull(decodedCert.altEmailNames);
  44845. #endif
  44846. wc_FreeDecodedCert(&decodedCert);
  44847. res = TEST_RES_CHECK(1);
  44848. #endif
  44849. return res;
  44850. }
  44851. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  44852. * the API safely errors out on badly-formed ASN input.
  44853. * NOTE: Test not compatible with released FIPS implementations!
  44854. */
  44855. static int test_wc_ParseCert_Error(void)
  44856. {
  44857. int res = TEST_SKIPPED;
  44858. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  44859. (!defined(HAVE_FIPS) || \
  44860. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  44861. DecodedCert decodedCert;
  44862. int i;
  44863. /* Certificate data */
  44864. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  44865. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44866. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44867. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  44868. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  44869. /* Test data */
  44870. const struct testStruct {
  44871. const byte* c;
  44872. const int cSz;
  44873. const int expRet;
  44874. } t[] = {
  44875. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  44876. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  44877. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  44878. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  44879. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  44880. };
  44881. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  44882. for (i = 0; i < tSz; i++) {
  44883. WOLFSSL_MSG_EX("i == %d", i);
  44884. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  44885. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  44886. wc_FreeDecodedCert(&decodedCert);
  44887. }
  44888. res = TEST_RES_CHECK(1);
  44889. #endif
  44890. return res;
  44891. }
  44892. static int test_MakeCertWithPathLen(void)
  44893. {
  44894. int res = TEST_SKIPPED;
  44895. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  44896. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  44897. const byte expectedPathLen = 7;
  44898. Cert cert;
  44899. DecodedCert decodedCert;
  44900. byte der[FOURK_BUF];
  44901. int derSize = 0;
  44902. WC_RNG rng;
  44903. ecc_key key;
  44904. AssertIntEQ(wc_InitRng(&rng), 0);
  44905. AssertIntEQ(wc_ecc_init(&key), 0);
  44906. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  44907. AssertIntEQ(wc_InitCert(&cert), 0);
  44908. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  44909. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  44910. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  44911. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  44912. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  44913. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  44914. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  44915. cert.selfSigned = 1;
  44916. cert.isCA = 1;
  44917. cert.pathLen = expectedPathLen;
  44918. cert.pathLenSet = 1;
  44919. cert.sigType = CTC_SHA256wECDSA;
  44920. #ifdef WOLFSSL_CERT_EXT
  44921. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  44922. #endif
  44923. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  44924. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  44925. &key, &rng);
  44926. AssertIntGE(derSize, 0);
  44927. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  44928. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44929. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  44930. wc_FreeDecodedCert(&decodedCert);
  44931. AssertIntEQ(wc_ecc_free(&key), 0);
  44932. AssertIntEQ(wc_FreeRng(&rng), 0);
  44933. res = TEST_RES_CHECK(1);
  44934. #endif
  44935. return res;
  44936. }
  44937. /*----------------------------------------------------------------------------*
  44938. | wolfCrypt ECC
  44939. *----------------------------------------------------------------------------*/
  44940. static int test_wc_ecc_get_curve_size_from_name(void)
  44941. {
  44942. int res = TEST_SKIPPED;
  44943. #ifdef HAVE_ECC
  44944. int ret;
  44945. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44946. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  44947. AssertIntEQ(ret, 32);
  44948. #endif
  44949. /* invalid case */
  44950. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  44951. AssertIntEQ(ret, -1);
  44952. /* NULL input */
  44953. ret = wc_ecc_get_curve_size_from_name(NULL);
  44954. AssertIntEQ(ret, BAD_FUNC_ARG);
  44955. res = TEST_RES_CHECK(1);
  44956. #endif /* HAVE_ECC */
  44957. return res;
  44958. }
  44959. static int test_wc_ecc_get_curve_id_from_name(void)
  44960. {
  44961. int res = TEST_SKIPPED;
  44962. #ifdef HAVE_ECC
  44963. int id;
  44964. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44965. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  44966. AssertIntEQ(id, ECC_SECP256R1);
  44967. #endif
  44968. /* invalid case */
  44969. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  44970. AssertIntEQ(id, -1);
  44971. /* NULL input */
  44972. id = wc_ecc_get_curve_id_from_name(NULL);
  44973. AssertIntEQ(id, BAD_FUNC_ARG);
  44974. res = TEST_RES_CHECK(1);
  44975. #endif /* HAVE_ECC */
  44976. return res;
  44977. }
  44978. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  44979. !defined(HAVE_SELFTEST) && \
  44980. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  44981. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  44982. {
  44983. int id;
  44984. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44985. int curve_id;
  44986. ecc_key* key;
  44987. const ecc_set_type* params;
  44988. int ret;
  44989. #endif
  44990. WOLFSSL_EC_KEY *ecKey = NULL;
  44991. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44992. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  44993. AssertIntEQ(id, ECC_SECP256R1);
  44994. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  44995. AssertNotNull(ecKey);
  44996. ret = EC_KEY_generate_key(ecKey);
  44997. if (ret == 0) {
  44998. /* normal test */
  44999. key = (ecc_key*)ecKey->internal;
  45000. params = key->dp;
  45001. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  45002. AssertIntEQ(curve_id, id);
  45003. }
  45004. #endif
  45005. /* invalid case, NULL input*/
  45006. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  45007. AssertIntEQ(id, BAD_FUNC_ARG);
  45008. wolfSSL_EC_KEY_free(ecKey);
  45009. return TEST_RES_CHECK(1);
  45010. }
  45011. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  45012. static int test_wc_ecc_get_curve_id_from_params(void)
  45013. {
  45014. int res = TEST_SKIPPED;
  45015. #ifdef HAVE_ECC
  45016. int id;
  45017. const byte prime[] =
  45018. {
  45019. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45020. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45021. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45022. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  45023. };
  45024. const byte primeInvalid[] =
  45025. {
  45026. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45027. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45028. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45029. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  45030. };
  45031. const byte Af[] =
  45032. {
  45033. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45034. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45035. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45036. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  45037. };
  45038. const byte Bf[] =
  45039. {
  45040. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  45041. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  45042. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  45043. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  45044. };
  45045. const byte order[] =
  45046. {
  45047. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  45048. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  45049. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  45050. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  45051. };
  45052. const byte Gx[] =
  45053. {
  45054. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  45055. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  45056. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  45057. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  45058. };
  45059. const byte Gy[] =
  45060. {
  45061. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  45062. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  45063. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  45064. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  45065. };
  45066. int cofactor = 1;
  45067. int fieldSize = 256;
  45068. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45069. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  45070. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45071. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  45072. AssertIntEQ(id, ECC_SECP256R1);
  45073. #endif
  45074. /* invalid case, fieldSize = 0 */
  45075. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  45076. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45077. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  45078. AssertIntEQ(id, ECC_CURVE_INVALID);
  45079. /* invalid case, NULL prime */
  45080. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  45081. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45082. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  45083. AssertIntEQ(id, BAD_FUNC_ARG);
  45084. /* invalid case, invalid prime */
  45085. id = wc_ecc_get_curve_id_from_params(fieldSize,
  45086. primeInvalid, sizeof(primeInvalid),
  45087. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45088. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  45089. AssertIntEQ(id, ECC_CURVE_INVALID);
  45090. res = TEST_RES_CHECK(1);
  45091. #endif
  45092. return res;
  45093. }
  45094. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  45095. {
  45096. int res = TEST_SKIPPED;
  45097. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45098. !defined(HAVE_FAST_RSA)
  45099. WOLFSSL_RSA* rsa = NULL;
  45100. WOLFSSL_EVP_PKEY* pkey = NULL;
  45101. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45102. const char* in = "What is easy to do is easy not to do.";
  45103. size_t inlen = XSTRLEN(in);
  45104. size_t outEncLen = 0;
  45105. byte* outEnc = NULL;
  45106. byte* outDec = NULL;
  45107. size_t outDecLen = 0;
  45108. size_t rsaKeySz = 2048/8; /* Bytes */
  45109. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45110. byte* inTmp = NULL;
  45111. byte* outEncTmp = NULL;
  45112. byte* outDecTmp = NULL;
  45113. #endif
  45114. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45115. XMEMSET(outEnc, 0, rsaKeySz);
  45116. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45117. XMEMSET(outDec, 0, rsaKeySz);
  45118. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45119. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45120. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45121. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45122. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45123. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45124. WOLFSSL_SUCCESS);
  45125. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  45126. since ctx uses it.*/
  45127. AssertIntEQ(pkey->ref.count, 2);
  45128. EVP_PKEY_free(pkey);
  45129. AssertIntEQ(pkey->ref.count, 1);
  45130. /* Encrypt data */
  45131. /* Check that we can get the required output buffer length by passing in a
  45132. * NULL output buffer. */
  45133. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  45134. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45135. AssertIntEQ(rsaKeySz, outEncLen);
  45136. /* Now do the actual encryption. */
  45137. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  45138. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45139. /* Decrypt data */
  45140. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45141. /* Check that we can get the required output buffer length by passing in a
  45142. * NULL output buffer. */
  45143. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  45144. WOLFSSL_SUCCESS);
  45145. AssertIntEQ(rsaKeySz, outDecLen);
  45146. /* Now do the actual decryption. */
  45147. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  45148. WOLFSSL_SUCCESS);
  45149. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  45150. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45151. /* The input length must be the same size as the RSA key.*/
  45152. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45153. XMEMSET(inTmp, 9, rsaKeySz);
  45154. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45155. XMEMSET(outEncTmp, 0, rsaKeySz);
  45156. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45157. XMEMSET(outDecTmp, 0, rsaKeySz);
  45158. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45159. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45160. WOLFSSL_SUCCESS);
  45161. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  45162. WOLFSSL_SUCCESS);
  45163. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45164. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  45165. WOLFSSL_SUCCESS);
  45166. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  45167. #endif
  45168. EVP_PKEY_CTX_free(ctx);
  45169. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45170. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45171. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45172. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45173. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45174. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45175. #endif
  45176. res = TEST_RES_CHECK(1);
  45177. #endif
  45178. return res;
  45179. }
  45180. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  45181. {
  45182. int res = TEST_SKIPPED;
  45183. #if defined(OPENSSL_EXTRA)
  45184. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45185. WOLFSSL_DSA* dsa = NULL;
  45186. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45187. WOLFSSL_EVP_PKEY* pkey = NULL;
  45188. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45189. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  45190. const char* in = "What is easy to do is easy not to do.";
  45191. size_t inlen = XSTRLEN(in);
  45192. byte hash[SHA256_DIGEST_LENGTH] = {0};
  45193. byte zero[SHA256_DIGEST_LENGTH] = {0};
  45194. SHA256_CTX c;
  45195. byte* sig = NULL;
  45196. byte* sigVerify = NULL;
  45197. size_t siglen;
  45198. size_t siglenOnlyLen;
  45199. size_t keySz = 2048/8; /* Bytes */
  45200. int i;
  45201. int encs[3] = {0};
  45202. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45203. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45204. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45205. encs[0] = EVP_PKEY_RSA;
  45206. #endif
  45207. #endif
  45208. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45209. encs[1] = EVP_PKEY_DSA;
  45210. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45211. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45212. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45213. encs[2] = EVP_PKEY_EC;
  45214. #endif
  45215. #endif
  45216. AssertNotNull(sig =
  45217. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45218. AssertNotNull(sigVerify =
  45219. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45220. for (i = 0; i < 3; i++) {
  45221. if (encs[i] == 0)
  45222. continue;
  45223. siglen = keySz;
  45224. XMEMSET(sig, 0, keySz);
  45225. XMEMSET(sigVerify, 0, keySz);
  45226. /* Generate hash */
  45227. SHA256_Init(&c);
  45228. SHA256_Update(&c, in, inlen);
  45229. SHA256_Final(hash, &c);
  45230. #ifdef WOLFSSL_SMALL_STACK_CACHE
  45231. /* workaround for small stack cache case */
  45232. wc_Sha256Free((wc_Sha256*)&c);
  45233. #endif
  45234. /* Generate key */
  45235. AssertNotNull(pkey = EVP_PKEY_new());
  45236. switch (encs[i]) {
  45237. case EVP_PKEY_RSA:
  45238. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45239. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45240. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45241. {
  45242. WOLFSSL_RSA* rsa = NULL;
  45243. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45244. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45245. }
  45246. #endif
  45247. #endif
  45248. break;
  45249. case EVP_PKEY_DSA:
  45250. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45251. AssertNotNull(dsa = DSA_new());
  45252. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  45253. NULL, 0, NULL, NULL, NULL), 1);
  45254. AssertIntEQ(DSA_generate_key(dsa), 1);
  45255. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  45256. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45257. break;
  45258. case EVP_PKEY_EC:
  45259. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45260. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45261. {
  45262. WOLFSSL_EC_KEY* ecKey = NULL;
  45263. AssertNotNull(ecKey = EC_KEY_new());
  45264. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  45265. AssertIntEQ(
  45266. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45267. }
  45268. #endif
  45269. #endif
  45270. break;
  45271. }
  45272. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45273. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45274. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45275. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45276. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45277. if (encs[i] == EVP_PKEY_RSA)
  45278. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45279. WOLFSSL_SUCCESS);
  45280. #endif
  45281. #endif
  45282. /* Check returning only length */
  45283. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  45284. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  45285. AssertIntGT(siglenOnlyLen, 0);
  45286. /* Sign data */
  45287. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  45288. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  45289. AssertIntGE(siglenOnlyLen, siglen);
  45290. /* Verify signature */
  45291. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  45292. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45293. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45294. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45295. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45296. if (encs[i] == EVP_PKEY_RSA)
  45297. AssertIntEQ(
  45298. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  45299. WOLFSSL_SUCCESS);
  45300. #endif
  45301. #endif
  45302. AssertIntEQ(EVP_PKEY_verify(
  45303. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  45304. WOLFSSL_SUCCESS);
  45305. AssertIntEQ(EVP_PKEY_verify(
  45306. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  45307. WOLFSSL_FAILURE);
  45308. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45309. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45310. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45311. if (encs[i] == EVP_PKEY_RSA) {
  45312. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  45313. /* Try RSA sign/verify with no padding. */
  45314. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45315. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45316. WOLFSSL_SUCCESS);
  45317. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  45318. siglen), WOLFSSL_SUCCESS);
  45319. AssertIntGE(siglenOnlyLen, siglen);
  45320. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45321. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45322. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  45323. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  45324. siglen), WOLFSSL_SUCCESS);
  45325. #endif
  45326. /* Wrong padding schemes. */
  45327. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45328. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  45329. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  45330. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  45331. siglen), WOLFSSL_SUCCESS);
  45332. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45333. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45334. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  45335. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  45336. siglen), WOLFSSL_SUCCESS);
  45337. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45338. WOLFSSL_SUCCESS);
  45339. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45340. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  45341. }
  45342. #endif
  45343. #endif
  45344. /* error cases */
  45345. siglen = keySz; /* Reset because sig size may vary slightly */
  45346. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  45347. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45348. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  45349. WOLFSSL_SUCCESS);
  45350. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  45351. WOLFSSL_SUCCESS);
  45352. EVP_PKEY_free(pkey);
  45353. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45354. DSA_free(dsa);
  45355. dsa = NULL;
  45356. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45357. EVP_PKEY_CTX_free(ctx_verify);
  45358. EVP_PKEY_CTX_free(ctx);
  45359. }
  45360. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45361. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45362. res = TEST_RES_CHECK(1);
  45363. #endif /* OPENSSL_EXTRA */
  45364. return res;
  45365. }
  45366. static int test_EVP_PKEY_rsa(void)
  45367. {
  45368. int res = TEST_SKIPPED;
  45369. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45370. WOLFSSL_RSA* rsa;
  45371. WOLFSSL_EVP_PKEY* pkey;
  45372. AssertNotNull(rsa = wolfSSL_RSA_new());
  45373. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45374. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  45375. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  45376. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45377. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  45378. wolfSSL_EVP_PKEY_free(pkey);
  45379. res = TEST_RES_CHECK(1);
  45380. #endif
  45381. return res;
  45382. }
  45383. static int test_EVP_PKEY_ec(void)
  45384. {
  45385. int res = TEST_SKIPPED;
  45386. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45387. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45388. WOLFSSL_EC_KEY* ecKey;
  45389. WOLFSSL_EVP_PKEY* pkey;
  45390. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  45391. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45392. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  45393. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  45394. /* Should fail since ecKey is empty */
  45395. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  45396. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45397. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45398. wolfSSL_EVP_PKEY_free(pkey);
  45399. res = TEST_RES_CHECK(1);
  45400. #endif
  45401. #endif
  45402. return res;
  45403. }
  45404. static int test_EVP_PKEY_cmp(void)
  45405. {
  45406. int res = TEST_SKIPPED;
  45407. #if defined(OPENSSL_EXTRA)
  45408. EVP_PKEY *a, *b;
  45409. const unsigned char *in;
  45410. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45411. in = client_key_der_2048;
  45412. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45413. &in, (long)sizeof_client_key_der_2048));
  45414. in = client_key_der_2048;
  45415. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45416. &in, (long)sizeof_client_key_der_2048));
  45417. /* Test success case RSA */
  45418. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45419. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  45420. #else
  45421. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  45422. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45423. EVP_PKEY_free(b);
  45424. EVP_PKEY_free(a);
  45425. #endif
  45426. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45427. in = ecc_clikey_der_256;
  45428. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45429. &in, (long)sizeof_ecc_clikey_der_256));
  45430. in = ecc_clikey_der_256;
  45431. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45432. &in, (long)sizeof_ecc_clikey_der_256));
  45433. /* Test success case ECC */
  45434. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45435. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  45436. #else
  45437. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  45438. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45439. EVP_PKEY_free(b);
  45440. EVP_PKEY_free(a);
  45441. #endif
  45442. /* Test failure cases */
  45443. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  45444. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45445. in = client_key_der_2048;
  45446. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45447. &in, (long)sizeof_client_key_der_2048));
  45448. in = ecc_clikey_der_256;
  45449. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45450. &in, (long)sizeof_ecc_clikey_der_256));
  45451. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45452. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  45453. #else
  45454. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  45455. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45456. EVP_PKEY_free(b);
  45457. EVP_PKEY_free(a);
  45458. #endif
  45459. /* invalid or empty failure cases */
  45460. a = EVP_PKEY_new();
  45461. b = EVP_PKEY_new();
  45462. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45463. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  45464. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  45465. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  45466. #ifdef NO_RSA
  45467. /* Type check will fail since RSA is the default EVP key type */
  45468. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  45469. #else
  45470. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  45471. #endif
  45472. #else
  45473. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  45474. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  45475. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  45476. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  45477. #endif
  45478. EVP_PKEY_free(b);
  45479. EVP_PKEY_free(a);
  45480. (void)in;
  45481. res = TEST_RES_CHECK(1);
  45482. #endif
  45483. return res;
  45484. }
  45485. static int test_ERR_load_crypto_strings(void)
  45486. {
  45487. int res = TEST_SKIPPED;
  45488. #if defined(OPENSSL_ALL)
  45489. ERR_load_crypto_strings();
  45490. res = TEST_RES_CHECK(1);
  45491. #endif
  45492. return res;
  45493. }
  45494. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45495. static void free_x509(X509* x)
  45496. {
  45497. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  45498. }
  45499. #endif
  45500. static int test_sk_X509(void)
  45501. {
  45502. int res = TEST_SKIPPED;
  45503. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45504. {
  45505. STACK_OF(X509)* s;
  45506. AssertNotNull(s = sk_X509_new_null());
  45507. AssertIntEQ(sk_X509_num(s), 0);
  45508. sk_X509_pop_free(s, NULL);
  45509. AssertNotNull(s = sk_X509_new_null());
  45510. AssertIntEQ(sk_X509_num(s), 0);
  45511. sk_X509_pop_free(s, NULL);
  45512. AssertNotNull(s = sk_X509_new_null());
  45513. sk_X509_push(s, (X509*)1);
  45514. AssertIntEQ(sk_X509_num(s), 1);
  45515. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  45516. sk_X509_push(s, (X509*)2);
  45517. AssertIntEQ(sk_X509_num(s), 2);
  45518. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  45519. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  45520. sk_X509_push(s, (X509*)2);
  45521. sk_X509_pop_free(s, free_x509);
  45522. }
  45523. {
  45524. /* Push a list of 10 X509s onto stack, then verify that
  45525. * value(), push(), shift(), and pop() behave as expected. */
  45526. STACK_OF(X509)* s;
  45527. X509* xList[10];
  45528. int i = 0;
  45529. const int len = (sizeof(xList) / sizeof(xList[0]));
  45530. for (i = 0; i < len; ++i)
  45531. AssertNotNull(xList[i] = X509_new());
  45532. /* test push, pop, and free */
  45533. AssertNotNull(s = sk_X509_new_null());
  45534. for (i = 0; i < len; ++i) {
  45535. sk_X509_push(s, xList[i]);
  45536. AssertIntEQ(sk_X509_num(s), i + 1);
  45537. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45538. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45539. }
  45540. /* pop returns and removes last pushed on stack, which is index 0
  45541. * in sk_x509_value */
  45542. for (i = 0; i < len; ++i) {
  45543. X509 * x = sk_X509_value(s, 0);
  45544. X509 * y = sk_X509_pop(s);
  45545. X509 * z = xList[len - 1 - i];
  45546. AssertIntEQ((x == y), 1);
  45547. AssertIntEQ((x == z), 1);
  45548. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  45549. }
  45550. sk_free(s);
  45551. /* test push, shift, and free */
  45552. AssertNotNull(s = sk_X509_new_null());
  45553. for (i = 0; i < len; ++i) {
  45554. sk_X509_push(s, xList[i]);
  45555. AssertIntEQ(sk_X509_num(s), i + 1);
  45556. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45557. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45558. }
  45559. /* shift returns and removes first pushed on stack, which is index i
  45560. * in sk_x509_value() */
  45561. for (i = 0; i < len; ++i) {
  45562. X509 * x = sk_X509_value(s, len - 1 - i);
  45563. X509 * y = sk_X509_shift(s);
  45564. X509 * z = xList[i];
  45565. AssertIntEQ((x == y), 1);
  45566. AssertIntEQ((x == z), 1);
  45567. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  45568. }
  45569. sk_free(s);
  45570. for (i = 0; i < len; ++i)
  45571. X509_free(xList[i]);
  45572. }
  45573. res = TEST_RES_CHECK(1);
  45574. #endif
  45575. return res;
  45576. }
  45577. static int test_sk_X509_CRL(void)
  45578. {
  45579. int res = TEST_SKIPPED;
  45580. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  45581. X509_CRL* crl;
  45582. XFILE fp;
  45583. STACK_OF(X509_CRL)* s;
  45584. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  45585. AssertTrue((fp != XBADFILE));
  45586. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  45587. XFCLOSE(fp);
  45588. AssertNotNull(s = sk_X509_CRL_new());
  45589. AssertIntEQ(sk_X509_CRL_num(s), 0);
  45590. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  45591. AssertIntEQ(sk_X509_CRL_num(s), 1);
  45592. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  45593. sk_X509_CRL_free(s);
  45594. res = TEST_RES_CHECK(1);
  45595. #endif
  45596. return res;
  45597. }
  45598. static int test_X509_get_signature_nid(void)
  45599. {
  45600. int res = TEST_SKIPPED;
  45601. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45602. X509* x509;
  45603. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  45604. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45605. SSL_FILETYPE_PEM));
  45606. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  45607. X509_free(x509);
  45608. res = TEST_RES_CHECK(1);
  45609. #endif
  45610. return res;
  45611. }
  45612. static int test_X509_REQ(void)
  45613. {
  45614. int res = TEST_SKIPPED;
  45615. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  45616. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  45617. X509_NAME* name;
  45618. #ifndef NO_RSA
  45619. X509_NAME* subject;
  45620. #endif
  45621. #if !defined(NO_RSA) || defined(HAVE_ECC)
  45622. X509_REQ* req;
  45623. EVP_PKEY* priv;
  45624. EVP_PKEY* pub;
  45625. unsigned char* der = NULL;
  45626. int len;
  45627. #endif
  45628. #ifndef NO_RSA
  45629. EVP_MD_CTX *mctx = NULL;
  45630. EVP_PKEY_CTX *pkctx = NULL;
  45631. #ifdef USE_CERT_BUFFERS_1024
  45632. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  45633. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  45634. #elif defined(USE_CERT_BUFFERS_2048)
  45635. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  45636. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  45637. #endif
  45638. #endif
  45639. #ifdef HAVE_ECC
  45640. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  45641. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  45642. #endif
  45643. AssertNotNull(name = X509_NAME_new());
  45644. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  45645. (byte*)"wolfssl.com", 11, 0, 1),
  45646. WOLFSSL_SUCCESS);
  45647. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  45648. (byte*)"support@wolfssl.com", 19, -1,
  45649. 1), WOLFSSL_SUCCESS);
  45650. #ifndef NO_RSA
  45651. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  45652. (long)sizeof_client_key_der_2048));
  45653. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  45654. (long)sizeof_client_keypub_der_2048));
  45655. AssertNotNull(req = X509_REQ_new());
  45656. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  45657. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  45658. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45659. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  45660. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  45661. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45662. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  45663. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45664. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  45665. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45666. len = i2d_X509_REQ(req, &der);
  45667. DEBUG_WRITE_DER(der, len, "req.der");
  45668. #ifdef USE_CERT_BUFFERS_1024
  45669. AssertIntEQ(len, 381);
  45670. #else
  45671. AssertIntEQ(len, 643);
  45672. #endif
  45673. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45674. der = NULL;
  45675. mctx = EVP_MD_CTX_new();
  45676. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  45677. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  45678. EVP_MD_CTX_free(mctx);
  45679. X509_REQ_free(NULL);
  45680. X509_REQ_free(req);
  45681. /* Test getting the subject from a newly created X509_REQ */
  45682. AssertNotNull(req = X509_REQ_new());
  45683. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  45684. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  45685. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  45686. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  45687. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  45688. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  45689. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  45690. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  45691. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  45692. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  45693. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  45694. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  45695. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  45696. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45697. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45698. len = i2d_X509_REQ(req, &der);
  45699. DEBUG_WRITE_DER(der, len, "req2.der");
  45700. #ifdef USE_CERT_BUFFERS_1024
  45701. AssertIntEQ(len, 435);
  45702. #else
  45703. AssertIntEQ(len, 696);
  45704. #endif
  45705. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45706. der = NULL;
  45707. EVP_PKEY_free(pub);
  45708. EVP_PKEY_free(priv);
  45709. X509_REQ_free(req);
  45710. #endif
  45711. #ifdef HAVE_ECC
  45712. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  45713. sizeof_ecc_clikey_der_256));
  45714. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  45715. sizeof_ecc_clikeypub_der_256));
  45716. AssertNotNull(req = X509_REQ_new());
  45717. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45718. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45719. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45720. /* Signature is random and may be shorter or longer. */
  45721. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  45722. AssertIntLE(len, 253);
  45723. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45724. X509_REQ_free(req);
  45725. EVP_PKEY_free(pub);
  45726. EVP_PKEY_free(priv);
  45727. #ifdef FP_ECC
  45728. wc_ecc_fp_free();
  45729. #endif
  45730. #endif /* HAVE_ECC */
  45731. X509_NAME_free(name);
  45732. res = TEST_RES_CHECK(1);
  45733. #endif
  45734. return res;
  45735. }
  45736. static int test_wolfssl_PKCS7(void)
  45737. {
  45738. int res = TEST_SKIPPED;
  45739. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45740. !defined(NO_RSA)
  45741. PKCS7* pkcs7;
  45742. byte data[FOURK_BUF];
  45743. word32 len = sizeof(data);
  45744. const byte* p = data;
  45745. byte content[] = "Test data to encode.";
  45746. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45747. BIO* bio;
  45748. byte key[sizeof(client_key_der_2048)];
  45749. word32 keySz = (word32)sizeof(key);
  45750. byte* out = NULL;
  45751. #endif
  45752. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  45753. (word32)sizeof(content),
  45754. 0, 0, 0, RSA_TYPE)), 0);
  45755. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  45756. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  45757. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45758. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  45759. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  45760. PKCS7_free(pkcs7);
  45761. /* fail case, without PKCS7_NOVERIFY */
  45762. p = data;
  45763. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45764. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45765. 0), WOLFSSL_FAILURE);
  45766. PKCS7_free(pkcs7);
  45767. /* success case, with PKCS7_NOVERIFY */
  45768. p = data;
  45769. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45770. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45771. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  45772. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45773. /* test i2d */
  45774. XMEMCPY(key, client_key_der_2048, keySz);
  45775. pkcs7->privateKey = key;
  45776. pkcs7->privateKeySz = (word32)sizeof(key);
  45777. pkcs7->encryptOID = RSAk;
  45778. #ifdef NO_SHA
  45779. pkcs7->hashOID = SHA256h;
  45780. #else
  45781. pkcs7->hashOID = SHAh;
  45782. #endif
  45783. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  45784. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  45785. #ifndef NO_ASN_TIME
  45786. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  45787. #else
  45788. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  45789. #endif
  45790. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45791. BIO_free(bio);
  45792. #endif
  45793. PKCS7_free(NULL);
  45794. PKCS7_free(pkcs7);
  45795. res = TEST_RES_CHECK(1);
  45796. #endif
  45797. return res;
  45798. }
  45799. static int test_wolfSSL_PKCS7_sign(void)
  45800. {
  45801. int res = TEST_SKIPPED;
  45802. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45803. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45804. PKCS7* p7 = NULL;
  45805. PKCS7* p7Ver = NULL;
  45806. byte* out = NULL;
  45807. byte* tmpPtr = NULL;
  45808. int outLen = 0;
  45809. int flags = 0;
  45810. byte data[] = "Test data to encode.";
  45811. const char* cert = "./certs/server-cert.pem";
  45812. const char* key = "./certs/server-key.pem";
  45813. const char* ca = "./certs/ca-cert.pem";
  45814. WOLFSSL_BIO* certBio = NULL;
  45815. WOLFSSL_BIO* keyBio = NULL;
  45816. WOLFSSL_BIO* caBio = NULL;
  45817. WOLFSSL_BIO* inBio = NULL;
  45818. X509* signCert = NULL;
  45819. EVP_PKEY* signKey = NULL;
  45820. X509* caCert = NULL;
  45821. X509_STORE* store = NULL;
  45822. /* read signer cert/key into BIO */
  45823. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  45824. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  45825. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  45826. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  45827. /* read CA cert into store (for verify) */
  45828. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  45829. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  45830. AssertNotNull(store = X509_STORE_new());
  45831. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  45832. /* data to be signed into BIO */
  45833. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  45834. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45835. /* PKCS7_sign, bad args: signer NULL */
  45836. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  45837. /* PKCS7_sign, bad args: signer key NULL */
  45838. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  45839. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  45840. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  45841. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  45842. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  45843. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  45844. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  45845. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  45846. {
  45847. flags = PKCS7_BINARY;
  45848. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45849. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45850. /* verify with d2i_PKCS7 */
  45851. tmpPtr = out;
  45852. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45853. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45854. PKCS7_free(p7Ver);
  45855. /* verify with wc_PKCS7_VerifySignedData */
  45856. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45857. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  45858. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45859. /* compare the signer found to expected signer */
  45860. AssertIntNE(p7Ver->verifyCertSz, 0);
  45861. tmpPtr = NULL;
  45862. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  45863. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  45864. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  45865. tmpPtr = NULL;
  45866. wc_PKCS7_Free(p7Ver);
  45867. AssertNotNull(out);
  45868. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45869. out = NULL;
  45870. PKCS7_free(p7);
  45871. }
  45872. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  45873. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  45874. {
  45875. /* re-populate input BIO, may have been consumed */
  45876. BIO_free(inBio);
  45877. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  45878. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45879. flags = PKCS7_BINARY | PKCS7_STREAM;
  45880. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45881. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45882. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45883. /* PKCS7_final, bad args: PKCS7 null */
  45884. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  45885. /* PKCS7_final, bad args: PKCS7 null */
  45886. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  45887. tmpPtr = out;
  45888. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45889. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45890. PKCS7_free(p7Ver);
  45891. AssertNotNull(out);
  45892. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45893. out = NULL;
  45894. PKCS7_free(p7);
  45895. }
  45896. /* TEST SUCCESS: Detached, not streaming, not MIME */
  45897. {
  45898. /* re-populate input BIO, may have been consumed */
  45899. BIO_free(inBio);
  45900. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  45901. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45902. flags = PKCS7_BINARY | PKCS7_DETACHED;
  45903. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45904. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45905. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45906. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45907. p7Ver->content = data;
  45908. p7Ver->contentSz = sizeof(data);
  45909. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45910. wc_PKCS7_Free(p7Ver);
  45911. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  45912. * yet support detached content */
  45913. tmpPtr = out;
  45914. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45915. PKCS7_free(p7Ver);
  45916. AssertNotNull(out);
  45917. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45918. out = NULL;
  45919. PKCS7_free(p7);
  45920. }
  45921. /* TEST SUCCESS: Detached, streaming, not MIME */
  45922. {
  45923. /* re-populate input BIO, may have been consumed */
  45924. BIO_free(inBio);
  45925. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  45926. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45927. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  45928. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45929. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45930. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45931. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45932. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45933. p7Ver->content = data;
  45934. p7Ver->contentSz = sizeof(data);
  45935. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45936. wc_PKCS7_Free(p7Ver);
  45937. AssertNotNull(out);
  45938. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45939. PKCS7_free(p7);
  45940. }
  45941. X509_STORE_free(store);
  45942. X509_free(caCert);
  45943. X509_free(signCert);
  45944. EVP_PKEY_free(signKey);
  45945. BIO_free(inBio);
  45946. BIO_free(keyBio);
  45947. BIO_free(certBio);
  45948. BIO_free(caBio);
  45949. res = TEST_RES_CHECK(1);
  45950. #endif
  45951. return res;
  45952. }
  45953. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  45954. {
  45955. int res = TEST_SKIPPED;
  45956. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  45957. PKCS7_SIGNED* pkcs7;
  45958. pkcs7 = PKCS7_SIGNED_new();
  45959. AssertNotNull(pkcs7);
  45960. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  45961. PKCS7_SIGNED_free(pkcs7);
  45962. res = TEST_RES_CHECK(1);
  45963. #endif
  45964. return res;
  45965. }
  45966. #ifndef NO_BIO
  45967. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  45968. {
  45969. int res = TEST_SKIPPED;
  45970. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  45971. PKCS7* pkcs7 = NULL;
  45972. BIO* bio = NULL;
  45973. const byte* cert_buf = NULL;
  45974. int ret = 0;
  45975. WC_RNG rng;
  45976. const byte data[] = { /* Hello World */
  45977. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  45978. 0x72,0x6c,0x64
  45979. };
  45980. #ifndef NO_RSA
  45981. #if defined(USE_CERT_BUFFERS_2048)
  45982. byte key[sizeof(client_key_der_2048)];
  45983. byte cert[sizeof(client_cert_der_2048)];
  45984. word32 keySz = (word32)sizeof(key);
  45985. word32 certSz = (word32)sizeof(cert);
  45986. XMEMSET(key, 0, keySz);
  45987. XMEMSET(cert, 0, certSz);
  45988. XMEMCPY(key, client_key_der_2048, keySz);
  45989. XMEMCPY(cert, client_cert_der_2048, certSz);
  45990. #elif defined(USE_CERT_BUFFERS_1024)
  45991. byte key[sizeof_client_key_der_1024];
  45992. byte cert[sizeof(sizeof_client_cert_der_1024)];
  45993. word32 keySz = (word32)sizeof(key);
  45994. word32 certSz = (word32)sizeof(cert);
  45995. XMEMSET(key, 0, keySz);
  45996. XMEMSET(cert, 0, certSz);
  45997. XMEMCPY(key, client_key_der_1024, keySz);
  45998. XMEMCPY(cert, client_cert_der_1024, certSz);
  45999. #else
  46000. unsigned char cert[ONEK_BUF];
  46001. unsigned char key[ONEK_BUF];
  46002. XFILE fp;
  46003. int certSz;
  46004. int keySz;
  46005. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  46006. AssertTrue((fp != XBADFILE));
  46007. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  46008. XFCLOSE(fp);
  46009. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  46010. AssertTrue(fp != XBADFILE);
  46011. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  46012. XFCLOSE(fp);
  46013. #endif
  46014. #elif defined(HAVE_ECC)
  46015. #if defined(USE_CERT_BUFFERS_256)
  46016. unsigned char cert[sizeof(cliecc_cert_der_256)];
  46017. unsigned char key[sizeof(ecc_clikey_der_256)];
  46018. int certSz = (int)sizeof(cert);
  46019. int keySz = (int)sizeof(key);
  46020. XMEMSET(cert, 0, certSz);
  46021. XMEMSET(key, 0, keySz);
  46022. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  46023. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  46024. #else
  46025. unsigned char cert[ONEK_BUF];
  46026. unsigned char key[ONEK_BUF];
  46027. XFILE fp;
  46028. int certSz, keySz;
  46029. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  46030. AssertTrue(fp != XBADFILE);
  46031. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  46032. XFCLOSE(fp);
  46033. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  46034. AssertTrue(fp != XBADFILE);
  46035. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  46036. XFCLOSE(fp);
  46037. #endif
  46038. #else
  46039. #error PKCS7 requires ECC or RSA
  46040. #endif
  46041. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  46042. /* initialize with DER encoded cert */
  46043. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  46044. /* init rng */
  46045. AssertIntEQ(wc_InitRng(&rng), 0);
  46046. pkcs7->rng = &rng;
  46047. pkcs7->content = (byte*)data; /* not used for ex */
  46048. pkcs7->contentSz = (word32)sizeof(data);
  46049. pkcs7->contentOID = SIGNED_DATA;
  46050. pkcs7->privateKey = key;
  46051. pkcs7->privateKeySz = (word32)sizeof(key);
  46052. pkcs7->encryptOID = RSAk;
  46053. #ifdef NO_SHA
  46054. pkcs7->hashOID = SHA256h;
  46055. #else
  46056. pkcs7->hashOID = SHAh;
  46057. #endif
  46058. pkcs7->signedAttribs = NULL;
  46059. pkcs7->signedAttribsSz = 0;
  46060. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  46061. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  46062. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  46063. /* Read PKCS#7 PEM from BIO */
  46064. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  46065. AssertIntGE(ret, 0);
  46066. BIO_free(bio);
  46067. wc_PKCS7_Free(pkcs7);
  46068. wc_FreeRng(&rng);
  46069. res = TEST_RES_CHECK(1);
  46070. #endif
  46071. return res;
  46072. }
  46073. #ifdef HAVE_SMIME
  46074. static int test_wolfSSL_SMIME_read_PKCS7(void)
  46075. {
  46076. int res = TEST_SKIPPED;
  46077. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  46078. !defined(NO_RSA)
  46079. PKCS7* pkcs7 = NULL;
  46080. BIO* bio = NULL;
  46081. BIO* bcont = NULL;
  46082. BIO* out = NULL;
  46083. const byte* outBuf = NULL;
  46084. int outBufLen = 0;
  46085. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  46086. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  46087. /* smime-test.p7s */
  46088. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  46089. AssertNotNull(bio);
  46090. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46091. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46092. AssertNotNull(pkcs7);
  46093. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46094. PKCS7_NOVERIFY), SSL_SUCCESS);
  46095. XFCLOSE(smimeTestFile);
  46096. if (bcont) BIO_free(bcont);
  46097. wolfSSL_PKCS7_free(pkcs7);
  46098. /* smime-test-multipart.p7s */
  46099. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  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-badsig.p7s */
  46109. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  46110. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46111. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46112. AssertNull(pkcs7);
  46113. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46114. PKCS7_NOVERIFY), SSL_FAILURE);
  46115. XFCLOSE(smimeTestFile);
  46116. if (bcont) BIO_free(bcont);
  46117. wolfSSL_PKCS7_free(pkcs7);
  46118. /* smime-test-canon.p7s */
  46119. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  46120. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46121. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46122. AssertNotNull(pkcs7);
  46123. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46124. PKCS7_NOVERIFY), SSL_SUCCESS);
  46125. XFCLOSE(smimeTestFile);
  46126. if (bcont) BIO_free(bcont);
  46127. wolfSSL_PKCS7_free(pkcs7);
  46128. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  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. out = wolfSSL_BIO_new(BIO_s_mem());
  46134. AssertNotNull(out);
  46135. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  46136. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  46137. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  46138. /* Content-Type should not show up at beginning of output buffer */
  46139. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  46140. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  46141. BIO_free(out);
  46142. BIO_free(bio);
  46143. if (bcont) BIO_free(bcont);
  46144. wolfSSL_PKCS7_free(pkcs7);
  46145. res = TEST_RES_CHECK(1);
  46146. #endif
  46147. return res;
  46148. }
  46149. static int test_wolfSSL_SMIME_write_PKCS7(void)
  46150. {
  46151. int res = TEST_SKIPPED;
  46152. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  46153. PKCS7* p7 = NULL;
  46154. PKCS7* p7Ver = NULL;
  46155. int flags = 0;
  46156. byte data[] = "Test data to encode.";
  46157. const char* cert = "./certs/server-cert.pem";
  46158. const char* key = "./certs/server-key.pem";
  46159. const char* ca = "./certs/ca-cert.pem";
  46160. WOLFSSL_BIO* certBio = NULL;
  46161. WOLFSSL_BIO* keyBio = NULL;
  46162. WOLFSSL_BIO* caBio = NULL;
  46163. WOLFSSL_BIO* inBio = NULL;
  46164. WOLFSSL_BIO* outBio = NULL;
  46165. WOLFSSL_BIO* content = NULL;
  46166. X509* signCert = NULL;
  46167. EVP_PKEY* signKey = NULL;
  46168. X509* caCert = NULL;
  46169. X509_STORE* store = NULL;
  46170. /* read signer cert/key into BIO */
  46171. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  46172. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  46173. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  46174. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  46175. /* read CA cert into store (for verify) */
  46176. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  46177. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  46178. AssertNotNull(store = X509_STORE_new());
  46179. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  46180. /* generate and verify SMIME: not detached */
  46181. {
  46182. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46183. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46184. flags = PKCS7_STREAM;
  46185. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46186. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  46187. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46188. /* bad arg: out NULL */
  46189. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  46190. /* bad arg: pkcs7 NULL */
  46191. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  46192. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46193. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46194. BIO_free(content);
  46195. BIO_free(inBio);
  46196. BIO_free(outBio);
  46197. PKCS7_free(p7Ver);
  46198. PKCS7_free(p7);
  46199. }
  46200. /* generate and verify SMIME: not detached, add Content-Type */
  46201. {
  46202. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46203. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46204. flags = PKCS7_STREAM | PKCS7_TEXT;
  46205. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46206. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  46207. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46208. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46209. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46210. BIO_free(content);
  46211. BIO_free(inBio);
  46212. BIO_free(outBio);
  46213. PKCS7_free(p7Ver);
  46214. PKCS7_free(p7);
  46215. }
  46216. /* generate and verify SMIME: detached */
  46217. {
  46218. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46219. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46220. flags = PKCS7_DETACHED | PKCS7_STREAM;
  46221. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46222. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  46223. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46224. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46225. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  46226. BIO_free(content);
  46227. BIO_free(inBio);
  46228. BIO_free(outBio);
  46229. PKCS7_free(p7Ver);
  46230. PKCS7_free(p7);
  46231. }
  46232. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  46233. {
  46234. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46235. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46236. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  46237. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46238. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  46239. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46240. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46241. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  46242. BIO_free(content);
  46243. BIO_free(inBio);
  46244. BIO_free(outBio);
  46245. PKCS7_free(p7Ver);
  46246. PKCS7_free(p7);
  46247. }
  46248. X509_STORE_free(store);
  46249. X509_free(caCert);
  46250. X509_free(signCert);
  46251. EVP_PKEY_free(signKey);
  46252. BIO_free(keyBio);
  46253. BIO_free(certBio);
  46254. BIO_free(caBio);
  46255. res = TEST_RES_CHECK(1);
  46256. #endif
  46257. return res;
  46258. }
  46259. #endif /* HAVE_SMIME */
  46260. #endif /* !NO_BIO */
  46261. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  46262. * returns 0) */
  46263. static int test_X509_STORE_No_SSL_CTX(void)
  46264. {
  46265. int res = TEST_SKIPPED;
  46266. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  46267. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  46268. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  46269. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  46270. !defined(NO_RSA)
  46271. X509_STORE * store;
  46272. X509_STORE_CTX * storeCtx;
  46273. X509_CRL * crl;
  46274. X509 * ca;
  46275. X509 * cert;
  46276. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  46277. const char srvCert[] = "./certs/server-cert.pem";
  46278. const char caCert[] = "./certs/ca-cert.pem";
  46279. const char caDir[] = "./certs/crl/hash_pem";
  46280. XFILE fp;
  46281. X509_LOOKUP * lookup;
  46282. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  46283. /* Set up store with CA */
  46284. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46285. SSL_FILETYPE_PEM)));
  46286. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46287. /* Add CRL lookup directory to store
  46288. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  46289. * of crl.pem */
  46290. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  46291. X509_LOOKUP_hash_dir())));
  46292. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  46293. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  46294. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46295. SSL_SUCCESS);
  46296. /* Add CRL to store NOT containing the verified certificate, which
  46297. * forces use of the CRL lookup directory */
  46298. fp = XFOPEN(cliCrlPem, "rb");
  46299. AssertTrue((fp != XBADFILE));
  46300. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46301. NULL, NULL));
  46302. XFCLOSE(fp);
  46303. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46304. /* Create verification context outside of an SSL session */
  46305. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  46306. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46307. SSL_FILETYPE_PEM)));
  46308. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46309. /* Perform verification, which should NOT indicate CRL missing due to the
  46310. * store CM's X509 store pointer being NULL */
  46311. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46312. X509_CRL_free(crl);
  46313. X509_STORE_free(store);
  46314. X509_STORE_CTX_free(storeCtx);
  46315. X509_free(cert);
  46316. X509_free(ca);
  46317. res = TEST_RES_CHECK(1);
  46318. #endif
  46319. return res;
  46320. }
  46321. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  46322. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  46323. static int test_X509_LOOKUP_add_dir(void)
  46324. {
  46325. int res = TEST_SKIPPED;
  46326. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  46327. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  46328. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  46329. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  46330. !defined(NO_RSA)
  46331. X509_STORE * store;
  46332. X509_STORE_CTX * storeCtx;
  46333. X509_CRL * crl;
  46334. X509 * ca;
  46335. X509 * cert;
  46336. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  46337. const char srvCert[] = "./certs/server-cert.pem";
  46338. const char caCert[] = "./certs/ca-cert.pem";
  46339. const char caDir[] = "./certs/crl/hash_der";
  46340. XFILE fp;
  46341. X509_LOOKUP * lookup;
  46342. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  46343. /* Set up store with CA */
  46344. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46345. SSL_FILETYPE_PEM)));
  46346. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46347. /* Add CRL lookup directory to store.
  46348. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  46349. * of crl.der */
  46350. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  46351. X509_LOOKUP_hash_dir())));
  46352. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  46353. SSL_SUCCESS);
  46354. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46355. SSL_SUCCESS);
  46356. /* Add CRL to store NOT containing the verified certificate, which
  46357. * forces use of the CRL lookup directory */
  46358. fp = XFOPEN(cliCrlPem, "rb");
  46359. AssertTrue((fp != XBADFILE));
  46360. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46361. NULL, NULL));
  46362. XFCLOSE(fp);
  46363. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46364. /* Create verification context outside of an SSL session */
  46365. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  46366. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46367. SSL_FILETYPE_PEM)));
  46368. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46369. /* Perform verification, which should NOT return CRL missing */
  46370. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46371. X509_CRL_free(crl);
  46372. X509_STORE_free(store);
  46373. X509_STORE_CTX_free(storeCtx);
  46374. X509_free(cert);
  46375. X509_free(ca);
  46376. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  46377. * We should get CRL_MISSING at the end, because the lookup
  46378. * dir has only ASN1 CRLs. */
  46379. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  46380. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46381. SSL_FILETYPE_PEM)));
  46382. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46383. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  46384. X509_LOOKUP_hash_dir())));
  46385. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  46386. SSL_SUCCESS);
  46387. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46388. SSL_SUCCESS);
  46389. fp = XFOPEN(cliCrlPem, "rb");
  46390. AssertTrue((fp != XBADFILE));
  46391. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46392. NULL, NULL));
  46393. XFCLOSE(fp);
  46394. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46395. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  46396. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46397. SSL_FILETYPE_PEM)));
  46398. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46399. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  46400. * in dir containing only ASN1/DER. */
  46401. AssertIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  46402. X509_CRL_free(crl);
  46403. X509_STORE_free(store);
  46404. X509_STORE_CTX_free(storeCtx);
  46405. X509_free(cert);
  46406. X509_free(ca);
  46407. res = TEST_RES_CHECK(1);
  46408. #endif
  46409. return res;
  46410. }
  46411. /*----------------------------------------------------------------------------*
  46412. | Certificate Failure Checks
  46413. *----------------------------------------------------------------------------*/
  46414. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  46415. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  46416. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  46417. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  46418. int type)
  46419. {
  46420. int ret;
  46421. WOLFSSL_CERT_MANAGER* cm = NULL;
  46422. switch (type) {
  46423. case TESTING_RSA:
  46424. #ifdef NO_RSA
  46425. fprintf(stderr, "RSA disabled, skipping test\n");
  46426. return ASN_SIG_CONFIRM_E;
  46427. #else
  46428. break;
  46429. #endif
  46430. case TESTING_ECC:
  46431. #ifndef HAVE_ECC
  46432. fprintf(stderr, "ECC disabled, skipping test\n");
  46433. return ASN_SIG_CONFIRM_E;
  46434. #else
  46435. break;
  46436. #endif
  46437. default:
  46438. fprintf(stderr, "Bad function argument\n");
  46439. return BAD_FUNC_ARG;
  46440. }
  46441. cm = wolfSSL_CertManagerNew();
  46442. if (cm == NULL) {
  46443. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  46444. return -1;
  46445. }
  46446. #ifndef NO_FILESYSTEM
  46447. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  46448. if (ret != WOLFSSL_SUCCESS) {
  46449. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  46450. wolfSSL_CertManagerFree(cm);
  46451. return ret;
  46452. }
  46453. #else
  46454. (void)ca;
  46455. #endif
  46456. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  46457. /* Let AssertIntEQ handle return code */
  46458. wolfSSL_CertManagerFree(cm);
  46459. return ret;
  46460. }
  46461. #if !defined(NO_FILESYSTEM)
  46462. static int test_RsaSigFailure_cm(void)
  46463. {
  46464. int ret = 0;
  46465. const char* ca_cert = "./certs/ca-cert.pem";
  46466. const char* server_cert = "./certs/server-cert.der";
  46467. byte* cert_buf = NULL;
  46468. size_t cert_sz = 0;
  46469. ret = load_file(server_cert, &cert_buf, &cert_sz);
  46470. if (ret == 0) {
  46471. /* corrupt DER - invert last byte, which is signature */
  46472. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46473. /* test bad cert */
  46474. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  46475. }
  46476. if (cert_buf)
  46477. free(cert_buf);
  46478. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46479. if (ret == WOLFSSL_FATAL_ERROR) {
  46480. ret = 0;
  46481. }
  46482. #else
  46483. if (ret == ASN_SIG_CONFIRM_E) {
  46484. ret = 0;
  46485. }
  46486. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  46487. return TEST_RES_CHECK(ret == 0);
  46488. }
  46489. static int test_EccSigFailure_cm(void)
  46490. {
  46491. int ret = 0;
  46492. /* self-signed ECC cert, so use server cert as CA */
  46493. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  46494. const char* server_cert = "./certs/server-ecc.der";
  46495. byte* cert_buf = NULL;
  46496. size_t cert_sz = 0;
  46497. ret = load_file(server_cert, &cert_buf, &cert_sz);
  46498. if (ret == 0) {
  46499. /* corrupt DER - invert last byte, which is signature */
  46500. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46501. /* test bad cert */
  46502. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  46503. }
  46504. if (cert_buf)
  46505. free(cert_buf);
  46506. #ifdef FP_ECC
  46507. wc_ecc_fp_free();
  46508. #endif
  46509. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46510. if (ret == WOLFSSL_FATAL_ERROR) {
  46511. ret = 0;
  46512. }
  46513. #else
  46514. if (ret == ASN_SIG_CONFIRM_E) {
  46515. ret = 0;
  46516. }
  46517. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  46518. return TEST_RES_CHECK(ret == 0);
  46519. }
  46520. #endif /* !NO_FILESYSTEM */
  46521. #endif /* NO_CERTS */
  46522. #ifdef WOLFSSL_TLS13
  46523. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  46524. #ifdef WC_SHA384_DIGEST_SIZE
  46525. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  46526. #else
  46527. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  46528. #endif
  46529. #endif
  46530. #ifdef WOLFSSL_EARLY_DATA
  46531. static const char earlyData[] = "Early Data";
  46532. static char earlyDataBuffer[1];
  46533. #endif
  46534. static int test_tls13_apis(void)
  46535. {
  46536. int ret = 0;
  46537. #ifndef WOLFSSL_NO_TLS12
  46538. #ifndef NO_WOLFSSL_CLIENT
  46539. WOLFSSL_CTX* clientTls12Ctx;
  46540. WOLFSSL* clientTls12Ssl;
  46541. #endif
  46542. #ifndef NO_WOLFSSL_SERVER
  46543. WOLFSSL_CTX* serverTls12Ctx;
  46544. WOLFSSL* serverTls12Ssl;
  46545. #endif
  46546. #endif
  46547. #ifndef NO_WOLFSSL_CLIENT
  46548. WOLFSSL_CTX* clientCtx;
  46549. WOLFSSL* clientSsl;
  46550. #endif
  46551. #ifndef NO_WOLFSSL_SERVER
  46552. WOLFSSL_CTX* serverCtx;
  46553. WOLFSSL* serverSsl;
  46554. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46555. const char* ourCert = svrCertFile;
  46556. const char* ourKey = svrKeyFile;
  46557. #endif
  46558. #endif
  46559. int required;
  46560. #ifdef WOLFSSL_EARLY_DATA
  46561. int outSz;
  46562. #endif
  46563. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  46564. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  46565. #ifdef HAVE_PQC
  46566. WOLFSSL_KYBER_LEVEL1
  46567. #else
  46568. WOLFSSL_ECC_SECP256R1
  46569. #endif
  46570. };
  46571. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  46572. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  46573. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  46574. int numGroups = 2;
  46575. #endif
  46576. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46577. char groupList[] =
  46578. #ifndef NO_ECC_SECP
  46579. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  46580. "P-521:"
  46581. #endif
  46582. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  46583. "P-384:"
  46584. #endif
  46585. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  46586. "P-256"
  46587. #ifdef HAVE_PQC
  46588. ":P256_KYBER_LEVEL1"
  46589. #endif
  46590. #endif
  46591. #ifdef HAVE_PQC
  46592. ":KYBER_LEVEL1"
  46593. #endif
  46594. "";
  46595. #endif /* !defined(NO_ECC_SECP) */
  46596. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46597. (void)ret;
  46598. #ifndef WOLFSSL_NO_TLS12
  46599. #ifndef NO_WOLFSSL_CLIENT
  46600. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  46601. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  46602. #endif
  46603. #ifndef NO_WOLFSSL_SERVER
  46604. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  46605. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46606. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  46607. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  46608. #endif
  46609. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  46610. #endif
  46611. #endif
  46612. #ifndef NO_WOLFSSL_CLIENT
  46613. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  46614. clientSsl = wolfSSL_new(clientCtx);
  46615. #endif
  46616. #ifndef NO_WOLFSSL_SERVER
  46617. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  46618. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46619. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  46620. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  46621. #endif
  46622. serverSsl = wolfSSL_new(serverCtx);
  46623. AssertNotNull(serverSsl);
  46624. #endif
  46625. #ifdef WOLFSSL_SEND_HRR_COOKIE
  46626. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  46627. #ifndef NO_WOLFSSL_CLIENT
  46628. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  46629. #endif
  46630. #ifndef NO_WOLFSSL_SERVER
  46631. #ifndef WOLFSSL_NO_TLS12
  46632. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  46633. #endif
  46634. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  46635. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  46636. WOLFSSL_SUCCESS);
  46637. #endif
  46638. #endif
  46639. #ifdef HAVE_SUPPORTED_CURVES
  46640. #ifdef HAVE_ECC
  46641. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  46642. #ifndef NO_WOLFSSL_SERVER
  46643. do {
  46644. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  46645. #ifdef WOLFSSL_ASYNC_CRYPT
  46646. if (ret == WC_PENDING_E)
  46647. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  46648. #endif
  46649. } while (ret == WC_PENDING_E);
  46650. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  46651. #endif
  46652. #ifndef NO_WOLFSSL_CLIENT
  46653. #ifndef WOLFSSL_NO_TLS12
  46654. do {
  46655. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  46656. #ifdef WOLFSSL_ASYNC_CRYPT
  46657. if (ret == WC_PENDING_E)
  46658. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  46659. #endif
  46660. } while (ret == WC_PENDING_E);
  46661. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  46662. #endif
  46663. do {
  46664. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  46665. #ifdef WOLFSSL_ASYNC_CRYPT
  46666. if (ret == WC_PENDING_E)
  46667. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  46668. #endif
  46669. } while (ret == WC_PENDING_E);
  46670. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  46671. #endif
  46672. #elif defined(HAVE_CURVE25519)
  46673. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  46674. #ifndef NO_WOLFSSL_SERVER
  46675. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  46676. WOLFSSL_SUCCESS);
  46677. #endif
  46678. #ifndef NO_WOLFSSL_CLIENT
  46679. #ifndef WOLFSSL_NO_TLS12
  46680. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  46681. WOLFSSL_SUCCESS);
  46682. #endif
  46683. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  46684. WOLFSSL_SUCCESS);
  46685. #endif
  46686. #elif defined(HAVE_CURVE448)
  46687. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  46688. #ifndef NO_WOLFSSL_SERVER
  46689. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  46690. WOLFSSL_SUCCESS);
  46691. #endif
  46692. #ifndef NO_WOLFSSL_CLIENT
  46693. #ifndef WOLFSSL_NO_TLS12
  46694. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  46695. WOLFSSL_SUCCESS);
  46696. #endif
  46697. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  46698. WOLFSSL_SUCCESS);
  46699. #endif
  46700. #else
  46701. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  46702. #ifndef NO_WOLFSSL_CLIENT
  46703. #ifndef WOLFSSL_NO_TLS12
  46704. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  46705. NOT_COMPILED_IN);
  46706. #endif
  46707. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  46708. NOT_COMPILED_IN);
  46709. #endif
  46710. #endif
  46711. #if defined(HAVE_PQC)
  46712. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  46713. #ifndef NO_WOLFSSL_SERVER
  46714. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  46715. WOLFSSL_SUCCESS);
  46716. #endif
  46717. #ifndef NO_WOLFSSL_CLIENT
  46718. #ifndef WOLFSSL_NO_TLS12
  46719. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  46720. BAD_FUNC_ARG);
  46721. #endif
  46722. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  46723. WOLFSSL_SUCCESS);
  46724. #endif
  46725. #endif
  46726. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  46727. #ifndef NO_WOLFSSL_SERVER
  46728. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  46729. #endif
  46730. #ifndef NO_WOLFSSL_CLIENT
  46731. #ifndef WOLFSSL_NO_TLS12
  46732. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  46733. #endif
  46734. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  46735. #endif
  46736. #endif /* HAVE_SUPPORTED_CURVES */
  46737. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46738. #ifndef NO_WOLFSSL_CLIENT
  46739. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  46740. #endif
  46741. #ifndef NO_WOLFSSL_SERVER
  46742. #ifndef WOLFSSL_NO_TLS12
  46743. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  46744. #endif
  46745. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  46746. #endif
  46747. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46748. #ifndef NO_WOLFSSL_CLIENT
  46749. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  46750. #endif
  46751. #ifndef NO_WOLFSSL_SERVER
  46752. #ifndef WOLFSSL_NO_TLS12
  46753. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  46754. #endif
  46755. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  46756. #endif
  46757. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46758. #ifndef NO_WOLFSSL_CLIENT
  46759. #ifndef WOLFSSL_NO_TLS12
  46760. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  46761. #endif
  46762. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  46763. #endif
  46764. #ifndef NO_WOLFSSL_SERVER
  46765. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  46766. #endif
  46767. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46768. #ifndef NO_WOLFSSL_CLIENT
  46769. #ifndef WOLFSSL_NO_TLS12
  46770. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  46771. #endif
  46772. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  46773. #endif
  46774. #ifndef NO_WOLFSSL_SERVER
  46775. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  46776. #endif
  46777. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  46778. #ifndef NO_WOLFSSL_CLIENT
  46779. #ifndef WOLFSSL_NO_TLS12
  46780. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  46781. #endif
  46782. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  46783. #endif
  46784. #ifndef NO_WOLFSSL_SERVER
  46785. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  46786. #endif
  46787. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  46788. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  46789. #ifndef NO_WOLFSSL_CLIENT
  46790. #ifndef WOLFSSL_NO_TLS12
  46791. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  46792. BAD_FUNC_ARG);
  46793. #endif
  46794. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  46795. #endif
  46796. #ifndef NO_WOLFSSL_SERVER
  46797. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  46798. #endif
  46799. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  46800. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46801. #ifndef NO_WOLFSSL_SERVER
  46802. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  46803. #endif
  46804. #ifndef NO_WOLFSSL_CLIENT
  46805. #ifndef WOLFSSL_NO_TLS12
  46806. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  46807. BAD_FUNC_ARG);
  46808. #endif
  46809. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  46810. #endif
  46811. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46812. #ifndef NO_WOLFSSL_SERVER
  46813. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  46814. #endif
  46815. #ifndef NO_WOLFSSL_CLIENT
  46816. #ifndef WOLFSSL_NO_TLS12
  46817. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  46818. BAD_FUNC_ARG);
  46819. #endif
  46820. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  46821. #endif
  46822. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  46823. #ifndef NO_WOLFSSL_CLIENT
  46824. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  46825. #endif
  46826. #ifndef NO_WOLFSSL_SERVER
  46827. #ifndef WOLFSSL_NO_TLS12
  46828. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  46829. BAD_FUNC_ARG);
  46830. #endif
  46831. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  46832. #endif
  46833. #endif
  46834. #ifdef HAVE_ECC
  46835. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  46836. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  46837. #ifndef NO_WOLFSSL_SERVER
  46838. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  46839. #endif
  46840. #ifndef NO_WOLFSSL_CLIENT
  46841. #ifndef WOLFSSL_NO_TLS12
  46842. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  46843. #endif
  46844. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  46845. #endif
  46846. #endif
  46847. #ifdef HAVE_SUPPORTED_CURVES
  46848. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46849. #ifndef NO_WOLFSSL_CLIENT
  46850. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  46851. #endif
  46852. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46853. #ifndef NO_WOLFSSL_CLIENT
  46854. #ifndef WOLFSSL_NO_TLS12
  46855. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  46856. BAD_FUNC_ARG);
  46857. #endif
  46858. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  46859. WOLFSSL_MAX_GROUP_COUNT + 1),
  46860. BAD_FUNC_ARG);
  46861. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  46862. WOLFSSL_SUCCESS);
  46863. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  46864. BAD_FUNC_ARG);
  46865. #endif
  46866. #ifndef NO_WOLFSSL_SERVER
  46867. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  46868. WOLFSSL_SUCCESS);
  46869. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  46870. BAD_FUNC_ARG);
  46871. #endif
  46872. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46873. #ifndef NO_WOLFSSL_CLIENT
  46874. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  46875. #endif
  46876. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46877. #ifndef NO_WOLFSSL_CLIENT
  46878. #ifndef WOLFSSL_NO_TLS12
  46879. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  46880. BAD_FUNC_ARG);
  46881. #endif
  46882. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  46883. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  46884. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  46885. WOLFSSL_SUCCESS);
  46886. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  46887. BAD_FUNC_ARG);
  46888. #endif
  46889. #ifndef NO_WOLFSSL_SERVER
  46890. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  46891. WOLFSSL_SUCCESS);
  46892. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  46893. BAD_FUNC_ARG);
  46894. #endif
  46895. #ifdef OPENSSL_EXTRA
  46896. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46897. #ifndef NO_WOLFSSL_CLIENT
  46898. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  46899. #endif
  46900. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  46901. #ifndef NO_WOLFSSL_CLIENT
  46902. #ifndef WOLFSSL_NO_TLS12
  46903. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  46904. WOLFSSL_FAILURE);
  46905. #endif
  46906. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  46907. WOLFSSL_SUCCESS);
  46908. #endif
  46909. #ifndef NO_WOLFSSL_SERVER
  46910. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  46911. WOLFSSL_SUCCESS);
  46912. #endif
  46913. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46914. #ifndef NO_WOLFSSL_CLIENT
  46915. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  46916. #endif
  46917. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  46918. #ifndef NO_WOLFSSL_CLIENT
  46919. #ifndef WOLFSSL_NO_TLS12
  46920. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  46921. WOLFSSL_FAILURE);
  46922. #endif
  46923. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  46924. WOLFSSL_SUCCESS);
  46925. #endif
  46926. #ifndef NO_WOLFSSL_SERVER
  46927. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  46928. WOLFSSL_SUCCESS);
  46929. #endif
  46930. #endif /* OPENSSL_EXTRA */
  46931. #endif /* HAVE_SUPPORTED_CURVES */
  46932. #endif /* HAVE_ECC */
  46933. #ifdef WOLFSSL_EARLY_DATA
  46934. #ifndef OPENSSL_EXTRA
  46935. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46936. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  46937. #else
  46938. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46939. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  46940. #endif
  46941. #ifndef NO_WOLFSSL_CLIENT
  46942. #ifndef OPENSSL_EXTRA
  46943. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  46944. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  46945. #else
  46946. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  46947. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  46948. #endif
  46949. #endif
  46950. #ifndef NO_WOLFSSL_SERVER
  46951. #ifndef WOLFSSL_NO_TLS12
  46952. #ifndef OPENSSL_EXTRA
  46953. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  46954. BAD_FUNC_ARG);
  46955. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  46956. #else
  46957. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  46958. BAD_FUNC_ARG);
  46959. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  46960. #endif
  46961. #endif
  46962. #ifndef OPENSSL_EXTRA
  46963. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46964. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), WOLFSSL_SUCCESS);
  46965. #else
  46966. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  46967. #endif
  46968. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  46969. #else
  46970. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  46971. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  46972. #endif
  46973. #endif
  46974. #ifndef OPENSSL_EXTRA
  46975. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46976. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  46977. #else
  46978. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46979. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  46980. #endif
  46981. #ifndef NO_WOLFSSL_CLIENT
  46982. #ifndef OPENSSL_EXTRA
  46983. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46984. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  46985. #else
  46986. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  46987. #endif
  46988. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  46989. #else
  46990. AssertIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  46991. AssertIntEQ(SSL_get_max_early_data(clientSsl), 17);
  46992. #endif
  46993. #endif
  46994. #ifndef NO_WOLFSSL_SERVER
  46995. #ifndef WOLFSSL_NO_TLS12
  46996. #ifndef OPENSSL_EXTRA
  46997. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  46998. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  46999. #else
  47000. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  47001. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  47002. #endif
  47003. #endif
  47004. #ifndef OPENSSL_EXTRA
  47005. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47006. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  47007. #else
  47008. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  47009. #endif
  47010. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  47011. #else
  47012. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  47013. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  47014. #endif
  47015. #endif
  47016. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  47017. &outSz), BAD_FUNC_ARG);
  47018. #ifndef NO_WOLFSSL_CLIENT
  47019. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  47020. &outSz), BAD_FUNC_ARG);
  47021. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  47022. BAD_FUNC_ARG);
  47023. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  47024. sizeof(earlyData), NULL),
  47025. BAD_FUNC_ARG);
  47026. #endif
  47027. #ifndef NO_WOLFSSL_SERVER
  47028. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  47029. sizeof(earlyData), &outSz),
  47030. SIDE_ERROR);
  47031. #endif
  47032. #ifndef NO_WOLFSSL_CLIENT
  47033. #ifndef WOLFSSL_NO_TLS12
  47034. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  47035. sizeof(earlyData), &outSz),
  47036. BAD_FUNC_ARG);
  47037. #endif
  47038. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  47039. sizeof(earlyData), &outSz),
  47040. WOLFSSL_FATAL_ERROR);
  47041. #endif
  47042. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  47043. sizeof(earlyDataBuffer), &outSz),
  47044. BAD_FUNC_ARG);
  47045. #ifndef NO_WOLFSSL_SERVER
  47046. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  47047. sizeof(earlyDataBuffer), &outSz),
  47048. BAD_FUNC_ARG);
  47049. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  47050. BAD_FUNC_ARG);
  47051. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  47052. sizeof(earlyDataBuffer), NULL),
  47053. BAD_FUNC_ARG);
  47054. #endif
  47055. #ifndef NO_WOLFSSL_CLIENT
  47056. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  47057. sizeof(earlyDataBuffer), &outSz),
  47058. SIDE_ERROR);
  47059. #endif
  47060. #ifndef NO_WOLFSSL_SERVER
  47061. #ifndef WOLFSSL_NO_TLS12
  47062. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  47063. sizeof(earlyDataBuffer), &outSz),
  47064. BAD_FUNC_ARG);
  47065. #endif
  47066. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  47067. sizeof(earlyDataBuffer), &outSz),
  47068. WOLFSSL_FATAL_ERROR);
  47069. #endif
  47070. #endif
  47071. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  47072. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  47073. #endif
  47074. #ifndef NO_WOLFSSL_SERVER
  47075. wolfSSL_free(serverSsl);
  47076. wolfSSL_CTX_free(serverCtx);
  47077. #endif
  47078. #ifndef NO_WOLFSSL_CLIENT
  47079. wolfSSL_free(clientSsl);
  47080. wolfSSL_CTX_free(clientCtx);
  47081. #endif
  47082. #ifndef WOLFSSL_NO_TLS12
  47083. #ifndef NO_WOLFSSL_SERVER
  47084. wolfSSL_free(serverTls12Ssl);
  47085. wolfSSL_CTX_free(serverTls12Ctx);
  47086. #endif
  47087. #ifndef NO_WOLFSSL_CLIENT
  47088. wolfSSL_free(clientTls12Ssl);
  47089. wolfSSL_CTX_free(clientTls12Ctx);
  47090. #endif
  47091. #endif
  47092. return TEST_RES_CHECK(1);
  47093. }
  47094. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  47095. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  47096. defined(BUILD_TLS_AES_256_GCM_SHA384)
  47097. /* Called when writing. */
  47098. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  47099. {
  47100. (void)ssl;
  47101. (void)buf;
  47102. (void)sz;
  47103. (void)ctx;
  47104. /* Force error return from wolfSSL_accept_TLSv13(). */
  47105. return WANT_WRITE;
  47106. }
  47107. /* Called when reading. */
  47108. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  47109. {
  47110. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  47111. int len = (int)msg->length;
  47112. (void)ssl;
  47113. (void)sz;
  47114. /* Pass back as much of message as will fit in buffer. */
  47115. if (len > sz)
  47116. len = sz;
  47117. XMEMCPY(buf, msg->buffer, len);
  47118. /* Move over returned data. */
  47119. msg->buffer += len;
  47120. msg->length -= len;
  47121. /* Amount actually copied. */
  47122. return len;
  47123. }
  47124. #endif
  47125. static int test_tls13_cipher_suites(void)
  47126. {
  47127. int res = TEST_SKIPPED;
  47128. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  47129. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  47130. defined(BUILD_TLS_AES_256_GCM_SHA384)
  47131. WOLFSSL_CTX* ctx;
  47132. WOLFSSL *ssl;
  47133. int i;
  47134. byte clientHello[] = {
  47135. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  47136. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  47137. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  47138. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  47139. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  47140. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  47141. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  47142. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  47143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  47145. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  47146. 0x13, 0x01,
  47147. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  47148. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  47149. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  47150. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  47151. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  47152. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  47153. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  47154. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  47155. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  47156. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  47157. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  47158. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  47159. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  47160. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  47161. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  47162. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  47163. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  47164. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  47165. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  47166. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  47167. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  47168. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  47169. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  47170. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  47171. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  47172. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  47173. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  47174. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  47175. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  47176. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  47177. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  47178. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  47179. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  47180. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  47181. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  47182. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47183. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47184. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  47185. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  47186. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  47187. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  47188. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  47189. };
  47190. WOLFSSL_BUFFER_INFO msg;
  47191. /* Offset into ClientHello message data of first cipher suite. */
  47192. const int csOff = 78;
  47193. /* Server cipher list. */
  47194. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  47195. /* Suite list with duplicates. */
  47196. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  47197. "TLS13-AES128-GCM-SHA256:"
  47198. "TLS13-AES256-GCM-SHA384:"
  47199. "TLS13-AES256-GCM-SHA384:"
  47200. "TLS13-AES128-GCM-SHA256";
  47201. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  47202. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  47203. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  47204. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  47205. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  47206. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  47207. #endif
  47208. /* Set up wolfSSL context. */
  47209. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47210. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  47211. WOLFSSL_FILETYPE_PEM));
  47212. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  47213. WOLFSSL_FILETYPE_PEM));
  47214. /* Read from 'msg'. */
  47215. wolfSSL_SetIORecv(ctx, CsRecv);
  47216. /* No where to send to - dummy sender. */
  47217. wolfSSL_SetIOSend(ctx, CsSend);
  47218. /* Test cipher suite list with many copies of a cipher suite. */
  47219. AssertNotNull(ssl = wolfSSL_new(ctx));
  47220. msg.buffer = clientHello;
  47221. msg.length = (unsigned int)sizeof(clientHello);
  47222. wolfSSL_SetIOReadCtx(ssl, &msg);
  47223. /* Force server to have as many occurrences of same cipher suite as
  47224. * possible. */
  47225. {
  47226. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  47227. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  47228. for (i = 0; i < suites->suiteSz; i += 2) {
  47229. suites->suites[i + 0] = TLS13_BYTE;
  47230. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  47231. }
  47232. }
  47233. /* Test multiple occurrences of same cipher suite. */
  47234. wolfSSL_accept_TLSv13(ssl);
  47235. wolfSSL_free(ssl);
  47236. /* Set client order opposite to server order:
  47237. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  47238. clientHello[csOff + 0] = TLS13_BYTE;
  47239. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  47240. clientHello[csOff + 2] = TLS13_BYTE;
  47241. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  47242. /* Test server order negotiation. */
  47243. AssertNotNull(ssl = wolfSSL_new(ctx));
  47244. msg.buffer = clientHello;
  47245. msg.length = (unsigned int)sizeof(clientHello);
  47246. wolfSSL_SetIOReadCtx(ssl, &msg);
  47247. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  47248. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  47249. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  47250. wolfSSL_accept_TLSv13(ssl);
  47251. /* Check refined order - server order. */
  47252. AssertIntEQ(ssl->suites->suiteSz, 4);
  47253. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  47254. AssertIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  47255. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  47256. AssertIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  47257. wolfSSL_free(ssl);
  47258. /* Test client order negotiation. */
  47259. AssertNotNull(ssl = wolfSSL_new(ctx));
  47260. msg.buffer = clientHello;
  47261. msg.length = (unsigned int)sizeof(clientHello);
  47262. wolfSSL_SetIOReadCtx(ssl, &msg);
  47263. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  47264. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  47265. AssertIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  47266. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  47267. wolfSSL_accept_TLSv13(ssl);
  47268. /* Check refined order - client order. */
  47269. AssertIntEQ(ssl->suites->suiteSz, 4);
  47270. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  47271. AssertIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  47272. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  47273. AssertIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  47274. wolfSSL_free(ssl);
  47275. /* Check duplicate detection is working. */
  47276. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  47277. AssertIntEQ(ctx->suites->suiteSz, 4);
  47278. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  47279. AssertIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  47280. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  47281. AssertIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  47282. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  47283. AssertIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  47284. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  47285. AssertIntEQ(ctx->suites->suiteSz, 4);
  47286. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  47287. AssertIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  47288. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  47289. AssertIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  47290. #endif
  47291. wolfSSL_CTX_free(ctx);
  47292. res = TEST_RES_CHECK(1);
  47293. #endif
  47294. return res;
  47295. }
  47296. #endif
  47297. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  47298. !defined(NO_OLD_TLS))
  47299. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  47300. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  47301. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47302. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  47303. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  47304. const unsigned char* priv, unsigned int privSz,
  47305. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  47306. unsigned char* out, unsigned int* outlen,
  47307. void* ctx)
  47308. {
  47309. int result;
  47310. /* Test fail when context associated with WOLFSSL is NULL */
  47311. if (ctx == NULL) {
  47312. return -1;
  47313. }
  47314. (void)ssl;
  47315. /* return 0 on success */
  47316. PRIVATE_KEY_UNLOCK();
  47317. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  47318. PRIVATE_KEY_LOCK();
  47319. return result;
  47320. }
  47321. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  47322. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  47323. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  47324. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  47325. WOLFSSL_SUCCESS);
  47326. #endif
  47327. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  47328. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  47329. WOLFSSL_SUCCESS);
  47330. #endif
  47331. }
  47332. static void test_dh_ssl_setup(WOLFSSL* ssl)
  47333. {
  47334. static int dh_test_ctx = 1;
  47335. int ret;
  47336. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  47337. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  47338. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  47339. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  47340. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  47341. }
  47342. }
  47343. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  47344. {
  47345. int ret;
  47346. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  47347. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  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. #endif
  47354. static int test_DhCallbacks(void)
  47355. {
  47356. int res = TEST_SKIPPED;
  47357. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  47358. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  47359. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47360. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  47361. WOLFSSL_CTX *ctx;
  47362. WOLFSSL *ssl;
  47363. tcp_ready ready;
  47364. func_args server_args;
  47365. func_args client_args;
  47366. THREAD_TYPE serverThread;
  47367. callback_functions func_cb_client;
  47368. callback_functions func_cb_server;
  47369. int test;
  47370. #ifndef NO_WOLFSSL_CLIENT
  47371. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  47372. #else
  47373. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47374. #endif
  47375. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  47376. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  47377. /* load client ca cert */
  47378. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  47379. WOLFSSL_SUCCESS);
  47380. /* test with NULL arguments */
  47381. wolfSSL_SetDhAgreeCtx(NULL, &test);
  47382. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  47383. /* test success case */
  47384. test = 1;
  47385. AssertNotNull(ssl = wolfSSL_new(ctx));
  47386. wolfSSL_SetDhAgreeCtx(ssl, &test);
  47387. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  47388. wolfSSL_free(ssl);
  47389. wolfSSL_CTX_free(ctx);
  47390. /* test a connection where callback is used */
  47391. #ifdef WOLFSSL_TIRTOS
  47392. fdOpenSession(Task_self());
  47393. #endif
  47394. XMEMSET(&server_args, 0, sizeof(func_args));
  47395. XMEMSET(&client_args, 0, sizeof(func_args));
  47396. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  47397. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  47398. StartTCP();
  47399. InitTcpReady(&ready);
  47400. #if defined(USE_WINDOWS_API)
  47401. /* use RNG to get random port if using windows */
  47402. ready.port = GetRandomPort();
  47403. #endif
  47404. server_args.signal = &ready;
  47405. client_args.signal = &ready;
  47406. server_args.return_code = TEST_FAIL;
  47407. client_args.return_code = TEST_FAIL;
  47408. /* set callbacks to use DH functions */
  47409. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47410. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  47411. #ifndef WOLFSSL_NO_TLS12
  47412. func_cb_client.method = wolfTLSv1_2_client_method;
  47413. #else
  47414. func_cb_client.method = wolfTLSv1_3_client_method;
  47415. #endif
  47416. client_args.callbacks = &func_cb_client;
  47417. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47418. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  47419. #ifndef WOLFSSL_NO_TLS12
  47420. func_cb_server.method = wolfTLSv1_2_server_method;
  47421. #else
  47422. func_cb_server.method = wolfTLSv1_3_server_method;
  47423. #endif
  47424. server_args.callbacks = &func_cb_server;
  47425. start_thread(test_server_nofail, &server_args, &serverThread);
  47426. wait_tcp_ready(&server_args);
  47427. test_client_nofail(&client_args, NULL);
  47428. join_thread(serverThread);
  47429. AssertTrue(client_args.return_code);
  47430. AssertTrue(server_args.return_code);
  47431. FreeTcpReady(&ready);
  47432. #ifdef WOLFSSL_TIRTOS
  47433. fdOpenSession(Task_self());
  47434. #endif
  47435. /* now set user ctx to not be 1 so that the callback returns fail case */
  47436. #ifdef WOLFSSL_TIRTOS
  47437. fdOpenSession(Task_self());
  47438. #endif
  47439. XMEMSET(&server_args, 0, sizeof(func_args));
  47440. XMEMSET(&client_args, 0, sizeof(func_args));
  47441. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  47442. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  47443. StartTCP();
  47444. InitTcpReady(&ready);
  47445. #if defined(USE_WINDOWS_API)
  47446. /* use RNG to get random port if using windows */
  47447. ready.port = GetRandomPort();
  47448. #endif
  47449. server_args.signal = &ready;
  47450. client_args.signal = &ready;
  47451. server_args.return_code = TEST_FAIL;
  47452. client_args.return_code = TEST_FAIL;
  47453. /* set callbacks to use DH functions */
  47454. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47455. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  47456. #ifndef WOLFSSL_NO_TLS12
  47457. func_cb_client.method = wolfTLSv1_2_client_method;
  47458. #else
  47459. func_cb_client.method = wolfTLSv1_3_client_method;
  47460. #endif
  47461. client_args.callbacks = &func_cb_client;
  47462. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47463. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  47464. #ifndef WOLFSSL_NO_TLS12
  47465. func_cb_server.method = wolfTLSv1_2_server_method;
  47466. #else
  47467. func_cb_server.method = wolfTLSv1_3_server_method;
  47468. #endif
  47469. server_args.callbacks = &func_cb_server;
  47470. start_thread(test_server_nofail, &server_args, &serverThread);
  47471. wait_tcp_ready(&server_args);
  47472. test_client_nofail(&client_args, NULL);
  47473. join_thread(serverThread);
  47474. AssertIntEQ(client_args.return_code, TEST_FAIL);
  47475. AssertIntEQ(server_args.return_code, TEST_FAIL);
  47476. FreeTcpReady(&ready);
  47477. #ifdef WOLFSSL_TIRTOS
  47478. fdOpenSession(Task_self());
  47479. #endif
  47480. res = TEST_RES_CHECK(1);
  47481. #endif
  47482. return res;
  47483. }
  47484. #endif /* HAVE_PK_CALLBACKS */
  47485. #ifdef HAVE_HASHDRBG
  47486. #ifdef TEST_RESEED_INTERVAL
  47487. static int test_wc_RNG_GenerateBlock_Reseed(void)
  47488. {
  47489. int i, ret;
  47490. WC_RNG rng;
  47491. byte key[32];
  47492. ret = wc_InitRng(&rng);
  47493. if (ret == 0) {
  47494. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  47495. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  47496. if (ret != 0) {
  47497. break;
  47498. }
  47499. }
  47500. }
  47501. wc_FreeRng(&rng);
  47502. return TEST_RES_CHECK(ret == 0);
  47503. }
  47504. #endif /* TEST_RESEED_INTERVAL */
  47505. static int test_wc_RNG_GenerateBlock(void)
  47506. {
  47507. int i, ret;
  47508. WC_RNG rng;
  47509. byte key[32];
  47510. ret = wc_InitRng(&rng);
  47511. if (ret == 0) {
  47512. for (i = 0; i < 10; i++) {
  47513. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  47514. if (ret != 0) {
  47515. break;
  47516. }
  47517. }
  47518. }
  47519. wc_FreeRng(&rng);
  47520. (void)rng; /* for WC_NO_RNG case */
  47521. (void)key;
  47522. return TEST_RES_CHECK(ret == 0);
  47523. }
  47524. #endif
  47525. /*
  47526. * Testing get_rand_digit
  47527. */
  47528. static int test_get_rand_digit(void)
  47529. {
  47530. int res = TEST_SKIPPED;
  47531. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  47532. int ret = 0;
  47533. WC_RNG rng;
  47534. mp_digit d;
  47535. ret = wc_InitRng(&rng);
  47536. if (ret == 0) {
  47537. ret = get_rand_digit(&rng, &d);
  47538. }
  47539. if (ret == 0) {
  47540. ret = get_rand_digit(NULL, NULL);
  47541. if (ret == BAD_FUNC_ARG) {
  47542. ret = 0;
  47543. }
  47544. }
  47545. if (ret == 0) {
  47546. ret = get_rand_digit(NULL, &d);
  47547. if (ret == BAD_FUNC_ARG) {
  47548. ret = 0;
  47549. }
  47550. }
  47551. if (ret == 0) {
  47552. ret = get_rand_digit(&rng, NULL);
  47553. if (ret == BAD_FUNC_ARG) {
  47554. ret = 0;
  47555. }
  47556. }
  47557. if (ret == 0) {
  47558. ret = wc_FreeRng(&rng);
  47559. }
  47560. res = TEST_RES_CHECK(ret == 0);
  47561. #endif
  47562. return res;
  47563. }/* End test_get_rand_digit*/
  47564. /*
  47565. * Testing get_digit_count
  47566. */
  47567. static int test_get_digit_count(void)
  47568. {
  47569. int res = TEST_SKIPPED;
  47570. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  47571. int ret = 0;
  47572. mp_int a;
  47573. if (mp_init(&a) != MP_OKAY) {
  47574. ret = -1;
  47575. }
  47576. if (ret == 0) {
  47577. ret = get_digit_count(NULL);
  47578. }
  47579. if (ret == 0) {
  47580. ret = get_digit_count(&a);
  47581. }
  47582. mp_clear(&a);
  47583. res = TEST_RES_CHECK(ret == 0);
  47584. #endif
  47585. return res;
  47586. }/* End test_get_digit_count*/
  47587. /*
  47588. * Testing mp_cond_copy
  47589. */
  47590. static int test_mp_cond_copy(void)
  47591. {
  47592. int res = TEST_SKIPPED;
  47593. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  47594. defined(WOLFSSL_PUBLIC_MP)
  47595. int ret = 0;
  47596. mp_int a;
  47597. mp_int b;
  47598. int copy = 0;
  47599. if (mp_init(&a) != MP_OKAY) {
  47600. ret = -1;
  47601. }
  47602. if (ret == 0) {
  47603. if (mp_init(&b) != MP_OKAY) {
  47604. ret = -1;
  47605. }
  47606. }
  47607. if (ret == 0) {
  47608. ret = mp_cond_copy(NULL, copy, NULL);
  47609. if (ret == BAD_FUNC_ARG) {
  47610. ret = 0;
  47611. }
  47612. }
  47613. if (ret == 0) {
  47614. ret = mp_cond_copy(NULL, copy, &b);
  47615. if (ret == BAD_FUNC_ARG) {
  47616. ret = 0;
  47617. }
  47618. }
  47619. if (ret == 0) {
  47620. ret = mp_cond_copy(&a, copy, NULL);
  47621. if (ret == BAD_FUNC_ARG) {
  47622. ret = 0;
  47623. }
  47624. }
  47625. if (ret == 0) {
  47626. ret = mp_cond_copy(&a, copy, &b);
  47627. }
  47628. mp_clear(&a);
  47629. mp_clear(&b);
  47630. res = TEST_RES_CHECK(ret == 0);
  47631. #endif
  47632. return res;
  47633. }/* End test_mp_cond_copy*/
  47634. /*
  47635. * Testing mp_rand
  47636. */
  47637. static int test_mp_rand(void)
  47638. {
  47639. int res = TEST_SKIPPED;
  47640. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  47641. int ret = 0;
  47642. mp_int a;
  47643. int digits = 1;
  47644. WC_RNG rng;
  47645. if (mp_init(&a) != MP_OKAY) {
  47646. ret = -1;
  47647. }
  47648. if (ret == 0) {
  47649. ret = wc_InitRng(&rng);
  47650. }
  47651. if (ret == 0) {
  47652. ret = mp_rand(&a, digits, NULL);
  47653. if (ret == MISSING_RNG_E) {
  47654. ret = 0;
  47655. }
  47656. }
  47657. if (ret == 0) {
  47658. ret = mp_rand(NULL, digits, &rng);
  47659. if (ret == BAD_FUNC_ARG) {
  47660. ret = 0;
  47661. }
  47662. }
  47663. if (ret == 0) {
  47664. ret = mp_rand(&a, 0, &rng);
  47665. if (ret == BAD_FUNC_ARG) {
  47666. ret = 0;
  47667. }
  47668. }
  47669. if (ret == 0) {
  47670. ret = mp_rand(&a, digits, &rng);
  47671. }
  47672. mp_clear(&a);
  47673. wc_FreeRng(&rng);
  47674. res = TEST_RES_CHECK(ret == 0);
  47675. #endif
  47676. return res;
  47677. }/* End test_mp_rand*/
  47678. /*
  47679. * Testing get_digit
  47680. */
  47681. static int test_get_digit(void)
  47682. {
  47683. int res = TEST_SKIPPED;
  47684. #if defined(WOLFSSL_PUBLIC_MP)
  47685. int ret = 0;
  47686. mp_int a;
  47687. int n = 0;
  47688. if (mp_init(&a) != MP_OKAY) {
  47689. ret = -1;
  47690. }
  47691. if (ret == 0) {
  47692. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  47693. ret = -1;
  47694. }
  47695. }
  47696. if (ret == 0) {
  47697. if (get_digit(NULL, n) == 0) { /* Should hit this */
  47698. ret = 0;
  47699. }
  47700. }
  47701. if (ret == 0) {
  47702. if (get_digit(&a, n) != 0) { /* Should not hit this */
  47703. ret = -1;
  47704. }
  47705. }
  47706. if (ret == 0) {
  47707. if (get_digit(&a, n) == 0) { /* Should hit this */
  47708. ret = 0;
  47709. }
  47710. }
  47711. mp_clear(&a);
  47712. res = TEST_RES_CHECK(ret == 0);
  47713. #endif
  47714. return res;
  47715. }/* End test_get_digit*/
  47716. /*
  47717. * Testing wc_export_int
  47718. */
  47719. static int test_wc_export_int(void)
  47720. {
  47721. int res = TEST_SKIPPED;
  47722. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  47723. defined(WOLFSSL_PUBLIC_MP)
  47724. int ret = 0;
  47725. mp_int mp;
  47726. byte buf[32];
  47727. word32 keySz = (word32)sizeof(buf);
  47728. word32 len = (word32)sizeof(buf);
  47729. if (mp_init(&mp) != MP_OKAY) {
  47730. ret = -1;
  47731. }
  47732. if (ret == 0) {
  47733. ret = mp_set(&mp, 1234);
  47734. }
  47735. if (ret == 0) {
  47736. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  47737. if (ret == BAD_FUNC_ARG) {
  47738. ret = 0;
  47739. }
  47740. }
  47741. if (ret == 0) {
  47742. len = sizeof(buf)-1;
  47743. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  47744. if (ret == BUFFER_E) {
  47745. ret = 0;
  47746. }
  47747. }
  47748. if (ret == 0) {
  47749. len = sizeof(buf);
  47750. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  47751. }
  47752. if (ret == 0) {
  47753. len = 4; /* test input too small */
  47754. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  47755. if (ret == BUFFER_E) {
  47756. ret = 0;
  47757. }
  47758. }
  47759. if (ret == 0) {
  47760. len = sizeof(buf);
  47761. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  47762. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  47763. if (ret == 0 && len != 5) {
  47764. ret = BAD_FUNC_ARG;
  47765. }
  47766. }
  47767. mp_clear(&mp);
  47768. res = TEST_RES_CHECK(ret == 0);
  47769. #endif
  47770. return res;
  47771. }/* End test_wc_export_int*/
  47772. static int test_wc_InitRngNonce(void)
  47773. {
  47774. int res = TEST_SKIPPED;
  47775. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47776. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  47777. int ret;
  47778. WC_RNG rng;
  47779. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47780. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47781. word32 nonceSz = sizeof(nonce);
  47782. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  47783. wc_FreeRng(&rng);
  47784. res = TEST_RES_CHECK(ret == 0);
  47785. #endif
  47786. return res;
  47787. }/* End test_wc_InitRngNonce*/
  47788. /*
  47789. * Testing wc_InitRngNonce_ex
  47790. */
  47791. static int test_wc_InitRngNonce_ex(void)
  47792. {
  47793. int res = TEST_SKIPPED;
  47794. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47795. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  47796. int ret;
  47797. WC_RNG rng;
  47798. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47799. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47800. word32 nonceSz = sizeof(nonce);
  47801. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
  47802. wc_FreeRng(&rng);
  47803. res = TEST_RES_CHECK(ret == 0);
  47804. #endif
  47805. return res;
  47806. }/*End test_wc_InitRngNonce_ex*/
  47807. static int test_wolfSSL_X509_CRL(void)
  47808. {
  47809. int res = TEST_SKIPPED;
  47810. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  47811. X509_CRL *crl;
  47812. char pem[][100] = {
  47813. "./certs/crl/crl.pem",
  47814. "./certs/crl/crl2.pem",
  47815. "./certs/crl/caEccCrl.pem",
  47816. "./certs/crl/eccCliCRL.pem",
  47817. "./certs/crl/eccSrvCRL.pem",
  47818. ""
  47819. };
  47820. #ifndef NO_BIO
  47821. BIO *bio;
  47822. #endif
  47823. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47824. char der[][100] = {
  47825. "./certs/crl/crl.der",
  47826. "./certs/crl/crl2.der",
  47827. ""};
  47828. #endif
  47829. XFILE fp;
  47830. int i;
  47831. for (i = 0; pem[i][0] != '\0'; i++)
  47832. {
  47833. fp = XFOPEN(pem[i], "rb");
  47834. AssertTrue((fp != XBADFILE));
  47835. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  47836. AssertNotNull(crl);
  47837. X509_CRL_free(crl);
  47838. XFCLOSE(fp);
  47839. fp = XFOPEN(pem[i], "rb");
  47840. AssertTrue((fp != XBADFILE));
  47841. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  47842. AssertNotNull(crl);
  47843. X509_CRL_free(crl);
  47844. XFCLOSE(fp);
  47845. }
  47846. #ifndef NO_BIO
  47847. for (i = 0; pem[i][0] != '\0'; i++)
  47848. {
  47849. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  47850. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  47851. X509_CRL_free(crl);
  47852. BIO_free(bio);
  47853. }
  47854. #endif
  47855. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47856. for (i = 0; der[i][0] != '\0'; i++) {
  47857. fp = XFOPEN(der[i], "rb");
  47858. AssertTrue((fp != XBADFILE));
  47859. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  47860. AssertNotNull(crl);
  47861. X509_CRL_free(crl);
  47862. XFCLOSE(fp);
  47863. fp = XFOPEN(der[i], "rb");
  47864. AssertTrue((fp != XBADFILE));
  47865. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  47866. AssertNotNull(crl);
  47867. X509_CRL_free(crl);
  47868. XFCLOSE(fp);
  47869. }
  47870. #endif
  47871. res = TEST_RES_CHECK(1);
  47872. #endif
  47873. return res;
  47874. }
  47875. static int test_wolfSSL_X509_load_crl_file(void)
  47876. {
  47877. int res = TEST_SKIPPED;
  47878. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  47879. !defined(NO_RSA) && !defined(NO_BIO)
  47880. int i;
  47881. char pem[][100] = {
  47882. "./certs/crl/crl.pem",
  47883. "./certs/crl/crl2.pem",
  47884. "./certs/crl/caEccCrl.pem",
  47885. "./certs/crl/eccCliCRL.pem",
  47886. "./certs/crl/eccSrvCRL.pem",
  47887. ""
  47888. };
  47889. char der[][100] = {
  47890. "./certs/crl/crl.der",
  47891. "./certs/crl/crl2.der",
  47892. ""
  47893. };
  47894. WOLFSSL_X509_STORE* store;
  47895. WOLFSSL_X509_LOOKUP* lookup;
  47896. AssertNotNull(store = wolfSSL_X509_STORE_new());
  47897. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47898. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47899. X509_FILETYPE_PEM), 1);
  47900. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47901. X509_FILETYPE_PEM), 1);
  47902. if (store) {
  47903. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47904. WOLFSSL_FILETYPE_PEM), 1);
  47905. /* since store hasn't yet known the revoked cert*/
  47906. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  47907. WOLFSSL_FILETYPE_PEM), 1);
  47908. }
  47909. for (i = 0; pem[i][0] != '\0'; i++)
  47910. {
  47911. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  47912. }
  47913. if (store) {
  47914. /* since store knows crl list */
  47915. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  47916. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  47917. }
  47918. /* once feeing store */
  47919. X509_STORE_free(store);
  47920. store = NULL;
  47921. AssertNotNull(store = wolfSSL_X509_STORE_new());
  47922. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47923. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47924. X509_FILETYPE_PEM), 1);
  47925. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47926. X509_FILETYPE_PEM), 1);
  47927. if (store) {
  47928. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47929. WOLFSSL_FILETYPE_PEM), 1);
  47930. /* since store hasn't yet known the revoked cert*/
  47931. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  47932. WOLFSSL_FILETYPE_PEM), 1);
  47933. }
  47934. for (i = 0; der[i][0] != '\0'; i++)
  47935. {
  47936. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  47937. }
  47938. if (store) {
  47939. /* since store knows crl list */
  47940. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  47941. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  47942. }
  47943. /* test for incorrect parameter */
  47944. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  47945. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  47946. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  47947. X509_STORE_free(store);
  47948. store = NULL;
  47949. res = TEST_RES_CHECK(1);
  47950. #endif
  47951. return res;
  47952. }
  47953. static int test_wolfSSL_d2i_X509_REQ(void)
  47954. {
  47955. int res = TEST_SKIPPED;
  47956. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  47957. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  47958. !defined(WOLFSSL_SP_MATH)
  47959. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  47960. * generated by libest
  47961. * ./certs/csr.attr.der contains sample attributes
  47962. * ./certs/csr.ext.der contains sample extensions */
  47963. const char* csrFile = "./certs/csr.signed.der";
  47964. const char* csrPopFile = "./certs/csr.attr.der";
  47965. const char* csrExtFile = "./certs/csr.ext.der";
  47966. /* ./certs/csr.dsa.pem is generated using
  47967. * openssl req -newkey dsa:certs/dsaparams.pem \
  47968. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  47969. * -outform PEM
  47970. * with the passphrase "wolfSSL"
  47971. */
  47972. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  47973. const char* csrDsaFile = "./certs/csr.dsa.pem";
  47974. XFILE f;
  47975. #endif
  47976. BIO* bio = NULL;
  47977. X509* req = NULL;
  47978. EVP_PKEY *pub_key = NULL;
  47979. {
  47980. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  47981. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  47982. /*
  47983. * Extract the public key from the CSR
  47984. */
  47985. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  47986. /*
  47987. * Verify the signature in the CSR
  47988. */
  47989. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  47990. X509_free(req);
  47991. BIO_free(bio);
  47992. EVP_PKEY_free(pub_key);
  47993. }
  47994. {
  47995. #ifdef OPENSSL_ALL
  47996. X509_ATTRIBUTE* attr;
  47997. ASN1_TYPE *at;
  47998. #endif
  47999. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  48000. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  48001. /*
  48002. * Extract the public key from the CSR
  48003. */
  48004. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48005. /*
  48006. * Verify the signature in the CSR
  48007. */
  48008. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48009. #ifdef OPENSSL_ALL
  48010. /*
  48011. * Obtain the challenge password from the CSR
  48012. */
  48013. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  48014. 1);
  48015. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  48016. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48017. AssertNotNull(at->value.asn1_string);
  48018. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  48019. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  48020. #endif
  48021. X509_free(req);
  48022. BIO_free(bio);
  48023. EVP_PKEY_free(pub_key);
  48024. }
  48025. {
  48026. #ifdef OPENSSL_ALL
  48027. X509_ATTRIBUTE* attr;
  48028. ASN1_TYPE *at;
  48029. STACK_OF(X509_EXTENSION) *exts = NULL;
  48030. #endif
  48031. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  48032. /* This CSR contains an Extension Request attribute so
  48033. * we test extension parsing in a CSR attribute here. */
  48034. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  48035. /*
  48036. * Extract the public key from the CSR
  48037. */
  48038. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48039. /*
  48040. * Verify the signature in the CSR
  48041. */
  48042. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48043. #ifdef OPENSSL_ALL
  48044. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  48045. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  48046. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  48047. /*
  48048. * Obtain the challenge password from the CSR
  48049. */
  48050. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  48051. 0);
  48052. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  48053. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48054. AssertNotNull(at->value.asn1_string);
  48055. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  48056. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  48057. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  48058. #endif
  48059. X509_free(req);
  48060. BIO_free(bio);
  48061. EVP_PKEY_free(pub_key);
  48062. }
  48063. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48064. {
  48065. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  48066. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  48067. /*
  48068. * Extract the public key from the CSR
  48069. */
  48070. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48071. /*
  48072. * Verify the signature in the CSR
  48073. */
  48074. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48075. X509_free(req);
  48076. BIO_free(bio);
  48077. /* Run the same test, but with a file pointer instead of a BIO.
  48078. * (PEM_read_X509_REQ)*/
  48079. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  48080. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  48081. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48082. X509_free(req);
  48083. EVP_PKEY_free(pub_key);
  48084. }
  48085. res = TEST_RES_CHECK(1);
  48086. #endif /* !NO_DSA && !HAVE_SELFTEST */
  48087. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  48088. return res;
  48089. }
  48090. static int test_wolfSSL_PEM_read_X509(void)
  48091. {
  48092. int res = TEST_SKIPPED;
  48093. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48094. !defined(NO_RSA)
  48095. X509 *x509 = NULL;
  48096. XFILE fp;
  48097. fp = XFOPEN(svrCertFile, "rb");
  48098. AssertTrue((fp != XBADFILE));
  48099. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  48100. X509_free(x509);
  48101. XFCLOSE(fp);
  48102. res = TEST_RES_CHECK(1);
  48103. #endif
  48104. return res;
  48105. }
  48106. static int test_wolfSSL_PEM_read(void)
  48107. {
  48108. int res = TEST_SKIPPED;
  48109. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  48110. const char* filename = "./certs/server-keyEnc.pem";
  48111. XFILE fp;
  48112. char* name = NULL;
  48113. char* header = NULL;
  48114. byte* data = NULL;
  48115. long len;
  48116. EVP_CIPHER_INFO cipher;
  48117. WOLFSSL_BIO* bio;
  48118. byte* fileData;
  48119. size_t fileDataSz;
  48120. byte* out;
  48121. fp = XFOPEN(filename, "rb");
  48122. AssertTrue((fp != XBADFILE));
  48123. /* Fail cases. */
  48124. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  48125. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  48126. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  48127. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  48128. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48129. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  48130. AssertIntGT(XSTRLEN(header), 0);
  48131. AssertIntGT(len, 0);
  48132. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  48133. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  48134. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  48135. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  48136. DYNAMIC_TYPE_TMP_BUFFER));
  48137. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  48138. XFCLOSE(fp);
  48139. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48140. /* Fail cases. */
  48141. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  48142. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  48143. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  48144. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  48145. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  48146. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  48147. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  48148. /* Fail cases. */
  48149. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  48150. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  48151. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  48152. #ifndef NO_DES3
  48153. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  48154. #endif
  48155. /* Fail cases. */
  48156. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  48157. (void*)"yassl123"), WOLFSSL_FAILURE);
  48158. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  48159. (void*)"yassl123"), WOLFSSL_FAILURE);
  48160. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  48161. (void*)"yassl123"), WOLFSSL_FAILURE);
  48162. #if !defined(NO_DES3) && !defined(NO_MD5)
  48163. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  48164. (void*)"yassl123"), WOLFSSL_SUCCESS);
  48165. #endif
  48166. BIO_free(bio);
  48167. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48168. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48169. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48170. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48171. name = NULL;
  48172. header = NULL;
  48173. data = NULL;
  48174. fp = XFOPEN(svrKeyFile, "rb");
  48175. AssertTrue((fp != XBADFILE));
  48176. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48177. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  48178. AssertIntEQ(XSTRLEN(header), 0);
  48179. AssertIntGT(len, 0);
  48180. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  48181. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  48182. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  48183. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  48184. DYNAMIC_TYPE_TMP_BUFFER));
  48185. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  48186. XFCLOSE(fp);
  48187. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48188. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  48189. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  48190. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  48191. BIO_free(bio);
  48192. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48193. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48194. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48195. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48196. res = TEST_RES_CHECK(1);
  48197. #endif
  48198. return res;
  48199. }
  48200. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  48201. {
  48202. int res = TEST_SKIPPED;
  48203. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48204. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48205. const byte iv[12] = { 0 };
  48206. const byte key[16] = { 0 };
  48207. const byte cleartext[16] = { 0 };
  48208. const byte aad[] = {
  48209. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  48210. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  48211. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  48212. 0x4f
  48213. };
  48214. byte out1Part[16];
  48215. byte outTag1Part[16];
  48216. byte out2Part[16];
  48217. byte outTag2Part[16];
  48218. byte decryptBuf[16];
  48219. int len;
  48220. int tlen;
  48221. EVP_CIPHER_CTX* ctx = NULL;
  48222. /* ENCRYPT */
  48223. /* Send AAD and data in 1 part */
  48224. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  48225. tlen = 0;
  48226. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48227. 1);
  48228. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48229. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  48230. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  48231. sizeof(cleartext)), 1);
  48232. tlen += len;
  48233. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  48234. tlen += len;
  48235. AssertIntEQ(tlen, sizeof(cleartext));
  48236. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  48237. outTag1Part), 1);
  48238. EVP_CIPHER_CTX_free(ctx);
  48239. /* DECRYPT */
  48240. /* Send AAD and data in 1 part */
  48241. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  48242. tlen = 0;
  48243. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48244. 1);
  48245. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48246. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  48247. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  48248. sizeof(cleartext)), 1);
  48249. tlen += len;
  48250. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  48251. outTag1Part), 1);
  48252. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  48253. tlen += len;
  48254. AssertIntEQ(tlen, sizeof(cleartext));
  48255. EVP_CIPHER_CTX_free(ctx);
  48256. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  48257. /* ENCRYPT */
  48258. /* Send AAD and data in 2 parts */
  48259. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  48260. tlen = 0;
  48261. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48262. 1);
  48263. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48264. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  48265. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  48266. 1);
  48267. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  48268. tlen += len;
  48269. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  48270. sizeof(cleartext) - 1), 1);
  48271. tlen += len;
  48272. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  48273. tlen += len;
  48274. AssertIntEQ(tlen, sizeof(cleartext));
  48275. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  48276. outTag2Part), 1);
  48277. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  48278. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  48279. EVP_CIPHER_CTX_free(ctx);
  48280. /* DECRYPT */
  48281. /* Send AAD and data in 2 parts */
  48282. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  48283. tlen = 0;
  48284. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48285. 1);
  48286. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48287. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  48288. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  48289. 1);
  48290. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  48291. tlen += len;
  48292. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  48293. sizeof(cleartext) - 1), 1);
  48294. tlen += len;
  48295. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  48296. outTag1Part), 1);
  48297. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  48298. tlen += len;
  48299. AssertIntEQ(tlen, sizeof(cleartext));
  48300. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  48301. /* Test AAD re-use */
  48302. EVP_CIPHER_CTX_free(ctx);
  48303. res = TEST_RES_CHECK(1);
  48304. #endif
  48305. return res;
  48306. }
  48307. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  48308. {
  48309. int res = TEST_SKIPPED;
  48310. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48311. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48312. /* Zero length plain text */
  48313. byte key[] = {
  48314. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48315. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48316. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48317. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48318. }; /* align */
  48319. byte iv[] = {
  48320. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48321. }; /* align */
  48322. byte plaintxt[1];
  48323. int ivSz = 12;
  48324. int plaintxtSz = 0;
  48325. unsigned char tag[16];
  48326. unsigned char tag_kat[] =
  48327. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  48328. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  48329. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48330. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48331. int ciphertxtSz = 0;
  48332. int decryptedtxtSz = 0;
  48333. int len = 0;
  48334. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48335. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48336. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  48337. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48338. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48339. plaintxtSz));
  48340. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48341. ciphertxtSz += len;
  48342. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  48343. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48344. AssertIntEQ(0, ciphertxtSz);
  48345. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  48346. EVP_CIPHER_CTX_init(de);
  48347. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  48348. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48349. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48350. decryptedtxtSz = len;
  48351. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  48352. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48353. decryptedtxtSz += len;
  48354. AssertIntEQ(0, decryptedtxtSz);
  48355. EVP_CIPHER_CTX_free(en);
  48356. EVP_CIPHER_CTX_free(de);
  48357. res = TEST_RES_CHECK(1);
  48358. #endif
  48359. return res;
  48360. }
  48361. static int test_wolfssl_EVP_aes_gcm(void)
  48362. {
  48363. int res = TEST_SKIPPED;
  48364. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48365. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48366. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48367. byte *key = (byte*)"01234567890123456789012345678901";
  48368. /* A 128 bit IV */
  48369. byte *iv = (byte*)"0123456789012345";
  48370. int ivSz = AES_BLOCK_SIZE;
  48371. /* Message to be encrypted */
  48372. byte *plaintxt = (byte*)"for things to change you have to change";
  48373. /* Additional non-confidential data */
  48374. byte *aad = (byte*)"Don't spend major time on minor things.";
  48375. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48376. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48377. int aadSz = (int)XSTRLEN((char*)aad);
  48378. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48379. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48380. int ciphertxtSz = 0;
  48381. int decryptedtxtSz = 0;
  48382. int len = 0;
  48383. int i = 0;
  48384. EVP_CIPHER_CTX en[2];
  48385. EVP_CIPHER_CTX de[2];
  48386. for (i = 0; i < 2; i++) {
  48387. EVP_CIPHER_CTX_init(&en[i]);
  48388. if (i == 0) {
  48389. /* Default uses 96-bits IV length */
  48390. #ifdef WOLFSSL_AES_128
  48391. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  48392. #elif defined(WOLFSSL_AES_192)
  48393. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  48394. #elif defined(WOLFSSL_AES_256)
  48395. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  48396. #endif
  48397. }
  48398. else {
  48399. #ifdef WOLFSSL_AES_128
  48400. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  48401. #elif defined(WOLFSSL_AES_192)
  48402. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  48403. #elif defined(WOLFSSL_AES_256)
  48404. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  48405. #endif
  48406. /* non-default must to set the IV length first */
  48407. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48408. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48409. }
  48410. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48411. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  48412. ciphertxtSz = len;
  48413. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48414. ciphertxtSz += len;
  48415. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  48416. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48417. EVP_CIPHER_CTX_init(&de[i]);
  48418. if (i == 0) {
  48419. /* Default uses 96-bits IV length */
  48420. #ifdef WOLFSSL_AES_128
  48421. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  48422. #elif defined(WOLFSSL_AES_192)
  48423. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  48424. #elif defined(WOLFSSL_AES_256)
  48425. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  48426. #endif
  48427. }
  48428. else {
  48429. #ifdef WOLFSSL_AES_128
  48430. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  48431. #elif defined(WOLFSSL_AES_192)
  48432. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  48433. #elif defined(WOLFSSL_AES_256)
  48434. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  48435. #endif
  48436. /* non-default must to set the IV length first */
  48437. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48438. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48439. }
  48440. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48441. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48442. decryptedtxtSz = len;
  48443. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  48444. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48445. decryptedtxtSz += len;
  48446. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  48447. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48448. /* modify tag*/
  48449. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48450. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48451. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  48452. /* fail due to wrong tag */
  48453. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48454. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48455. AssertIntEQ(0, len);
  48456. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48457. }
  48458. res = TEST_RES_CHECK(1);
  48459. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48460. return res;
  48461. }
  48462. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  48463. {
  48464. int res = TEST_SKIPPED;
  48465. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48466. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48467. /* Zero length plain text */
  48468. byte key[] = {
  48469. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48470. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48471. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48472. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48473. }; /* align */
  48474. byte iv[] = {
  48475. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48476. }; /* align */
  48477. byte plaintxt[1];
  48478. int ivSz = 12;
  48479. int plaintxtSz = 0;
  48480. unsigned char tag[16];
  48481. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48482. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48483. int ciphertxtSz = 0;
  48484. int decryptedtxtSz = 0;
  48485. int len = 0;
  48486. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48487. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48488. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  48489. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48490. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48491. plaintxtSz));
  48492. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48493. ciphertxtSz += len;
  48494. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48495. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48496. AssertIntEQ(0, ciphertxtSz);
  48497. EVP_CIPHER_CTX_init(de);
  48498. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  48499. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48500. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48501. decryptedtxtSz = len;
  48502. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48503. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48504. decryptedtxtSz += len;
  48505. AssertIntEQ(0, decryptedtxtSz);
  48506. EVP_CIPHER_CTX_free(en);
  48507. EVP_CIPHER_CTX_free(de);
  48508. res = TEST_RES_CHECK(1);
  48509. #endif
  48510. return res;
  48511. }
  48512. static int test_wolfssl_EVP_aes_ccm(void)
  48513. {
  48514. int res = TEST_SKIPPED;
  48515. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48516. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48517. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48518. byte *key = (byte*)"01234567890123456789012345678901";
  48519. /* A 128 bit IV */
  48520. byte *iv = (byte*)"0123456789012";
  48521. int ivSz = (int)XSTRLEN((char*)iv);
  48522. /* Message to be encrypted */
  48523. byte *plaintxt = (byte*)"for things to change you have to change";
  48524. /* Additional non-confidential data */
  48525. byte *aad = (byte*)"Don't spend major time on minor things.";
  48526. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48527. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48528. int aadSz = (int)XSTRLEN((char*)aad);
  48529. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48530. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48531. int ciphertxtSz = 0;
  48532. int decryptedtxtSz = 0;
  48533. int len = 0;
  48534. int i = 0;
  48535. EVP_CIPHER_CTX en[2];
  48536. EVP_CIPHER_CTX de[2];
  48537. for (i = 0; i < 2; i++) {
  48538. EVP_CIPHER_CTX_init(&en[i]);
  48539. if (i == 0) {
  48540. /* Default uses 96-bits IV length */
  48541. #ifdef WOLFSSL_AES_128
  48542. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48543. EVP_aes_128_ccm(), NULL, key, iv));
  48544. #elif defined(WOLFSSL_AES_192)
  48545. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48546. EVP_aes_192_ccm(), NULL, key, iv));
  48547. #elif defined(WOLFSSL_AES_256)
  48548. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48549. EVP_aes_256_ccm(), NULL, key, iv));
  48550. #endif
  48551. }
  48552. else {
  48553. #ifdef WOLFSSL_AES_128
  48554. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48555. EVP_aes_128_ccm(), NULL, NULL, NULL));
  48556. #elif defined(WOLFSSL_AES_192)
  48557. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48558. EVP_aes_192_ccm(), NULL, NULL, NULL));
  48559. #elif defined(WOLFSSL_AES_256)
  48560. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48561. EVP_aes_256_ccm(), NULL, NULL, NULL));
  48562. #endif
  48563. /* non-default must to set the IV length first */
  48564. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
  48565. EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48566. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48567. NULL, NULL, key, iv));
  48568. }
  48569. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48570. AssertIntEQ(1, EVP_EncryptUpdate(&en[i],
  48571. ciphertxt, &len, plaintxt, plaintxtSz));
  48572. ciphertxtSz = len;
  48573. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48574. ciphertxtSz += len;
  48575. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
  48576. EVP_CTRL_CCM_GET_TAG, AES_BLOCK_SIZE, tag));
  48577. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48578. EVP_CIPHER_CTX_init(&de[i]);
  48579. if (i == 0) {
  48580. /* Default uses 96-bits IV length */
  48581. #ifdef WOLFSSL_AES_128
  48582. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48583. EVP_aes_128_ccm(), NULL, key, iv));
  48584. #elif defined(WOLFSSL_AES_192)
  48585. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48586. EVP_aes_192_ccm(), NULL, key, iv));
  48587. #elif defined(WOLFSSL_AES_256)
  48588. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48589. EVP_aes_256_ccm(), NULL, key, iv));
  48590. #endif
  48591. }
  48592. else {
  48593. #ifdef WOLFSSL_AES_128
  48594. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48595. EVP_aes_128_ccm(), NULL, NULL, NULL));
  48596. #elif defined(WOLFSSL_AES_192)
  48597. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48598. EVP_aes_192_ccm(), NULL, NULL, NULL));
  48599. #elif defined(WOLFSSL_AES_256)
  48600. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48601. EVP_aes_256_ccm(), NULL, NULL, NULL));
  48602. #endif
  48603. /* non-default must to set the IV length first */
  48604. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  48605. EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48606. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48607. }
  48608. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48609. AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
  48610. decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48611. decryptedtxtSz = len;
  48612. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  48613. EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
  48614. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i],
  48615. decryptedtxt, &len));
  48616. decryptedtxtSz += len;
  48617. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  48618. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48619. /* modify tag*/
  48620. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48621. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48622. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  48623. EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
  48624. /* fail due to wrong tag */
  48625. AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
  48626. decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48627. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48628. AssertIntEQ(0, len);
  48629. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48630. }
  48631. res = TEST_RES_CHECK(1);
  48632. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  48633. return res;
  48634. }
  48635. static int test_wolfssl_EVP_chacha20_poly1305(void)
  48636. {
  48637. int res = TEST_SKIPPED;
  48638. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  48639. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  48640. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  48641. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48642. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  48643. byte cipherText[sizeof(plainText)];
  48644. byte decryptedText[sizeof(plainText)];
  48645. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  48646. EVP_CIPHER_CTX* ctx;
  48647. int outSz;
  48648. /* Encrypt. */
  48649. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48650. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48651. NULL), WOLFSSL_SUCCESS);
  48652. /* Invalid IV length. */
  48653. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48654. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  48655. /* Valid IV length. */
  48656. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48657. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48658. /* Invalid tag length. */
  48659. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48660. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  48661. /* Valid tag length. */
  48662. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48663. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  48664. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48665. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48666. WOLFSSL_SUCCESS);
  48667. AssertIntEQ(outSz, sizeof(aad));
  48668. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48669. sizeof(plainText)), WOLFSSL_SUCCESS);
  48670. AssertIntEQ(outSz, sizeof(plainText));
  48671. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48672. AssertIntEQ(outSz, 0);
  48673. /* Invalid tag length. */
  48674. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48675. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  48676. /* Valid tag length. */
  48677. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48678. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48679. EVP_CIPHER_CTX_free(ctx);
  48680. /* Decrypt. */
  48681. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48682. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48683. NULL), WOLFSSL_SUCCESS);
  48684. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48685. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48686. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48687. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48688. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48689. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48690. WOLFSSL_SUCCESS);
  48691. AssertIntEQ(outSz, sizeof(aad));
  48692. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48693. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48694. AssertIntEQ(outSz, sizeof(cipherText));
  48695. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48696. WOLFSSL_SUCCESS);
  48697. AssertIntEQ(outSz, 0);
  48698. EVP_CIPHER_CTX_free(ctx);
  48699. /* Test partial Inits. CipherInit() allow setting of key and iv
  48700. * in separate calls. */
  48701. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48702. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  48703. key, NULL, 1), WOLFSSL_SUCCESS);
  48704. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48705. WOLFSSL_SUCCESS);
  48706. AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  48707. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  48708. AssertIntEQ(outSz, sizeof(aad));
  48709. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48710. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48711. AssertIntEQ(outSz, sizeof(cipherText));
  48712. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48713. WOLFSSL_SUCCESS);
  48714. AssertIntEQ(outSz, 0);
  48715. EVP_CIPHER_CTX_free(ctx);
  48716. res = TEST_RES_CHECK(1);
  48717. #endif
  48718. return res;
  48719. }
  48720. static int test_wolfssl_EVP_chacha20(void)
  48721. {
  48722. int res = TEST_SKIPPED;
  48723. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  48724. byte key[CHACHA_MAX_KEY_SZ];
  48725. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  48726. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48727. byte cipherText[sizeof(plainText)];
  48728. byte decryptedText[sizeof(plainText)];
  48729. EVP_CIPHER_CTX* ctx;
  48730. int outSz;
  48731. /* Encrypt. */
  48732. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48733. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48734. NULL), WOLFSSL_SUCCESS);
  48735. /* Any tag length must fail - not an AEAD cipher. */
  48736. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48737. 16, NULL), WOLFSSL_FAILURE);
  48738. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48739. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48740. sizeof(plainText)), WOLFSSL_SUCCESS);
  48741. AssertIntEQ(outSz, sizeof(plainText));
  48742. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48743. AssertIntEQ(outSz, 0);
  48744. EVP_CIPHER_CTX_free(ctx);
  48745. /* Decrypt. */
  48746. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48747. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48748. NULL), WOLFSSL_SUCCESS);
  48749. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48750. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48751. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48752. AssertIntEQ(outSz, sizeof(cipherText));
  48753. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48754. WOLFSSL_SUCCESS);
  48755. AssertIntEQ(outSz, 0);
  48756. EVP_CIPHER_CTX_free(ctx);
  48757. /* Test partial Inits. CipherInit() allow setting of key and iv
  48758. * in separate calls. */
  48759. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48760. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  48761. key, NULL, 1), WOLFSSL_SUCCESS);
  48762. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48763. WOLFSSL_SUCCESS);
  48764. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48765. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48766. AssertIntEQ(outSz, sizeof(cipherText));
  48767. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48768. WOLFSSL_SUCCESS);
  48769. AssertIntEQ(outSz, 0);
  48770. EVP_CIPHER_CTX_free(ctx);
  48771. res = TEST_RES_CHECK(1);
  48772. #endif
  48773. return res;
  48774. }
  48775. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  48776. {
  48777. int res = TEST_SKIPPED;
  48778. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  48779. EVP_PKEY_CTX* ctx;
  48780. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  48781. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  48782. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  48783. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  48784. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  48785. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  48786. byte outKey[34];
  48787. size_t outKeySz = sizeof(outKey);
  48788. /* These expected outputs were gathered by running the same test below using
  48789. * OpenSSL. */
  48790. const byte extractAndExpand[] = {
  48791. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  48792. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  48793. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  48794. };
  48795. const byte extractOnly[] = {
  48796. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  48797. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  48798. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  48799. };
  48800. const byte expandOnly[] = {
  48801. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  48802. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  48803. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  48804. };
  48805. const byte extractAndExpandAddInfo[] = {
  48806. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  48807. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  48808. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  48809. };
  48810. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  48811. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  48812. /* NULL ctx. */
  48813. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  48814. /* NULL md. */
  48815. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  48816. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  48817. /* NULL ctx. */
  48818. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  48819. WOLFSSL_FAILURE);
  48820. /* NULL salt is ok. */
  48821. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  48822. WOLFSSL_SUCCESS);
  48823. /* Salt length <= 0. */
  48824. /* Length 0 salt is ok. */
  48825. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  48826. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  48827. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  48828. WOLFSSL_SUCCESS);
  48829. /* NULL ctx. */
  48830. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  48831. WOLFSSL_FAILURE);
  48832. /* NULL key. */
  48833. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  48834. WOLFSSL_FAILURE);
  48835. /* Key length <= 0 */
  48836. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  48837. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  48838. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  48839. WOLFSSL_SUCCESS);
  48840. /* NULL ctx. */
  48841. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  48842. WOLFSSL_FAILURE);
  48843. /* NULL info is ok. */
  48844. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  48845. WOLFSSL_SUCCESS);
  48846. /* Info length <= 0 */
  48847. /* Length 0 info is ok. */
  48848. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  48849. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  48850. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  48851. WOLFSSL_SUCCESS);
  48852. /* NULL ctx. */
  48853. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  48854. WOLFSSL_FAILURE);
  48855. /* Extract and expand (default). */
  48856. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48857. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  48858. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  48859. /* Extract only. */
  48860. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  48861. WOLFSSL_SUCCESS);
  48862. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48863. AssertIntEQ(outKeySz, sizeof(extractOnly));
  48864. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  48865. outKeySz = sizeof(outKey);
  48866. /* Expand only. */
  48867. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  48868. WOLFSSL_SUCCESS);
  48869. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48870. AssertIntEQ(outKeySz, sizeof(expandOnly));
  48871. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  48872. outKeySz = sizeof(outKey);
  48873. /* Extract and expand with appended additional info. */
  48874. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  48875. WOLFSSL_SUCCESS);
  48876. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  48877. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  48878. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48879. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  48880. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  48881. EVP_PKEY_CTX_free(ctx);
  48882. res = TEST_RES_CHECK(1);
  48883. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  48884. return res;
  48885. }
  48886. #ifndef NO_BIO
  48887. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  48888. {
  48889. int res = TEST_SKIPPED;
  48890. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48891. BIO* bio;
  48892. X509_INFO* info;
  48893. STACK_OF(X509_INFO)* sk;
  48894. char* subject;
  48895. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  48896. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  48897. AssertNotNull(bio = BIO_new(BIO_s_file()));
  48898. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  48899. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  48900. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  48901. /* using dereference to maintain testing for Apache port*/
  48902. AssertNotNull(info = sk_X509_INFO_pop(sk));
  48903. AssertNotNull(subject =
  48904. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  48905. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  48906. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  48907. X509_INFO_free(info);
  48908. AssertNotNull(info = sk_X509_INFO_pop(sk));
  48909. AssertNotNull(subject =
  48910. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  48911. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  48912. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  48913. X509_INFO_free(info);
  48914. AssertNull(info = sk_X509_INFO_pop(sk));
  48915. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  48916. BIO_free(bio);
  48917. res = TEST_RES_CHECK(1);
  48918. #endif
  48919. return res;
  48920. }
  48921. #endif /* !NO_BIO */
  48922. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  48923. {
  48924. int res = TEST_SKIPPED;
  48925. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48926. X509 *x509;
  48927. X509_NAME* name;
  48928. int idx = 0;
  48929. X509_NAME_ENTRY *ne;
  48930. ASN1_OBJECT *object = NULL;
  48931. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  48932. AssertNotNull(x509);
  48933. name = X509_get_subject_name(x509);
  48934. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  48935. AssertIntGE(idx, 0);
  48936. ne = X509_NAME_get_entry(name, idx);
  48937. AssertNotNull(ne);
  48938. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  48939. X509_free(x509);
  48940. res = TEST_RES_CHECK(1);
  48941. #endif
  48942. return res;
  48943. }
  48944. static int test_wolfSSL_X509_STORE_get1_certs(void)
  48945. {
  48946. int res = TEST_SKIPPED;
  48947. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  48948. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48949. X509_STORE_CTX *storeCtx;
  48950. X509_STORE *store;
  48951. X509 *caX509;
  48952. X509 *svrX509;
  48953. X509_NAME *subject;
  48954. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  48955. AssertNotNull(caX509 =
  48956. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  48957. AssertNotNull((svrX509 =
  48958. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  48959. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  48960. AssertNotNull(store = X509_STORE_new());
  48961. AssertNotNull(subject = X509_get_subject_name(caX509));
  48962. /* Errors */
  48963. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  48964. AssertNull(X509_STORE_get1_certs(NULL, subject));
  48965. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  48966. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  48967. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  48968. /* Should find the cert */
  48969. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  48970. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  48971. sk_X509_pop_free(certs, NULL);
  48972. /* Should not find the cert */
  48973. AssertNotNull(subject = X509_get_subject_name(svrX509));
  48974. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  48975. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  48976. sk_X509_pop_free(certs, NULL);
  48977. X509_STORE_free(store);
  48978. X509_STORE_CTX_free(storeCtx);
  48979. X509_free(svrX509);
  48980. X509_free(caX509);
  48981. res = TEST_RES_CHECK(1);
  48982. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  48983. return res;
  48984. }
  48985. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  48986. * have default (hidden) visibility, and in the absence of visibility, it's
  48987. * benign to mask out the library implementation.
  48988. */
  48989. #define WOLFSSL_MISC_INCLUDED
  48990. #include <wolfcrypt/src/misc.c>
  48991. static int test_ForceZero(void)
  48992. {
  48993. unsigned char data[32];
  48994. unsigned int i, j, len;
  48995. /* Test case with 0 length */
  48996. ForceZero(data, 0);
  48997. /* Test ForceZero */
  48998. for (i = 0; i < sizeof(data); i++) {
  48999. for (len = 1; len < sizeof(data) - i; len++) {
  49000. for (j = 0; j < sizeof(data); j++)
  49001. data[j] = j + 1;
  49002. ForceZero(data + i, len);
  49003. for (j = 0; j < sizeof(data); j++) {
  49004. if (j < i || j >= i + len) {
  49005. if (data[j] == 0x00)
  49006. return -10200;
  49007. }
  49008. else if (data[j] != 0x00)
  49009. return -10201;
  49010. }
  49011. }
  49012. }
  49013. return TEST_RES_CHECK(1);
  49014. }
  49015. #ifndef NO_BIO
  49016. static int test_wolfSSL_X509_print(void)
  49017. {
  49018. int res = TEST_SKIPPED;
  49019. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49020. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  49021. X509 *x509;
  49022. BIO *bio;
  49023. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49024. const X509_ALGOR *cert_sig_alg;
  49025. #endif
  49026. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  49027. AssertNotNull(x509);
  49028. /* print to memory */
  49029. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  49030. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49031. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  49032. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  49033. /* Will print IP address subject alt name. */
  49034. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  49035. #elif defined(NO_ASN_TIME)
  49036. /* Will print IP address subject alt name but not Validity. */
  49037. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  49038. #else
  49039. /* Will print IP address subject alt name. */
  49040. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  49041. #endif
  49042. #elif defined(NO_ASN_TIME)
  49043. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  49044. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  49045. #else
  49046. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  49047. #endif
  49048. BIO_free(bio);
  49049. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49050. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49051. /* Print signature */
  49052. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  49053. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  49054. #endif
  49055. /* print to stderr */
  49056. #if !defined(NO_WOLFSSL_DIR)
  49057. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49058. #endif
  49059. /* print again */
  49060. AssertIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  49061. X509_free(x509);
  49062. BIO_free(bio);
  49063. res = TEST_RES_CHECK(1);
  49064. #endif
  49065. return res;
  49066. }
  49067. static int test_wolfSSL_X509_CRL_print(void)
  49068. {
  49069. int res = TEST_SKIPPED;
  49070. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  49071. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  49072. X509_CRL* crl;
  49073. BIO *bio;
  49074. XFILE fp;
  49075. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  49076. AssertTrue((fp != XBADFILE));
  49077. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49078. NULL, NULL));
  49079. XFCLOSE(fp);
  49080. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  49081. AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  49082. X509_CRL_free(crl);
  49083. BIO_free(bio);
  49084. res = TEST_RES_CHECK(1);
  49085. #endif
  49086. return res;
  49087. }
  49088. static int test_wolfSSL_BIO_get_len(void)
  49089. {
  49090. int res = TEST_SKIPPED;
  49091. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49092. BIO *bio = NULL;
  49093. const char txt[] = "Some example text to push to the BIO.";
  49094. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  49095. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49096. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  49097. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  49098. BIO_free(bio);
  49099. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49100. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  49101. BIO_free(bio);
  49102. res = TEST_RES_CHECK(1);
  49103. #endif
  49104. return res;
  49105. }
  49106. #endif /* !NO_BIO */
  49107. static int test_wolfSSL_RSA(void)
  49108. {
  49109. int res = TEST_SKIPPED;
  49110. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  49111. defined(WOLFSSL_KEY_GEN)
  49112. RSA* rsa;
  49113. const BIGNUM *n;
  49114. const BIGNUM *e;
  49115. const BIGNUM *d;
  49116. const BIGNUM *p;
  49117. const BIGNUM *q;
  49118. const BIGNUM *dmp1;
  49119. const BIGNUM *dmq1;
  49120. const BIGNUM *iqmp;
  49121. AssertNotNull(rsa = RSA_new());
  49122. AssertIntEQ(RSA_size(NULL), 0);
  49123. AssertIntEQ(RSA_size(rsa), 0);
  49124. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  49125. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  49126. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  49127. #ifdef WOLFSSL_RSA_KEY_CHECK
  49128. AssertIntEQ(RSA_check_key(rsa), 0);
  49129. #endif
  49130. RSA_free(rsa);
  49131. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49132. AssertIntEQ(RSA_size(rsa), 256);
  49133. #ifdef WOLFSSL_RSA_KEY_CHECK
  49134. AssertIntEQ(RSA_check_key(NULL), 0);
  49135. AssertIntEQ(RSA_check_key(rsa), 1);
  49136. #endif
  49137. /* sanity check */
  49138. AssertIntEQ(RSA_bits(NULL), 0);
  49139. /* key */
  49140. AssertIntEQ(RSA_bits(rsa), 2048);
  49141. RSA_get0_key(rsa, &n, &e, &d);
  49142. AssertPtrEq(rsa->n, n);
  49143. AssertPtrEq(rsa->e, e);
  49144. AssertPtrEq(rsa->d, d);
  49145. AssertNotNull(n = BN_new());
  49146. AssertNotNull(e = BN_new());
  49147. AssertNotNull(d = BN_new());
  49148. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  49149. AssertPtrEq(rsa->n, n);
  49150. AssertPtrEq(rsa->e, e);
  49151. AssertPtrEq(rsa->d, d);
  49152. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  49153. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  49154. /* crt_params */
  49155. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  49156. AssertPtrEq(rsa->dmp1, dmp1);
  49157. AssertPtrEq(rsa->dmq1, dmq1);
  49158. AssertPtrEq(rsa->iqmp, iqmp);
  49159. AssertNotNull(dmp1 = BN_new());
  49160. AssertNotNull(dmq1 = BN_new());
  49161. AssertNotNull(iqmp = BN_new());
  49162. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49163. (BIGNUM*)iqmp), 1);
  49164. AssertPtrEq(rsa->dmp1, dmp1);
  49165. AssertPtrEq(rsa->dmq1, dmq1);
  49166. AssertPtrEq(rsa->iqmp, iqmp);
  49167. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  49168. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49169. (BIGNUM*)iqmp), 0);
  49170. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  49171. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  49172. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  49173. AssertNull(dmp1);
  49174. AssertNull(dmq1);
  49175. AssertNull(iqmp);
  49176. /* factors */
  49177. RSA_get0_factors(rsa, NULL, NULL);
  49178. RSA_get0_factors(rsa, &p, &q);
  49179. AssertPtrEq(rsa->p, p);
  49180. AssertPtrEq(rsa->q, q);
  49181. AssertNotNull(p = BN_new());
  49182. AssertNotNull(q = BN_new());
  49183. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  49184. AssertPtrEq(rsa->p, p);
  49185. AssertPtrEq(rsa->q, q);
  49186. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  49187. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  49188. RSA_get0_factors(NULL, NULL, NULL);
  49189. RSA_get0_factors(NULL, &p, &q);
  49190. AssertNull(p);
  49191. AssertNull(q);
  49192. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  49193. AssertIntEQ(RSA_bits(rsa), 21);
  49194. RSA_free(rsa);
  49195. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  49196. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  49197. AssertIntEQ(RSA_size(rsa), 384);
  49198. AssertIntEQ(RSA_bits(rsa), 3072);
  49199. RSA_free(rsa);
  49200. #endif
  49201. /* remove for now with odd key size until adjusting rsa key size check with
  49202. wc_MakeRsaKey()
  49203. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  49204. RSA_free(rsa);
  49205. */
  49206. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  49207. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  49208. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  49209. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  49210. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  49211. {
  49212. byte buff[FOURK_BUF];
  49213. byte der[FOURK_BUF];
  49214. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  49215. XFILE f;
  49216. int bytes;
  49217. /* test loading encrypted RSA private pem w/o password */
  49218. f = XFOPEN(PrivKeyPemFile, "rb");
  49219. AssertTrue((f != XBADFILE));
  49220. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  49221. XFCLOSE(f);
  49222. XMEMSET(der, 0, sizeof(der));
  49223. /* test that error value is returned with no password */
  49224. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  49225. }
  49226. #endif
  49227. res = TEST_RES_CHECK(1);
  49228. #endif
  49229. return res;
  49230. }
  49231. static int test_wolfSSL_RSA_DER(void)
  49232. {
  49233. int res = TEST_SKIPPED;
  49234. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49235. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  49236. RSA *rsa;
  49237. int i;
  49238. const unsigned char *buff = NULL;
  49239. unsigned char *newBuff = NULL;
  49240. struct tbl_s
  49241. {
  49242. const unsigned char *der;
  49243. int sz;
  49244. } tbl[] = {
  49245. #ifdef USE_CERT_BUFFERS_1024
  49246. {client_key_der_1024, sizeof_client_key_der_1024},
  49247. {server_key_der_1024, sizeof_server_key_der_1024},
  49248. #endif
  49249. #ifdef USE_CERT_BUFFERS_2048
  49250. {client_key_der_2048, sizeof_client_key_der_2048},
  49251. {server_key_der_2048, sizeof_server_key_der_2048},
  49252. #endif
  49253. {NULL, 0}
  49254. };
  49255. /* Public Key DER */
  49256. struct tbl_s pub[] = {
  49257. #ifdef USE_CERT_BUFFERS_1024
  49258. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  49259. #endif
  49260. #ifdef USE_CERT_BUFFERS_2048
  49261. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  49262. #endif
  49263. {NULL, 0}
  49264. };
  49265. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  49266. buff = pub[0].der;
  49267. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  49268. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  49269. buff = tbl[0].der;
  49270. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  49271. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  49272. rsa = RSA_new();
  49273. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  49274. RSA_free(rsa);
  49275. for (i = 0; tbl[i].der != NULL; i++)
  49276. {
  49277. /* Passing in pointer results in pointer moving. */
  49278. buff = tbl[i].der;
  49279. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  49280. AssertNotNull(rsa);
  49281. RSA_free(rsa);
  49282. }
  49283. for (i = 0; tbl[i].der != NULL; i++)
  49284. {
  49285. /* Passing in pointer results in pointer moving. */
  49286. buff = tbl[i].der;
  49287. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  49288. AssertNotNull(rsa);
  49289. RSA_free(rsa);
  49290. }
  49291. for (i = 0; pub[i].der != NULL; i++)
  49292. {
  49293. buff = pub[i].der;
  49294. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  49295. AssertNotNull(rsa);
  49296. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  49297. newBuff = NULL;
  49298. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  49299. AssertNotNull(newBuff);
  49300. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  49301. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49302. RSA_free(rsa);
  49303. }
  49304. res = TEST_RES_CHECK(1);
  49305. #endif
  49306. return res;
  49307. }
  49308. static int test_wolfSSL_RSA_print(void)
  49309. {
  49310. int res = TEST_SKIPPED;
  49311. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49312. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49313. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  49314. BIO *bio;
  49315. WOLFSSL_RSA* rsa = NULL;
  49316. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49317. AssertNotNull(rsa = RSA_new());
  49318. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  49319. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  49320. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  49321. AssertIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  49322. /* Some very large number of indent spaces. */
  49323. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  49324. /* RSA is empty. */
  49325. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  49326. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  49327. RSA_free(rsa);
  49328. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49329. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  49330. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  49331. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  49332. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  49333. AssertIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  49334. AssertIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  49335. BIO_free(bio);
  49336. RSA_free(rsa);
  49337. res = TEST_RES_CHECK(1);
  49338. #endif
  49339. return res;
  49340. }
  49341. #ifndef NO_RSA
  49342. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  49343. {
  49344. int res = TEST_SKIPPED;
  49345. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49346. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49347. RSA *rsa;
  49348. const unsigned char *derBuf = client_key_der_2048;
  49349. unsigned char em[256] = {0}; /* len = 2048/8 */
  49350. /* Random data simulating a hash */
  49351. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  49352. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49353. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49354. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49355. };
  49356. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49357. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  49358. RSA_PSS_SALTLEN_DIGEST), 0);
  49359. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  49360. RSA_PSS_SALTLEN_DIGEST), 0);
  49361. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  49362. RSA_PSS_SALTLEN_DIGEST), 0);
  49363. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  49364. RSA_PSS_SALTLEN_DIGEST), 0);
  49365. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  49366. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  49367. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49368. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  49369. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49370. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  49371. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49372. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  49373. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49374. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49375. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49376. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  49377. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49378. RSA_PSS_SALTLEN_DIGEST), 1);
  49379. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49380. RSA_PSS_SALTLEN_DIGEST), 1);
  49381. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49382. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49383. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49384. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49385. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49386. RSA_PSS_SALTLEN_MAX), 1);
  49387. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49388. RSA_PSS_SALTLEN_MAX), 1);
  49389. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  49390. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  49391. RSA_free(rsa);
  49392. res = TEST_RES_CHECK(1);
  49393. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  49394. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  49395. return res;
  49396. }
  49397. #endif
  49398. static int test_wolfSSL_RSA_sign_sha3(void)
  49399. {
  49400. int res = TEST_SKIPPED;
  49401. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  49402. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49403. RSA *rsa;
  49404. const unsigned char *derBuf = client_key_der_2048;
  49405. unsigned char sigRet[256] = {0};
  49406. unsigned int sigLen = sizeof(sigRet);
  49407. /* Random data simulating a hash */
  49408. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  49409. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49410. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49411. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49412. };
  49413. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49414. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  49415. &sigLen, rsa), 1);
  49416. RSA_free(rsa);
  49417. res = TEST_RES_CHECK(1);
  49418. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  49419. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  49420. return res;
  49421. }
  49422. static int test_wolfSSL_RSA_get0_key(void)
  49423. {
  49424. int res = TEST_SKIPPED;
  49425. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  49426. RSA *rsa = NULL;
  49427. const BIGNUM* n = NULL;
  49428. const BIGNUM* e = NULL;
  49429. const BIGNUM* d = NULL;
  49430. const unsigned char* der;
  49431. int derSz;
  49432. #ifdef USE_CERT_BUFFERS_1024
  49433. der = client_key_der_1024;
  49434. derSz = sizeof_client_key_der_1024;
  49435. #elif defined(USE_CERT_BUFFERS_2048)
  49436. der = client_key_der_2048;
  49437. derSz = sizeof_client_key_der_2048;
  49438. #else
  49439. der = NULL;
  49440. derSz = 0;
  49441. #endif
  49442. if (der != NULL) {
  49443. RSA_get0_key(NULL, NULL, NULL, NULL);
  49444. RSA_get0_key(rsa, NULL, NULL, NULL);
  49445. RSA_get0_key(NULL, &n, &e, &d);
  49446. AssertNull(n);
  49447. AssertNull(e);
  49448. AssertNull(d);
  49449. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  49450. AssertNotNull(rsa);
  49451. RSA_get0_key(rsa, NULL, NULL, NULL);
  49452. RSA_get0_key(rsa, &n, NULL, NULL);
  49453. AssertNotNull(n);
  49454. RSA_get0_key(rsa, NULL, &e, NULL);
  49455. AssertNotNull(e);
  49456. RSA_get0_key(rsa, NULL, NULL, &d);
  49457. AssertNotNull(d);
  49458. RSA_get0_key(rsa, &n, &e, &d);
  49459. AssertNotNull(n);
  49460. AssertNotNull(e);
  49461. AssertNotNull(d);
  49462. RSA_free(rsa);
  49463. }
  49464. res = TEST_RES_CHECK(1);
  49465. #endif
  49466. return res;
  49467. }
  49468. static int test_wolfSSL_RSA_meth(void)
  49469. {
  49470. int res = TEST_SKIPPED;
  49471. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49472. RSA *rsa;
  49473. RSA_METHOD *rsa_meth;
  49474. #ifdef WOLFSSL_KEY_GEN
  49475. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49476. RSA_free(rsa);
  49477. #else
  49478. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49479. #endif
  49480. AssertNotNull(RSA_get_default_method());
  49481. wolfSSL_RSA_meth_free(NULL);
  49482. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  49483. AssertNotNull(rsa_meth =
  49484. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  49485. #ifndef NO_WOLFSSL_STUB
  49486. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  49487. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  49488. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  49489. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  49490. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  49491. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  49492. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  49493. #endif
  49494. AssertIntEQ(RSA_flags(NULL), 0);
  49495. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  49496. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  49497. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  49498. AssertNotNull(rsa = RSA_new());
  49499. /* No method set. */
  49500. AssertIntEQ(RSA_flags(rsa), 0);
  49501. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49502. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49503. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49504. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  49505. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  49506. AssertNull(RSA_get_method(NULL));
  49507. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  49508. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  49509. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49510. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49511. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  49512. RSA_METHOD_FLAG_NO_CHECK);
  49513. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49514. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49515. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  49516. /* rsa_meth is freed here */
  49517. RSA_free(rsa);
  49518. res = TEST_RES_CHECK(1);
  49519. #endif
  49520. return res;
  49521. }
  49522. static int test_wolfSSL_RSA_verify(void)
  49523. {
  49524. int res = TEST_SKIPPED;
  49525. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  49526. !defined(NO_FILESYSTEM)
  49527. #ifndef NO_BIO
  49528. XFILE fp;
  49529. RSA *pKey, *pubKey;
  49530. X509 *cert;
  49531. const char *text = "Hello wolfSSL !";
  49532. unsigned char hash[SHA256_DIGEST_LENGTH];
  49533. unsigned char signature[2048/8];
  49534. unsigned int signatureLength;
  49535. byte *buf;
  49536. BIO *bio;
  49537. SHA256_CTX c;
  49538. EVP_PKEY *evpPkey, *evpPubkey;
  49539. size_t sz;
  49540. /* generate hash */
  49541. SHA256_Init(&c);
  49542. SHA256_Update(&c, text, strlen(text));
  49543. SHA256_Final(hash, &c);
  49544. #ifdef WOLFSSL_SMALL_STACK_CACHE
  49545. /* workaround for small stack cache case */
  49546. wc_Sha256Free((wc_Sha256*)&c);
  49547. #endif
  49548. /* read privete key file */
  49549. fp = XFOPEN(svrKeyFile, "rb");
  49550. AssertTrue((fp != XBADFILE));
  49551. AssertIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  49552. sz = XFTELL(fp);
  49553. AssertIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  49554. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  49555. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  49556. XFCLOSE(fp);
  49557. /* read private key and sign hash data */
  49558. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  49559. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  49560. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  49561. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  49562. signature, &signatureLength, pKey), SSL_SUCCESS);
  49563. /* read public key and verify signed data */
  49564. fp = XFOPEN(svrCertFile,"rb");
  49565. AssertTrue((fp != XBADFILE));
  49566. cert = PEM_read_X509(fp, 0, 0, 0 );
  49567. XFCLOSE(fp);
  49568. evpPubkey = X509_get_pubkey(cert);
  49569. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  49570. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  49571. signatureLength, pubKey), SSL_SUCCESS);
  49572. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  49573. signatureLength, NULL), SSL_FAILURE);
  49574. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  49575. signatureLength, pubKey), SSL_FAILURE);
  49576. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  49577. signatureLength, pubKey), SSL_FAILURE);
  49578. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  49579. signatureLength, NULL), SSL_FAILURE);
  49580. RSA_free(pKey);
  49581. EVP_PKEY_free(evpPkey);
  49582. RSA_free(pubKey);
  49583. EVP_PKEY_free(evpPubkey);
  49584. X509_free(cert);
  49585. BIO_free(bio);
  49586. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  49587. res = TEST_RES_CHECK(1);
  49588. #endif
  49589. #endif
  49590. return res;
  49591. }
  49592. static int test_wolfSSL_RSA_sign(void)
  49593. {
  49594. int res = TEST_SKIPPED;
  49595. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49596. RSA *rsa;
  49597. unsigned char hash[SHA256_DIGEST_LENGTH];
  49598. #ifdef USE_CERT_BUFFERS_1024
  49599. const unsigned char* privDer = client_key_der_1024;
  49600. size_t privDerSz = sizeof_client_key_der_1024;
  49601. const unsigned char* pubDer = client_keypub_der_1024;
  49602. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49603. unsigned char signature[1024/8];
  49604. #else
  49605. const unsigned char* privDer = client_key_der_2048;
  49606. size_t privDerSz = sizeof_client_key_der_2048;
  49607. const unsigned char* pubDer = client_keypub_der_2048;
  49608. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49609. unsigned char signature[2048/8];
  49610. #endif
  49611. unsigned int signatureLen;
  49612. const unsigned char* der;
  49613. XMEMSET(hash, 0, sizeof(hash));
  49614. der = privDer;
  49615. rsa = NULL;
  49616. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49617. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  49618. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  49619. &signatureLen, rsa), 0);
  49620. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  49621. &signatureLen, rsa), 0);
  49622. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  49623. &signatureLen, rsa), 0);
  49624. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49625. NULL, rsa), 0);
  49626. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49627. &signatureLen, NULL), 0);
  49628. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49629. &signatureLen, rsa), 1);
  49630. RSA_free(rsa);
  49631. der = pubDer;
  49632. rsa = NULL;
  49633. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49634. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49635. signatureLen, rsa), 1);
  49636. RSA_free(rsa);
  49637. res = TEST_RES_CHECK(1);
  49638. #endif
  49639. return res;
  49640. }
  49641. static int test_wolfSSL_RSA_sign_ex(void)
  49642. {
  49643. int res = TEST_SKIPPED;
  49644. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49645. RSA *rsa;
  49646. unsigned char hash[SHA256_DIGEST_LENGTH];
  49647. #ifdef USE_CERT_BUFFERS_1024
  49648. const unsigned char* privDer = client_key_der_1024;
  49649. size_t privDerSz = sizeof_client_key_der_1024;
  49650. const unsigned char* pubDer = client_keypub_der_1024;
  49651. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49652. unsigned char signature[1024/8];
  49653. #else
  49654. const unsigned char* privDer = client_key_der_2048;
  49655. size_t privDerSz = sizeof_client_key_der_2048;
  49656. const unsigned char* pubDer = client_keypub_der_2048;
  49657. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49658. unsigned char signature[2048/8];
  49659. #endif
  49660. unsigned int signatureLen;
  49661. const unsigned char* der;
  49662. unsigned char encodedHash[51];
  49663. unsigned int encodedHashLen;
  49664. const unsigned char expEncHash[] = {
  49665. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  49666. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  49667. 0x00, 0x04, 0x20,
  49668. /* Hash data */
  49669. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49670. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49671. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49672. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49673. };
  49674. XMEMSET(hash, 0, sizeof(hash));
  49675. AssertNotNull(rsa = wolfSSL_RSA_new());
  49676. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49677. &signatureLen, rsa, 1), 0);
  49678. wolfSSL_RSA_free(rsa);
  49679. der = privDer;
  49680. rsa = NULL;
  49681. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49682. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  49683. -1), 0);
  49684. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  49685. signature, &signatureLen, rsa, 1), 0);
  49686. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49687. &signatureLen, rsa, 1), 0);
  49688. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49689. &signatureLen, rsa, 1), 0);
  49690. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49691. NULL, rsa, 1), 0);
  49692. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49693. &signatureLen, NULL, 1), 0);
  49694. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49695. &signatureLen, rsa, -1), 0);
  49696. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49697. &signatureLen, rsa, 0), 0);
  49698. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49699. &signatureLen, rsa, 0), 0);
  49700. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49701. NULL, rsa, 0), 0);
  49702. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49703. &signatureLen, rsa, 1), 1);
  49704. /* Test returning encoded hash. */
  49705. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  49706. &encodedHashLen, rsa, 0), 1);
  49707. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  49708. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  49709. RSA_free(rsa);
  49710. der = pubDer;
  49711. rsa = NULL;
  49712. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49713. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49714. signatureLen, rsa), 1);
  49715. RSA_free(rsa);
  49716. res = TEST_RES_CHECK(1);
  49717. #endif
  49718. return res;
  49719. }
  49720. static int test_wolfSSL_RSA_public_decrypt(void)
  49721. {
  49722. int res = TEST_SKIPPED;
  49723. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49724. RSA *rsa;
  49725. unsigned char msg[SHA256_DIGEST_LENGTH];
  49726. #ifdef USE_CERT_BUFFERS_1024
  49727. const unsigned char* pubDer = client_keypub_der_1024;
  49728. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49729. unsigned char decMsg[1024/8];
  49730. const unsigned char encMsg[] = {
  49731. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  49732. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  49733. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  49734. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  49735. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  49736. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  49737. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  49738. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  49739. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  49740. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  49741. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  49742. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  49743. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  49744. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  49745. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  49746. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  49747. };
  49748. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49749. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49750. defined(WC_RSA_NO_PADDING)
  49751. const unsigned char encMsgNoPad[] = {
  49752. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  49753. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  49754. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  49755. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  49756. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  49757. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  49758. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  49759. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  49760. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  49761. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  49762. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  49763. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  49764. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  49765. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  49766. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  49767. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  49768. };
  49769. #endif
  49770. #else
  49771. const unsigned char* pubDer = client_keypub_der_2048;
  49772. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49773. unsigned char decMsg[2048/8];
  49774. const unsigned char encMsg[] = {
  49775. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  49776. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  49777. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  49778. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  49779. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  49780. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  49781. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  49782. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  49783. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  49784. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  49785. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  49786. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  49787. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  49788. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  49789. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  49790. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  49791. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  49792. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  49793. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  49794. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  49795. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  49796. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  49797. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  49798. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  49799. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  49800. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  49801. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  49802. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  49803. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  49804. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  49805. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  49806. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  49807. };
  49808. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49809. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49810. defined(WC_RSA_NO_PADDING)
  49811. const unsigned char encMsgNoPad[] = {
  49812. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  49813. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  49814. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  49815. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  49816. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  49817. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  49818. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  49819. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  49820. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  49821. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  49822. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  49823. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  49824. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  49825. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  49826. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  49827. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  49828. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  49829. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  49830. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  49831. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  49832. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  49833. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  49834. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  49835. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  49836. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  49837. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  49838. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  49839. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  49840. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  49841. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  49842. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  49843. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  49844. };
  49845. #endif
  49846. #endif
  49847. const unsigned char* der;
  49848. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49849. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49850. defined(WC_RSA_NO_PADDING)
  49851. int i;
  49852. #endif
  49853. XMEMSET(msg, 0, sizeof(msg));
  49854. der = pubDer;
  49855. rsa = NULL;
  49856. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49857. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  49858. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  49859. RSA_PKCS1_PADDING), -1);
  49860. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  49861. RSA_PKCS1_PADDING), -1);
  49862. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  49863. RSA_PKCS1_PADDING), -1);
  49864. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  49865. RSA_PKCS1_PADDING), -1);
  49866. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  49867. RSA_PKCS1_PSS_PADDING), -1);
  49868. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  49869. RSA_PKCS1_PADDING), 32);
  49870. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  49871. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49872. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49873. defined(WC_RSA_NO_PADDING)
  49874. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  49875. rsa, RSA_NO_PADDING), sizeof(decMsg));
  49876. /* Zeros before actual data. */
  49877. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  49878. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  49879. }
  49880. /* Check actual data. */
  49881. XMEMSET(msg, 0x01, sizeof(msg));
  49882. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  49883. #endif
  49884. RSA_free(rsa);
  49885. res = TEST_RES_CHECK(1);
  49886. #endif
  49887. return res;
  49888. }
  49889. static int test_wolfSSL_RSA_private_encrypt(void)
  49890. {
  49891. int res = TEST_SKIPPED;
  49892. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49893. RSA *rsa;
  49894. unsigned char msg[SHA256_DIGEST_LENGTH];
  49895. #ifdef USE_CERT_BUFFERS_1024
  49896. const unsigned char* privDer = client_key_der_1024;
  49897. size_t privDerSz = sizeof_client_key_der_1024;
  49898. unsigned char encMsg[1024/8];
  49899. const unsigned char expEncMsg[] = {
  49900. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  49901. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  49902. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  49903. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  49904. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  49905. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  49906. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  49907. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  49908. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  49909. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  49910. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  49911. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  49912. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  49913. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  49914. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  49915. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  49916. };
  49917. #ifdef WC_RSA_NO_PADDING
  49918. const unsigned char expEncMsgNoPad[] = {
  49919. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  49920. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  49921. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  49922. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  49923. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  49924. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  49925. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  49926. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  49927. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  49928. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  49929. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  49930. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  49931. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  49932. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  49933. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  49934. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  49935. };
  49936. #endif
  49937. #else
  49938. const unsigned char* privDer = client_key_der_2048;
  49939. size_t privDerSz = sizeof_client_key_der_2048;
  49940. unsigned char encMsg[2048/8];
  49941. const unsigned char expEncMsg[] = {
  49942. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  49943. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  49944. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  49945. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  49946. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  49947. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  49948. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  49949. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  49950. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  49951. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  49952. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  49953. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  49954. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  49955. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  49956. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  49957. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  49958. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  49959. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  49960. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  49961. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  49962. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  49963. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  49964. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  49965. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  49966. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  49967. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  49968. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  49969. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  49970. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  49971. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  49972. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  49973. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  49974. };
  49975. #ifdef WC_RSA_NO_PADDING
  49976. const unsigned char expEncMsgNoPad[] = {
  49977. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  49978. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  49979. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  49980. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  49981. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  49982. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  49983. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  49984. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  49985. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  49986. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  49987. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  49988. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  49989. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  49990. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  49991. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  49992. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  49993. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  49994. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  49995. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  49996. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  49997. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  49998. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  49999. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50000. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50001. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50002. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50003. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50004. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50005. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50006. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50007. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50008. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50009. };
  50010. #endif
  50011. #endif
  50012. const unsigned char* der;
  50013. XMEMSET(msg, 0x00, sizeof(msg));
  50014. der = privDer;
  50015. rsa = NULL;
  50016. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50017. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  50018. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  50019. -1);
  50020. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50021. RSA_PKCS1_PADDING), -1);
  50022. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  50023. RSA_PKCS1_PADDING), -1);
  50024. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  50025. RSA_PKCS1_PADDING), -1);
  50026. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50027. RSA_PKCS1_PSS_PADDING), -1);
  50028. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50029. RSA_PKCS1_PADDING), sizeof(encMsg));
  50030. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  50031. #ifdef WC_RSA_NO_PADDING
  50032. /* Non-zero message. */
  50033. XMEMSET(msg, 0x01, sizeof(msg));
  50034. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50035. RSA_NO_PADDING), sizeof(encMsg));
  50036. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  50037. #endif
  50038. RSA_free(rsa);
  50039. res = TEST_RES_CHECK(1);
  50040. #endif
  50041. return res;
  50042. }
  50043. static int test_wolfSSL_RSA_public_encrypt(void)
  50044. {
  50045. int res = TEST_SKIPPED;
  50046. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50047. RSA* rsa;
  50048. const unsigned char msg[2048/8] = { 0 };
  50049. unsigned char encMsg[2048/8];
  50050. AssertNotNull(rsa = RSA_new());
  50051. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  50052. RSA_PKCS1_PADDING), -1);
  50053. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50054. RSA_PKCS1_PADDING), -1);
  50055. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  50056. RSA_PKCS1_PADDING), -1);
  50057. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  50058. RSA_PKCS1_PADDING), -1);
  50059. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50060. RSA_PKCS1_PSS_PADDING), -1);
  50061. /* Empty RSA key. */
  50062. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50063. RSA_PKCS1_PADDING), -1);
  50064. RSA_free(rsa);
  50065. res = TEST_RES_CHECK(1);
  50066. #endif
  50067. return res;
  50068. }
  50069. static int test_wolfSSL_RSA_private_decrypt(void)
  50070. {
  50071. int res = TEST_SKIPPED;
  50072. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50073. RSA* rsa;
  50074. unsigned char msg[2048/8];
  50075. const unsigned char encMsg[2048/8] = { 0 };
  50076. AssertNotNull(rsa = RSA_new());
  50077. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  50078. RSA_PKCS1_PADDING), -1);
  50079. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  50080. RSA_PKCS1_PADDING), -1);
  50081. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50082. RSA_PKCS1_PADDING), -1);
  50083. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  50084. RSA_PKCS1_PADDING), -1);
  50085. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50086. RSA_PKCS1_PSS_PADDING), -1);
  50087. /* Empty RSA key. */
  50088. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50089. RSA_PKCS1_PADDING), -1);
  50090. RSA_free(rsa);
  50091. res = TEST_RES_CHECK(1);
  50092. #endif
  50093. return res;
  50094. }
  50095. static int test_wolfSSL_RSA_GenAdd(void)
  50096. {
  50097. int res = TEST_SKIPPED;
  50098. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50099. RSA *rsa;
  50100. #ifdef USE_CERT_BUFFERS_1024
  50101. const unsigned char* privDer = client_key_der_1024;
  50102. size_t privDerSz = sizeof_client_key_der_1024;
  50103. const unsigned char* pubDer = client_keypub_der_1024;
  50104. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50105. #else
  50106. const unsigned char* privDer = client_key_der_2048;
  50107. size_t privDerSz = sizeof_client_key_der_2048;
  50108. const unsigned char* pubDer = client_keypub_der_2048;
  50109. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50110. #endif
  50111. const unsigned char* der;
  50112. der = privDer;
  50113. rsa = NULL;
  50114. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50115. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  50116. #ifndef RSA_LOW_MEM
  50117. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  50118. #else
  50119. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  50120. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50121. #endif
  50122. RSA_free(rsa);
  50123. der = pubDer;
  50124. rsa = NULL;
  50125. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50126. /* Need private values. */
  50127. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50128. RSA_free(rsa);
  50129. res = TEST_RES_CHECK(1);
  50130. #endif
  50131. return res;
  50132. }
  50133. static int test_wolfSSL_RSA_blinding_on(void)
  50134. {
  50135. int res = TEST_SKIPPED;
  50136. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  50137. RSA *rsa;
  50138. WOLFSSL_BN_CTX *bnCtx;
  50139. #ifdef USE_CERT_BUFFERS_1024
  50140. const unsigned char* privDer = client_key_der_1024;
  50141. size_t privDerSz = sizeof_client_key_der_1024;
  50142. #else
  50143. const unsigned char* privDer = client_key_der_2048;
  50144. size_t privDerSz = sizeof_client_key_der_2048;
  50145. #endif
  50146. const unsigned char* der;
  50147. der = privDer;
  50148. rsa = NULL;
  50149. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50150. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  50151. /* Does nothing so all parameters are valid. */
  50152. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  50153. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  50154. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  50155. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  50156. wolfSSL_BN_CTX_free(bnCtx);
  50157. RSA_free(rsa);
  50158. res = TEST_RES_CHECK(1);
  50159. #endif
  50160. return res;
  50161. }
  50162. static int test_wolfSSL_RSA_ex_data(void)
  50163. {
  50164. int res = TEST_SKIPPED;
  50165. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  50166. RSA* rsa;
  50167. unsigned char data[1];
  50168. rsa = RSA_new();
  50169. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  50170. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50171. #ifdef MAX_EX_DATA
  50172. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  50173. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  50174. #endif
  50175. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  50176. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  50177. #ifdef HAVE_EX_DATA
  50178. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  50179. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  50180. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  50181. #else
  50182. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  50183. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  50184. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50185. #endif
  50186. RSA_free(rsa);
  50187. res = TEST_RES_CHECK(1);
  50188. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50189. return res;
  50190. }
  50191. static int test_wolfSSL_RSA_LoadDer(void)
  50192. {
  50193. int res = TEST_SKIPPED;
  50194. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  50195. defined(OPENSSL_EXTRA_X509_SMALL))
  50196. RSA *rsa;
  50197. #ifdef USE_CERT_BUFFERS_1024
  50198. const unsigned char* privDer = client_key_der_1024;
  50199. size_t privDerSz = sizeof_client_key_der_1024;
  50200. #else
  50201. const unsigned char* privDer = client_key_der_2048;
  50202. size_t privDerSz = sizeof_client_key_der_2048;
  50203. #endif
  50204. AssertNotNull(rsa = RSA_new());
  50205. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  50206. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  50207. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  50208. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  50209. RSA_free(rsa);
  50210. res = TEST_RES_CHECK(1);
  50211. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50212. return res;
  50213. }
  50214. /* Local API. */
  50215. static int test_wolfSSL_RSA_To_Der(void)
  50216. {
  50217. int res = TEST_SKIPPED;
  50218. #ifdef WOLFSSL_TEST_STATIC_BUILD
  50219. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  50220. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50221. RSA* rsa;
  50222. #ifdef USE_CERT_BUFFERS_1024
  50223. const unsigned char* privDer = client_key_der_1024;
  50224. size_t privDerSz = sizeof_client_key_der_1024;
  50225. const unsigned char* pubDer = client_keypub_der_1024;
  50226. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50227. unsigned char out[sizeof(client_key_der_1024)];
  50228. #else
  50229. const unsigned char* privDer = client_key_der_2048;
  50230. size_t privDerSz = sizeof_client_key_der_2048;
  50231. const unsigned char* pubDer = client_keypub_der_2048;
  50232. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50233. unsigned char out[sizeof(client_key_der_2048)];
  50234. #endif
  50235. const unsigned char* der;
  50236. unsigned char* outDer = NULL;
  50237. der = privDer;
  50238. rsa = NULL;
  50239. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50240. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50241. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  50242. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  50243. outDer = out;
  50244. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50245. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  50246. outDer = NULL;
  50247. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50248. AssertNotNull(outDer);
  50249. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  50250. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  50251. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  50252. outDer = out;
  50253. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  50254. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  50255. RSA_free(rsa);
  50256. AssertNotNull(rsa = RSA_new());
  50257. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50258. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  50259. RSA_free(rsa);
  50260. der = pubDer;
  50261. rsa = NULL;
  50262. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50263. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50264. RSA_free(rsa);
  50265. res = TEST_RES_CHECK(1);
  50266. #endif
  50267. #endif
  50268. return res;
  50269. }
  50270. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50271. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  50272. {
  50273. int res = TEST_SKIPPED;
  50274. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  50275. XFILE file;
  50276. const char* fname = "./certs/server-keyPub.pem";
  50277. RSA *rsa;
  50278. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  50279. file = XFOPEN(fname, "rb");
  50280. AssertTrue((file != XBADFILE));
  50281. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  50282. AssertIntEQ(RSA_size(rsa), 256);
  50283. RSA_free(rsa);
  50284. XFCLOSE(file);
  50285. res = TEST_RES_CHECK(1);
  50286. #endif
  50287. return res;
  50288. }
  50289. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50290. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  50291. {
  50292. int res = TEST_SKIPPED;
  50293. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50294. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  50295. RSA* rsa = NULL;
  50296. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  50297. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  50298. /* Valid but stub so returns 0. */
  50299. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  50300. res = TEST_RES_CHECK(1);
  50301. #endif
  50302. return res;
  50303. }
  50304. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  50305. {
  50306. int res = TEST_SKIPPED;
  50307. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50308. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50309. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  50310. RSA* rsa;
  50311. #ifdef USE_CERT_BUFFERS_1024
  50312. const unsigned char* privDer = client_key_der_1024;
  50313. size_t privDerSz = sizeof_client_key_der_1024;
  50314. #else
  50315. const unsigned char* privDer = client_key_der_2048;
  50316. size_t privDerSz = sizeof_client_key_der_2048;
  50317. #endif
  50318. const unsigned char* der;
  50319. #ifndef NO_AES
  50320. unsigned char passwd[] = "password";
  50321. #endif
  50322. AssertNotNull(rsa = RSA_new());
  50323. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50324. NULL, NULL), 0);
  50325. RSA_free(rsa);
  50326. der = privDer;
  50327. rsa = NULL;
  50328. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50329. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  50330. NULL, NULL), 0);
  50331. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  50332. NULL, NULL), 0);
  50333. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50334. NULL, NULL), 1);
  50335. #ifndef NO_AES
  50336. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50337. NULL, 0, NULL, NULL), 1);
  50338. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50339. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  50340. #endif
  50341. RSA_free(rsa);
  50342. res = TEST_RES_CHECK(1);
  50343. #endif
  50344. return res;
  50345. }
  50346. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  50347. {
  50348. int res = TEST_SKIPPED;
  50349. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50350. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50351. defined(WOLFSSL_DER_TO_PEM))
  50352. RSA* rsa;
  50353. #ifdef USE_CERT_BUFFERS_1024
  50354. const unsigned char* privDer = client_key_der_1024;
  50355. size_t privDerSz = sizeof_client_key_der_1024;
  50356. #else
  50357. const unsigned char* privDer = client_key_der_2048;
  50358. size_t privDerSz = sizeof_client_key_der_2048;
  50359. #endif
  50360. const unsigned char* der;
  50361. #ifndef NO_AES
  50362. unsigned char passwd[] = "password";
  50363. #endif
  50364. unsigned char* pem;
  50365. int plen;
  50366. AssertNotNull(rsa = RSA_new());
  50367. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50368. &plen), 0);
  50369. RSA_free(rsa);
  50370. der = privDer;
  50371. rsa = NULL;
  50372. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50373. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  50374. &plen), 0);
  50375. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  50376. &plen), 0);
  50377. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50378. NULL), 0);
  50379. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50380. &plen), 1);
  50381. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50382. #ifndef NO_AES
  50383. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50384. NULL, 0, &pem, &plen), 1);
  50385. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50386. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50387. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  50388. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50389. #endif
  50390. RSA_free(rsa);
  50391. res = TEST_RES_CHECK(1);
  50392. #endif
  50393. return res;
  50394. }
  50395. static int test_wolfSSL_DH(void)
  50396. {
  50397. int res = TEST_SKIPPED;
  50398. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50399. DH *dh = NULL;
  50400. BIGNUM* p;
  50401. BIGNUM* q;
  50402. BIGNUM* g;
  50403. BIGNUM* pub;
  50404. BIGNUM* priv;
  50405. #if defined(OPENSSL_ALL)
  50406. #if !defined(HAVE_FIPS) || \
  50407. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  50408. FILE* f = NULL;
  50409. unsigned char buf[268];
  50410. const unsigned char* pt = buf;
  50411. long len = 0;
  50412. dh = NULL;
  50413. XMEMSET(buf, 0, sizeof(buf));
  50414. /* Test 2048 bit parameters */
  50415. f = XFOPEN("./certs/dh2048.der", "rb");
  50416. AssertTrue(f != XBADFILE);
  50417. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  50418. XFCLOSE(f);
  50419. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  50420. AssertNotNull(dh->p);
  50421. AssertNotNull(dh->g);
  50422. AssertTrue(pt == buf);
  50423. AssertIntEQ(DH_generate_key(dh), 1);
  50424. AssertIntEQ(DH_generate_key(dh), 1);
  50425. AssertIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  50426. AssertNotNull(pub = BN_new());
  50427. AssertIntEQ(BN_set_word(pub, 1), 1);
  50428. AssertIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  50429. AssertIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  50430. AssertIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  50431. AssertIntEQ(DH_compute_key(buf, pub, NULL), -1);
  50432. AssertIntEQ(DH_compute_key(buf, NULL, dh), -1);
  50433. AssertIntEQ(DH_compute_key(NULL, pub, dh), -1);
  50434. AssertIntEQ(DH_compute_key(buf, pub, dh), -1);
  50435. BN_free(pub);
  50436. DH_get0_pqg(dh, (const BIGNUM**)&p,
  50437. (const BIGNUM**)&q,
  50438. (const BIGNUM**)&g);
  50439. AssertPtrEq(p, dh->p);
  50440. AssertPtrEq(q, dh->q);
  50441. AssertPtrEq(g, dh->g);
  50442. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  50443. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  50444. AssertPtrEq(pub, dh->pub_key);
  50445. AssertPtrEq(priv, dh->priv_key);
  50446. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  50447. AssertPtrEq(pub, dh->pub_key);
  50448. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  50449. AssertPtrEq(priv, dh->priv_key);
  50450. AssertNotNull(pub = BN_new());
  50451. AssertNotNull(priv = BN_new());
  50452. AssertIntEQ(DH_set0_key(NULL, pub, priv), 0);
  50453. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  50454. AssertNotNull(pub = BN_new());
  50455. AssertIntEQ(DH_set0_key(dh, pub, NULL), 1);
  50456. AssertNotNull(priv = BN_new());
  50457. AssertIntEQ(DH_set0_key(dh, NULL, priv), 1);
  50458. AssertPtrEq(pub, dh->pub_key);
  50459. AssertPtrEq(priv, dh->priv_key);
  50460. DH_free(dh);
  50461. AssertNotNull(dh = DH_new());
  50462. AssertNotNull(p = BN_new());
  50463. AssertIntEQ(BN_set_word(p, 1), 1);
  50464. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  50465. AssertNotNull(pub = BN_new());
  50466. AssertNotNull(priv = BN_new());
  50467. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  50468. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  50469. BN_free(p);
  50470. DH_free(dh);
  50471. #ifdef WOLFSSL_KEY_GEN
  50472. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  50473. AssertIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  50474. DH_free(dh);
  50475. #endif
  50476. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  50477. #endif /* OPENSSL_ALL */
  50478. (void)dh;
  50479. (void)p;
  50480. (void)q;
  50481. (void)g;
  50482. (void)pub;
  50483. (void)priv;
  50484. dh = wolfSSL_DH_new();
  50485. AssertNotNull(dh);
  50486. /* invalid parameters test */
  50487. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  50488. (const BIGNUM**)&q,
  50489. (const BIGNUM**)&g);
  50490. DH_get0_pqg(dh, NULL,
  50491. (const BIGNUM**)&q,
  50492. (const BIGNUM**)&g);
  50493. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  50494. DH_get0_pqg(dh, NULL, NULL, NULL);
  50495. AssertTrue(1);
  50496. DH_get0_pqg(dh, (const BIGNUM**)&p,
  50497. (const BIGNUM**)&q,
  50498. (const BIGNUM**)&g);
  50499. AssertPtrEq(p, NULL);
  50500. AssertPtrEq(q, NULL);
  50501. AssertPtrEq(g, NULL);
  50502. DH_free(dh);
  50503. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  50504. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  50505. #if defined(OPENSSL_ALL) || \
  50506. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50507. dh = wolfSSL_DH_new();
  50508. AssertNotNull(dh);
  50509. p = wolfSSL_BN_new();
  50510. AssertNotNull(p);
  50511. AssertIntEQ(BN_set_word(p, 11), 1);
  50512. g = wolfSSL_BN_new();
  50513. AssertNotNull(g);
  50514. AssertIntEQ(BN_set_word(g, 2), 1);
  50515. q = wolfSSL_BN_new();
  50516. AssertNotNull(q);
  50517. AssertIntEQ(BN_set_word(q, 5), 1);
  50518. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  50519. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  50520. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  50521. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  50522. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  50523. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  50524. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  50525. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  50526. /* Don't need q. */
  50527. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50528. /* Setting again will free the p and g. */
  50529. wolfSSL_BN_free(q);
  50530. DH_free(dh);
  50531. dh = wolfSSL_DH_new();
  50532. AssertNotNull(dh);
  50533. p = wolfSSL_BN_new();
  50534. AssertNotNull(p);
  50535. AssertIntEQ(BN_set_word(p, 11), 1);
  50536. g = wolfSSL_BN_new();
  50537. AssertNotNull(g);
  50538. AssertIntEQ(BN_set_word(g, 2), 1);
  50539. q = wolfSSL_BN_new();
  50540. AssertNotNull(q);
  50541. AssertIntEQ(BN_set_word(q, 5), 1);
  50542. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  50543. /* p, q and g are now owned by dh - don't free. */
  50544. p = wolfSSL_BN_new();
  50545. AssertNotNull(p);
  50546. AssertIntEQ(BN_set_word(p, 11), 1);
  50547. g = wolfSSL_BN_new();
  50548. AssertNotNull(g);
  50549. AssertIntEQ(BN_set_word(g, 2), 1);
  50550. q = wolfSSL_BN_new();
  50551. AssertNotNull(q);
  50552. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  50553. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  50554. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  50555. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  50556. /* p, q and g are now owned by dh - don't free. */
  50557. DH_free(dh);
  50558. AssertIntEQ(DH_generate_key(NULL), 0);
  50559. AssertNotNull(dh = DH_new());
  50560. AssertIntEQ(DH_generate_key(dh), 0);
  50561. p = wolfSSL_BN_new();
  50562. AssertNotNull(p);
  50563. AssertIntEQ(BN_set_word(p, 0), 1);
  50564. g = wolfSSL_BN_new();
  50565. AssertNotNull(g);
  50566. AssertIntEQ(BN_set_word(g, 2), 1);
  50567. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50568. AssertIntEQ(DH_generate_key(dh), 0);
  50569. DH_free(dh);
  50570. #endif
  50571. #endif
  50572. /* Test DH_up_ref() */
  50573. dh = wolfSSL_DH_new();
  50574. AssertNotNull(dh);
  50575. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  50576. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  50577. DH_free(dh); /* decrease ref count */
  50578. DH_free(dh); /* free WOLFSSL_DH */
  50579. AssertNull((dh = DH_new_by_nid(NID_sha1)));
  50580. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  50581. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  50582. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  50583. #ifdef HAVE_FFDHE_2048
  50584. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50585. DH_free(dh);
  50586. #endif
  50587. #ifdef HAVE_FFDHE_3072
  50588. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  50589. DH_free(dh);
  50590. #endif
  50591. #ifdef HAVE_FFDHE_4096
  50592. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  50593. DH_free(dh);
  50594. #endif
  50595. #else
  50596. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50597. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  50598. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  50599. AssertIntEQ(wolfSSL_DH_size(NULL), -1);
  50600. res = TEST_RES_CHECK(1);
  50601. #endif /* OPENSSL_EXTRA && !NO_DH */
  50602. return res;
  50603. }
  50604. static int test_wolfSSL_DH_dup(void)
  50605. {
  50606. int res = TEST_SKIPPED;
  50607. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  50608. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  50609. defined(OPENSSL_EXTRA)
  50610. DH *dh;
  50611. DH *dhDup;
  50612. WOLFSSL_BIGNUM* p;
  50613. WOLFSSL_BIGNUM* g;
  50614. AssertNotNull(p = wolfSSL_BN_new());
  50615. AssertNotNull(g = wolfSSL_BN_new());
  50616. AssertIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  50617. AssertIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  50618. dhDup = wolfSSL_DH_dup(NULL);
  50619. AssertNull(dhDup);
  50620. dh = wolfSSL_DH_new();
  50621. AssertNotNull(dh);
  50622. dhDup = wolfSSL_DH_dup(dh);
  50623. AssertNull(dhDup);
  50624. #if defined(OPENSSL_ALL) || \
  50625. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50626. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50627. dhDup = wolfSSL_DH_dup(dh);
  50628. AssertNotNull(dhDup);
  50629. wolfSSL_DH_free(dhDup);
  50630. #else
  50631. wolfSSL_BN_free(p);
  50632. wolfSSL_BN_free(g);
  50633. #endif
  50634. wolfSSL_DH_free(dh);
  50635. res = TEST_RES_CHECK(1);
  50636. #endif
  50637. #endif
  50638. return res;
  50639. }
  50640. static int test_wolfSSL_DH_check(void)
  50641. {
  50642. int res = TEST_SKIPPED;
  50643. #ifdef OPENSSL_ALL
  50644. #ifndef NO_DH
  50645. #ifndef NO_BIO
  50646. #ifndef NO_DSA
  50647. byte buf[6000];
  50648. char file[] = "./certs/dsaparams.pem";
  50649. XFILE f;
  50650. int bytes;
  50651. BIO* bio;
  50652. DSA* dsa;
  50653. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50654. static const byte dh2048[] = {
  50655. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  50656. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  50657. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  50658. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  50659. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  50660. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  50661. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  50662. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  50663. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  50664. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  50665. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  50666. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  50667. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  50668. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  50669. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  50670. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  50671. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  50672. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  50673. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  50674. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  50675. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  50676. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  50677. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  50678. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  50679. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  50680. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  50681. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  50682. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  50683. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  50684. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  50685. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  50686. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  50687. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  50688. 0x93, 0x02, 0x01, 0x02
  50689. };
  50690. const byte* params;
  50691. #endif
  50692. DH* dh = NULL;
  50693. WOLFSSL_BIGNUM* p;
  50694. WOLFSSL_BIGNUM* g;
  50695. WOLFSSL_BIGNUM* pTmp = NULL;
  50696. WOLFSSL_BIGNUM* gTmp = NULL;
  50697. int codes = -1;
  50698. #ifndef NO_DSA
  50699. /* Initialize DH */
  50700. f = XFOPEN(file, "rb");
  50701. AssertTrue((f != XBADFILE));
  50702. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  50703. XFCLOSE(f);
  50704. bio = BIO_new_mem_buf((void*)buf, bytes);
  50705. AssertNotNull(bio);
  50706. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  50707. AssertNotNull(dsa);
  50708. dh = wolfSSL_DSA_dup_DH(dsa);
  50709. AssertNotNull(dh);
  50710. BIO_free(bio);
  50711. DSA_free(dsa);
  50712. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50713. params = dh2048;
  50714. dh = wolfSSL_d2i_DHparams(NULL, &params, (long)sizeof(dh2048));
  50715. AssertNotNull(dh);
  50716. #else
  50717. dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048);
  50718. AssertNotNull(dh);
  50719. #endif
  50720. /* Test assumed to be valid dh.
  50721. * Should return WOLFSSL_SUCCESS
  50722. * codes should be 0
  50723. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  50724. */
  50725. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50726. AssertIntEQ(codes, 0);
  50727. /* Test NULL dh: expected BAD_FUNC_ARG */
  50728. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  50729. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  50730. pTmp = dh->p;
  50731. dh->p = NULL;
  50732. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50733. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50734. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50735. /* set dh->p back to normal so it wont fail on next tests */
  50736. dh->p = pTmp;
  50737. pTmp = NULL;
  50738. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  50739. gTmp = dh->g;
  50740. dh->g = NULL;
  50741. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50742. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50743. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  50744. dh->g = gTmp;
  50745. gTmp = NULL;
  50746. /* Cleanup */
  50747. DH_free(dh);
  50748. dh = DH_new();
  50749. AssertNotNull(dh);
  50750. /* Check empty DH. */
  50751. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50752. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50753. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  50754. /* Check non-prime valued p. */
  50755. AssertNotNull(p = BN_new());
  50756. AssertIntEQ(BN_set_word(p, 4), 1);
  50757. AssertNotNull(g = BN_new());
  50758. AssertIntEQ(BN_set_word(g, 2), 1);
  50759. AssertIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  50760. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50761. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50762. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50763. DH_free(dh);
  50764. res = TEST_RES_CHECK(1);
  50765. #endif
  50766. #endif /* !NO_DH && !NO_DSA */
  50767. #endif
  50768. return res;
  50769. }
  50770. static int test_wolfSSL_DH_prime(void)
  50771. {
  50772. int res = TEST_SKIPPED;
  50773. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50774. WOLFSSL_BIGNUM* bn;
  50775. #if WOLFSSL_MAX_BN_BITS >= 768
  50776. WOLFSSL_BIGNUM* bn2;
  50777. #endif
  50778. bn = wolfSSL_DH_768_prime(NULL);
  50779. #if WOLFSSL_MAX_BN_BITS >= 768
  50780. AssertNotNull(bn);
  50781. bn2 = wolfSSL_DH_768_prime(bn);
  50782. AssertNotNull(bn2);
  50783. AssertTrue(bn == bn2);
  50784. wolfSSL_BN_free(bn);
  50785. #else
  50786. AssertNull(bn);
  50787. #endif
  50788. bn = wolfSSL_DH_1024_prime(NULL);
  50789. #if WOLFSSL_MAX_BN_BITS >= 1024
  50790. AssertNotNull(bn);
  50791. wolfSSL_BN_free(bn);
  50792. #else
  50793. AssertNull(bn);
  50794. #endif
  50795. bn = wolfSSL_DH_2048_prime(NULL);
  50796. #if WOLFSSL_MAX_BN_BITS >= 2048
  50797. AssertNotNull(bn);
  50798. wolfSSL_BN_free(bn);
  50799. #else
  50800. AssertNull(bn);
  50801. #endif
  50802. bn = wolfSSL_DH_3072_prime(NULL);
  50803. #if WOLFSSL_MAX_BN_BITS >= 3072
  50804. AssertNotNull(bn);
  50805. wolfSSL_BN_free(bn);
  50806. #else
  50807. AssertNull(bn);
  50808. #endif
  50809. bn = wolfSSL_DH_4096_prime(NULL);
  50810. #if WOLFSSL_MAX_BN_BITS >= 4096
  50811. AssertNotNull(bn);
  50812. wolfSSL_BN_free(bn);
  50813. #else
  50814. AssertNull(bn);
  50815. #endif
  50816. bn = wolfSSL_DH_6144_prime(NULL);
  50817. #if WOLFSSL_MAX_BN_BITS >= 6144
  50818. AssertNotNull(bn);
  50819. wolfSSL_BN_free(bn);
  50820. #else
  50821. AssertNull(bn);
  50822. #endif
  50823. bn = wolfSSL_DH_8192_prime(NULL);
  50824. #if WOLFSSL_MAX_BN_BITS >= 8192
  50825. AssertNotNull(bn);
  50826. wolfSSL_BN_free(bn);
  50827. #else
  50828. AssertNull(bn);
  50829. #endif
  50830. res = TEST_RES_CHECK(1);
  50831. #endif
  50832. return res;
  50833. }
  50834. static int test_wolfSSL_DH_1536_prime(void)
  50835. {
  50836. int res = TEST_SKIPPED;
  50837. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50838. BIGNUM* bn;
  50839. unsigned char bits[200];
  50840. int sz = 192; /* known binary size */
  50841. const byte expected[] = {
  50842. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  50843. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  50844. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  50845. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  50846. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  50847. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  50848. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  50849. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  50850. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  50851. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  50852. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  50853. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  50854. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  50855. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  50856. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  50857. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  50858. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  50859. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  50860. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  50861. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  50862. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  50863. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  50864. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  50865. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  50866. };
  50867. bn = get_rfc3526_prime_1536(NULL);
  50868. AssertNotNull(bn);
  50869. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  50870. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  50871. BN_free(bn);
  50872. res = TEST_RES_CHECK(1);
  50873. #endif
  50874. return res;
  50875. }
  50876. static int test_wolfSSL_DH_get_2048_256(void)
  50877. {
  50878. int res = TEST_SKIPPED;
  50879. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50880. WOLFSSL_DH* dh;
  50881. const WOLFSSL_BIGNUM* pBn;
  50882. const WOLFSSL_BIGNUM* gBn;
  50883. const WOLFSSL_BIGNUM* qBn;
  50884. const byte pExpected[] = {
  50885. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  50886. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  50887. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  50888. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  50889. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  50890. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  50891. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  50892. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  50893. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  50894. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  50895. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  50896. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  50897. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  50898. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  50899. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  50900. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  50901. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  50902. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  50903. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  50904. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  50905. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  50906. 0x1E, 0x1A, 0x15, 0x97
  50907. };
  50908. const byte gExpected[] = {
  50909. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  50910. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  50911. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  50912. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  50913. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  50914. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  50915. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  50916. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  50917. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  50918. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  50919. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  50920. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  50921. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  50922. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  50923. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  50924. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  50925. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  50926. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  50927. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  50928. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  50929. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  50930. 0x6C, 0xC4, 0x16, 0x59
  50931. };
  50932. const byte qExpected[] = {
  50933. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  50934. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  50935. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  50936. };
  50937. int pSz;
  50938. int qSz;
  50939. int gSz;
  50940. byte* pReturned;
  50941. byte* qReturned;
  50942. byte* gReturned;
  50943. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  50944. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  50945. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  50946. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  50947. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  50948. AssertIntEQ(pSz, sizeof(pExpected));
  50949. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  50950. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  50951. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  50952. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  50953. AssertIntEQ(qSz, sizeof(qExpected));
  50954. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  50955. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  50956. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  50957. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  50958. AssertIntEQ(gSz, sizeof(gExpected));
  50959. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  50960. wolfSSL_DH_free(dh);
  50961. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50962. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50963. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50964. res = TEST_RES_CHECK(1);
  50965. #endif
  50966. return res;
  50967. }
  50968. static int test_wolfSSL_PEM_write_DHparams(void)
  50969. {
  50970. int res = TEST_SKIPPED;
  50971. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  50972. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  50973. DH* dh;
  50974. BIO* bio;
  50975. XFILE fp;
  50976. byte pem[2048];
  50977. int pemSz;
  50978. const char expected[] =
  50979. "-----BEGIN DH PARAMETERS-----\n"
  50980. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  50981. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  50982. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  50983. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  50984. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  50985. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  50986. "-----END DH PARAMETERS-----\n";
  50987. const char badPem[] =
  50988. "-----BEGIN DH PARAMETERS-----\n"
  50989. "-----END DH PARAMETERS-----\n";
  50990. const char emptySeqPem[] =
  50991. "-----BEGIN DH PARAMETERS-----\n"
  50992. "MAA=\n"
  50993. "-----END DH PARAMETERS-----\n";
  50994. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  50995. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  50996. XFCLOSE(fp);
  50997. AssertNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  50998. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  50999. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51000. AssertIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  51001. (int)sizeof(badPem));
  51002. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51003. BIO_free(bio);
  51004. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  51005. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51006. AssertIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  51007. (int)sizeof(emptySeqPem));
  51008. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51009. BIO_free(bio);
  51010. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  51011. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  51012. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51013. BIO_free(bio);
  51014. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  51015. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  51016. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  51017. DH_free(dh);
  51018. dh = wolfSSL_DH_new();
  51019. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  51020. XFCLOSE(fp);
  51021. wolfSSL_DH_free(dh);
  51022. /* check results */
  51023. XMEMSET(pem, 0, sizeof(pem));
  51024. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  51025. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51026. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  51027. XFCLOSE(fp);
  51028. res = TEST_RES_CHECK(1);
  51029. #endif
  51030. return res;
  51031. }
  51032. static int test_wolfSSL_d2i_DHparams(void)
  51033. {
  51034. int res = TEST_SKIPPED;
  51035. #ifdef OPENSSL_ALL
  51036. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51037. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51038. FILE* f = NULL;
  51039. unsigned char buf[4096];
  51040. const unsigned char* pt = buf;
  51041. #ifdef HAVE_FFDHE_2048
  51042. const char* params1 = "./certs/dh2048.der";
  51043. #endif
  51044. #ifdef HAVE_FFDHE_3072
  51045. const char* params2 = "./certs/dh3072.der";
  51046. #endif
  51047. long len = 0;
  51048. WOLFSSL_DH* dh = NULL;
  51049. XMEMSET(buf, 0, sizeof(buf));
  51050. /* Test 2048 bit parameters */
  51051. #ifdef HAVE_FFDHE_2048
  51052. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  51053. f = XFOPEN(params1, "rb");
  51054. AssertTrue(f != XBADFILE);
  51055. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51056. XFCLOSE(f);
  51057. /* Valid case */
  51058. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51059. AssertNotNull(dh->p);
  51060. AssertNotNull(dh->g);
  51061. AssertTrue(pt == buf);
  51062. AssertIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  51063. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  51064. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  51065. /* Invalid cases */
  51066. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51067. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51068. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  51069. DH_free(dh);
  51070. *buf = 0;
  51071. pt = buf;
  51072. res = TEST_RES_CHECK(1);
  51073. }
  51074. #endif /* HAVE_FFDHE_2048 */
  51075. /* Test 3072 bit parameters */
  51076. #ifdef HAVE_FFDHE_3072
  51077. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  51078. f = XFOPEN(params2, "rb");
  51079. AssertTrue(f != XBADFILE);
  51080. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51081. XFCLOSE(f);
  51082. /* Valid case */
  51083. AssertNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  51084. AssertNotNull(dh->p);
  51085. AssertNotNull(dh->g);
  51086. AssertTrue(pt != buf);
  51087. AssertIntEQ(DH_generate_key(dh), 1);
  51088. /* Invalid cases */
  51089. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51090. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51091. DH_free(dh);
  51092. res = TEST_RES_CHECK(1);
  51093. }
  51094. #endif /* HAVE_FFDHE_3072 */
  51095. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51096. #endif /* !NO_DH */
  51097. #endif
  51098. return res;
  51099. }
  51100. static int test_wolfSSL_DH_LoadDer(void)
  51101. {
  51102. int res = TEST_SKIPPED;
  51103. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  51104. defined(OPENSSL_EXTRA)
  51105. static const byte dh2048[] = {
  51106. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  51107. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  51108. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  51109. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  51110. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  51111. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  51112. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  51113. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  51114. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  51115. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  51116. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  51117. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  51118. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  51119. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  51120. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  51121. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  51122. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  51123. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  51124. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  51125. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  51126. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  51127. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  51128. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  51129. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  51130. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  51131. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  51132. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  51133. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  51134. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  51135. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  51136. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  51137. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  51138. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  51139. 0x93, 0x02, 0x01, 0x02
  51140. };
  51141. WOLFSSL_DH* dh;
  51142. dh = wolfSSL_DH_new();
  51143. AssertNotNull(dh);
  51144. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  51145. AssertIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  51146. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  51147. AssertIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  51148. wolfSSL_DH_free(dh);
  51149. res = TEST_RES_CHECK(1);
  51150. #endif
  51151. return res;
  51152. }
  51153. static int test_wolfSSL_i2d_DHparams(void)
  51154. {
  51155. int res = TEST_SKIPPED;
  51156. #ifdef OPENSSL_ALL
  51157. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51158. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51159. FILE* f;
  51160. unsigned char buf[4096];
  51161. const unsigned char* pt;
  51162. unsigned char* pt2;
  51163. #ifdef HAVE_FFDHE_2048
  51164. const char* params1 = "./certs/dh2048.der";
  51165. #endif
  51166. #ifdef HAVE_FFDHE_3072
  51167. const char* params2 = "./certs/dh3072.der";
  51168. #endif
  51169. long len;
  51170. WOLFSSL_DH* dh;
  51171. /* Test 2048 bit parameters */
  51172. #ifdef HAVE_FFDHE_2048
  51173. pt = buf;
  51174. pt2 = buf;
  51175. f = XFOPEN(params1, "rb");
  51176. AssertTrue(f != XBADFILE);
  51177. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51178. XFCLOSE(f);
  51179. /* Valid case */
  51180. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51181. AssertTrue(pt == buf);
  51182. AssertIntEQ(DH_generate_key(dh), 1);
  51183. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  51184. /* Invalid case */
  51185. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51186. /* Return length only */
  51187. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  51188. DH_free(dh);
  51189. *buf = 0;
  51190. #endif
  51191. /* Test 3072 bit parameters */
  51192. #ifdef HAVE_FFDHE_3072
  51193. pt = buf;
  51194. pt2 = buf;
  51195. f = XFOPEN(params2, "rb");
  51196. AssertTrue(f != XBADFILE);
  51197. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51198. XFCLOSE(f);
  51199. /* Valid case */
  51200. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51201. AssertTrue(pt == buf);
  51202. AssertIntEQ(DH_generate_key(dh), 1);
  51203. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  51204. /* Invalid case */
  51205. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51206. /* Return length only */
  51207. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  51208. DH_free(dh);
  51209. #endif
  51210. dh = DH_new();
  51211. AssertNotNull(dh);
  51212. pt2 = buf;
  51213. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  51214. DH_free(dh);
  51215. res = TEST_RES_CHECK(1);
  51216. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51217. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  51218. #endif
  51219. return res;
  51220. }
  51221. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  51222. /*----------------------------------------------------------------------------*
  51223. | EC
  51224. *----------------------------------------------------------------------------*/
  51225. static int test_wolfSSL_EC_GROUP(void)
  51226. {
  51227. int res = TEST_SKIPPED;
  51228. #ifdef OPENSSL_EXTRA
  51229. EC_GROUP *group;
  51230. EC_GROUP *group2;
  51231. EC_GROUP *group3;
  51232. #ifndef HAVE_ECC_BRAINPOOL
  51233. EC_GROUP *group4;
  51234. #endif
  51235. WOLFSSL_BIGNUM* order;
  51236. int group_bits;
  51237. int i;
  51238. static const int knownEccNids[] = {
  51239. NID_X9_62_prime192v1,
  51240. NID_X9_62_prime192v2,
  51241. NID_X9_62_prime192v3,
  51242. NID_X9_62_prime239v1,
  51243. NID_X9_62_prime239v2,
  51244. NID_X9_62_prime239v3,
  51245. NID_X9_62_prime256v1,
  51246. NID_secp112r1,
  51247. NID_secp112r2,
  51248. NID_secp128r1,
  51249. NID_secp128r2,
  51250. NID_secp160r1,
  51251. NID_secp160r2,
  51252. NID_secp224r1,
  51253. NID_secp384r1,
  51254. NID_secp521r1,
  51255. NID_secp160k1,
  51256. NID_secp192k1,
  51257. NID_secp224k1,
  51258. NID_secp256k1,
  51259. NID_brainpoolP160r1,
  51260. NID_brainpoolP192r1,
  51261. NID_brainpoolP224r1,
  51262. NID_brainpoolP256r1,
  51263. NID_brainpoolP320r1,
  51264. NID_brainpoolP384r1,
  51265. NID_brainpoolP512r1,
  51266. };
  51267. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  51268. static const int knownEccEnums[] = {
  51269. ECC_SECP192R1,
  51270. ECC_PRIME192V2,
  51271. ECC_PRIME192V3,
  51272. ECC_PRIME239V1,
  51273. ECC_PRIME239V2,
  51274. ECC_PRIME239V3,
  51275. ECC_SECP256R1,
  51276. ECC_SECP112R1,
  51277. ECC_SECP112R2,
  51278. ECC_SECP128R1,
  51279. ECC_SECP128R2,
  51280. ECC_SECP160R1,
  51281. ECC_SECP160R2,
  51282. ECC_SECP224R1,
  51283. ECC_SECP384R1,
  51284. ECC_SECP521R1,
  51285. ECC_SECP160K1,
  51286. ECC_SECP192K1,
  51287. ECC_SECP224K1,
  51288. ECC_SECP256K1,
  51289. ECC_BRAINPOOLP160R1,
  51290. ECC_BRAINPOOLP192R1,
  51291. ECC_BRAINPOOLP224R1,
  51292. ECC_BRAINPOOLP256R1,
  51293. ECC_BRAINPOOLP320R1,
  51294. ECC_BRAINPOOLP384R1,
  51295. ECC_BRAINPOOLP512R1,
  51296. };
  51297. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  51298. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51299. AssertNotNull(group2 = EC_GROUP_dup(group));
  51300. AssertNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  51301. #ifndef HAVE_ECC_BRAINPOOL
  51302. AssertNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  51303. NID_brainpoolP256r1));
  51304. #endif
  51305. AssertNull(EC_GROUP_dup(NULL));
  51306. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  51307. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  51308. AssertIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  51309. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  51310. #ifndef HAVE_ECC_BRAINPOOL
  51311. AssertIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  51312. #endif
  51313. AssertIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  51314. AssertIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  51315. AssertNotNull(order = BN_new());
  51316. AssertIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  51317. AssertIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  51318. AssertIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  51319. AssertIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  51320. wolfSSL_BN_free(order);
  51321. AssertNotNull(EC_GROUP_method_of(group));
  51322. AssertIntEQ(EC_METHOD_get_field_type(NULL), 0);
  51323. AssertIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  51324. NID_X9_62_prime_field);
  51325. AssertIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  51326. AssertIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  51327. AssertIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  51328. AssertIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  51329. #ifndef NO_WOLFSSL_STUB
  51330. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  51331. #endif
  51332. #ifndef HAVE_ECC_BRAINPOOL
  51333. EC_GROUP_free(group4);
  51334. #endif
  51335. EC_GROUP_free(group3);
  51336. EC_GROUP_free(group2);
  51337. EC_GROUP_free(group);
  51338. for (i = 0; i < knowEccNidsLen; i++) {
  51339. AssertNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  51340. AssertIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  51341. EC_GROUP_free(group);
  51342. }
  51343. for (i = 0; i < knowEccEnumsLen; i++) {
  51344. AssertNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  51345. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  51346. EC_GROUP_free(group);
  51347. }
  51348. res = TEST_RES_CHECK(1);
  51349. #endif
  51350. return res;
  51351. }
  51352. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  51353. {
  51354. int res = TEST_SKIPPED;
  51355. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51356. EC_GROUP *group;
  51357. BIO* bio;
  51358. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  51359. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  51360. EC_GROUP *ret;
  51361. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  51362. "BgUrgQQAIg==\n"
  51363. "-----END EC PARAMETERS-----";
  51364. #endif
  51365. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  51366. "MAA=\n"
  51367. "-----END EC PARAMETERS-----";
  51368. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  51369. "BgA=\n"
  51370. "-----END EC PARAMETERS-----";
  51371. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  51372. "BgE=\n"
  51373. "-----END EC PARAMETERS-----";
  51374. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  51375. "BgE*\n"
  51376. "-----END EC PARAMETERS-----";
  51377. /* Test that first parameter, bio, being NULL fails. */
  51378. AssertNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  51379. /* Test that reading named parameters works. */
  51380. AssertNotNull(bio = BIO_new(BIO_s_file()));
  51381. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  51382. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51383. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  51384. BIO_free(bio);
  51385. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  51386. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  51387. /* Test that reusing group works. */
  51388. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  51389. sizeof(ec_nc_p384)));
  51390. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  51391. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  51392. BIO_free(bio);
  51393. EC_GROUP_free(group);
  51394. group = NULL;
  51395. /* Test that returning through group works. */
  51396. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  51397. sizeof(ec_nc_p384)));
  51398. AssertNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  51399. AssertIntEQ(group == ret, 1);
  51400. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  51401. BIO_free(bio);
  51402. #endif
  51403. EC_GROUP_free(group);
  51404. /* Test 0x30, 0x00 (not and object id) fails. */
  51405. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  51406. sizeof(ec_nc_bad_1)));
  51407. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51408. BIO_free(bio);
  51409. /* Test 0x06, 0x00 (empty object id) fails. */
  51410. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  51411. sizeof(ec_nc_bad_2)));
  51412. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51413. BIO_free(bio);
  51414. /* Test 0x06, 0x01 (badly formed object id) fails. */
  51415. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  51416. sizeof(ec_nc_bad_3)));
  51417. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51418. BIO_free(bio);
  51419. /* Test invalid PEM encoding - invalid character. */
  51420. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  51421. sizeof(ec_nc_bad_4)));
  51422. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51423. BIO_free(bio);
  51424. res = TEST_RES_CHECK(1);
  51425. #endif
  51426. return res;
  51427. }
  51428. static int test_wolfSSL_EC_POINT(void)
  51429. {
  51430. int res = TEST_SKIPPED;
  51431. #if !defined(WOLFSSL_SP_MATH) && \
  51432. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  51433. #ifdef OPENSSL_EXTRA
  51434. BN_CTX* ctx;
  51435. EC_GROUP* group;
  51436. #ifndef HAVE_ECC_BRAINPOOL
  51437. EC_GROUP* group2;
  51438. #endif
  51439. EC_POINT* Gxy;
  51440. EC_POINT* new_point;
  51441. EC_POINT* set_point;
  51442. EC_POINT* infinity;
  51443. BIGNUM* k = NULL;
  51444. BIGNUM* Gx = NULL;
  51445. BIGNUM* Gy = NULL;
  51446. BIGNUM* Gz = NULL;
  51447. BIGNUM* X;
  51448. BIGNUM* Y;
  51449. BIGNUM* set_point_bn;
  51450. char* hexStr;
  51451. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  51452. "17AF381913FF7A96314EA47055EA0FD0";
  51453. /* NISTP256R1 Gx/Gy */
  51454. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  51455. "77037D812DEB33A0F4A13945D898C296";
  51456. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  51457. "2BCE33576B315ECECBB6406837BF51F5";
  51458. #ifndef HAVE_SELFTEST
  51459. EC_POINT *tmp;
  51460. size_t bin_len;
  51461. unsigned int blen;
  51462. unsigned char* buf = NULL;
  51463. unsigned char bufInf[1] = { 0x00 };
  51464. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  51465. "77037D812DEB33A0F4A13945D898C296"
  51466. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  51467. "2BCE33576B315ECECBB6406837BF51F5";
  51468. const unsigned char binUncompG[] = {
  51469. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51470. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51471. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51472. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51473. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51474. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51475. };
  51476. const unsigned char binUncompGBad[] = {
  51477. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51478. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51479. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51480. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51481. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51482. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51483. };
  51484. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  51485. "77037D812DEB33A0F4A13945D898C296";
  51486. #ifdef HAVE_COMP_KEY
  51487. const unsigned char binCompG[] = {
  51488. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51489. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51490. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51491. };
  51492. #endif
  51493. #endif
  51494. AssertNotNull(ctx = BN_CTX_new());
  51495. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51496. #ifndef HAVE_ECC_BRAINPOOL
  51497. /* Used to make groups curve_idx == -1. */
  51498. AssertNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  51499. #endif
  51500. AssertNull(EC_POINT_new(NULL));
  51501. AssertNotNull(Gxy = EC_POINT_new(group));
  51502. AssertNotNull(new_point = EC_POINT_new(group));
  51503. AssertNotNull(set_point = EC_POINT_new(group));
  51504. AssertNotNull(X = BN_new());
  51505. AssertNotNull(Y = BN_new());
  51506. AssertNotNull(set_point_bn = BN_new());
  51507. AssertNotNull(infinity = EC_POINT_new(group));
  51508. /* load test values */
  51509. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  51510. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  51511. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  51512. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  51513. /* populate coordinates for input point */
  51514. Gxy->X = Gx;
  51515. Gxy->Y = Gy;
  51516. Gxy->Z = Gz;
  51517. /* Test handling of NULL point. */
  51518. EC_POINT_clear_free(NULL);
  51519. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51520. NULL, NULL, ctx), 0);
  51521. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51522. NULL, NULL, ctx), 0);
  51523. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51524. NULL, NULL, ctx), 0);
  51525. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51526. X, NULL, ctx), 0);
  51527. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51528. NULL, Y, ctx), 0);
  51529. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51530. X, Y, ctx), 0);
  51531. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51532. X, Y, ctx), 0);
  51533. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51534. NULL, Y, ctx), 0);
  51535. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51536. X, NULL, ctx), 0);
  51537. /* Getting point at infinity returns an error. */
  51538. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  51539. X, Y, ctx), 0);
  51540. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  51541. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  51542. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51543. AssertIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  51544. AssertIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  51545. AssertIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  51546. AssertIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  51547. AssertIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  51548. AssertIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  51549. AssertIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  51550. AssertIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  51551. AssertIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  51552. AssertIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  51553. AssertIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  51554. AssertIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  51555. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  51556. /* perform point multiplication */
  51557. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  51558. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51559. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  51560. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  51561. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  51562. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51563. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  51564. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  51565. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  51566. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51567. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  51568. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  51569. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  51570. AssertIntEQ(BN_is_zero(new_point->X), 1);
  51571. AssertIntEQ(BN_is_zero(new_point->Y), 1);
  51572. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  51573. /* Set point to something. */
  51574. AssertIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  51575. #else
  51576. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  51577. ctx), 1);
  51578. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51579. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  51580. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  51581. #endif
  51582. /* check if point X coordinate is zero */
  51583. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51584. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  51585. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  51586. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  51587. #endif
  51588. /* extract the coordinates from point */
  51589. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51590. ctx), WOLFSSL_SUCCESS);
  51591. /* check if point X coordinate is zero */
  51592. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  51593. /* set the same X and Y points in another object */
  51594. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  51595. ctx), WOLFSSL_SUCCESS);
  51596. /* compare points as they should be the same */
  51597. AssertIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  51598. AssertIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  51599. AssertIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  51600. AssertIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  51601. AssertIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  51602. AssertIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  51603. AssertIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  51604. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  51605. /* Test copying */
  51606. AssertIntEQ(EC_POINT_copy(NULL, NULL), 0);
  51607. AssertIntEQ(EC_POINT_copy(NULL, set_point), 0);
  51608. AssertIntEQ(EC_POINT_copy(new_point, NULL), 0);
  51609. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  51610. /* Test inverting */
  51611. AssertIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  51612. AssertIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  51613. AssertIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  51614. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  51615. /* Test getting affine converts from projective. */
  51616. AssertIntEQ(EC_POINT_copy(set_point, new_point), 1);
  51617. /* Force non-affine coordinates */
  51618. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  51619. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  51620. new_point->inSet = 0;
  51621. /* extract the coordinates from point */
  51622. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51623. ctx), WOLFSSL_SUCCESS);
  51624. /* check if point ordinates have changed. */
  51625. AssertIntNE(BN_cmp(X, set_point->X), 0);
  51626. AssertIntNE(BN_cmp(Y, set_point->Y), 0);
  51627. /* Test check for infinity */
  51628. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  51629. AssertIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  51630. AssertIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  51631. AssertIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  51632. AssertIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  51633. AssertIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  51634. #else
  51635. AssertIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  51636. #endif
  51637. AssertPtrEq(EC_POINT_point2bn(group, set_point,
  51638. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  51639. /* check bn2hex */
  51640. hexStr = BN_bn2hex(k);
  51641. AssertStrEQ(hexStr, kTest);
  51642. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51643. BN_print_fp(stderr, k);
  51644. fprintf(stderr, "\n");
  51645. #endif
  51646. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51647. hexStr = BN_bn2hex(Gx);
  51648. AssertStrEQ(hexStr, kGx);
  51649. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51650. BN_print_fp(stderr, Gx);
  51651. fprintf(stderr, "\n");
  51652. #endif
  51653. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51654. hexStr = BN_bn2hex(Gy);
  51655. AssertStrEQ(hexStr, kGy);
  51656. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51657. BN_print_fp(stderr, Gy);
  51658. fprintf(stderr, "\n");
  51659. #endif
  51660. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51661. #ifndef HAVE_SELFTEST
  51662. /* Test point to hex */
  51663. AssertNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51664. ctx));
  51665. AssertNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51666. ctx));
  51667. AssertNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51668. ctx));
  51669. #ifndef HAVE_ECC_BRAINPOOL
  51670. /* Group not supported in wolfCrypt. */
  51671. AssertNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51672. ctx));
  51673. #endif
  51674. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  51675. AssertStrEQ(hexStr, uncompG);
  51676. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51677. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  51678. AssertStrEQ(hexStr, compG);
  51679. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51680. /* Test point to oct */
  51681. AssertIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51682. NULL, 0, ctx), 0);
  51683. AssertIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51684. NULL, 0, ctx), 0);
  51685. AssertIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51686. NULL, 0, ctx), 0);
  51687. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51688. NULL, 0, ctx);
  51689. AssertIntEQ(bin_len, sizeof(binUncompG));
  51690. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  51691. DYNAMIC_TYPE_ECC));
  51692. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51693. buf, bin_len, ctx), bin_len);
  51694. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  51695. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51696. /* Infinity (x=0, y=0) encodes as '0x00'. */
  51697. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  51698. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  51699. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  51700. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  51701. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  51702. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  51703. AssertIntEQ(bufInf[0], 0);
  51704. wolfSSL_EC_POINT_dump(NULL, NULL);
  51705. /* Test point i2d */
  51706. AssertIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  51707. AssertIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  51708. AssertIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  51709. AssertIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  51710. AssertIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  51711. AssertIntEQ(blen, sizeof(binUncompG));
  51712. AssertNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  51713. blen -= 1;
  51714. AssertIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  51715. blen += 1;
  51716. AssertIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  51717. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  51718. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51719. #ifdef HAVE_COMP_KEY
  51720. /* Test point to oct compressed */
  51721. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  51722. 0, ctx);
  51723. AssertIntEQ(bin_len, sizeof(binCompG));
  51724. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  51725. DYNAMIC_TYPE_ECC));
  51726. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  51727. bin_len, ctx), bin_len);
  51728. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  51729. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51730. #endif
  51731. /* Test point BN */
  51732. AssertNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  51733. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51734. AssertNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  51735. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51736. AssertNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  51737. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51738. AssertNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  51739. /* Test oct to point */
  51740. AssertNotNull(tmp = EC_POINT_new(group));
  51741. AssertIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  51742. ctx), 0);
  51743. AssertIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  51744. ctx), 0);
  51745. AssertIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  51746. ctx), 0);
  51747. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  51748. sizeof(binUncompGBad), ctx), 0);
  51749. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  51750. ctx), 1);
  51751. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51752. EC_POINT_free(tmp);
  51753. /* Test setting BN ordinates. */
  51754. AssertNotNull(tmp = EC_POINT_new(group));
  51755. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  51756. NULL, ctx), 0);
  51757. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  51758. NULL, ctx), 0);
  51759. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  51760. NULL, ctx), 0);
  51761. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  51762. NULL, ctx), 0);
  51763. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  51764. Gy, ctx), 0);
  51765. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  51766. ctx), 0);
  51767. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  51768. ctx), 0);
  51769. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  51770. Gy, ctx), 0);
  51771. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  51772. NULL, ctx), 0);
  51773. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  51774. ctx), 1);
  51775. EC_POINT_free(tmp);
  51776. /* Test point d2i */
  51777. AssertNotNull(tmp = EC_POINT_new(group));
  51778. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  51779. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  51780. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  51781. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  51782. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  51783. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  51784. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  51785. AssertIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  51786. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  51787. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51788. EC_POINT_free(tmp);
  51789. #ifdef HAVE_COMP_KEY
  51790. /* Test oct compressed to point */
  51791. AssertNotNull(tmp = EC_POINT_new(group));
  51792. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  51793. 1);
  51794. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51795. EC_POINT_free(tmp);
  51796. /* Test point d2i - compressed */
  51797. AssertNotNull(tmp = EC_POINT_new(group));
  51798. AssertIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  51799. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51800. EC_POINT_free(tmp);
  51801. #endif
  51802. #endif
  51803. /* test BN_mod_add */
  51804. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  51805. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  51806. 1);
  51807. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  51808. /* cleanup */
  51809. BN_free(X);
  51810. BN_free(Y);
  51811. BN_free(k);
  51812. BN_free(set_point_bn);
  51813. EC_POINT_free(infinity);
  51814. EC_POINT_free(new_point);
  51815. EC_POINT_free(set_point);
  51816. EC_POINT_clear_free(Gxy);
  51817. #ifndef HAVE_ECC_BRAINPOOL
  51818. EC_GROUP_free(group2);
  51819. #endif
  51820. EC_GROUP_free(group);
  51821. BN_CTX_free(ctx);
  51822. res = TEST_RES_CHECK(1);
  51823. #endif
  51824. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  51825. return res;
  51826. }
  51827. static int test_wolfSSL_EC_KEY_generate(void)
  51828. {
  51829. int res = TEST_SKIPPED;
  51830. #ifdef OPENSSL_EXTRA
  51831. WOLFSSL_EC_KEY* key;
  51832. #ifndef HAVE_ECC_BRAINPOOL
  51833. WOLFSSL_EC_GROUP* group;
  51834. #endif
  51835. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51836. AssertIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  51837. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  51838. wolfSSL_EC_KEY_free(key);
  51839. #ifndef HAVE_ECC_BRAINPOOL
  51840. AssertNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  51841. NID_brainpoolP256r1));
  51842. AssertNotNull(key = wolfSSL_EC_KEY_new());
  51843. AssertIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  51844. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  51845. wolfSSL_EC_KEY_free(key);
  51846. wolfSSL_EC_GROUP_free(group);
  51847. #endif
  51848. res = TEST_RES_CHECK(1);
  51849. #endif
  51850. return res;
  51851. }
  51852. static int test_EC_i2d(void)
  51853. {
  51854. int res = TEST_SKIPPED;
  51855. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  51856. EC_KEY *key;
  51857. EC_KEY *copy = NULL;
  51858. int len;
  51859. unsigned char *buf = NULL;
  51860. unsigned char *p;
  51861. const unsigned char *tmp = NULL;
  51862. const unsigned char octBad[] = {
  51863. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51864. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51865. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51866. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51867. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51868. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51869. };
  51870. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51871. AssertIntEQ(EC_KEY_generate_key(key), 1);
  51872. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  51873. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51874. DYNAMIC_TYPE_TMP_BUFFER));
  51875. p = buf;
  51876. AssertIntEQ(i2d_EC_PUBKEY(key, &p), len);
  51877. AssertNull(o2i_ECPublicKey(NULL, NULL, -1));
  51878. AssertNull(o2i_ECPublicKey(&copy, NULL, -1));
  51879. AssertNull(o2i_ECPublicKey(&key, NULL, -1));
  51880. AssertNull(o2i_ECPublicKey(NULL, &tmp, -1));
  51881. AssertNull(o2i_ECPublicKey(NULL, NULL, 0));
  51882. AssertNull(o2i_ECPublicKey(&key, NULL, 0));
  51883. AssertNull(o2i_ECPublicKey(&key, &tmp, 0));
  51884. tmp = buf;
  51885. AssertNull(o2i_ECPublicKey(NULL, &tmp, 0));
  51886. AssertNull(o2i_ECPublicKey(&copy, &tmp, 0));
  51887. AssertNull(o2i_ECPublicKey(NULL, &tmp, -1));
  51888. AssertNull(o2i_ECPublicKey(&key, &tmp, -1));
  51889. AssertIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  51890. AssertIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  51891. tmp = buf;
  51892. AssertNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  51893. AssertNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  51894. AssertNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  51895. AssertNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  51896. AssertNull(d2i_ECPrivateKey(&key, &tmp, 0));
  51897. AssertIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  51898. AssertIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  51899. AssertIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  51900. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  51901. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  51902. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  51903. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  51904. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  51905. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51906. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  51907. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51908. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  51909. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51910. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  51911. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51912. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  51913. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  51914. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  51915. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  51916. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  51917. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  51918. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51919. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51920. buf = NULL;
  51921. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  51922. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51923. DYNAMIC_TYPE_TMP_BUFFER));
  51924. p = buf;
  51925. AssertIntEQ(i2d_ECPrivateKey(key, &p), len);
  51926. p = NULL;
  51927. AssertIntEQ(i2d_ECPrivateKey(key, &p), len);
  51928. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51929. /* Bad point is also an invalid private key. */
  51930. tmp = octBad;
  51931. AssertNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  51932. tmp = buf;
  51933. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  51934. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51935. buf = NULL;
  51936. AssertIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  51937. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51938. DYNAMIC_TYPE_TMP_BUFFER));
  51939. p = buf;
  51940. AssertIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  51941. p = NULL;
  51942. AssertIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  51943. tmp = buf;
  51944. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  51945. tmp = octBad;
  51946. AssertNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  51947. AssertIntEQ(EC_KEY_check_key(NULL), 0);
  51948. AssertIntEQ(EC_KEY_check_key(key), 1);
  51949. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  51950. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  51951. EC_KEY_free(key);
  51952. EC_KEY_free(copy);
  51953. res = TEST_RES_CHECK(1);
  51954. #endif
  51955. return res;
  51956. }
  51957. static int test_wolfSSL_EC_curve(void)
  51958. {
  51959. int res = TEST_SKIPPED;
  51960. #if defined(OPENSSL_EXTRA)
  51961. int nid = NID_secp160k1;
  51962. const char* nid_name;
  51963. AssertNull(EC_curve_nid2nist(NID_sha256));
  51964. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  51965. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  51966. AssertIntEQ(EC_curve_nist2nid("INVALID"), 0);
  51967. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  51968. res = TEST_RES_CHECK(1);
  51969. #endif
  51970. return res;
  51971. }
  51972. static int test_wolfSSL_EC_KEY_dup(void)
  51973. {
  51974. int res = TEST_SKIPPED;
  51975. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  51976. WOLFSSL_EC_KEY* ecKey;
  51977. WOLFSSL_EC_KEY* dupKey;
  51978. ecc_key* srcKey;
  51979. ecc_key* destKey;
  51980. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  51981. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  51982. /* Valid cases */
  51983. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51984. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  51985. /* Compare pubkey */
  51986. srcKey = (ecc_key*)ecKey->internal;
  51987. destKey = (ecc_key*)dupKey->internal;
  51988. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  51989. /* compare EC_GROUP */
  51990. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  51991. /* compare EC_POINT */
  51992. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  51993. dupKey->pub_key, NULL), MP_EQ);
  51994. /* compare BIGNUM */
  51995. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  51996. wolfSSL_EC_KEY_free(dupKey);
  51997. /* Invalid cases */
  51998. /* NULL key */
  51999. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  52000. /* NULL ecc_key */
  52001. wc_ecc_free((ecc_key*)ecKey->internal);
  52002. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  52003. ecKey->internal = NULL; /* Set ecc_key to NULL */
  52004. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52005. wolfSSL_EC_KEY_free(ecKey);
  52006. wolfSSL_EC_KEY_free(dupKey);
  52007. /* NULL Group */
  52008. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52009. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52010. wolfSSL_EC_GROUP_free(ecKey->group);
  52011. ecKey->group = NULL; /* Set group to NULL */
  52012. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52013. wolfSSL_EC_KEY_free(ecKey);
  52014. wolfSSL_EC_KEY_free(dupKey);
  52015. /* NULL public key */
  52016. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52017. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52018. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  52019. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  52020. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52021. wolfSSL_EC_POINT_free(ecKey->pub_key);
  52022. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  52023. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52024. wolfSSL_EC_KEY_free(ecKey);
  52025. wolfSSL_EC_KEY_free(dupKey);
  52026. /* NULL private key */
  52027. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52028. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52029. wolfSSL_BN_free(ecKey->priv_key);
  52030. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  52031. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52032. wolfSSL_EC_KEY_free(ecKey);
  52033. wolfSSL_EC_KEY_free(dupKey);
  52034. /* Test EC_KEY_up_ref */
  52035. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52036. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  52037. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  52038. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  52039. /* reference count doesn't follow duplicate */
  52040. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52041. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  52042. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  52043. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  52044. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  52045. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  52046. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  52047. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  52048. res = TEST_RES_CHECK(1);
  52049. #endif
  52050. return res;
  52051. }
  52052. static int test_wolfSSL_EC_KEY_set_group(void)
  52053. {
  52054. int res = TEST_SKIPPED;
  52055. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52056. defined(OPENSSL_EXTRA)
  52057. EC_KEY *key = NULL;
  52058. EC_GROUP *group = NULL;
  52059. const EC_GROUP *group2 = NULL;
  52060. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52061. AssertNotNull(key = EC_KEY_new());
  52062. AssertNull(EC_KEY_get0_group(NULL));
  52063. AssertIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  52064. AssertIntEQ(EC_KEY_set_group(key, NULL), 0);
  52065. AssertIntEQ(EC_KEY_set_group(NULL, group), 0);
  52066. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  52067. AssertNotNull(group2 = EC_KEY_get0_group(key));
  52068. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  52069. EC_GROUP_free(group);
  52070. EC_KEY_free(key);
  52071. res = TEST_RES_CHECK(1);
  52072. #endif
  52073. return res;
  52074. }
  52075. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  52076. {
  52077. int res = TEST_SKIPPED;
  52078. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52079. BIO* bio;
  52080. EC_KEY* key;
  52081. /* Error condition: NULL key. */
  52082. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  52083. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  52084. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  52085. /* Conversion form defaults to uncompressed. */
  52086. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52087. #ifdef HAVE_COMP_KEY
  52088. /* Explicitly set to compressed. */
  52089. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52090. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  52091. #else
  52092. /* Will still work just won't change anything. */
  52093. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52094. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52095. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  52096. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52097. #endif
  52098. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  52099. BIO_free(bio);
  52100. EC_KEY_free(key);
  52101. res = TEST_RES_CHECK(1);
  52102. #endif
  52103. return res;
  52104. }
  52105. static int test_wolfSSL_EC_KEY_private_key(void)
  52106. {
  52107. int res = TEST_SKIPPED;
  52108. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52109. WOLFSSL_EC_KEY* key;
  52110. WOLFSSL_BIGNUM* priv = NULL;
  52111. WOLFSSL_BIGNUM* priv2 = NULL;
  52112. WOLFSSL_BIGNUM* bn;
  52113. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52114. AssertNotNull(priv = wolfSSL_BN_new());
  52115. AssertNotNull(priv2 = wolfSSL_BN_new());
  52116. AssertIntNE(BN_set_word(priv, 2), 0);
  52117. AssertIntNE(BN_set_word(priv2, 2), 0);
  52118. AssertNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  52119. /* No private key set. */
  52120. AssertNull(wolfSSL_EC_KEY_get0_private_key(key));
  52121. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  52122. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  52123. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  52124. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  52125. AssertNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52126. AssertPtrNE(bn, priv);
  52127. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  52128. AssertNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52129. AssertPtrNE(bn, priv2);
  52130. wolfSSL_BN_free(priv2);
  52131. wolfSSL_BN_free(priv);
  52132. wolfSSL_EC_KEY_free(key);
  52133. res = TEST_RES_CHECK(1);
  52134. #endif
  52135. return res;
  52136. }
  52137. static int test_wolfSSL_EC_KEY_public_key(void)
  52138. {
  52139. int res = TEST_SKIPPED;
  52140. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52141. WOLFSSL_EC_KEY* key;
  52142. WOLFSSL_EC_POINT* pub;
  52143. WOLFSSL_EC_POINT* point;
  52144. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52145. AssertNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  52146. AssertNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  52147. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  52148. AssertNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  52149. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  52150. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  52151. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  52152. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  52153. AssertNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  52154. AssertPtrEq(point, pub);
  52155. wolfSSL_EC_KEY_free(key);
  52156. res = TEST_RES_CHECK(1);
  52157. #endif
  52158. return res;
  52159. }
  52160. static int test_wolfSSL_EC_KEY_print_fp(void)
  52161. {
  52162. int res = TEST_SKIPPED;
  52163. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  52164. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  52165. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  52166. !defined(NO_STDIO_FILESYSTEM)
  52167. EC_KEY* key = NULL;
  52168. /* Bad file pointer. */
  52169. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  52170. /* NULL key. */
  52171. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  52172. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  52173. /* Negative indent. */
  52174. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  52175. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52176. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  52177. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52178. wolfSSL_EC_KEY_free(key);
  52179. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  52180. NID_X9_62_prime256v1)));
  52181. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  52182. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52183. wolfSSL_EC_KEY_free(key);
  52184. res = TEST_RES_CHECK(1);
  52185. #endif
  52186. return res;
  52187. }
  52188. static int test_wolfSSL_EC_get_builtin_curves(void)
  52189. {
  52190. int res = TEST_SKIPPED;
  52191. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  52192. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52193. EC_builtin_curve* curves = NULL;
  52194. size_t crv_len = 0;
  52195. size_t i = 0;
  52196. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  52197. AssertNotNull(curves = (EC_builtin_curve*)XMALLOC(
  52198. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  52199. AssertIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  52200. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  52201. for (i = 0; i < crv_len; i++) {
  52202. if (curves[i].comment != NULL) {
  52203. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  52204. }
  52205. }
  52206. if (crv_len > 1) {
  52207. AssertIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  52208. }
  52209. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52210. res = TEST_RES_CHECK(1);
  52211. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52212. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  52213. return res;
  52214. }
  52215. static int test_wolfSSL_ECDSA_SIG(void)
  52216. {
  52217. int res = TEST_SKIPPED;
  52218. #ifdef OPENSSL_EXTRA
  52219. WOLFSSL_ECDSA_SIG* sig = NULL;
  52220. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  52221. WOLFSSL_BIGNUM* r;
  52222. WOLFSSL_BIGNUM* s;
  52223. const WOLFSSL_BIGNUM* r2;
  52224. const WOLFSSL_BIGNUM* s2;
  52225. const unsigned char* cp;
  52226. unsigned char* p;
  52227. unsigned char outSig[8];
  52228. unsigned char sigData[8] =
  52229. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  52230. unsigned char sigDataBad[8] =
  52231. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  52232. wolfSSL_ECDSA_SIG_free(NULL);
  52233. AssertNotNull(sig = wolfSSL_ECDSA_SIG_new());
  52234. AssertNotNull(r = wolfSSL_BN_new());
  52235. AssertNotNull(s = wolfSSL_BN_new());
  52236. AssertIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  52237. AssertIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  52238. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  52239. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  52240. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  52241. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  52242. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  52243. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  52244. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  52245. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  52246. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  52247. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  52248. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  52249. r2 = NULL;
  52250. s2 = NULL;
  52251. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  52252. AssertNull(r2);
  52253. AssertNull(s2);
  52254. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  52255. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  52256. AssertPtrEq(r2, r);
  52257. AssertPtrEq(s2, s);
  52258. r2 = NULL;
  52259. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  52260. AssertPtrEq(r2, r);
  52261. s2 = NULL;
  52262. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  52263. AssertPtrEq(s2, s);
  52264. /* r and s are freed when sig is freed. */
  52265. wolfSSL_ECDSA_SIG_free(sig);
  52266. AssertNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  52267. cp = sigDataBad;
  52268. AssertNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  52269. cp = sigData;
  52270. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  52271. AssertIntEQ((cp == sigData + 8), 1);
  52272. cp = sigData;
  52273. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  52274. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  52275. AssertIntEQ((sig == sig2), 1);
  52276. cp = outSig;
  52277. p = outSig;
  52278. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  52279. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  52280. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  52281. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  52282. AssertIntEQ((p == outSig + 8), 1);
  52283. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  52284. wolfSSL_ECDSA_SIG_free(sig);
  52285. res = TEST_RES_CHECK(1);
  52286. #endif
  52287. return res;
  52288. }
  52289. static int test_ECDSA_size_sign(void)
  52290. {
  52291. int res = TEST_SKIPPED;
  52292. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  52293. EC_KEY* key;
  52294. ECDSA_SIG* ecdsaSig;
  52295. int id;
  52296. byte hash[WC_MAX_DIGEST_SIZE];
  52297. byte hash2[WC_MAX_DIGEST_SIZE];
  52298. byte sig[ECC_MAX_SIG_SIZE];
  52299. unsigned int sigSz = sizeof(sig);
  52300. XMEMSET(hash, 123, sizeof(hash));
  52301. XMEMSET(hash2, 234, sizeof(hash2));
  52302. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  52303. AssertIntEQ(id, ECC_SECP256R1);
  52304. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52305. AssertIntEQ(EC_KEY_generate_key(key), 1);
  52306. AssertIntGE(ECDSA_size(NULL), 0);
  52307. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  52308. AssertIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  52309. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  52310. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  52311. AssertIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  52312. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  52313. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  52314. AssertIntGE(ECDSA_size(key), sigSz);
  52315. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  52316. AssertIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  52317. AssertNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  52318. AssertNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  52319. AssertNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  52320. AssertNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  52321. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  52322. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  52323. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  52324. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  52325. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  52326. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  52327. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  52328. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  52329. AssertIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  52330. ECDSA_SIG_free(ecdsaSig);
  52331. EC_KEY_free(key);
  52332. res = TEST_RES_CHECK(1);
  52333. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  52334. return res;
  52335. }
  52336. static int test_ECDH_compute_key(void)
  52337. {
  52338. int res = TEST_SKIPPED;
  52339. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52340. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52341. EC_KEY* key1;
  52342. EC_KEY* key2;
  52343. EC_POINT* pub1;
  52344. EC_POINT* pub2;
  52345. byte secret1[32];
  52346. byte secret2[32];
  52347. int i;
  52348. AssertNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52349. AssertIntEQ(EC_KEY_generate_key(key1), 1);
  52350. AssertNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  52351. AssertNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52352. AssertIntEQ(EC_KEY_generate_key(key2), 1);
  52353. AssertNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  52354. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  52355. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  52356. 0);
  52357. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  52358. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  52359. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  52360. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  52361. 0);
  52362. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  52363. 0);
  52364. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  52365. NULL), 0);
  52366. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  52367. sizeof(secret1));
  52368. AssertIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  52369. sizeof(secret2));
  52370. for (i = 0; i < (int)sizeof(secret1); i++) {
  52371. AssertIntEQ(secret1[i], secret2[i]);
  52372. }
  52373. EC_KEY_free(key2);
  52374. EC_KEY_free(key1);
  52375. res = TEST_RES_CHECK(1);
  52376. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  52377. * !WOLF_CRYPTO_CB_ONLY_ECC */
  52378. return res;
  52379. }
  52380. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  52381. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  52382. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52383. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  52384. {
  52385. X509* x509 = NULL;
  52386. BIGNUM* serial_number = NULL;
  52387. X509_NAME* name = NULL;
  52388. time_t epoch_off = 0;
  52389. ASN1_INTEGER* asn1_serial_number;
  52390. long not_before, not_after;
  52391. AssertNotNull(x509 = X509_new());
  52392. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  52393. AssertNotNull(serial_number = BN_new());
  52394. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  52395. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  52396. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  52397. /* version 3 */
  52398. AssertIntNE(X509_set_version(x509, 2L), 0);
  52399. AssertNotNull(name = X509_NAME_new());
  52400. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  52401. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  52402. AssertIntNE(X509_set_subject_name(x509, name), 0);
  52403. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  52404. not_before = (long)wc_Time(NULL);
  52405. not_after = not_before + (365 * 24 * 60 * 60);
  52406. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  52407. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  52408. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  52409. BN_free(serial_number);
  52410. X509_NAME_free(name);
  52411. X509_free(x509);
  52412. return 0;
  52413. }
  52414. #endif
  52415. static int test_openssl_generate_key_and_cert(void)
  52416. {
  52417. int res = TEST_SKIPPED;
  52418. #if defined(OPENSSL_EXTRA)
  52419. #if !defined(NO_RSA)
  52420. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  52421. EVP_PKEY* pkey = EVP_PKEY_new();
  52422. int key_length = 2048;
  52423. BIGNUM* exponent = BN_new();
  52424. RSA* rsa = RSA_new();
  52425. AssertNotNull(pkey);
  52426. AssertNotNull(exponent);
  52427. AssertNotNull(rsa);
  52428. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  52429. #ifndef WOLFSSL_KEY_GEN
  52430. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52431. #if defined(USE_CERT_BUFFERS_1024)
  52432. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  52433. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52434. key_length = 1024;
  52435. #elif defined(USE_CERT_BUFFERS_2048)
  52436. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  52437. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52438. #else
  52439. RSA_free(rsa);
  52440. rsa = NULL;
  52441. #endif
  52442. #else
  52443. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  52444. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  52445. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  52446. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52447. #endif
  52448. if (rsa) {
  52449. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  52450. BN_free(exponent);
  52451. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52452. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52453. test_openssl_make_self_signed_certificate(pkey);
  52454. #endif
  52455. }
  52456. EVP_PKEY_free(pkey);
  52457. res = TEST_RES_CHECK(1);
  52458. }
  52459. #endif /* !NO_RSA */
  52460. #ifdef HAVE_ECC
  52461. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  52462. EVP_PKEY* pkey = EVP_PKEY_new();
  52463. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  52464. AssertNotNull(pkey);
  52465. AssertNotNull(ec_key);
  52466. #ifndef NO_WOLFSSL_STUB
  52467. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  52468. #endif
  52469. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  52470. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  52471. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52472. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52473. test_openssl_make_self_signed_certificate(pkey);
  52474. #endif
  52475. EVP_PKEY_free(pkey);
  52476. res = TEST_RES_CHECK(1);
  52477. }
  52478. #endif /* HAVE_ECC */
  52479. #endif /* OPENSSL_EXTRA */
  52480. return res;
  52481. }
  52482. static int test_stubs_are_stubs(void)
  52483. {
  52484. int res = TEST_SKIPPED;
  52485. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  52486. WOLFSSL_CTX* ctx = NULL;
  52487. WOLFSSL_CTX* ctxN = NULL;
  52488. #ifndef NO_WOLFSSL_CLIENT
  52489. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  52490. AssertNotNull(ctx);
  52491. #elif !defined(NO_WOLFSSL_SERVER)
  52492. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  52493. AssertNotNull(ctx);
  52494. #else
  52495. return res;
  52496. #endif
  52497. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  52498. AssertIntEQ((int) x(z), 0)
  52499. /* test logic, all stubs return same result regardless of ctx being NULL
  52500. * as there are no sanity checks, it's just a stub! If at some
  52501. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  52502. * if invalid inputs are supplied. Test calling both
  52503. * with and without valid inputs, if a stub functionality remains unchanged.
  52504. */
  52505. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  52506. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  52507. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  52508. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  52509. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  52510. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  52511. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  52512. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  52513. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  52514. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  52515. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  52516. wolfSSL_CTX_free(ctx);
  52517. ctx = NULL;
  52518. res = TEST_RES_CHECK(1);
  52519. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  52520. return res;
  52521. }
  52522. static int test_CONF_modules_xxx(void)
  52523. {
  52524. int res = TEST_SKIPPED;
  52525. #if defined(OPENSSL_EXTRA)
  52526. CONF_modules_free();
  52527. AssertTrue(1); /* to confirm previous call gives no harm */
  52528. CONF_modules_unload(0);
  52529. AssertTrue(1);
  52530. CONF_modules_unload(1);
  52531. AssertTrue(1);
  52532. CONF_modules_unload(-1);
  52533. AssertTrue(1);
  52534. res = TEST_RES_CHECK(1);
  52535. #endif /* OPENSSL_EXTRA */
  52536. return res;
  52537. }
  52538. static int test_CRYPTO_set_dynlock_xxx(void)
  52539. {
  52540. int res = TEST_SKIPPED;
  52541. #if defined(OPENSSL_EXTRA)
  52542. CRYPTO_set_dynlock_create_callback(
  52543. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  52544. CRYPTO_set_dynlock_create_callback(
  52545. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  52546. CRYPTO_set_dynlock_destroy_callback(
  52547. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  52548. CRYPTO_set_dynlock_destroy_callback(
  52549. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  52550. CRYPTO_set_dynlock_lock_callback(
  52551. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  52552. CRYPTO_set_dynlock_lock_callback(
  52553. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  52554. AssertTrue(1); /* to confirm previous call gives no harm */
  52555. res = TEST_RES_CHECK(1);
  52556. #endif /* OPENSSL_EXTRA */
  52557. return res;
  52558. }
  52559. static int test_CRYPTO_THREADID_xxx(void)
  52560. {
  52561. int res = TEST_SKIPPED;
  52562. #if defined(OPENSSL_EXTRA)
  52563. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  52564. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  52565. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  52566. res = TEST_RES_CHECK(1);
  52567. #endif /* OPENSSL_EXTRA */
  52568. return res;
  52569. }
  52570. static int test_ENGINE_cleanup(void)
  52571. {
  52572. int res = TEST_SKIPPED;
  52573. #if defined(OPENSSL_EXTRA)
  52574. ENGINE_cleanup();
  52575. AssertTrue(1); /* to confirm previous call gives no harm */
  52576. res = TEST_RES_CHECK(1);
  52577. #endif /* OPENSSL_EXTRA */
  52578. return res;
  52579. }
  52580. static int test_wolfSSL_CTX_LoadCRL(void)
  52581. {
  52582. int res = TEST_SKIPPED;
  52583. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  52584. WOLFSSL_CTX* ctx = NULL;
  52585. WOLFSSL* ssl = NULL;
  52586. const char* badPath = "dummypath";
  52587. const char* validPath = "./certs/crl";
  52588. const char* validFilePath = "./certs/crl/cliCrl.pem";
  52589. const char* issuerCert = "./certs/client-cert.pem";
  52590. int derType = WOLFSSL_FILETYPE_ASN1;
  52591. int pemType = WOLFSSL_FILETYPE_PEM;
  52592. int monitor = WOLFSSL_CRL_MONITOR;
  52593. WOLFSSL_CERT_MANAGER* cm = NULL;
  52594. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  52595. BAD_FUNC_ARG)
  52596. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  52597. WOLFSSL_SUCCESS)
  52598. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  52599. #ifndef NO_WOLFSSL_CLIENT
  52600. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52601. #elif !defined(NO_WOLFSSL_SERVER)
  52602. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52603. #else
  52604. return;
  52605. #endif
  52606. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  52607. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  52608. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  52609. wolfSSL_CTX_free(ctx);
  52610. #ifndef NO_WOLFSSL_CLIENT
  52611. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52612. #elif !defined(NO_WOLFSSL_SERVER)
  52613. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52614. #else
  52615. return;
  52616. #endif
  52617. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  52618. WOLFSSL_SUCCESS);
  52619. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  52620. wolfSSL_CTX_free(ctx);
  52621. #ifndef NO_WOLFSSL_CLIENT
  52622. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52623. #elif !defined(NO_WOLFSSL_SERVER)
  52624. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52625. #else
  52626. return;
  52627. #endif
  52628. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  52629. WOLFSSL_SUCCESS);
  52630. AssertNotNull(ssl = wolfSSL_new(ctx));
  52631. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  52632. wolfSSL_free(ssl);
  52633. wolfSSL_CTX_free(ctx);
  52634. AssertNotNull(cm = wolfSSL_CertManagerNew());
  52635. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  52636. WOLFSSL_SUCCESS);
  52637. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  52638. wolfSSL_CertManagerFree(cm);
  52639. res = TEST_RES_CHECK(1);
  52640. #endif
  52641. return res;
  52642. }
  52643. static int test_SetTmpEC_DHE_Sz(void)
  52644. {
  52645. int res = TEST_SKIPPED;
  52646. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  52647. WOLFSSL_CTX *ctx;
  52648. WOLFSSL *ssl;
  52649. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  52650. AssertNotNull(ctx);
  52651. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  52652. ssl = wolfSSL_new(ctx);
  52653. AssertNotNull(ssl);
  52654. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  52655. wolfSSL_free(ssl);
  52656. wolfSSL_CTX_free(ctx);
  52657. res = TEST_RES_CHECK(1);
  52658. #endif
  52659. return res;
  52660. }
  52661. static int test_wolfSSL_CTX_get0_privatekey(void)
  52662. {
  52663. int res = TEST_SKIPPED;
  52664. #ifdef OPENSSL_ALL
  52665. WOLFSSL_CTX* ctx = NULL;
  52666. #ifndef NO_RSA
  52667. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52668. AssertNull(SSL_CTX_get0_privatekey(ctx));
  52669. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  52670. WOLFSSL_FILETYPE_PEM));
  52671. AssertNull(SSL_CTX_get0_privatekey(ctx));
  52672. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  52673. WOLFSSL_FILETYPE_PEM));
  52674. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  52675. wolfSSL_CTX_free(ctx);
  52676. #endif
  52677. #ifdef HAVE_ECC
  52678. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52679. AssertNull(SSL_CTX_get0_privatekey(ctx));
  52680. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  52681. WOLFSSL_FILETYPE_PEM));
  52682. AssertNull(SSL_CTX_get0_privatekey(ctx));
  52683. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  52684. WOLFSSL_FILETYPE_PEM));
  52685. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  52686. wolfSSL_CTX_free(ctx);
  52687. #endif
  52688. res = TEST_RES_CHECK(1);
  52689. #endif
  52690. return res;
  52691. }
  52692. static int test_wolfSSL_dtls_set_mtu(void)
  52693. {
  52694. int res = TEST_SKIPPED;
  52695. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  52696. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  52697. WOLFSSL_CTX* ctx = NULL;
  52698. WOLFSSL* ssl = NULL;
  52699. const char* testCertFile;
  52700. const char* testKeyFile;
  52701. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  52702. #ifndef NO_RSA
  52703. testCertFile = svrCertFile;
  52704. testKeyFile = svrKeyFile;
  52705. #elif defined(HAVE_ECC)
  52706. testCertFile = eccCertFile;
  52707. testKeyFile = eccKeyFile;
  52708. #endif
  52709. if (testCertFile != NULL && testKeyFile != NULL) {
  52710. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  52711. WOLFSSL_FILETYPE_PEM));
  52712. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52713. WOLFSSL_FILETYPE_PEM));
  52714. }
  52715. AssertNotNull(ssl = wolfSSL_new(ctx));
  52716. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  52717. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  52718. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  52719. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  52720. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  52721. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  52722. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  52723. wolfSSL_free(ssl);
  52724. wolfSSL_CTX_free(ctx);
  52725. res = TEST_RES_CHECK(1);
  52726. #endif
  52727. return res;
  52728. }
  52729. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  52730. defined(WOLFSSL_DTLS)
  52731. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  52732. enum HandShakeType hsType, word16 length)
  52733. {
  52734. size_t idx = 0;
  52735. byte* l;
  52736. /* record layer */
  52737. /* handshake type */
  52738. out[idx++] = handshake;
  52739. /* protocol version */
  52740. out[idx++] = 0xfe;
  52741. out[idx++] = 0xfd; /* DTLS 1.2 */
  52742. /* epoch 0 */
  52743. XMEMSET(out + idx, 0, 2);
  52744. idx += 2;
  52745. /* sequence number */
  52746. XMEMSET(out + idx, 0, 6);
  52747. c32toa(seq, out + idx + 2);
  52748. idx += 6;
  52749. /* length in BE */
  52750. if (length)
  52751. c16toa(length, out + idx);
  52752. else
  52753. c16toa(outSz - idx - 2, out + idx);
  52754. idx += 2;
  52755. /* handshake layer */
  52756. /* handshake type */
  52757. out[idx++] = (byte)hsType;
  52758. /* length */
  52759. l = out + idx;
  52760. idx += 3;
  52761. /* message seq */
  52762. c16toa(0, out + idx);
  52763. idx += 2;
  52764. /* frag offset */
  52765. c32to24(0, out + idx);
  52766. idx += 3;
  52767. /* frag length */
  52768. c32to24((word32)outSz - (word32)idx - 3, l);
  52769. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  52770. idx += 3;
  52771. XMEMSET(out + idx, 0, outSz - idx);
  52772. }
  52773. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  52774. {
  52775. byte msg[] = "This is a msg for the client";
  52776. byte reply[40];
  52777. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  52778. reply[sizeof(reply) - 1] = '\0';
  52779. fprintf(stderr, "Client message: %s\n", reply);
  52780. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  52781. }
  52782. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  52783. {
  52784. byte ch[50];
  52785. int fd = wolfSSL_get_fd(ssl);
  52786. byte msg[] = "This is a msg for the server";
  52787. byte reply[40];
  52788. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  52789. /* Server should ignore this datagram */
  52790. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  52791. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  52792. /* Server should ignore this datagram */
  52793. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  52794. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  52795. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  52796. reply[sizeof(reply) - 1] = '\0';
  52797. fprintf(stderr, "Server response: %s\n", reply);
  52798. }
  52799. static int test_wolfSSL_dtls_plaintext(void)
  52800. {
  52801. callback_functions func_cb_client;
  52802. callback_functions func_cb_server;
  52803. size_t i;
  52804. struct test_params {
  52805. method_provider client_meth;
  52806. method_provider server_meth;
  52807. ssl_callback on_result_server;
  52808. ssl_callback on_result_client;
  52809. } params[] = {
  52810. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  52811. test_wolfSSL_dtls_plaintext_server,
  52812. test_wolfSSL_dtls_plaintext_client},
  52813. };
  52814. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  52815. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  52816. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  52817. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  52818. func_cb_server.method = params[i].server_meth;
  52819. func_cb_client.method = params[i].client_meth;
  52820. func_cb_client.on_result = params[i].on_result_client;
  52821. func_cb_server.on_result = params[i].on_result_server;
  52822. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  52823. if (!func_cb_client.return_code)
  52824. return TEST_FAIL;
  52825. if (!func_cb_server.return_code)
  52826. return TEST_FAIL;
  52827. }
  52828. return TEST_RES_CHECK(1);
  52829. }
  52830. #else
  52831. static int test_wolfSSL_dtls_plaintext(void) {
  52832. return TEST_SKIPPED;
  52833. }
  52834. #endif
  52835. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  52836. defined(WOLFSSL_DTLS)
  52837. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  52838. {
  52839. byte b[1100]; /* buffer for the messages to send */
  52840. size_t idx = 0;
  52841. size_t seq_offset = 0;
  52842. size_t msg_offset = 0;
  52843. int i;
  52844. int fd = wolfSSL_get_fd(ssl);
  52845. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  52846. word32 seq_number = 100; /* start high so server definitely reads this */
  52847. word16 msg_number = 50; /* start high so server has to buffer this */
  52848. AssertIntEQ(ret, 1);
  52849. /* Now let's start spamming the peer with fragments it needs to store */
  52850. XMEMSET(b, -1, sizeof(b));
  52851. /* record layer */
  52852. /* handshake type */
  52853. b[idx++] = 22;
  52854. /* protocol version */
  52855. b[idx++] = 0xfe;
  52856. b[idx++] = 0xfd; /* DTLS 1.2 */
  52857. /* epoch 0 */
  52858. XMEMSET(b + idx, 0, 2);
  52859. idx += 2;
  52860. /* sequence number */
  52861. XMEMSET(b + idx, 0, 6);
  52862. seq_offset = idx + 2; /* increment only the low 32 bits */
  52863. idx += 6;
  52864. /* static length in BE */
  52865. c16toa(42, b + idx);
  52866. idx += 2;
  52867. /* handshake layer */
  52868. /* cert type */
  52869. b[idx++] = 11;
  52870. /* length */
  52871. c32to24(1000, b + idx);
  52872. idx += 3;
  52873. /* message seq */
  52874. c16toa(0, b + idx);
  52875. msg_offset = idx;
  52876. idx += 2;
  52877. /* frag offset */
  52878. c32to24(500, b + idx);
  52879. idx += 3;
  52880. /* frag length */
  52881. c32to24(30, b + idx);
  52882. idx += 3;
  52883. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  52884. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  52885. seq_number++, msg_number++, i++) {
  52886. struct timespec delay;
  52887. XMEMSET(&delay, 0, sizeof(delay));
  52888. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  52889. c32toa(seq_number, b + seq_offset);
  52890. c16toa(msg_number, b + msg_offset);
  52891. ret = (int)send(fd, b, 55, 0);
  52892. nanosleep(&delay, NULL);
  52893. }
  52894. }
  52895. #ifdef WOLFSSL_DTLS13
  52896. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  52897. {
  52898. const word16 sendCountMax = 100;
  52899. byte b[150]; /* buffer for the messages to send */
  52900. size_t idx = 0;
  52901. size_t msg_offset = 0;
  52902. int fd = wolfSSL_get_fd(ssl);
  52903. word16 msg_number = 10; /* start high so server has to buffer this */
  52904. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  52905. AssertIntEQ(ret, 1);
  52906. /* Now let's start spamming the peer with fragments it needs to store */
  52907. XMEMSET(b, -1, sizeof(b));
  52908. /* handshake type */
  52909. b[idx++] = 11;
  52910. /* length */
  52911. c32to24(10000, b + idx);
  52912. idx += 3;
  52913. /* message_seq */
  52914. msg_offset = idx;
  52915. idx += 2;
  52916. /* fragment_offset */
  52917. c32to24(5000, b + idx);
  52918. idx += 3;
  52919. /* fragment_length */
  52920. c32to24(100, b + idx);
  52921. idx += 3;
  52922. /* fragment contents */
  52923. idx += 100;
  52924. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  52925. byte sendBuf[150];
  52926. int sendSz = sizeof(sendBuf);
  52927. struct timespec delay;
  52928. XMEMSET(&delay, 0, sizeof(delay));
  52929. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  52930. c16toa(msg_number, b + msg_offset);
  52931. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  52932. (int)idx, handshake, 0, 0, 0);
  52933. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  52934. nanosleep(&delay, NULL);
  52935. }
  52936. }
  52937. #endif
  52938. static int test_wolfSSL_dtls_fragments(void)
  52939. {
  52940. callback_functions func_cb_client;
  52941. callback_functions func_cb_server;
  52942. size_t i;
  52943. struct test_params {
  52944. method_provider client_meth;
  52945. method_provider server_meth;
  52946. ssl_callback spammer;
  52947. } params[] = {
  52948. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  52949. test_wolfSSL_dtls12_fragments_spammer},
  52950. #ifdef WOLFSSL_DTLS13
  52951. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  52952. test_wolfSSL_dtls13_fragments_spammer},
  52953. #endif
  52954. };
  52955. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  52956. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  52957. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  52958. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  52959. func_cb_server.method = params[i].server_meth;
  52960. func_cb_client.method = params[i].client_meth;
  52961. func_cb_client.ssl_ready = params[i].spammer;
  52962. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  52963. AssertFalse(func_cb_client.return_code);
  52964. AssertFalse(func_cb_server.return_code);
  52965. /* The socket should be closed by the server resulting in a
  52966. * socket error, fatal error or reading a close notify alert */
  52967. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  52968. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  52969. func_cb_client.last_err != FATAL_ERROR) {
  52970. AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  52971. }
  52972. /* Check the server returned an error indicating the msg buffer
  52973. * was full */
  52974. AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  52975. }
  52976. return TEST_RES_CHECK(1);
  52977. }
  52978. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  52979. {
  52980. int fd, ret;
  52981. byte alert_msg[] = {
  52982. 0x15, /* alert type */
  52983. 0xfe, 0xfd, /* version */
  52984. 0x00, 0x00, /* epoch */
  52985. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  52986. 0x00, 0x02, /* length */
  52987. 0x02, /* level: fatal */
  52988. 0x46 /* protocol version */
  52989. };
  52990. fd = wolfSSL_get_fd(ssl);
  52991. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  52992. AssertIntGT(ret, 0);
  52993. }
  52994. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  52995. {
  52996. callback_functions client_cbs, server_cbs;
  52997. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  52998. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  52999. client_cbs.doUdp = server_cbs.doUdp = 1;
  53000. if (version12) {
  53001. client_cbs.method = wolfDTLSv1_2_client_method;
  53002. server_cbs.method = wolfDTLSv1_2_server_method;
  53003. }
  53004. else {
  53005. #ifdef WOLFSSL_DTLS13
  53006. client_cbs.method = wolfDTLSv1_3_client_method;
  53007. server_cbs.method = wolfDTLSv1_3_server_method;
  53008. #else
  53009. return TEST_SKIPPED;
  53010. #endif /* WOLFSSL_DTLS13 */
  53011. }
  53012. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  53013. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53014. if (!client_cbs.return_code)
  53015. return TEST_FAIL;
  53016. if (!server_cbs.return_code)
  53017. return TEST_FAIL;
  53018. return TEST_SUCCESS;
  53019. }
  53020. static int test_wolfSSL_ignore_alert_before_cookie(void)
  53021. {
  53022. int ret;
  53023. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  53024. if (ret != 0)
  53025. return ret;
  53026. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  53027. if (ret != 0)
  53028. return ret;
  53029. return 0;
  53030. }
  53031. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  53032. {
  53033. int ret;
  53034. int fd;
  53035. byte bad_msg[] = {
  53036. 0x17, /* app data */
  53037. 0xaa, 0xfd, /* bad version */
  53038. 0x00, 0x01, /* epoch 1 */
  53039. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  53040. 0x00, 0x26, /* length: 38 bytes */
  53041. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  53042. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  53043. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  53044. 0x03, 0x18, 0x72
  53045. };
  53046. fd = wolfSSL_get_fd(ssl);
  53047. AssertIntGE(fd, 0);
  53048. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  53049. AssertIntEQ(ret, sizeof(bad_msg));
  53050. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  53051. AssertIntEQ(ret, sizeof("badrecordtest"));
  53052. }
  53053. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  53054. {
  53055. byte buf[100];
  53056. int ret;
  53057. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  53058. AssertIntGT(ret, 0);
  53059. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  53060. }
  53061. static int _test_wolfSSL_dtls_bad_record(
  53062. method_provider client_method, method_provider server_method)
  53063. {
  53064. callback_functions client_cbs, server_cbs;
  53065. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53066. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53067. client_cbs.doUdp = server_cbs.doUdp = 1;
  53068. client_cbs.method = client_method;
  53069. server_cbs.method = server_method;
  53070. client_cbs.on_result = test_wolfSSL_send_bad_record;
  53071. server_cbs.on_result = test_wolfSSL_read_string;
  53072. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53073. if (!client_cbs.return_code)
  53074. return TEST_FAIL;
  53075. if (!server_cbs.return_code)
  53076. return TEST_FAIL;
  53077. return TEST_SUCCESS;
  53078. }
  53079. static int test_wolfSSL_dtls_bad_record(void)
  53080. {
  53081. int ret;
  53082. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  53083. wolfDTLSv1_2_server_method);
  53084. #ifdef WOLFSSL_DTLS13
  53085. if (ret != TEST_SUCCESS)
  53086. return ret;
  53087. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  53088. wolfDTLSv1_3_server_method);
  53089. #else
  53090. return ret;
  53091. #endif /* WOLFSSL_DTLS13 */
  53092. }
  53093. #else
  53094. static int test_wolfSSL_dtls_fragments(void) {
  53095. return TEST_SKIPPED;
  53096. }
  53097. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  53098. return TEST_SKIPPED;
  53099. }
  53100. static int test_wolfSSL_dtls_bad_record(void) {
  53101. return TEST_SKIPPED;
  53102. }
  53103. #endif
  53104. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  53105. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  53106. defined(HAVE_IO_TESTS_DEPENDENCIES)
  53107. static byte test_AEAD_fail_decryption = 0;
  53108. static byte test_AEAD_seq_num = 0;
  53109. static byte test_AEAD_done = 0;
  53110. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  53111. {
  53112. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  53113. if (ret > 0) {
  53114. if (test_AEAD_fail_decryption) {
  53115. /* Modify the packet to trigger a decryption failure */
  53116. buf[ret/2] ^= 0xFF;
  53117. if (test_AEAD_fail_decryption == 1)
  53118. test_AEAD_fail_decryption = 0;
  53119. }
  53120. }
  53121. (void)ctx;
  53122. return ret;
  53123. }
  53124. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  53125. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  53126. {
  53127. if (sendLimit)
  53128. w64Zero(sendLimit);
  53129. switch (ssl->specs.bulk_cipher_algorithm) {
  53130. case wolfssl_aes_gcm:
  53131. if (sendLimit)
  53132. *sendLimit = AEAD_AES_LIMIT;
  53133. FALL_THROUGH;
  53134. case wolfssl_chacha:
  53135. if (hardLimit)
  53136. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  53137. if (keyUpdateLimit)
  53138. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  53139. break;
  53140. case wolfssl_aes_ccm:
  53141. if (sendLimit)
  53142. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  53143. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  53144. if (hardLimit)
  53145. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  53146. if (keyUpdateLimit)
  53147. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  53148. }
  53149. else {
  53150. if (hardLimit)
  53151. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  53152. if (keyUpdateLimit)
  53153. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  53154. }
  53155. break;
  53156. default:
  53157. fprintf(stderr, "Unrecognized bulk cipher");
  53158. AssertFalse(1);
  53159. break;
  53160. }
  53161. }
  53162. static void test_AEAD_limit_client(WOLFSSL* ssl)
  53163. {
  53164. int ret;
  53165. int i;
  53166. int didReKey = 0;
  53167. char msgBuf[20];
  53168. w64wrapper hardLimit;
  53169. w64wrapper keyUpdateLimit;
  53170. w64wrapper counter;
  53171. w64wrapper sendLimit;
  53172. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  53173. w64Zero(&counter);
  53174. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  53175. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  53176. for (i = 0; i < 10; i++) {
  53177. /* Test some failed decryptions */
  53178. test_AEAD_fail_decryption = 1;
  53179. w64Increment(&counter);
  53180. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53181. /* Should succeed since decryption failures are dropped */
  53182. AssertIntGT(ret, 0);
  53183. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  53184. }
  53185. test_AEAD_fail_decryption = 1;
  53186. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  53187. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  53188. /* 100 read calls should be enough to complete the key update */
  53189. w64Zero(&counter);
  53190. for (i = 0; i < 100; i++) {
  53191. /* Key update should be sent and negotiated */
  53192. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53193. AssertIntGT(ret, 0);
  53194. /* Epoch after one key update is 4 */
  53195. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  53196. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  53197. didReKey = 1;
  53198. break;
  53199. }
  53200. }
  53201. AssertTrue(didReKey);
  53202. if (!w64IsZero(sendLimit)) {
  53203. /* Test the sending limit for AEAD ciphers */
  53204. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  53205. test_AEAD_seq_num = 1;
  53206. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  53207. AssertIntGT(ret, 0);
  53208. didReKey = 0;
  53209. w64Zero(&counter);
  53210. /* 100 read calls should be enough to complete the key update */
  53211. for (i = 0; i < 100; i++) {
  53212. /* Key update should be sent and negotiated */
  53213. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53214. AssertIntGT(ret, 0);
  53215. /* Epoch after another key update is 5 */
  53216. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  53217. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  53218. didReKey = 1;
  53219. break;
  53220. }
  53221. }
  53222. AssertTrue(didReKey);
  53223. }
  53224. test_AEAD_fail_decryption = 2;
  53225. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  53226. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  53227. /* Connection should fail with a DECRYPT_ERROR */
  53228. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53229. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  53230. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  53231. test_AEAD_done = 1;
  53232. }
  53233. int counter = 0;
  53234. static void test_AEAD_limit_server(WOLFSSL* ssl)
  53235. {
  53236. char msgBuf[] = "Sending data";
  53237. int ret = WOLFSSL_SUCCESS;
  53238. w64wrapper sendLimit;
  53239. SOCKET_T fd = wolfSSL_get_fd(ssl);
  53240. struct timespec delay;
  53241. XMEMSET(&delay, 0, sizeof(delay));
  53242. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  53243. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  53244. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  53245. while (!test_AEAD_done && ret > 0) {
  53246. counter++;
  53247. if (test_AEAD_seq_num) {
  53248. /* We need to update the seq number so that we can understand the
  53249. * peer. Otherwise we will incorrectly interpret the seq number. */
  53250. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  53251. AssertNotNull(e);
  53252. e->nextPeerSeqNumber = sendLimit;
  53253. test_AEAD_seq_num = 0;
  53254. }
  53255. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53256. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  53257. nanosleep(&delay, NULL);
  53258. }
  53259. }
  53260. static int test_wolfSSL_dtls_AEAD_limit(void)
  53261. {
  53262. callback_functions func_cb_client;
  53263. callback_functions func_cb_server;
  53264. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53265. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53266. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53267. func_cb_server.method = wolfDTLSv1_3_server_method;
  53268. func_cb_client.method = wolfDTLSv1_3_client_method;
  53269. func_cb_server.on_result = test_AEAD_limit_server;
  53270. func_cb_client.on_result = test_AEAD_limit_client;
  53271. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53272. if (!func_cb_client.return_code)
  53273. return TEST_FAIL;
  53274. if (!func_cb_server.return_code)
  53275. return TEST_FAIL;
  53276. return TEST_SUCCESS;
  53277. }
  53278. #else
  53279. static int test_wolfSSL_dtls_AEAD_limit(void)
  53280. {
  53281. return TEST_SKIPPED;
  53282. }
  53283. #endif
  53284. #if defined(WOLFSSL_DTLS) && \
  53285. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  53286. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  53287. {
  53288. int fd, ret;
  53289. byte ch_msg[] = {
  53290. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  53291. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  53292. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  53293. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  53294. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  53295. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53296. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53297. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53298. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53299. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53300. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53301. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53302. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53303. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  53304. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  53305. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  53306. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  53307. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  53308. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  53309. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  53310. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  53311. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  53312. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  53313. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  53314. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  53315. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  53316. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  53317. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  53318. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  53319. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  53320. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  53321. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  53322. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  53323. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  53324. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  53325. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  53326. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  53327. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  53328. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  53329. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  53330. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  53331. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  53332. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  53333. 0x31, 0x68, 0x4c
  53334. };
  53335. fd = wolfSSL_get_fd(ssl);
  53336. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  53337. AssertIntGT(ret, 0);
  53338. /* consume the HRR otherwise handshake will fail */
  53339. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  53340. AssertIntGT(ret, 0);
  53341. }
  53342. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53343. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  53344. {
  53345. int fd, ret;
  53346. byte ch_msh_invalid_cookie[] = {
  53347. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  53348. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  53349. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  53350. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  53351. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  53352. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53353. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53354. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53355. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53356. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53357. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53358. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53359. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53360. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  53361. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53362. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53363. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53364. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53365. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53366. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  53367. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  53368. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  53369. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  53370. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  53371. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  53372. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  53373. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  53374. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  53375. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  53376. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  53377. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  53378. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  53379. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  53380. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  53381. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  53382. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  53383. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  53384. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  53385. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  53386. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  53387. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  53388. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  53389. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  53390. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  53391. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  53392. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  53393. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  53394. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  53395. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  53396. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  53397. 0x23, 0x00, 0x00
  53398. };
  53399. byte alert_reply[50];
  53400. byte expected_alert_reply[] = {
  53401. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  53402. 0x02, 0x02, 0x2f
  53403. };
  53404. fd = wolfSSL_get_fd(ssl);
  53405. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  53406. AssertIntGT(ret, 0);
  53407. /* should reply with an illegal_parameter reply */
  53408. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  53409. AssertIntEQ(ret, sizeof(expected_alert_reply));
  53410. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  53411. }
  53412. #endif
  53413. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  53414. {
  53415. #ifndef NO_MD5
  53416. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  53417. #elif !defined(NO_SHA)
  53418. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  53419. #elif !defined(NO_SHA256)
  53420. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  53421. #else
  53422. #error "We need a digest to hash the WOLFSSL object"
  53423. #endif
  53424. byte hashBuf[WC_MAX_DIGEST_SIZE];
  53425. wc_HashAlg hash;
  53426. const TLSX* exts = ssl->extensions;
  53427. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  53428. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  53429. * InitHandshakeHashes */
  53430. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  53431. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  53432. /* Following fields are not important to compare */
  53433. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53434. sslCopy.buffers.inputBuffer.buffer = NULL;
  53435. sslCopy.buffers.inputBuffer.bufferSize = 0;
  53436. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  53437. sslCopy.buffers.inputBuffer.offset = 0;
  53438. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53439. sslCopy.buffers.outputBuffer.buffer = NULL;
  53440. sslCopy.buffers.outputBuffer.bufferSize = 0;
  53441. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  53442. sslCopy.buffers.outputBuffer.offset = 0;
  53443. sslCopy.error = 0;
  53444. sslCopy.curSize = 0;
  53445. sslCopy.keys.curSeq_lo = 0;
  53446. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  53447. #ifdef WOLFSSL_DTLS13
  53448. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  53449. sslCopy.dtls13FastTimeout = 0;
  53450. #endif
  53451. sslCopy.keys.dtls_peer_handshake_number = 0;
  53452. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  53453. sslCopy.hsHashes = NULL;
  53454. #ifdef WOLFSSL_ASYNC_IO
  53455. #ifdef WOLFSSL_ASYNC_CRYPT
  53456. sslCopy.asyncDev = NULL;
  53457. #endif
  53458. sslCopy.async = NULL;
  53459. #endif
  53460. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  53461. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  53462. /* hash extension list */
  53463. while (exts != NULL) {
  53464. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  53465. exts = exts->next;
  53466. }
  53467. /* Hash suites */
  53468. if (sslCopy.suites != NULL) {
  53469. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  53470. sizeof(struct Suites)), 0);
  53471. }
  53472. /* Hash hsHashes */
  53473. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  53474. sizeof(*hsHashes)), 0);
  53475. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  53476. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  53477. return MakeWordFromHash(hashBuf);
  53478. }
  53479. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  53480. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  53481. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  53482. char *buf, int sz, void *ctx)
  53483. {
  53484. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  53485. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  53486. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  53487. }
  53488. else {
  53489. return WOLFSSL_CBIO_ERR_WANT_READ;
  53490. }
  53491. }
  53492. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  53493. {
  53494. /* Compare the ssl object before and after one ClientHello msg */
  53495. SOCKET_T fd = wolfSSL_get_fd(ssl);
  53496. int res;
  53497. int err;
  53498. word32 initHash;
  53499. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  53500. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  53501. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  53502. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  53503. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  53504. (void)initHash;
  53505. res = tcp_select(fd, 5);
  53506. /* We are expecting a msg. A timeout indicates failure. */
  53507. AssertIntEQ(res, TEST_RECV_READY);
  53508. res = wolfSSL_accept(ssl);
  53509. err = wolfSSL_get_error(ssl, res);
  53510. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  53511. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  53512. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  53513. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  53514. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  53515. }
  53516. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53517. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  53518. {
  53519. int ret;
  53520. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  53521. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  53522. test_wolfSSL_dtls_compare_stateless(ssl);
  53523. }
  53524. #endif
  53525. static int test_wolfSSL_dtls_stateless(void)
  53526. {
  53527. callback_functions client_cbs, server_cbs;
  53528. size_t i;
  53529. struct {
  53530. method_provider client_meth;
  53531. method_provider server_meth;
  53532. ssl_callback client_ssl_ready;
  53533. ssl_callback server_ssl_ready;
  53534. } test_params[] = {
  53535. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53536. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  53537. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53538. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53539. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  53540. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53541. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  53542. #endif
  53543. };
  53544. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  53545. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53546. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53547. client_cbs.doUdp = server_cbs.doUdp = 1;
  53548. client_cbs.method = test_params[i].client_meth;
  53549. server_cbs.method = test_params[i].server_meth;
  53550. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  53551. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  53552. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53553. if (!client_cbs.return_code)
  53554. return TEST_FAIL;
  53555. if (!server_cbs.return_code)
  53556. return TEST_FAIL;
  53557. }
  53558. return TEST_SUCCESS;
  53559. }
  53560. #else
  53561. static int test_wolfSSL_dtls_stateless(void)
  53562. {
  53563. return TEST_SKIPPED;
  53564. }
  53565. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  53566. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  53567. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  53568. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  53569. !defined(WOLFSSL_NO_CLIENT_AUTH))
  53570. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  53571. {
  53572. int ret;
  53573. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  53574. fprintf(stderr, "loading cert %s failed\n", certA);
  53575. fprintf(stderr, "Error: (%d): %s\n", ret,
  53576. wolfSSL_ERR_reason_error_string(ret));
  53577. return -1;
  53578. }
  53579. return 0;
  53580. }
  53581. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  53582. {
  53583. int ret;
  53584. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  53585. != WOLFSSL_SUCCESS) {
  53586. fprintf(stderr, "could not verify the cert: %s\n", certA);
  53587. fprintf(stderr, "Error: (%d): %s\n", ret,
  53588. wolfSSL_ERR_reason_error_string(ret));
  53589. return -1;
  53590. }
  53591. else {
  53592. fprintf(stderr, "successfully verified: %s\n", certA);
  53593. }
  53594. return 0;
  53595. }
  53596. #define LOAD_ONE_CA(a, b, c, d) \
  53597. do { \
  53598. (a) = load_ca_into_cm(c, d); \
  53599. if ((a) != 0) \
  53600. return (b); \
  53601. else \
  53602. (b)--; \
  53603. } while(0)
  53604. #define VERIFY_ONE_CERT(a, b, c, d) \
  53605. do { \
  53606. (a) = verify_cert_with_cm(c, d); \
  53607. if ((a) != 0) \
  53608. return (b); \
  53609. else \
  53610. (b)--; \
  53611. } while(0)
  53612. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  53613. {
  53614. int ret;
  53615. int i = -1;
  53616. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  53617. char chainGArr[9][50] = {"certs/ca-cert.pem",
  53618. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  53619. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  53620. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  53621. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  53622. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  53623. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  53624. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  53625. "certs/test-pathlen/chainG-entity.pem"};
  53626. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  53627. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  53628. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  53629. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  53630. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  53631. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  53632. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  53633. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  53634. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  53635. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  53636. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  53637. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  53638. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  53639. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  53640. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  53641. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  53642. /* test validating the entity twice, should have no effect on pathLen since
  53643. * entity/leaf cert */
  53644. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  53645. return ret;
  53646. }
  53647. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  53648. {
  53649. int ret;
  53650. int i = -1;
  53651. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  53652. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  53653. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  53654. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  53655. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  53656. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  53657. */
  53658. char chainHArr[6][50] = {"certs/ca-cert.pem",
  53659. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  53660. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  53661. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  53662. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  53663. "certs/test-pathlen/chainH-entity.pem"};
  53664. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  53665. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  53666. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  53667. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  53668. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  53669. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  53670. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  53671. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  53672. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  53673. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  53674. return ret;
  53675. }
  53676. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  53677. {
  53678. int ret;
  53679. int i = -1;
  53680. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  53681. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  53682. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  53683. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  53684. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  53685. */
  53686. char chainIArr[5][50] = {"certs/ca-cert.pem",
  53687. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  53688. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  53689. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  53690. "certs/test-pathlen/chainI-entity.pem"};
  53691. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  53692. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  53693. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  53694. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  53695. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  53696. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  53697. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  53698. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  53699. return ret;
  53700. }
  53701. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  53702. {
  53703. int ret;
  53704. int i = -1;
  53705. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  53706. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  53707. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  53708. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  53709. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  53710. */
  53711. char chainJArr[6][50] = {"certs/ca-cert.pem",
  53712. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  53713. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  53714. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  53715. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  53716. "certs/test-pathlen/chainJ-entity.pem"};
  53717. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  53718. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  53719. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  53720. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  53721. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  53722. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  53723. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  53724. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  53725. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  53726. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  53727. return ret;
  53728. }
  53729. static int test_various_pathlen_chains(void)
  53730. {
  53731. int ret;
  53732. WOLFSSL_CERT_MANAGER* cm;
  53733. /* Test chain G (large chain with varying pathLens) */
  53734. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53735. fprintf(stderr, "cert manager new failed\n");
  53736. return -1;
  53737. }
  53738. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  53739. AssertIntEQ(test_chainG(cm), -1);
  53740. #else
  53741. AssertIntEQ(test_chainG(cm), 0);
  53742. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  53743. ret = wolfSSL_CertManagerUnloadCAs(cm);
  53744. if (ret != WOLFSSL_SUCCESS)
  53745. return -1;
  53746. wolfSSL_CertManagerFree(cm);
  53747. /* end test chain G */
  53748. /* Test chain H (5 chain with same pathLens) */
  53749. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53750. fprintf(stderr, "cert manager new failed\n");
  53751. return -1;
  53752. }
  53753. AssertIntLT(test_chainH(cm), 0);
  53754. wolfSSL_CertManagerUnloadCAs(cm);
  53755. wolfSSL_CertManagerFree(cm);
  53756. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53757. fprintf(stderr, "cert manager new failed\n");
  53758. return -1;
  53759. }
  53760. ret = wolfSSL_CertManagerUnloadCAs(cm);
  53761. if (ret != WOLFSSL_SUCCESS)
  53762. return -1;
  53763. wolfSSL_CertManagerFree(cm);
  53764. /* end test chain H */
  53765. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  53766. * followed by 2 ICA's, should pass) */
  53767. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53768. fprintf(stderr, "cert manager new failed\n");
  53769. return -1;
  53770. }
  53771. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  53772. AssertIntEQ(test_chainI(cm), -1);
  53773. #else
  53774. AssertIntEQ(test_chainI(cm), 0);
  53775. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  53776. wolfSSL_CertManagerUnloadCAs(cm);
  53777. wolfSSL_CertManagerFree(cm);
  53778. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53779. fprintf(stderr, "cert manager new failed\n");
  53780. return -1;
  53781. }
  53782. ret = wolfSSL_CertManagerUnloadCAs(cm);
  53783. if (ret != WOLFSSL_SUCCESS)
  53784. return -1;
  53785. wolfSSL_CertManagerFree(cm);
  53786. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  53787. * this time followed by 3 ICA's, should fail */
  53788. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53789. fprintf(stderr, "cert manager new failed\n");
  53790. return -1;
  53791. }
  53792. AssertIntLT(test_chainJ(cm), 0);
  53793. wolfSSL_CertManagerUnloadCAs(cm);
  53794. wolfSSL_CertManagerFree(cm);
  53795. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53796. fprintf(stderr, "cert manager new failed\n");
  53797. return -1;
  53798. }
  53799. ret = wolfSSL_CertManagerUnloadCAs(cm);
  53800. wolfSSL_CertManagerFree(cm);
  53801. return TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  53802. }
  53803. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  53804. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  53805. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  53806. {
  53807. byte ekm[100] = {0};
  53808. (void)ctx;
  53809. /* Succes Cases */
  53810. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53811. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  53812. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53813. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  53814. /* Use some random context */
  53815. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53816. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  53817. /* Failure cases */
  53818. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53819. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  53820. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53821. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  53822. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53823. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  53824. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53825. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  53826. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53827. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  53828. return TEST_RES_CHECK(1);
  53829. }
  53830. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  53831. {
  53832. wolfSSL_KeepArrays(ssl);
  53833. }
  53834. static int test_export_keying_material(void)
  53835. {
  53836. int res = TEST_SKIPPED;
  53837. #ifndef SINGLE_THREADED
  53838. tcp_ready ready;
  53839. callback_functions clientCb;
  53840. func_args client_args;
  53841. func_args server_args;
  53842. THREAD_TYPE serverThread;
  53843. XMEMSET(&client_args, 0, sizeof(func_args));
  53844. XMEMSET(&server_args, 0, sizeof(func_args));
  53845. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  53846. #ifdef WOLFSSL_TIRTOS
  53847. fdOpenSession(Task_self());
  53848. #endif
  53849. StartTCP();
  53850. InitTcpReady(&ready);
  53851. #if defined(USE_WINDOWS_API)
  53852. /* use RNG to get random port if using windows */
  53853. ready.port = GetRandomPort();
  53854. #endif
  53855. server_args.signal = &ready;
  53856. client_args.signal = &ready;
  53857. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  53858. client_args.callbacks = &clientCb;
  53859. start_thread(test_server_nofail, &server_args, &serverThread);
  53860. wait_tcp_ready(&server_args);
  53861. test_client_nofail(&client_args, test_export_keying_material_cb);
  53862. join_thread(serverThread);
  53863. AssertTrue(client_args.return_code);
  53864. AssertTrue(server_args.return_code);
  53865. FreeTcpReady(&ready);
  53866. #ifdef WOLFSSL_TIRTOS
  53867. fdOpenSession(Task_self());
  53868. #endif
  53869. res = TEST_RES_CHECK(1);
  53870. #endif /* !SINGLE_THREADED */
  53871. return res;
  53872. }
  53873. #endif /* HAVE_KEYING_MATERIAL */
  53874. static int test_wolfSSL_THREADID_hash(void)
  53875. {
  53876. int result = TEST_SKIPPED;
  53877. #if defined(OPENSSL_EXTRA)
  53878. unsigned long res;
  53879. CRYPTO_THREADID id;
  53880. CRYPTO_THREADID_current(NULL);
  53881. AssertTrue(1);
  53882. res = CRYPTO_THREADID_hash(NULL);
  53883. AssertTrue( res == 0UL);
  53884. XMEMSET(&id, 0, sizeof(id));
  53885. res = CRYPTO_THREADID_hash(&id);
  53886. AssertTrue( res == 0UL);
  53887. result = TEST_RES_CHECK(1);
  53888. #endif /* OPENSSL_EXTRA */
  53889. return result;
  53890. }
  53891. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  53892. {
  53893. int res = TEST_SKIPPED;
  53894. #if defined(OPENSSL_EXTRA)
  53895. WOLFSSL_CTX* ctx = NULL;
  53896. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  53897. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  53898. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  53899. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  53900. res = TEST_RES_CHECK(1);
  53901. #endif /* OPENSSL_EXTRA */
  53902. return res;
  53903. }
  53904. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  53905. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  53906. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  53907. {
  53908. callback_functions* callbacks = NULL;
  53909. WOLFSSL_CTX* ctx = NULL;
  53910. WOLFSSL* ssl = NULL;
  53911. SOCKET_T sfd = 0;
  53912. SOCKET_T cfd = 0;
  53913. word16 port;
  53914. char msg[] = "I hear you fa shizzle!";
  53915. int len = (int) XSTRLEN(msg);
  53916. char input[1024];
  53917. int ret, err;
  53918. if (!args)
  53919. return 0;
  53920. ((func_args*)args)->return_code = TEST_FAIL;
  53921. callbacks = ((func_args*)args)->callbacks;
  53922. ctx = wolfSSL_CTX_new(callbacks->method());
  53923. #if defined(USE_WINDOWS_API)
  53924. port = ((func_args*)args)->signal->port;
  53925. #else
  53926. /* Let tcp_listen assign port */
  53927. port = 0;
  53928. #endif
  53929. #ifdef WOLFSSL_TIRTOS
  53930. fdOpenSession(Task_self());
  53931. #endif
  53932. AssertIntEQ(WOLFSSL_SUCCESS,
  53933. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  53934. AssertIntEQ(WOLFSSL_SUCCESS,
  53935. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53936. WOLFSSL_FILETYPE_PEM));
  53937. AssertIntEQ(WOLFSSL_SUCCESS,
  53938. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53939. WOLFSSL_FILETYPE_PEM));
  53940. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  53941. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  53942. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53943. #elif !defined(NO_DH)
  53944. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  53945. #endif
  53946. if (callbacks->ctx_ready)
  53947. callbacks->ctx_ready(ctx);
  53948. ssl = wolfSSL_new(ctx);
  53949. AssertNotNull(ssl);
  53950. /* listen and accept */
  53951. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  53952. CloseSocket(sfd);
  53953. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  53954. if (callbacks->ssl_ready)
  53955. callbacks->ssl_ready(ssl);
  53956. do {
  53957. err = 0; /* Reset error */
  53958. ret = wolfSSL_accept(ssl);
  53959. if (ret != WOLFSSL_SUCCESS) {
  53960. err = wolfSSL_get_error(ssl, 0);
  53961. }
  53962. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  53963. if (ret != WOLFSSL_SUCCESS) {
  53964. wolfSSL_free(ssl);
  53965. wolfSSL_CTX_free(ctx);
  53966. CloseSocket(cfd);
  53967. ((func_args*)args)->return_code = TEST_FAIL;
  53968. return 0;
  53969. }
  53970. /* read and write data */
  53971. XMEMSET( input, 0, sizeof(input));
  53972. while (1) {
  53973. ret = wolfSSL_read(ssl, input, sizeof(input));
  53974. if (ret > 0) {
  53975. break;
  53976. }
  53977. else {
  53978. err = wolfSSL_get_error(ssl,ret);
  53979. if (err == WOLFSSL_ERROR_WANT_READ) {
  53980. continue;
  53981. }
  53982. break;
  53983. }
  53984. }
  53985. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  53986. do {
  53987. ret = wolfSSL_write(ssl, msg, len);
  53988. if (ret > 0) {
  53989. break;
  53990. }
  53991. } while (ret < 0);
  53992. }
  53993. /* bidirectional shutdown */
  53994. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  53995. continue;
  53996. }
  53997. /* wait for the peer to disconnect the tcp connection */
  53998. do {
  53999. ret = wolfSSL_read(ssl, input, sizeof(input));
  54000. err = wolfSSL_get_error(ssl, ret);
  54001. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  54002. /* detect TCP disconnect */
  54003. AssertIntLE(ret,WOLFSSL_FAILURE);
  54004. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  54005. ((func_args*)args)->return_code = TEST_SUCCESS;
  54006. wolfSSL_free(ssl);
  54007. wolfSSL_CTX_free(ctx);
  54008. CloseSocket(cfd);
  54009. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54010. wc_ecc_fp_free(); /* free per thread cache */
  54011. #endif
  54012. return 0;
  54013. }
  54014. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  54015. {
  54016. callback_functions* callbacks = NULL;
  54017. WOLFSSL_CTX* ctx = NULL;
  54018. WOLFSSL* ssl = NULL;
  54019. SOCKET_T sfd = 0;
  54020. char msg[] = "hello wolfssl server!";
  54021. int len = (int) XSTRLEN(msg);
  54022. char input[1024];
  54023. int idx;
  54024. int ret, err;
  54025. if (!args)
  54026. return 0;
  54027. ((func_args*)args)->return_code = TEST_FAIL;
  54028. callbacks = ((func_args*)args)->callbacks;
  54029. ctx = wolfSSL_CTX_new(callbacks->method());
  54030. #ifdef WOLFSSL_TIRTOS
  54031. fdOpenSession(Task_self());
  54032. #endif
  54033. AssertIntEQ(WOLFSSL_SUCCESS,
  54034. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  54035. AssertIntEQ(WOLFSSL_SUCCESS,
  54036. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  54037. WOLFSSL_FILETYPE_PEM));
  54038. AssertIntEQ(WOLFSSL_SUCCESS,
  54039. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  54040. WOLFSSL_FILETYPE_PEM));
  54041. AssertNotNull((ssl = wolfSSL_new(ctx)));
  54042. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  54043. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  54044. do {
  54045. err = 0; /* Reset error */
  54046. ret = wolfSSL_connect(ssl);
  54047. if (ret != WOLFSSL_SUCCESS) {
  54048. err = wolfSSL_get_error(ssl, 0);
  54049. }
  54050. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54051. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  54052. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  54053. input[idx] = 0;
  54054. }
  54055. ret = wolfSSL_shutdown(ssl);
  54056. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  54057. ret = wolfSSL_shutdown(ssl);
  54058. }
  54059. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  54060. ((func_args*)args)->return_code = TEST_SUCCESS;
  54061. wolfSSL_free(ssl);
  54062. wolfSSL_CTX_free(ctx);
  54063. CloseSocket(sfd);
  54064. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54065. wc_ecc_fp_free(); /* free per thread cache */
  54066. #endif
  54067. return 0;
  54068. }
  54069. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  54070. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  54071. /* This test is to check wolfSSL_read behaves as same as
  54072. * openSSL when it is called after SSL_shutdown completes.
  54073. */
  54074. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  54075. {
  54076. int res = TEST_SKIPPED;
  54077. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54078. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54079. tcp_ready ready;
  54080. func_args client_args;
  54081. func_args server_args;
  54082. THREAD_TYPE serverThread;
  54083. THREAD_TYPE clientThread;
  54084. callback_functions server_cbf;
  54085. callback_functions client_cbf;
  54086. #ifdef WOLFSSL_TIRTOS
  54087. fdOpenSession(Task_self());
  54088. #endif
  54089. StartTCP();
  54090. InitTcpReady(&ready);
  54091. #if defined(USE_WINDOWS_API)
  54092. /* use RNG to get random port if using windows */
  54093. ready.port = GetRandomPort();
  54094. #endif
  54095. XMEMSET(&client_args, 0, sizeof(func_args));
  54096. XMEMSET(&server_args, 0, sizeof(func_args));
  54097. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  54098. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  54099. server_cbf.method = wolfTLSv1_2_server_method;
  54100. client_cbf.method = wolfTLSv1_2_client_method;
  54101. server_args.callbacks = &server_cbf;
  54102. client_args.callbacks = &client_cbf;
  54103. server_args.signal = &ready;
  54104. client_args.signal = &ready;
  54105. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  54106. wait_tcp_ready(&server_args);
  54107. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  54108. join_thread(clientThread);
  54109. join_thread(serverThread);
  54110. AssertTrue(client_args.return_code);
  54111. AssertTrue(server_args.return_code);
  54112. FreeTcpReady(&ready);
  54113. res = TEST_RES_CHECK(1);
  54114. #endif
  54115. return res;
  54116. }
  54117. static int test_wolfSSL_CTX_get_min_proto_version(void)
  54118. {
  54119. int res = TEST_SKIPPED;
  54120. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54121. WOLFSSL_CTX *ctx;
  54122. (void)ctx;
  54123. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54124. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  54125. #ifdef WOLFSSL_ALLOW_SSLV3
  54126. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  54127. #else
  54128. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  54129. #endif
  54130. wolfSSL_CTX_free(ctx);
  54131. #ifdef WOLFSSL_ALLOW_TLSV10
  54132. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  54133. #else
  54134. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54135. #endif
  54136. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  54137. #ifdef WOLFSSL_ALLOW_TLSV10
  54138. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  54139. #else
  54140. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  54141. #endif
  54142. wolfSSL_CTX_free(ctx);
  54143. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54144. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  54145. #ifndef NO_OLD_TLS
  54146. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  54147. #else
  54148. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  54149. #endif
  54150. wolfSSL_CTX_free(ctx);
  54151. #ifndef WOLFSSL_NO_TLS12
  54152. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  54153. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  54154. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  54155. wolfSSL_CTX_free(ctx);
  54156. #endif
  54157. #ifdef WOLFSSL_TLS13
  54158. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  54159. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  54160. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  54161. wolfSSL_CTX_free(ctx);
  54162. #endif
  54163. res = TEST_RES_CHECK(1);
  54164. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  54165. return res;
  54166. }
  54167. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54168. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54169. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54170. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  54171. static int test_wolfSSL_set_SSL_CTX(void)
  54172. {
  54173. int res = TEST_SKIPPED;
  54174. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  54175. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  54176. !defined(NO_RSA)
  54177. WOLFSSL_CTX *ctx1, *ctx2;
  54178. WOLFSSL *ssl;
  54179. const byte *session_id1 = (const byte *)"CTX1";
  54180. const byte *session_id2 = (const byte *)"CTX2";
  54181. AssertNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  54182. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  54183. WOLFSSL_FILETYPE_PEM));
  54184. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  54185. WOLFSSL_FILETYPE_PEM));
  54186. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  54187. WOLFSSL_SUCCESS);
  54188. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  54189. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  54190. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  54191. WOLFSSL_SUCCESS);
  54192. AssertNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  54193. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  54194. WOLFSSL_FILETYPE_PEM));
  54195. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  54196. WOLFSSL_FILETYPE_PEM));
  54197. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  54198. WOLFSSL_SUCCESS);
  54199. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  54200. WOLFSSL_SUCCESS);
  54201. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  54202. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  54203. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  54204. WOLFSSL_SUCCESS);
  54205. #ifdef HAVE_SESSION_TICKET
  54206. AssertIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  54207. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  54208. AssertIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  54209. #endif
  54210. AssertNotNull(ssl = wolfSSL_new(ctx2));
  54211. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  54212. #ifdef WOLFSSL_INT_H
  54213. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  54214. AssertTrue(ssl->buffers.certificate == ctx2->certificate);
  54215. AssertTrue(ssl->buffers.certChain == ctx2->certChain);
  54216. #endif
  54217. #ifdef HAVE_SESSION_TICKET
  54218. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  54219. #endif
  54220. /* Set the ctx1 that has TLSv1.3 as max proto version */
  54221. AssertNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  54222. /* MUST not change proto versions of ssl */
  54223. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  54224. #ifdef HAVE_SESSION_TICKET
  54225. /* MUST not change */
  54226. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  54227. #endif
  54228. /* MUST change */
  54229. #ifdef WOLFSSL_INT_H
  54230. AssertTrue(ssl->buffers.certificate == ctx1->certificate);
  54231. AssertTrue(ssl->buffers.certChain == ctx1->certChain);
  54232. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  54233. #endif
  54234. wolfSSL_free(ssl);
  54235. wolfSSL_CTX_free(ctx1);
  54236. wolfSSL_CTX_free(ctx2);
  54237. res = TEST_RES_CHECK(1);
  54238. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  54239. return res;
  54240. }
  54241. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54242. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54243. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54244. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  54245. static int test_wolfSSL_security_level(void)
  54246. {
  54247. int res = TEST_SKIPPED;
  54248. #if defined(OPENSSL_EXTRA)
  54249. SSL_CTX *ctx;
  54250. #ifdef WOLFSSL_TLS13
  54251. #ifdef NO_WOLFSSL_SERVER
  54252. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54253. #else
  54254. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54255. #endif
  54256. SSL_CTX_set_security_level(ctx, 1);
  54257. AssertTrue(1);
  54258. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  54259. SSL_CTX_free(ctx);
  54260. #else
  54261. (void)ctx;
  54262. #endif
  54263. res = TEST_RES_CHECK(1);
  54264. #endif
  54265. return res;
  54266. }
  54267. static int test_wolfSSL_SSL_in_init(void)
  54268. {
  54269. int res = TEST_SKIPPED;
  54270. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  54271. SSL_CTX* ctx;
  54272. SSL* ssl;
  54273. const char* testCertFile;
  54274. const char* testKeyFile;
  54275. #ifdef WOLFSSL_TLS13
  54276. #ifdef NO_WOLFSSL_SERVER
  54277. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54278. #else
  54279. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54280. #endif
  54281. #ifndef NO_RSA
  54282. testCertFile = svrCertFile;
  54283. testKeyFile = svrKeyFile;
  54284. #elif defined(HAVE_ECC)
  54285. testCertFile = eccCertFile;
  54286. testKeyFile = eccKeyFile;
  54287. #else
  54288. testCertFile = NULL;
  54289. testKeyFile = NULL;
  54290. #endif
  54291. if (testCertFile != NULL && testKeyFile != NULL) {
  54292. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54293. SSL_FILETYPE_PEM));
  54294. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54295. SSL_FILETYPE_PEM));
  54296. }
  54297. ssl = SSL_new(ctx);
  54298. AssertNotNull(ssl);
  54299. AssertIntEQ(SSL_in_init(ssl), 1);
  54300. SSL_CTX_free(ctx);
  54301. SSL_free(ssl);
  54302. #else
  54303. (void)ctx;
  54304. (void)ssl;
  54305. (void)testCertFile;
  54306. (void)testKeyFile;
  54307. #endif
  54308. res = TEST_RES_CHECK(1);
  54309. #endif
  54310. return res;
  54311. }
  54312. static int test_wolfSSL_CTX_set_timeout(void)
  54313. {
  54314. int res = TEST_SKIPPED;
  54315. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  54316. int timeout;
  54317. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  54318. (void)timeout;
  54319. AssertNotNull(ctx);
  54320. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  54321. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  54322. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  54323. */
  54324. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  54325. /* giving 0 as timeout value sets default timeout */
  54326. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  54327. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  54328. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  54329. #else
  54330. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  54331. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  54332. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  54333. #endif
  54334. wolfSSL_CTX_free(ctx);
  54335. res = TEST_RES_CHECK(1);
  54336. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  54337. return res;
  54338. }
  54339. static int test_wolfSSL_OpenSSL_version(void)
  54340. {
  54341. int res = TEST_SKIPPED;
  54342. #if defined(OPENSSL_EXTRA)
  54343. const char* ver;
  54344. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  54345. AssertNotNull(ver = OpenSSL_version(0));
  54346. #else
  54347. AssertNotNull(ver = OpenSSL_version());
  54348. #endif
  54349. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  54350. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  54351. res = TEST_RES_CHECK(1);
  54352. #endif
  54353. return res;
  54354. }
  54355. static int test_CONF_CTX_CMDLINE(void)
  54356. {
  54357. int res = TEST_SKIPPED;
  54358. #if defined(OPENSSL_ALL)
  54359. SSL_CTX* ctx = NULL;
  54360. SSL_CONF_CTX* cctx = NULL;
  54361. AssertNotNull(cctx = SSL_CONF_CTX_new());
  54362. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54363. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54364. AssertTrue(1);
  54365. /* set flags */
  54366. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  54367. WOLFSSL_CONF_FLAG_CMDLINE);
  54368. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54369. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54370. /* cmd invalid command */
  54371. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54372. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54373. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54374. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54375. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54376. /* cmd Certificate and Private Key*/
  54377. {
  54378. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54379. const char* ourCert = svrCertFile;
  54380. const char* ourKey = svrKeyFile;
  54381. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  54382. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  54383. WOLFSSL_SUCCESS);
  54384. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  54385. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  54386. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54387. #endif
  54388. }
  54389. /* cmd curves */
  54390. {
  54391. #if defined(HAVE_ECC)
  54392. const char* curve = "secp256r1";
  54393. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  54394. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  54395. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54396. #endif
  54397. }
  54398. /* cmd CipherString */
  54399. {
  54400. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54401. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  54402. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  54403. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54404. }
  54405. /* cmd DH parameter */
  54406. {
  54407. #if !defined(NO_DH) && !defined(NO_BIO)
  54408. const char* ourdhcert = "./certs/dh2048.pem";
  54409. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  54410. -3);
  54411. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  54412. WOLFSSL_SUCCESS);
  54413. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54414. #endif
  54415. }
  54416. SSL_CTX_free(ctx);
  54417. SSL_CONF_CTX_free(cctx);
  54418. res = TEST_RES_CHECK(1);
  54419. #endif /* OPENSSL_EXTRA */
  54420. return res;
  54421. }
  54422. static int test_CONF_CTX_FILE(void)
  54423. {
  54424. int res = TEST_SKIPPED;
  54425. #if defined(OPENSSL_ALL)
  54426. SSL_CTX* ctx = NULL;
  54427. SSL_CONF_CTX* cctx = NULL;
  54428. AssertNotNull(cctx = SSL_CONF_CTX_new());
  54429. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54430. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54431. AssertTrue(1);
  54432. /* set flags */
  54433. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  54434. WOLFSSL_CONF_FLAG_FILE);
  54435. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54436. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54437. /* sanity check */
  54438. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54439. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54440. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54441. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54442. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54443. /* cmd Certificate and Private Key*/
  54444. {
  54445. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54446. const char* ourCert = svrCertFile;
  54447. const char* ourKey = svrKeyFile;
  54448. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  54449. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  54450. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  54451. WOLFSSL_SUCCESS);
  54452. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  54453. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54454. #endif
  54455. }
  54456. /* cmd curves */
  54457. {
  54458. #if defined(HAVE_ECC)
  54459. const char* curve = "secp256r1";
  54460. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  54461. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  54462. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54463. #endif
  54464. }
  54465. /* cmd CipherString */
  54466. {
  54467. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54468. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  54469. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  54470. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54471. }
  54472. /* cmd DH parameter */
  54473. {
  54474. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  54475. const char* ourdhcert = "./certs/dh3072.pem";
  54476. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  54477. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  54478. WOLFSSL_SUCCESS);
  54479. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54480. #endif
  54481. }
  54482. SSL_CTX_free(ctx);
  54483. SSL_CONF_CTX_free(cctx);
  54484. res = TEST_RES_CHECK(1);
  54485. #endif /* OPENSSL_EXTRA */
  54486. return res;
  54487. }
  54488. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  54489. {
  54490. int res = TEST_SKIPPED;
  54491. #ifdef HAVE_EX_DATA
  54492. int idx1, idx2;
  54493. /* test for unsupported class index */
  54494. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  54495. 0,NULL, NULL, NULL, NULL ), -1);
  54496. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  54497. 0,NULL, NULL, NULL, NULL ), -1);
  54498. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  54499. 0,NULL, NULL, NULL, NULL ), -1);
  54500. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  54501. 0,NULL, NULL, NULL, NULL ), -1);
  54502. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  54503. 0,NULL, NULL, NULL, NULL ), -1);
  54504. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  54505. 0,NULL, NULL, NULL, NULL ), -1);
  54506. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  54507. 0,NULL, NULL, NULL, NULL ), -1);
  54508. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  54509. 0,NULL, NULL, NULL, NULL ), -1);
  54510. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  54511. 0,NULL, NULL, NULL, NULL ), -1);
  54512. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  54513. 0,NULL, NULL, NULL, NULL ), -1);
  54514. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  54515. 0,NULL, NULL, NULL, NULL ), -1);
  54516. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  54517. 0,NULL, NULL, NULL, NULL ), -1);
  54518. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  54519. /* test for supported class index */
  54520. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  54521. 0,NULL, NULL, NULL, NULL );
  54522. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  54523. 0,NULL, NULL, NULL, NULL );
  54524. AssertIntNE(idx1, -1);
  54525. AssertIntNE(idx2, -1);
  54526. AssertIntNE(idx1, idx2);
  54527. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  54528. 0,NULL, NULL, NULL, NULL );
  54529. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  54530. 0,NULL, NULL, NULL, NULL );
  54531. AssertIntNE(idx1, -1);
  54532. AssertIntNE(idx2, -1);
  54533. AssertIntNE(idx1, idx2);
  54534. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  54535. 0,NULL, NULL, NULL, NULL );
  54536. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  54537. 0,NULL, NULL, NULL, NULL );
  54538. AssertIntNE(idx1, -1);
  54539. AssertIntNE(idx2, -1);
  54540. AssertIntNE(idx1, idx2);
  54541. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  54542. 0,NULL, NULL, NULL, NULL );
  54543. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  54544. 0,NULL, NULL, NULL, NULL );
  54545. AssertIntNE(idx1, -1);
  54546. AssertIntNE(idx2, -1);
  54547. AssertIntNE(idx1, idx2);
  54548. res = TEST_RES_CHECK(1);
  54549. #endif /* HAVE_EX_DATA */
  54550. return res;
  54551. }
  54552. #if defined(HAVE_EX_DATA) && \
  54553. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54554. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54555. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54556. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  54557. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  54558. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  54559. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  54560. #define SESSION_NEW_IDX_PTR "Testing"
  54561. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  54562. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  54563. {
  54564. AssertNotNull(p);
  54565. AssertNull(ptr);
  54566. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  54567. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  54568. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  54569. }
  54570. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  54571. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  54572. void* arg)
  54573. {
  54574. AssertNotNull(out);
  54575. AssertNotNull(in);
  54576. AssertPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  54577. AssertPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  54578. AssertPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  54579. AssertIntEQ(argV, SESSION_NEW_IDX_LONG);
  54580. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  54581. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  54582. return SSL_SUCCESS;
  54583. }
  54584. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  54585. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  54586. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  54587. {
  54588. AssertNotNull(p);
  54589. AssertNull(ptr);
  54590. AssertPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  54591. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  54592. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  54593. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  54594. }
  54595. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  54596. {
  54597. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  54598. (void*)SESSION_NEW_IDX_PTR,
  54599. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  54600. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  54601. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  54602. SSL_SESSION* s = SSL_SESSION_new();
  54603. SSL_SESSION* d = NULL;
  54604. AssertNotNull(s);
  54605. AssertPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  54606. AssertNotNull(d = SSL_SESSION_dup(s));
  54607. AssertPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  54608. SSL_SESSION_free(s);
  54609. AssertIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  54610. SSL_SESSION_free(d);
  54611. AssertIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  54612. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  54613. crypto_ex_cb_ctx_session = NULL;
  54614. return TEST_RES_CHECK(1);
  54615. }
  54616. #else
  54617. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  54618. {
  54619. return TEST_SKIPPED;
  54620. }
  54621. #endif
  54622. static int test_wolfSSL_set_psk_use_session_callback(void)
  54623. {
  54624. int res = TEST_SKIPPED;
  54625. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  54626. SSL_CTX* ctx;
  54627. SSL* ssl;
  54628. const char* testCertFile;
  54629. const char* testKeyFile;
  54630. #ifdef WOLFSSL_TLS13
  54631. #ifdef NO_WOLFSSL_SERVER
  54632. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54633. #else
  54634. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54635. #endif
  54636. #ifndef NO_RSA
  54637. testCertFile = svrCertFile;
  54638. testKeyFile = svrKeyFile;
  54639. #elif defined(HAVE_ECC)
  54640. testCertFile = eccCertFile;
  54641. testKeyFile = eccKeyFile;
  54642. #else
  54643. testCertFile = NULL;
  54644. testKeyFile = NULL;
  54645. #endif
  54646. if (testCertFile != NULL && testKeyFile != NULL) {
  54647. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54648. SSL_FILETYPE_PEM));
  54649. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54650. SSL_FILETYPE_PEM));
  54651. }
  54652. ssl = SSL_new(ctx);
  54653. AssertNotNull(ssl);
  54654. SSL_set_psk_use_session_callback(ssl,
  54655. my_psk_use_session_cb);
  54656. AssertTrue(1);
  54657. SSL_CTX_free(ctx);
  54658. SSL_free(ssl);
  54659. #else
  54660. (void)ctx;
  54661. (void)ssl;
  54662. (void)testCertFile;
  54663. (void)testKeyFile;
  54664. #endif
  54665. res = TEST_RES_CHECK(1);
  54666. #endif
  54667. return res;
  54668. }
  54669. static int test_wolfSSL_ERR_strings(void)
  54670. {
  54671. int res = TEST_SKIPPED;
  54672. #if !defined(NO_ERROR_STRINGS)
  54673. const char* err1 = "unsupported cipher suite";
  54674. const char* err2 = "wolfSSL PEM routines";
  54675. const char* err = NULL;
  54676. (void)err;
  54677. (void)err1;
  54678. (void)err2;
  54679. #if defined(OPENSSL_EXTRA)
  54680. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  54681. AssertTrue(err != NULL);
  54682. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  54683. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  54684. AssertTrue(err != NULL);
  54685. AssertIntEQ((*err == '\0'), 1);
  54686. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  54687. AssertTrue(err != NULL);
  54688. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  54689. #else
  54690. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  54691. AssertTrue(err != NULL);
  54692. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  54693. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  54694. AssertTrue(err != NULL);
  54695. AssertIntEQ((*err == '\0'), 1);
  54696. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  54697. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  54698. AssertTrue(err != NULL);
  54699. AssertIntEQ((*err == '\0'), 1);
  54700. #endif
  54701. res = TEST_RES_CHECK(1);
  54702. #endif
  54703. return res;
  54704. }
  54705. static int test_wolfSSL_EVP_shake128(void)
  54706. {
  54707. int res = TEST_SKIPPED;
  54708. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  54709. defined(WOLFSSL_SHAKE128)
  54710. const EVP_MD* md = NULL;
  54711. md = EVP_shake128();
  54712. AssertTrue(md != NULL);
  54713. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  54714. res = TEST_RES_CHECK(1);
  54715. #endif
  54716. return res;
  54717. }
  54718. static int test_wolfSSL_EVP_shake256(void)
  54719. {
  54720. int res = TEST_SKIPPED;
  54721. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  54722. defined(WOLFSSL_SHAKE256)
  54723. const EVP_MD* md = NULL;
  54724. md = EVP_shake256();
  54725. AssertTrue(md != NULL);
  54726. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  54727. res = TEST_RES_CHECK(1);
  54728. #endif
  54729. return res;
  54730. }
  54731. static int test_EVP_blake2(void)
  54732. {
  54733. int res = TEST_SKIPPED;
  54734. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  54735. const EVP_MD* md = NULL;
  54736. (void)md;
  54737. #if defined(HAVE_BLAKE2)
  54738. md = EVP_blake2b512();
  54739. AssertTrue(md != NULL);
  54740. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  54741. #endif
  54742. #if defined(HAVE_BLAKE2S)
  54743. md = EVP_blake2s256();
  54744. AssertTrue(md != NULL);
  54745. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  54746. #endif
  54747. res = TEST_RES_CHECK(1);
  54748. #endif
  54749. return res;
  54750. }
  54751. #if defined(OPENSSL_EXTRA)
  54752. static void list_md_fn(const EVP_MD* m, const char* from,
  54753. const char* to, void* arg)
  54754. {
  54755. const char* mn;
  54756. BIO *bio;
  54757. (void) from;
  54758. (void) to;
  54759. (void) arg;
  54760. (void) mn;
  54761. (void) bio;
  54762. if (!m) {
  54763. /* alias */
  54764. AssertNull(m);
  54765. AssertNotNull(to);
  54766. }
  54767. else {
  54768. AssertNotNull(m);
  54769. AssertNull(to);
  54770. }
  54771. AssertNotNull(from);
  54772. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  54773. mn = EVP_get_digestbyname(from);
  54774. /* print to stderr */
  54775. AssertNotNull(arg);
  54776. bio = BIO_new(BIO_s_file());
  54777. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  54778. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  54779. BIO_free(bio);
  54780. #endif
  54781. }
  54782. #endif
  54783. static int test_EVP_MD_do_all(void)
  54784. {
  54785. int res = TEST_SKIPPED;
  54786. #if defined(OPENSSL_EXTRA)
  54787. EVP_MD_do_all(NULL, stderr);
  54788. /* to confirm previous call gives no harm */
  54789. AssertTrue(1);
  54790. EVP_MD_do_all(list_md_fn, stderr);
  54791. /* to confirm previous call gives no harm */
  54792. AssertTrue(1);
  54793. res = TEST_RES_CHECK(1);
  54794. #endif
  54795. return res;
  54796. }
  54797. #if defined(OPENSSL_EXTRA)
  54798. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  54799. {
  54800. (void)arg;
  54801. (void)nm;
  54802. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  54803. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  54804. /* print to stderr */
  54805. AssertNotNull(arg);
  54806. bio = BIO_new(BIO_s_file());
  54807. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  54808. BIO_printf(bio, "%s\n", mn);
  54809. BIO_free(bio);
  54810. #endif
  54811. }
  54812. #endif
  54813. static int test_OBJ_NAME_do_all(void)
  54814. {
  54815. int res = TEST_SKIPPED;
  54816. #if defined(OPENSSL_EXTRA)
  54817. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  54818. /* to confirm previous call gives no harm */
  54819. AssertTrue(1);
  54820. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  54821. /* to confirm previous call gives no harm */
  54822. AssertTrue(1);
  54823. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  54824. AssertTrue(1);
  54825. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  54826. AssertTrue(1);
  54827. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  54828. AssertTrue(1);
  54829. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  54830. AssertTrue(1);
  54831. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  54832. AssertTrue(1);
  54833. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  54834. AssertTrue(1);
  54835. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  54836. AssertTrue(1);
  54837. res = TEST_RES_CHECK(1);
  54838. #endif
  54839. return res;
  54840. }
  54841. static int test_SSL_CIPHER_get_xxx(void)
  54842. {
  54843. int res = TEST_SKIPPED;
  54844. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  54845. !defined(NO_FILESYSTEM)
  54846. const SSL_CIPHER* cipher = NULL;
  54847. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  54848. int i, numCiphers = 0;
  54849. SSL_CTX* ctx = NULL;
  54850. SSL* ssl = NULL;
  54851. const char* testCertFile;
  54852. const char* testKeyFile;
  54853. char buf[256] = {0};
  54854. const char* cipher_id = NULL;
  54855. int expect_nid1 = NID_undef;
  54856. int expect_nid2 = NID_undef;
  54857. int expect_nid3 = NID_undef;
  54858. int expect_nid4 = NID_undef;
  54859. int expect_nid5 = 0;
  54860. const char* cipher_id2 = NULL;
  54861. int expect_nid21 = NID_undef;
  54862. int expect_nid22 = NID_undef;
  54863. int expect_nid23 = NID_undef;
  54864. int expect_nid24 = NID_undef;
  54865. int expect_nid25 = 0;
  54866. (void)cipher;
  54867. (void)supportedCiphers;
  54868. (void)i;
  54869. (void)numCiphers;
  54870. (void)ctx;
  54871. (void)ssl;
  54872. (void)testCertFile;
  54873. (void)testKeyFile;
  54874. #if defined(WOLFSSL_TLS13)
  54875. cipher_id = "TLS13-AES128-GCM-SHA256";
  54876. expect_nid1 = NID_auth_rsa;
  54877. expect_nid2 = NID_aes_128_gcm;
  54878. expect_nid3 = NID_sha256;
  54879. expect_nid4 = NID_kx_any;
  54880. expect_nid5 = 1;
  54881. #if !defined(WOLFSSL_NO_TLS12)
  54882. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  54883. expect_nid21 = NID_auth_rsa;
  54884. expect_nid22 = NID_aes_256_gcm;
  54885. expect_nid23 = NID_sha384;
  54886. expect_nid24 = NID_kx_ecdhe;
  54887. expect_nid25 = 1;
  54888. #endif
  54889. #endif
  54890. #ifdef NO_WOLFSSL_SERVER
  54891. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54892. #else
  54893. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54894. #endif
  54895. if (cipher_id) {
  54896. #ifndef NO_RSA
  54897. testCertFile = svrCertFile;
  54898. testKeyFile = svrKeyFile;
  54899. #elif defined(HAVE_ECC)
  54900. testCertFile = eccCertFile;
  54901. testKeyFile = eccKeyFile;
  54902. #else
  54903. testCertFile = NULL;
  54904. testKeyFile = NULL;
  54905. #endif
  54906. if (testCertFile != NULL && testKeyFile != NULL) {
  54907. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54908. SSL_FILETYPE_PEM));
  54909. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54910. SSL_FILETYPE_PEM));
  54911. }
  54912. ssl = SSL_new(ctx);
  54913. AssertNotNull(ssl);
  54914. AssertIntEQ(SSL_in_init(ssl), 1);
  54915. supportedCiphers = SSL_get_ciphers(ssl);
  54916. numCiphers = sk_num(supportedCiphers);
  54917. for (i = 0; i < numCiphers; ++i) {
  54918. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  54919. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  54920. }
  54921. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  54922. break;
  54923. }
  54924. }
  54925. /* test case for */
  54926. if (i != numCiphers) {
  54927. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  54928. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  54929. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  54930. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  54931. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  54932. }
  54933. if (cipher_id2) {
  54934. for (i = 0; i < numCiphers; ++i) {
  54935. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  54936. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  54937. }
  54938. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  54939. break;
  54940. }
  54941. }
  54942. /* test case for */
  54943. if (i != numCiphers) {
  54944. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  54945. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  54946. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  54947. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  54948. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  54949. }
  54950. }
  54951. }
  54952. if (ctx)
  54953. SSL_CTX_free(ctx);
  54954. if (ssl)
  54955. SSL_free(ssl);
  54956. res = TEST_RES_CHECK(1);
  54957. #endif
  54958. return res;
  54959. }
  54960. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  54961. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  54962. int* keyFormat)
  54963. {
  54964. int ret;
  54965. byte* key_buf = NULL;
  54966. size_t key_sz = 0;
  54967. EncryptedInfo encInfo;
  54968. XMEMSET(&encInfo, 0, sizeof(encInfo));
  54969. ret = load_file(privKeyFile, &key_buf, &key_sz);
  54970. if (ret == 0) {
  54971. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  54972. NULL, &encInfo, keyFormat);
  54973. }
  54974. if (key_buf != NULL) {
  54975. free(key_buf); key_buf = NULL;
  54976. }
  54977. (void)encInfo; /* not used in this test */
  54978. #ifdef DEBUG_WOLFSSL
  54979. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  54980. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  54981. (*pDer)->length);
  54982. #endif
  54983. return ret;
  54984. }
  54985. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  54986. {
  54987. int ret = CRYPTOCB_UNAVAILABLE;
  54988. const char* privKeyFile = (const char*)ctx;
  54989. DerBuffer* pDer = NULL;
  54990. int keyFormat = 0;
  54991. if (info->algo_type == WC_ALGO_TYPE_PK) {
  54992. #ifdef DEBUG_WOLFSSL
  54993. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  54994. #endif
  54995. #ifndef NO_RSA
  54996. if (info->pk.type == WC_PK_TYPE_RSA) {
  54997. switch (info->pk.rsa.type) {
  54998. case RSA_PUBLIC_ENCRYPT:
  54999. case RSA_PUBLIC_DECRYPT:
  55000. /* perform software based RSA public op */
  55001. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  55002. break;
  55003. case RSA_PRIVATE_ENCRYPT:
  55004. case RSA_PRIVATE_DECRYPT:
  55005. {
  55006. RsaKey key;
  55007. /* perform software based RSA private op */
  55008. #ifdef DEBUG_WOLFSSL
  55009. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  55010. #endif
  55011. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  55012. &keyFormat);
  55013. if (ret != 0) {
  55014. return ret;
  55015. }
  55016. ret = wc_InitRsaKey(&key, HEAP_HINT);
  55017. if (ret == 0) {
  55018. word32 keyIdx = 0;
  55019. /* load RSA private key and perform private transform */
  55020. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  55021. &key, pDer->length);
  55022. if (ret == 0) {
  55023. ret = wc_RsaFunction(
  55024. info->pk.rsa.in, info->pk.rsa.inLen,
  55025. info->pk.rsa.out, info->pk.rsa.outLen,
  55026. info->pk.rsa.type, &key, info->pk.rsa.rng);
  55027. }
  55028. else {
  55029. /* if decode fails, then fall-back to software based crypto */
  55030. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  55031. "key decode failed %d, falling back to "
  55032. "software\n", ret);
  55033. ret = CRYPTOCB_UNAVAILABLE;
  55034. }
  55035. wc_FreeRsaKey(&key);
  55036. }
  55037. wc_FreeDer(&pDer); pDer = NULL;
  55038. break;
  55039. }
  55040. }
  55041. #ifdef DEBUG_WOLFSSL
  55042. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  55043. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  55044. #endif
  55045. }
  55046. #endif /* !NO_RSA */
  55047. #ifdef HAVE_ECC
  55048. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  55049. /* mark this key as ephemeral */
  55050. if (info->pk.eckg.key != NULL) {
  55051. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  55052. sizeof(info->pk.eckg.key->label));
  55053. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  55054. }
  55055. }
  55056. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  55057. ecc_key key;
  55058. /* perform software based ECC sign */
  55059. #ifdef DEBUG_WOLFSSL
  55060. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  55061. #endif
  55062. if (info->pk.eccsign.key != NULL &&
  55063. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  55064. /* this is an empheral key */
  55065. #ifdef DEBUG_WOLFSSL
  55066. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  55067. "ephemeral key\n");
  55068. #endif
  55069. return CRYPTOCB_UNAVAILABLE;
  55070. }
  55071. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55072. if (ret != 0) {
  55073. return ret;
  55074. }
  55075. ret = wc_ecc_init(&key);
  55076. if (ret == 0) {
  55077. word32 keyIdx = 0;
  55078. /* load ECC private key and perform private transform */
  55079. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  55080. &key, pDer->length);
  55081. if (ret == 0) {
  55082. ret = wc_ecc_sign_hash(
  55083. info->pk.eccsign.in, info->pk.eccsign.inlen,
  55084. info->pk.eccsign.out, info->pk.eccsign.outlen,
  55085. info->pk.eccsign.rng, &key);
  55086. }
  55087. else {
  55088. /* if decode fails, then fall-back to software based crypto */
  55089. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  55090. "decode failed %d, falling back to software\n", ret);
  55091. ret = CRYPTOCB_UNAVAILABLE;
  55092. }
  55093. wc_ecc_free(&key);
  55094. }
  55095. wc_FreeDer(&pDer); pDer = NULL;
  55096. #ifdef DEBUG_WOLFSSL
  55097. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  55098. ret, *info->pk.eccsign.outlen);
  55099. #endif
  55100. }
  55101. #endif /* HAVE_ECC */
  55102. #ifdef HAVE_ED25519
  55103. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  55104. ed25519_key key;
  55105. /* perform software based ED25519 sign */
  55106. #ifdef DEBUG_WOLFSSL
  55107. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  55108. #endif
  55109. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55110. if (ret != 0) {
  55111. return ret;
  55112. }
  55113. ret = wc_ed25519_init(&key);
  55114. if (ret == 0) {
  55115. word32 keyIdx = 0;
  55116. /* load ED25519 private key and perform private transform */
  55117. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  55118. &key, pDer->length);
  55119. if (ret == 0) {
  55120. /* calculate public key */
  55121. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  55122. if (ret == 0) {
  55123. key.pubKeySet = 1;
  55124. ret = wc_ed25519_sign_msg_ex(
  55125. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  55126. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  55127. &key, info->pk.ed25519sign.type,
  55128. info->pk.ed25519sign.context,
  55129. info->pk.ed25519sign.contextLen);
  55130. }
  55131. }
  55132. else {
  55133. /* if decode fails, then fall-back to software based crypto */
  55134. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  55135. "decode failed %d, falling back to software\n", ret);
  55136. ret = CRYPTOCB_UNAVAILABLE;
  55137. }
  55138. wc_ed25519_free(&key);
  55139. }
  55140. wc_FreeDer(&pDer); pDer = NULL;
  55141. #ifdef DEBUG_WOLFSSL
  55142. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  55143. ret, *info->pk.ed25519sign.outLen);
  55144. #endif
  55145. }
  55146. #endif /* HAVE_ED25519 */
  55147. }
  55148. (void)thisDevId;
  55149. (void)keyFormat;
  55150. return ret;
  55151. }
  55152. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  55153. static void test_wc_CryptoCb_TLS(int tlsVer,
  55154. const char* cliCaPemFile, const char* cliCertPemFile,
  55155. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  55156. const char* svrCaPemFile, const char* svrCertPemFile,
  55157. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  55158. {
  55159. callback_functions client_cbf;
  55160. callback_functions server_cbf;
  55161. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55162. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55163. if (tlsVer == WOLFSSL_TLSV1_3) {
  55164. #ifdef WOLFSSL_TLS13
  55165. server_cbf.method = wolfTLSv1_3_server_method;
  55166. client_cbf.method = wolfTLSv1_3_client_method;
  55167. #endif
  55168. }
  55169. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55170. #ifndef WOLFSSL_NO_TLS12
  55171. server_cbf.method = wolfTLSv1_2_server_method;
  55172. client_cbf.method = wolfTLSv1_2_client_method;
  55173. #endif
  55174. }
  55175. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55176. #ifndef NO_OLD_TLS
  55177. server_cbf.method = wolfTLSv1_1_server_method;
  55178. client_cbf.method = wolfTLSv1_1_client_method;
  55179. #endif
  55180. }
  55181. else if (tlsVer == WOLFSSL_TLSV1) {
  55182. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55183. server_cbf.method = wolfTLSv1_server_method;
  55184. client_cbf.method = wolfTLSv1_client_method;
  55185. #endif
  55186. }
  55187. else if (tlsVer == WOLFSSL_SSLV3) {
  55188. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55189. defined(WOLFSSL_STATIC_RSA)
  55190. server_cbf.method = wolfSSLv3_server_method;
  55191. client_cbf.method = wolfSSLv3_client_method;
  55192. #endif
  55193. }
  55194. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55195. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55196. server_cbf.method = wolfDTLSv1_2_server_method;
  55197. client_cbf.method = wolfDTLSv1_2_client_method;
  55198. #endif
  55199. }
  55200. else if (tlsVer == WOLFSSL_DTLSV1) {
  55201. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55202. server_cbf.method = wolfDTLSv1_server_method;
  55203. client_cbf.method = wolfDTLSv1_client_method;
  55204. #endif
  55205. }
  55206. if (server_cbf.method == NULL) {
  55207. /* not enabled */
  55208. return;
  55209. }
  55210. /* Setup the keys for the TLS test */
  55211. client_cbf.certPemFile = cliCertPemFile;
  55212. client_cbf.keyPemFile = cliPubKeyPemFile;
  55213. client_cbf.caPemFile = cliCaPemFile;
  55214. server_cbf.certPemFile = svrCertPemFile;
  55215. server_cbf.keyPemFile = svrPubKeyPemFile;
  55216. server_cbf.caPemFile = svrCaPemFile;
  55217. /* Setup a crypto callback with pointer to private key file for testing */
  55218. client_cbf.devId = 1;
  55219. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  55220. (void*)cliPrivKeyPemFile);
  55221. server_cbf.devId = 2;
  55222. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  55223. (void*)svrPrivKeyPemFile);
  55224. /* Perform TLS server and client test */
  55225. /* First test is at WOLFSSL_CTX level */
  55226. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55227. /* Check for success */
  55228. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55229. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55230. /* Second test is a WOLFSSL object level */
  55231. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55232. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55233. /* Check for success */
  55234. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55235. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55236. /* Un register the devId's */
  55237. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  55238. client_cbf.devId = INVALID_DEVID;
  55239. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  55240. server_cbf.devId = INVALID_DEVID;
  55241. }
  55242. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  55243. static int test_wc_CryptoCb(void)
  55244. {
  55245. int res = TEST_SKIPPED;
  55246. #ifdef WOLF_CRYPTO_CB
  55247. /* TODO: Add crypto callback API tests */
  55248. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55249. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55250. int tlsVer;
  55251. #endif
  55252. #ifndef NO_RSA
  55253. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55254. test_wc_CryptoCb_TLS(tlsVer,
  55255. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  55256. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  55257. }
  55258. #endif
  55259. #ifdef HAVE_ECC
  55260. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55261. test_wc_CryptoCb_TLS(tlsVer,
  55262. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  55263. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  55264. }
  55265. #endif
  55266. #ifdef HAVE_ED25519
  55267. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  55268. if (tlsVer == WOLFSSL_DTLSV1) continue;
  55269. test_wc_CryptoCb_TLS(tlsVer,
  55270. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  55271. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  55272. }
  55273. #endif
  55274. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  55275. res = TEST_RES_CHECK(1);
  55276. #endif /* WOLF_CRYPTO_CB */
  55277. return res;
  55278. }
  55279. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  55280. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  55281. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  55282. const char* cliCaPemFile, const char* cliCertPemFile,
  55283. const char* cliPrivKeyPemFile,
  55284. const char* svrCaPemFile, const char* svrCertPemFile,
  55285. const char* svrPrivKeyPemFile,
  55286. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  55287. {
  55288. callback_functions client_cbf;
  55289. callback_functions server_cbf;
  55290. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55291. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55292. if (tlsVer == WOLFSSL_TLSV1_3) {
  55293. #ifdef WOLFSSL_TLS13
  55294. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  55295. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  55296. #endif
  55297. }
  55298. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55299. #ifndef WOLFSSL_NO_TLS12
  55300. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  55301. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  55302. #endif
  55303. }
  55304. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55305. #ifndef NO_OLD_TLS
  55306. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  55307. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  55308. #endif
  55309. }
  55310. else if (tlsVer == WOLFSSL_TLSV1) {
  55311. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55312. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  55313. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  55314. #endif
  55315. }
  55316. else if (tlsVer == WOLFSSL_SSLV3) {
  55317. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55318. defined(WOLFSSL_STATIC_RSA)
  55319. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  55320. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  55321. #endif
  55322. }
  55323. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55324. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55325. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  55326. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  55327. #endif
  55328. }
  55329. else if (tlsVer == WOLFSSL_DTLSV1) {
  55330. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55331. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  55332. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  55333. #endif
  55334. }
  55335. if (server_cbf.method_ex == NULL) {
  55336. /* not enabled */
  55337. return;
  55338. }
  55339. /* Setup the keys for the TLS test */
  55340. client_cbf.certPemFile = cliCertPemFile;
  55341. client_cbf.keyPemFile = cliPrivKeyPemFile;
  55342. client_cbf.caPemFile = cliCaPemFile;
  55343. server_cbf.certPemFile = svrCertPemFile;
  55344. server_cbf.keyPemFile = svrPrivKeyPemFile;
  55345. server_cbf.caPemFile = svrCaPemFile;
  55346. client_cbf.mem = cliMem;
  55347. client_cbf.memSz = cliMemSz;
  55348. server_cbf.mem = svrMem;
  55349. server_cbf.memSz = svrMemSz;
  55350. client_cbf.devId = INVALID_DEVID;
  55351. server_cbf.devId = INVALID_DEVID;
  55352. /* Perform TLS server and client test */
  55353. /* First test is at WOLFSSL_CTX level */
  55354. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55355. /* Check for success */
  55356. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55357. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55358. /* Second test is a WOLFSSL object level */
  55359. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55360. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55361. /* Check for success */
  55362. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55363. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55364. }
  55365. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  55366. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55367. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  55368. defined(SESSION_CERTS)
  55369. #ifdef OPENSSL_EXTRA
  55370. #define TEST_TLS_STATIC_MEMSZ (400000)
  55371. #else
  55372. #define TEST_TLS_STATIC_MEMSZ (320000)
  55373. #endif
  55374. #else
  55375. #define TEST_TLS_STATIC_MEMSZ (80000)
  55376. #endif
  55377. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  55378. {
  55379. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  55380. WOLFSSL_MEM_STATS mem_stats;
  55381. WOLFSSL_MEM_CONN_STATS ssl_stats;
  55382. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  55383. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55384. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55385. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55386. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55387. #endif
  55388. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  55389. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  55390. /* this should fail because kMaxCtxClients == 2 */
  55391. AssertNull((ssl3 = wolfSSL_new(ctx)));
  55392. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  55393. #ifdef DEBUG_WOLFSSL
  55394. wolfSSL_PrintStatsConn(&ssl_stats);
  55395. #endif
  55396. (void)ssl_stats;
  55397. }
  55398. /* display collected statistics */
  55399. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  55400. #ifdef DEBUG_WOLFSSL
  55401. wolfSSL_PrintStats(&mem_stats);
  55402. #endif
  55403. (void)mem_stats;
  55404. }
  55405. wolfSSL_free(ssl1);
  55406. wolfSSL_free(ssl2);
  55407. return TEST_RES_CHECK(1);
  55408. }
  55409. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55410. static int test_wolfSSL_CTX_StaticMemory(void)
  55411. {
  55412. int res = TEST_SKIPPED;
  55413. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55414. wolfSSL_method_func method_func;
  55415. WOLFSSL_CTX* ctx;
  55416. const int kMaxCtxClients = 2;
  55417. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55418. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55419. int tlsVer;
  55420. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  55421. #endif
  55422. #endif
  55423. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  55424. #ifndef NO_WOLFSSL_SERVER
  55425. #ifndef WOLFSSL_NO_TLS12
  55426. method_func = wolfTLSv1_2_server_method_ex;
  55427. #else
  55428. method_func = wolfTLSv1_3_server_method_ex;
  55429. #endif
  55430. #else
  55431. #ifndef WOLFSSL_NO_TLS12
  55432. method_func = wolfTLSv1_2_client_method_ex;
  55433. #else
  55434. method_func = wolfTLSv1_3_client_method_ex;
  55435. #endif
  55436. #endif
  55437. /* Test creating CTX directly from static memory pool */
  55438. ctx = NULL;
  55439. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  55440. &ctx, method_func, svrMem, sizeof(svrMem),
  55441. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55442. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  55443. wolfSSL_CTX_free(ctx);
  55444. ctx = NULL;
  55445. /* Test for heap allocated CTX, then assigning static pool to it */
  55446. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  55447. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  55448. NULL, svrMem, sizeof(svrMem),
  55449. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55450. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  55451. wolfSSL_CTX_free(ctx);
  55452. /* TLS Level Tests using static memory */
  55453. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55454. #ifndef NO_RSA
  55455. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55456. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55457. svrCertFile, cliCertFile, cliKeyFile,
  55458. cliCertFile, svrCertFile, svrKeyFile,
  55459. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  55460. }
  55461. #endif
  55462. #ifdef HAVE_ECC
  55463. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55464. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55465. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  55466. cliEccCertFile, eccCertFile, eccKeyFile,
  55467. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  55468. }
  55469. #endif
  55470. #ifdef HAVE_ED25519
  55471. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  55472. if (tlsVer == WOLFSSL_DTLSV1) continue;
  55473. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55474. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  55475. cliEdCertFile, edCertFile, edKeyFile,
  55476. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  55477. }
  55478. #endif
  55479. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  55480. res = TEST_RES_CHECK(1);
  55481. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55482. return res;
  55483. }
  55484. static int test_openssl_FIPS_drbg(void)
  55485. {
  55486. int res = TEST_SKIPPED;
  55487. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  55488. DRBG_CTX* dctx;
  55489. byte data1[32], data2[32], zeroData[32];
  55490. byte testSeed[16];
  55491. size_t dlen = sizeof(data1);
  55492. int i;
  55493. XMEMSET(data1, 0, dlen);
  55494. XMEMSET(data2, 0, dlen);
  55495. XMEMSET(zeroData, 0, sizeof(zeroData));
  55496. for (i=0; i<(int)sizeof(testSeed); i++) {
  55497. testSeed[i] = (byte)i;
  55498. }
  55499. AssertNotNull(dctx = FIPS_get_default_drbg());
  55500. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  55501. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  55502. WOLFSSL_SUCCESS);
  55503. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  55504. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  55505. WOLFSSL_SUCCESS);
  55506. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  55507. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  55508. WOLFSSL_SUCCESS);
  55509. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  55510. WOLFSSL_SUCCESS);
  55511. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  55512. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  55513. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  55514. #ifndef HAVE_GLOBAL_RNG
  55515. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  55516. wolfSSL_FIPS_drbg_free(dctx);
  55517. #endif
  55518. res = TEST_RES_CHECK(1);
  55519. #endif
  55520. return res;
  55521. }
  55522. static int test_wolfSSL_FIPS_mode(void)
  55523. {
  55524. int res = TEST_SKIPPED;
  55525. #if defined(OPENSSL_ALL)
  55526. #ifdef HAVE_FIPS
  55527. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  55528. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  55529. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  55530. #else
  55531. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  55532. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  55533. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  55534. #endif
  55535. res = TEST_RES_CHECK(1);
  55536. #endif
  55537. return res;
  55538. }
  55539. #ifdef WOLFSSL_DTLS
  55540. /* Prints out the current window */
  55541. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  55542. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  55543. int i;
  55544. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  55545. word32 b = w[i];
  55546. int j;
  55547. /* Prints out a 32 bit binary number in big endian order */
  55548. for (j = 0; j < 32; j++, b <<= 1) {
  55549. if (b & (((word32)1) << 31))
  55550. fprintf(stderr, "1");
  55551. else
  55552. fprintf(stderr, "0");
  55553. }
  55554. fprintf(stderr, " ");
  55555. }
  55556. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  55557. #else
  55558. (void)h;
  55559. (void)l;
  55560. (void)w;
  55561. #endif
  55562. }
  55563. /* a - cur_hi
  55564. * b - cur_lo
  55565. * c - next_hi
  55566. * d - next_lo
  55567. * e - window
  55568. * f - expected next_hi
  55569. * g - expected next_lo
  55570. * h - expected window[1]
  55571. * i - expected window[0]
  55572. */
  55573. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  55574. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  55575. DUW_TEST_print_window_binary((a), (b), (e)); \
  55576. AssertIntEQ((c), (f)); \
  55577. AssertIntEQ((d), (g)); \
  55578. AssertIntEQ((e)[1], (h)); \
  55579. AssertIntEQ((e)[0], (i)); \
  55580. } while (0)
  55581. static int test_wolfSSL_DtlsUpdateWindow(void)
  55582. {
  55583. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  55584. word32 next_lo = 0;
  55585. word16 next_hi = 0;
  55586. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  55587. fprintf(stderr, "\n");
  55588. #endif
  55589. XMEMSET(window, 0, sizeof window);
  55590. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  55591. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  55592. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  55593. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  55594. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  55595. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  55596. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  55597. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  55598. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  55599. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  55600. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  55601. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  55602. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  55603. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  55604. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  55605. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  55606. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  55607. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  55608. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  55609. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  55610. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  55611. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  55612. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  55613. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  55614. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  55615. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  55616. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  55617. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55618. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55619. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55620. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  55621. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  55622. return TEST_RES_CHECK(1);
  55623. }
  55624. #endif /* WOLFSSL_DTLS */
  55625. #ifdef WOLFSSL_DTLS
  55626. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  55627. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  55628. {
  55629. DtlsMsg* cur;
  55630. static byte msg[100];
  55631. static byte msgInit = 0;
  55632. if (!msgInit) {
  55633. int i;
  55634. for (i = 0; i < 100; i++)
  55635. msg[i] = i + 1;
  55636. msgInit = 1;
  55637. }
  55638. /* Sanitize test parameters */
  55639. if (len > sizeof(msg))
  55640. return -1;
  55641. if (f_offset + f_len > sizeof(msg))
  55642. return -1;
  55643. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  55644. if (ssl->dtls_rx_msg_list == NULL)
  55645. return -100;
  55646. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  55647. return -200;
  55648. if (cur->fragBucketListCount != f_count)
  55649. return -300;
  55650. if (cur->ready != ready)
  55651. return -400;
  55652. if (cur->bytesReceived != bytesReceived)
  55653. return -500;
  55654. if (ready) {
  55655. if (cur->fragBucketList != NULL)
  55656. return -600;
  55657. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  55658. return -700;
  55659. }
  55660. else {
  55661. DtlsFragBucket* fb;
  55662. if (cur->fragBucketList == NULL)
  55663. return -800;
  55664. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  55665. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  55666. return -900;
  55667. }
  55668. }
  55669. return 0;
  55670. }
  55671. static void DFB_TEST_RESET(WOLFSSL* ssl)
  55672. {
  55673. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  55674. ssl->dtls_rx_msg_list = NULL;
  55675. ssl->dtls_rx_msg_list_sz = 0;
  55676. }
  55677. static int test_wolfSSL_DTLS_fragment_buckets(void)
  55678. {
  55679. WOLFSSL ssl[1];
  55680. XMEMSET(ssl, 0, sizeof(*ssl));
  55681. AssertIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  55682. AssertIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55683. AssertIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  55684. AssertIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  55685. AssertIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  55686. AssertIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  55687. /* Test all permutations of 3 regions */
  55688. /* 1 2 3 */
  55689. AssertIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55690. AssertIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  55691. AssertIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  55692. /* 1 3 2 */
  55693. AssertIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55694. AssertIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  55695. AssertIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  55696. /* 2 1 3 */
  55697. AssertIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  55698. AssertIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  55699. AssertIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  55700. /* 2 3 1 */
  55701. AssertIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  55702. AssertIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  55703. AssertIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  55704. /* 3 1 2 */
  55705. AssertIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  55706. AssertIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  55707. AssertIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  55708. /* 3 2 1 */
  55709. AssertIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  55710. AssertIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  55711. AssertIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  55712. /* Test overlapping regions */
  55713. AssertIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55714. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  55715. AssertIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  55716. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  55717. AssertIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  55718. /* Test overlapping multiple regions */
  55719. AssertIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55720. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  55721. AssertIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  55722. AssertIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  55723. AssertIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  55724. AssertIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  55725. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  55726. AssertIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  55727. AssertIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  55728. AssertIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  55729. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55730. AssertIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  55731. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  55732. AssertIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  55733. DFB_TEST_RESET(ssl);
  55734. return TEST_RES_CHECK(1);
  55735. }
  55736. #endif
  55737. #if !defined(NO_FILESYSTEM) && \
  55738. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55739. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  55740. static int test_wolfSSL_dtls_stateless2(void)
  55741. {
  55742. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  55743. struct test_memio_ctx test_ctx;
  55744. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  55745. int ret;
  55746. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55747. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55748. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  55749. if (ret != 0)
  55750. return -1;
  55751. ssl_c2 = wolfSSL_new(ctx_c);
  55752. if (ssl_c2 == NULL)
  55753. return -2;
  55754. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55755. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55756. /* send CH */
  55757. ret = wolfSSL_connect(ssl_c2);
  55758. if (ret == 0 || ssl_c2->error != WANT_READ)
  55759. return -3;
  55760. ret = wolfSSL_accept(ssl_s);
  55761. if (ret == 0 || ssl_s->error != WANT_READ)
  55762. return -4;
  55763. if (test_ctx.c_len == 0)
  55764. return -5;
  55765. /* consume HRR */
  55766. test_ctx.c_len = 0;
  55767. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55768. if (ret != 0)
  55769. return -6;
  55770. wolfSSL_free(ssl_c2);
  55771. wolfSSL_free(ssl_c);
  55772. wolfSSL_free(ssl_s);
  55773. wolfSSL_CTX_free(ctx_c);
  55774. wolfSSL_CTX_free(ctx_s);
  55775. return TEST_SUCCESS;
  55776. }
  55777. #ifdef HAVE_MAX_FRAGMENT
  55778. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  55779. {
  55780. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  55781. struct test_memio_ctx test_ctx;
  55782. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  55783. word16 max_fragment;
  55784. int ret;
  55785. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55786. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55787. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  55788. if (ret != 0)
  55789. return -1;
  55790. ssl_c2 = wolfSSL_new(ctx_c);
  55791. if (ssl_c2 == NULL)
  55792. return -2;
  55793. ret = wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8);
  55794. if (ret != WOLFSSL_SUCCESS)
  55795. return -3;
  55796. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55797. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55798. max_fragment = ssl_s->max_fragment;
  55799. /* send CH */
  55800. ret = wolfSSL_connect(ssl_c2);
  55801. if (ret == 0 || ssl_c2->error != WANT_READ)
  55802. return -4;
  55803. ret = wolfSSL_accept(ssl_s);
  55804. if (ret == 0 || ssl_s->error != WANT_READ)
  55805. return -5;
  55806. /* CH without cookie shouldn't change state */
  55807. if (ssl_s->max_fragment != max_fragment)
  55808. return -6;
  55809. if (test_ctx.c_len == 0)
  55810. return -7;
  55811. /* consume HRR from buffer */
  55812. test_ctx.c_len = 0;
  55813. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55814. if (ret != 0)
  55815. return -8;
  55816. wolfSSL_free(ssl_c2);
  55817. wolfSSL_free(ssl_c);
  55818. wolfSSL_free(ssl_s);
  55819. wolfSSL_CTX_free(ctx_c);
  55820. wolfSSL_CTX_free(ctx_s);
  55821. return TEST_SUCCESS;
  55822. }
  55823. #endif /* HAVE_MAX_FRAGMENT */
  55824. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  55825. #define ROUNDS_WITH_HVR 4
  55826. #define ROUNDS_WITHOUT_HVR 2
  55827. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  55828. static int buf_is_hvr(const byte *data, int len)
  55829. {
  55830. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  55831. return 0;
  55832. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  55833. }
  55834. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  55835. {
  55836. struct test_memio_ctx test_ctx;
  55837. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  55838. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  55839. WOLFSSL_SESSION *sess;
  55840. int ret, round_trips;
  55841. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55842. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55843. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  55844. if (ret != 0)
  55845. return -1;
  55846. #ifdef HAVE_SESSION_TICKET
  55847. if (useticket) {
  55848. ret = wolfSSL_UseSessionTicket(ssl_c);
  55849. if (ret != WOLFSSL_SUCCESS)
  55850. return -2;
  55851. }
  55852. #endif
  55853. round_trips = ROUNDS_WITH_HVR;
  55854. ret = test_memio_do_handshake(ssl_c, ssl_s, round_trips, &round_trips);
  55855. if (ret != 0)
  55856. return -3;
  55857. if (round_trips != ROUNDS_WITH_HVR)
  55858. return -4;
  55859. sess = wolfSSL_get1_session(ssl_c);
  55860. if (sess == NULL)
  55861. return -5;
  55862. wolfSSL_shutdown(ssl_c);
  55863. wolfSSL_shutdown(ssl_s);
  55864. wolfSSL_free(ssl_c);
  55865. wolfSSL_free(ssl_s);
  55866. test_ctx.c_len = test_ctx.s_len = 0;
  55867. /* make resumption invalid */
  55868. if (bad) {
  55869. if (useticket) {
  55870. #ifdef HAVE_SESSION_TICKET
  55871. sess->ticket[0] = !sess->ticket[0];
  55872. #endif /* HAVE_SESSION_TICKET */
  55873. }
  55874. else {
  55875. sess->sessionID[0] = !sess->sessionID[0];
  55876. }
  55877. }
  55878. ssl_c = wolfSSL_new(ctx_c);
  55879. ssl_s = wolfSSL_new(ctx_s);
  55880. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  55881. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  55882. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  55883. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  55884. ret = wolfSSL_set_session(ssl_c, sess);
  55885. if (ret != WOLFSSL_SUCCESS)
  55886. return -6;
  55887. ret = wolfSSL_connect(ssl_c);
  55888. if (ret == WOLFSSL_SUCCESS || ssl_c->error != WANT_READ)
  55889. return -7;
  55890. ret = wolfSSL_accept(ssl_s);
  55891. if (ret == WOLFSSL_SUCCESS || ssl_s->error != WANT_READ)
  55892. return -8;
  55893. if (bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  55894. return -9;
  55895. if (!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  55896. return -10;
  55897. if (!useticket) {
  55898. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips);
  55899. if (ret != 0)
  55900. return -11;
  55901. if (bad && round_trips != ROUNDS_WITH_HVR - 1)
  55902. return -12;
  55903. if (!bad && round_trips != ROUNDS_WITHOUT_HVR - 1)
  55904. return -13;
  55905. }
  55906. wolfSSL_SESSION_free(sess);
  55907. wolfSSL_free(ssl_c);
  55908. wolfSSL_free(ssl_s);
  55909. wolfSSL_CTX_free(ctx_c);
  55910. wolfSSL_CTX_free(ctx_s);
  55911. return 0;
  55912. }
  55913. static int test_wolfSSL_dtls_stateless_resume(void)
  55914. {
  55915. int ret;
  55916. #ifdef HAVE_SESSION_TICKET
  55917. ret = _test_wolfSSL_dtls_stateless_resume(1, 0);
  55918. if (ret != 0)
  55919. return TEST_RES_CHECK(ret);
  55920. ret = _test_wolfSSL_dtls_stateless_resume(1, 1);
  55921. if (ret != 0)
  55922. return TEST_RES_CHECK(ret - 100);
  55923. #endif /* HAVE_SESION_TICKET */
  55924. ret = _test_wolfSSL_dtls_stateless_resume(0, 0);
  55925. if (ret != 0)
  55926. return TEST_RES_CHECK(ret - 200);
  55927. ret = _test_wolfSSL_dtls_stateless_resume(0, 1);
  55928. if (ret != 0)
  55929. return TEST_RES_CHECK(ret - 300);
  55930. return TEST_RES_CHECK(TEST_SUCCESS);
  55931. }
  55932. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  55933. #if !defined(NO_OLD_TLS)
  55934. static int test_wolfSSL_dtls_stateless_downgrade(void)
  55935. {
  55936. WOLFSSL_CTX *ctx_c = NULL, *ctx_c2 = NULL, *ctx_s = NULL;
  55937. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  55938. struct test_memio_ctx test_ctx;
  55939. int ret;
  55940. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55941. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55942. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  55943. if (ret != 0)
  55944. return -1;
  55945. ret = wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1);
  55946. if (ret != WOLFSSL_SUCCESS)
  55947. return -2;
  55948. ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method());
  55949. if (ctx_c2 == NULL)
  55950. return -3;
  55951. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  55952. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  55953. ssl_c2 = wolfSSL_new(ctx_c2);
  55954. if (ssl_c2 == NULL)
  55955. return -4;
  55956. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55957. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55958. /* send CH */
  55959. ret = wolfSSL_connect(ssl_c2);
  55960. if (ret == 0 || ssl_c2->error != WANT_READ)
  55961. return -5;
  55962. ret = wolfSSL_accept(ssl_s);
  55963. if (ret == 0 || ssl_s->error != WANT_READ)
  55964. return -6;
  55965. if (test_ctx.c_len == 0)
  55966. return -7;
  55967. /* consume HRR */
  55968. test_ctx.c_len = 0;
  55969. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55970. if (ret != 0)
  55971. return -8;
  55972. wolfSSL_free(ssl_c2);
  55973. wolfSSL_free(ssl_c);
  55974. wolfSSL_free(ssl_s);
  55975. wolfSSL_CTX_free(ctx_c);
  55976. wolfSSL_CTX_free(ctx_c2);
  55977. wolfSSL_CTX_free(ctx_s);
  55978. return TEST_SUCCESS;
  55979. }
  55980. #endif /* !defined(NO_OLD_TLS) */
  55981. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55982. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  55983. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55984. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55985. !defined(NO_OLD_TLS)
  55986. static int test_WOLFSSL_dtls_version_alert(void)
  55987. {
  55988. struct test_memio_ctx test_ctx;
  55989. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  55990. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  55991. int ret;
  55992. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55993. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55994. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method);
  55995. if (ret != 0)
  55996. return -1;
  55997. /* client hello */
  55998. ret = wolfSSL_connect(ssl_c);
  55999. if (ret == 0 || ssl_c->error != WANT_READ )
  56000. return -2;
  56001. /* hrr */
  56002. ret = wolfSSL_accept(ssl_s);
  56003. if (ret == 0 || ssl_s->error != WANT_READ )
  56004. return -3;
  56005. /* client hello 1 */
  56006. ret = wolfSSL_connect(ssl_c);
  56007. if (ret == 0 || ssl_c->error != WANT_READ )
  56008. return -4;
  56009. /* server hello */
  56010. ret = wolfSSL_accept(ssl_s);
  56011. if (ret == 0 || ssl_s->error != WANT_READ )
  56012. return -5;
  56013. /* should fail */
  56014. ret = wolfSSL_connect(ssl_c);
  56015. if (ret == 0 || ssl_c->error != VERSION_ERROR)
  56016. return -6;
  56017. /* shuould fail */
  56018. ret = wolfSSL_accept(ssl_s);
  56019. if (ret == 0 ||
  56020. (ssl_s->error != VERSION_ERROR && ssl_s->error != FATAL_ERROR))
  56021. return -7;
  56022. wolfSSL_free(ssl_c);
  56023. wolfSSL_free(ssl_s);
  56024. wolfSSL_CTX_free(ctx_c);
  56025. wolfSSL_CTX_free(ctx_s);
  56026. return TEST_RES_CHECK(1);
  56027. }
  56028. #else
  56029. static int test_WOLFSSL_dtls_version_alert(void)
  56030. {
  56031. return TEST_SKIPPED;
  56032. }
  56033. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  56034. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  56035. * !defined(NO_OLD_TLS)
  56036. */
  56037. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  56038. && defined(WOLFSSL_TLS13) && \
  56039. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  56040. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  56041. {
  56042. struct test_memio_ctx *test_ctx;
  56043. byte buf[2048];
  56044. int idx, sz;
  56045. word32 tmp;
  56046. int ret;
  56047. idx = 5; /* space for record header */
  56048. buf[idx] = session_ticket; /* type */
  56049. idx++;
  56050. tmp = OPAQUE32_LEN +
  56051. OPAQUE32_LEN +
  56052. OPAQUE8_LEN + nonceLength +
  56053. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  56054. c32to24(tmp, buf + idx);
  56055. idx += OPAQUE24_LEN;
  56056. c32toa((word32)12345, buf+idx); /* lifetime */
  56057. idx += OPAQUE32_LEN;
  56058. c32toa((word32)12345, buf+idx); /* add */
  56059. idx += OPAQUE32_LEN;
  56060. buf[idx] = nonceLength; /* nonce length */
  56061. idx++;
  56062. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  56063. idx += nonceLength;
  56064. tmp = 1; /* ticket len */
  56065. c16toa((word16)tmp, buf+idx);
  56066. idx += 2;
  56067. buf[idx] = 0xFF; /* ticket */
  56068. idx++;
  56069. tmp = 0; /* ext len */
  56070. c16toa((word16)tmp, buf+idx);
  56071. idx += 2;
  56072. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  56073. handshake, 0, 0, 0);
  56074. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  56075. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  56076. return !(ret == sz);
  56077. }
  56078. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  56079. {
  56080. int i;
  56081. if (sess == NULL)
  56082. return -1;
  56083. if (sess->ticketNonce.len != len)
  56084. return -1;
  56085. for (i = 0; i < len; i++)
  56086. if (sess->ticketNonce.data[i] != len)
  56087. return -1;
  56088. return 0;
  56089. }
  56090. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56091. {
  56092. char *buf[1024];
  56093. int ret;
  56094. ret = send_new_session_ticket(ssl_s, len, len);
  56095. if (ret != 0)
  56096. return -1;
  56097. ret = wolfSSL_recv(ssl_c, buf, 1024, 0);
  56098. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  56099. return -1;
  56100. return test_ticket_nonce_check(ssl_c->session, len);
  56101. }
  56102. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56103. {
  56104. WOLFSSL_SESSION *sess, *cached;
  56105. WOLFSSL_CTX *ctx;
  56106. int ret;
  56107. ctx = ssl_c->ctx;
  56108. ret = test_ticket_nonce_malloc_do(ssl_s, ssl_c, len);
  56109. if (ret != 0)
  56110. return -1;
  56111. sess = wolfSSL_get1_session(ssl_c);
  56112. if (sess == NULL)
  56113. return -1;
  56114. ret = AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  56115. NULL, ssl_c->options.side, 1,NULL);
  56116. if (ret != 0)
  56117. return -1;
  56118. cached = wolfSSL_SESSION_new();
  56119. if (cached == NULL)
  56120. return -1;
  56121. ret = wolfSSL_GetSessionFromCache(ssl_c, cached);
  56122. if (ret != WOLFSSL_SUCCESS)
  56123. return -1;
  56124. ret = test_ticket_nonce_check(cached, len);
  56125. if (ret != 0)
  56126. return -1;
  56127. wolfSSL_SESSION_free(cached);
  56128. wolfSSL_SESSION_free(sess);
  56129. return 0;
  56130. }
  56131. static int test_ticket_nonce_malloc(void)
  56132. {
  56133. struct test_memio_ctx test_ctx;
  56134. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56135. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56136. byte small, medium, big;
  56137. int ret;
  56138. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56139. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56140. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method);
  56141. if (ret != 0)
  56142. return -1;
  56143. /* will send ticket manually */
  56144. wolfSSL_no_ticket_TLSv13(ssl_s);
  56145. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  56146. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  56147. while (!ssl_c->options.handShakeDone && !ssl_s->options.handShakeDone) {
  56148. ret = wolfSSL_connect(ssl_c);
  56149. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  56150. return -2;
  56151. ret = wolfSSL_accept(ssl_s);
  56152. if (ret != WOLFSSL_SUCCESS && ssl_s->error != WANT_READ)
  56153. return -3;
  56154. }
  56155. small = TLS13_TICKET_NONCE_STATIC_SZ;
  56156. medium = small + 20 <= 255 ? small + 20 : 255;
  56157. big = medium + 20 <= 255 ? small + 20 : 255;
  56158. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  56159. return -1;
  56160. if (ssl_c->session->ticketNonce.data !=
  56161. ssl_c->session->ticketNonce.dataStatic)
  56162. return -1;
  56163. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  56164. return -1;
  56165. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, big))
  56166. return -1;
  56167. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  56168. return -5;
  56169. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  56170. return -6;
  56171. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  56172. return -1;
  56173. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  56174. return -1;
  56175. if (test_ticket_nonce_cache(ssl_s, ssl_c, big))
  56176. return -1;
  56177. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  56178. return -1;
  56179. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  56180. return -1;
  56181. wolfSSL_free(ssl_c);
  56182. wolfSSL_free(ssl_s);
  56183. wolfSSL_CTX_free(ctx_c);
  56184. wolfSSL_CTX_free(ctx_s);
  56185. return 0;
  56186. }
  56187. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  56188. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  56189. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  56190. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56191. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  56192. defined(HAVE_ECC)
  56193. static int test_ticket_ret_create(void)
  56194. {
  56195. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56196. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56197. byte ticket[SESSION_TICKET_LEN];
  56198. struct test_memio_ctx test_ctx;
  56199. WOLFSSL_SESSION *sess = NULL;
  56200. word16 ticketLen;
  56201. int ret;
  56202. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56203. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56204. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  56205. if (ret != 0)
  56206. return TEST_FAIL;
  56207. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  56208. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  56209. wolfSSL_CTX_UseSessionTicket(ctx_c);
  56210. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  56211. if (ret != 0)
  56212. return TEST_FAIL;
  56213. sess = wolfSSL_get1_session(ssl_c);
  56214. if (sess->ticketLen > SESSION_TICKET_LEN)
  56215. return TEST_FAIL;
  56216. ticketLen = sess->ticketLen;
  56217. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  56218. wolfSSL_free(ssl_c);
  56219. wolfSSL_free(ssl_s);
  56220. ssl_s = wolfSSL_new(ctx_s);
  56221. if (ssl_s == NULL)
  56222. return TEST_FAIL;
  56223. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  56224. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  56225. ssl_c = wolfSSL_new(ctx_c);
  56226. if (ssl_c == NULL)
  56227. return TEST_FAIL;
  56228. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  56229. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  56230. wolfSSL_set_session(ssl_c, sess);
  56231. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  56232. if (ret != 0)
  56233. return TEST_FAIL;
  56234. if (ssl_c->session->ticketLen > SESSION_TICKET_LEN)
  56235. return TEST_FAIL;
  56236. if (ssl_c->session->ticketLen != ticketLen)
  56237. return TEST_FAIL;
  56238. if (XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) == 0)
  56239. return TEST_FAIL;
  56240. wolfSSL_SESSION_free(sess);
  56241. wolfSSL_free(ssl_c);
  56242. wolfSSL_free(ssl_s);
  56243. wolfSSL_CTX_free(ctx_c);
  56244. wolfSSL_CTX_free(ctx_s);
  56245. return TEST_SUCCESS;
  56246. }
  56247. #else
  56248. static int test_ticket_ret_create(void)
  56249. {
  56250. return TEST_SKIPPED;
  56251. }
  56252. #endif
  56253. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  56254. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  56255. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56256. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  56257. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  56258. {
  56259. int ret;
  56260. WOLFSSL_SESSION* session = NULL;
  56261. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  56262. if (!wolfSSL_is_server(ssl)) {
  56263. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  56264. AssertNotNull(session);
  56265. }
  56266. do {
  56267. ret = wolfSSL_shutdown(ssl);
  56268. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  56269. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  56270. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  56271. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  56272. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  56273. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  56274. #endif
  56275. if (!wolfSSL_is_server(ssl)) {
  56276. /* client */
  56277. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56278. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56279. wolfSSL_set_session(ssl, session);
  56280. wolfSSL_SESSION_free(session);
  56281. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  56282. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  56283. }
  56284. else {
  56285. /* server */
  56286. /* Different ciphersuite so that the ticket will be invalidated based on
  56287. * the ciphersuite */
  56288. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  56289. WOLFSSL_SUCCESS);
  56290. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  56291. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56292. }
  56293. }
  56294. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  56295. {
  56296. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  56297. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56298. WOLFSSL_SUCCESS);
  56299. }
  56300. static int test_ticket_and_psk_mixing(void)
  56301. {
  56302. /* Test mixing tickets and regular PSK */
  56303. callback_functions client_cbs, server_cbs;
  56304. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56305. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56306. client_cbs.method = wolfTLSv1_3_client_method;
  56307. server_cbs.method = wolfTLSv1_3_server_method;
  56308. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  56309. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  56310. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  56311. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56312. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56313. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56314. return TEST_RES_CHECK(1);
  56315. }
  56316. #else
  56317. static int test_ticket_and_psk_mixing(void)
  56318. {
  56319. return TEST_SKIPPED;
  56320. }
  56321. #endif
  56322. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  56323. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  56324. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56325. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  56326. static int test_prioritize_psk_cb_called = FALSE;
  56327. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  56328. const char* identity, unsigned char* key, unsigned int key_max_len,
  56329. const char** ciphersuite)
  56330. {
  56331. test_prioritize_psk_cb_called = TRUE;
  56332. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  56333. }
  56334. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  56335. {
  56336. int ret;
  56337. WOLFSSL_SESSION* session = NULL;
  56338. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  56339. if (!wolfSSL_is_server(ssl)) {
  56340. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  56341. AssertNotNull(session);
  56342. }
  56343. do {
  56344. ret = wolfSSL_shutdown(ssl);
  56345. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  56346. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  56347. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  56348. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  56349. * important. */
  56350. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56351. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56352. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  56353. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  56354. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  56355. #endif
  56356. if (!wolfSSL_is_server(ssl)) {
  56357. /* client */
  56358. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  56359. wolfSSL_set_session(ssl, session);
  56360. wolfSSL_SESSION_free(session);
  56361. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  56362. }
  56363. else {
  56364. /* server */
  56365. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  56366. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56367. #ifdef WOLFSSL_PRIORITIZE_PSK
  56368. /* The ticket should be first tried with all ciphersuites and chosen */
  56369. AssertFalse(test_prioritize_psk_cb_called);
  56370. #else
  56371. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  56372. * callback that sets this var. */
  56373. AssertTrue(test_prioritize_psk_cb_called);
  56374. #endif
  56375. }
  56376. }
  56377. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  56378. {
  56379. if (!wolfSSL_is_server(ssl))
  56380. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56381. WOLFSSL_SUCCESS);
  56382. else
  56383. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56384. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56385. }
  56386. static int test_prioritize_psk(void)
  56387. {
  56388. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  56389. * of the PSK's will be followed instead of the ciphersuite. */
  56390. callback_functions client_cbs, server_cbs;
  56391. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56392. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56393. client_cbs.method = wolfTLSv1_3_client_method;
  56394. server_cbs.method = wolfTLSv1_3_server_method;
  56395. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56396. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56397. client_cbs.on_result = test_prioritize_psk_on_result;
  56398. server_cbs.on_result = test_prioritize_psk_on_result;
  56399. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56400. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56401. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56402. return TEST_RES_CHECK(1);
  56403. }
  56404. #else
  56405. static int test_prioritize_psk(void)
  56406. {
  56407. return TEST_SKIPPED;
  56408. }
  56409. #endif
  56410. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  56411. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56412. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56413. !defined(WOLFSSL_NO_TLS12)
  56414. static void test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  56415. {
  56416. AssertTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  56417. /* Set TLS 1.3 specific suite */
  56418. AssertTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  56419. }
  56420. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56421. {
  56422. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  56423. * do a 1.2 connection. */
  56424. callback_functions client_cbs, server_cbs;
  56425. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56426. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56427. client_cbs.method = wolfTLSv1_2_client_method;
  56428. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  56429. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  56430. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56431. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56432. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56433. return TEST_RES_CHECK(1);
  56434. }
  56435. #else
  56436. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56437. {
  56438. return TEST_SKIPPED;
  56439. }
  56440. #endif
  56441. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  56442. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56443. static void test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  56444. {
  56445. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56446. }
  56447. static void test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  56448. {
  56449. WOLFSSL_ALERT_HISTORY h;
  56450. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  56451. AssertIntEQ(h.last_rx.level, alert_fatal);
  56452. AssertIntEQ(h.last_rx.code, certificate_revoked);
  56453. }
  56454. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56455. {
  56456. callback_functions client_cbs, server_cbs;
  56457. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56458. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56459. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  56460. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  56461. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  56462. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  56463. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  56464. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56465. AssertIntEQ(client_cbs.return_code, TEST_FAIL);
  56466. AssertIntEQ(server_cbs.return_code, TEST_FAIL);
  56467. return TEST_RES_CHECK(1);
  56468. }
  56469. #else
  56470. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56471. {
  56472. return TEST_SKIPPED;
  56473. }
  56474. #endif
  56475. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  56476. && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56477. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56478. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  56479. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  56480. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56481. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  56482. static int test_TLS_13_ticket_different_ciphers_run = 0;
  56483. static void test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  56484. {
  56485. switch (test_TLS_13_ticket_different_ciphers_run) {
  56486. case 0:
  56487. /* First run */
  56488. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56489. WOLFSSL_SUCCESS);
  56490. if (wolfSSL_is_server(ssl)) {
  56491. AssertNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  56492. wolfSSL_get_SSL_CTX(ssl));
  56493. AssertIntEQ(WOLFSSL_SUCCESS,
  56494. wolfSSL_CTX_up_ref(test_TLS_13_ticket_different_ciphers_ctx));
  56495. }
  56496. break;
  56497. case 1:
  56498. /* Second run */
  56499. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56500. "TLS13-AES128-GCM-SHA256"),
  56501. WOLFSSL_SUCCESS);
  56502. if (!wolfSSL_is_server(ssl)) {
  56503. AssertIntEQ(wolfSSL_set_session(ssl,
  56504. test_TLS_13_ticket_different_ciphers_session),
  56505. WOLFSSL_SUCCESS);
  56506. }
  56507. break;
  56508. default:
  56509. /* Bad state? */
  56510. Fail(("Should not enter here"), ("Should not enter here"));
  56511. }
  56512. }
  56513. static void test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  56514. {
  56515. switch (test_TLS_13_ticket_different_ciphers_run) {
  56516. case 0:
  56517. /* First run */
  56518. AssertNotNull(test_TLS_13_ticket_different_ciphers_session =
  56519. wolfSSL_get1_session(ssl));
  56520. break;
  56521. case 1:
  56522. /* Second run */
  56523. AssertTrue(wolfSSL_session_reused(ssl));
  56524. break;
  56525. default:
  56526. /* Bad state? */
  56527. Fail(("Should not enter here"), ("Should not enter here"));
  56528. }
  56529. }
  56530. static int test_TLS_13_ticket_different_ciphers(void)
  56531. {
  56532. /* Check that we handle the connection when the ticket doesn't match
  56533. * the first ciphersuite. */
  56534. callback_functions client_cbs, server_cbs;
  56535. struct test_params {
  56536. method_provider client_meth;
  56537. method_provider server_meth;
  56538. int doUdp;
  56539. } params[] = {
  56540. #ifdef WOLFSSL_DTLS13
  56541. /* Test that the stateless code handles sessions correctly */
  56542. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  56543. #endif
  56544. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  56545. };
  56546. size_t i;
  56547. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56548. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56549. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56550. test_TLS_13_ticket_different_ciphers_run = 0;
  56551. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  56552. client_cbs.method = params[i].client_meth;
  56553. server_cbs.method = params[i].server_meth;
  56554. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  56555. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  56556. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  56557. server_cbs.ticNoInit = 1;
  56558. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56559. AssertTrue(client_cbs.return_code);
  56560. AssertTrue(server_cbs.return_code);
  56561. test_TLS_13_ticket_different_ciphers_run++;
  56562. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  56563. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56564. AssertTrue(client_cbs.return_code);
  56565. AssertTrue(server_cbs.return_code);
  56566. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  56567. test_TLS_13_ticket_different_ciphers_session = NULL;
  56568. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  56569. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56570. }
  56571. return TEST_RES_CHECK(1);
  56572. }
  56573. #else
  56574. static int test_TLS_13_ticket_different_ciphers(void)
  56575. {
  56576. return TEST_SKIPPED;
  56577. }
  56578. #endif
  56579. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  56580. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56581. #define TEST_WRONG_CS_CLIENT "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"
  56582. byte test_extra_alerts_wrong_cs_sh[] = {
  56583. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  56584. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  56585. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  56586. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  56587. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  56588. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  56589. 0xbb, 0x07, 0x54, 0x1c,
  56590. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  56591. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  56592. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  56593. };
  56594. static int test_extra_alerts_wrong_cs(void)
  56595. {
  56596. struct test_memio_ctx test_ctx;
  56597. WOLFSSL_CTX *ctx_c = NULL;
  56598. WOLFSSL_ALERT_HISTORY h;
  56599. WOLFSSL *ssl_c = NULL;
  56600. int ret, err;
  56601. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56602. ret = test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  56603. wolfTLSv1_2_client_method, NULL);
  56604. if (ret != 0)
  56605. return TEST_FAIL;
  56606. ret = wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT);
  56607. if (ret != WOLFSSL_SUCCESS) {
  56608. wolfSSL_free(ssl_c);
  56609. wolfSSL_CTX_free(ctx_c);
  56610. return TEST_SKIPPED;
  56611. }
  56612. /* CH */
  56613. ret = wolfSSL_connect(ssl_c);
  56614. err = wolfSSL_get_error(ssl_c, ret);
  56615. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  56616. return TEST_FAIL;
  56617. /* consume CH */
  56618. test_ctx.s_len = 0;
  56619. /* inject SH */
  56620. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  56621. sizeof(test_extra_alerts_wrong_cs_sh));
  56622. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  56623. ret = wolfSSL_connect(ssl_c);
  56624. err = wolfSSL_get_error(ssl_c, ret);
  56625. if (ret == WOLFSSL_SUCCESS || err == WOLFSSL_ERROR_WANT_READ)
  56626. return TEST_FAIL;
  56627. ret = wolfSSL_get_alert_history(ssl_c, &h);
  56628. if (ret != WOLFSSL_SUCCESS)
  56629. return TEST_FAIL;
  56630. if (h.last_tx.code != illegal_parameter)
  56631. return TEST_FAIL;
  56632. if (h.last_tx.level != alert_fatal)
  56633. return TEST_FAIL;
  56634. wolfSSL_free(ssl_c);
  56635. wolfSSL_CTX_free(ctx_c);
  56636. return TEST_SUCCESS;
  56637. }
  56638. #else
  56639. static int test_extra_alerts_wrong_cs(void)
  56640. {
  56641. return TEST_SKIPPED;
  56642. }
  56643. #endif
  56644. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  56645. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  56646. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  56647. {
  56648. tail_len -= msg_length;
  56649. XMEMMOVE(msg, msg + msg_length, tail_len);
  56650. *len = *len - msg_length;
  56651. }
  56652. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  56653. byte *found)
  56654. {
  56655. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  56656. const unsigned int _RECORD_HEADER_SZ = 5;
  56657. const int _change_cipher_hs = 55;
  56658. const int _change_cipher = 20;
  56659. const int _handshake = 22;
  56660. unsigned int tail_len;
  56661. byte *idx, *curr;
  56662. word8 currType;
  56663. word16 rLength;
  56664. word32 hLength;
  56665. idx = buf;
  56666. tail_len = *len;
  56667. *found = 0;
  56668. while (tail_len > _RECORD_HEADER_SZ) {
  56669. curr = idx;
  56670. currType = *idx;
  56671. ato16(idx + 3, &rLength);
  56672. idx += _RECORD_HEADER_SZ;
  56673. tail_len -= _RECORD_HEADER_SZ;
  56674. if (tail_len < rLength)
  56675. return -1;
  56676. if (type == _change_cipher_hs && currType == _change_cipher) {
  56677. if (rLength != 1)
  56678. return -1;
  56679. /* match */
  56680. test_remove_msg(curr, *len - (int)(curr - buf),
  56681. len, _RECORD_HEADER_SZ + 1);
  56682. *found = 1;
  56683. return 0;
  56684. }
  56685. if (currType != _handshake) {
  56686. idx += rLength;
  56687. tail_len -= rLength;
  56688. continue;
  56689. }
  56690. if (rLength < _HANDSHAKE_HEADER_SZ)
  56691. return -1;
  56692. currType = *idx;
  56693. ato24(idx+1, &hLength);
  56694. hLength += _HANDSHAKE_HEADER_SZ;
  56695. if (tail_len < hLength)
  56696. return -1;
  56697. if (currType != type) {
  56698. idx += hLength;
  56699. tail_len -= hLength;
  56700. continue;
  56701. }
  56702. /* match */
  56703. test_remove_msg(curr, *len - (int)(curr - buf), len,
  56704. hLength + _RECORD_HEADER_SZ);
  56705. *found = 1;
  56706. return 0;
  56707. }
  56708. /* not found */
  56709. return 0;
  56710. }
  56711. static int test_remove_hs_message(byte hs_message_type,
  56712. int extra_round, byte alert_type)
  56713. {
  56714. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56715. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56716. struct test_memio_ctx test_ctx;
  56717. WOLFSSL_ALERT_HISTORY h;
  56718. int ret, err;
  56719. byte found;
  56720. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56721. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56722. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  56723. AssertIntEQ(ret, 0);
  56724. ret = wolfSSL_connect(ssl_c);
  56725. err = wolfSSL_get_error(ssl_c, ret);
  56726. AssertIntNE(ret, WOLFSSL_SUCCESS);
  56727. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56728. ret = wolfSSL_accept(ssl_s);
  56729. err = wolfSSL_get_error(ssl_s, ret);
  56730. AssertIntNE(ret, WOLFSSL_SUCCESS);
  56731. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56732. if (extra_round) {
  56733. ret = wolfSSL_connect(ssl_c);
  56734. err = wolfSSL_get_error(ssl_c, ret);
  56735. AssertIntNE(ret, WOLFSSL_SUCCESS);
  56736. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56737. /* this will complete handshake from server side */
  56738. ret = wolfSSL_accept(ssl_s);
  56739. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56740. }
  56741. ret = test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  56742. &test_ctx.c_len, hs_message_type, &found);
  56743. AssertIntEQ(ret, 0);
  56744. if (!found) {
  56745. wolfSSL_free(ssl_c);
  56746. wolfSSL_CTX_free(ctx_c);
  56747. wolfSSL_free(ssl_s);
  56748. wolfSSL_CTX_free(ctx_s);
  56749. return TEST_SKIPPED;
  56750. }
  56751. ret = wolfSSL_connect(ssl_c);
  56752. err = wolfSSL_get_error(ssl_c, ret);
  56753. AssertIntNE(ret, WOLFSSL_SUCCESS);
  56754. AssertIntNE(err, WOLFSSL_ERROR_WANT_READ);
  56755. ret = wolfSSL_get_alert_history(ssl_c, &h);
  56756. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56757. if (alert_type != 0xff && h.last_tx.code != alert_type)
  56758. return TEST_FAIL;
  56759. if (h.last_tx.level != alert_fatal)
  56760. return TEST_FAIL;
  56761. wolfSSL_free(ssl_c);
  56762. wolfSSL_CTX_free(ctx_c);
  56763. wolfSSL_free(ssl_s);
  56764. wolfSSL_CTX_free(ctx_s);
  56765. return TEST_SUCCESS;
  56766. }
  56767. static int test_extra_alerts_skip_hs(void)
  56768. {
  56769. const byte _server_key_exchange = 12;
  56770. const byte _server_hello = 2;
  56771. const byte _certificate = 11;
  56772. int ret;
  56773. /* server_hello */
  56774. ret = test_remove_hs_message(_server_hello, 0,
  56775. unexpected_message);
  56776. AssertIntNE(ret, TEST_FAIL);
  56777. ret = test_remove_hs_message(_certificate, 0,
  56778. 0xff);
  56779. AssertIntNE(ret, TEST_FAIL);
  56780. ret = test_remove_hs_message(_server_key_exchange, 0,
  56781. unexpected_message);
  56782. AssertIntNE(ret, TEST_FAIL);
  56783. return TEST_SUCCESS;
  56784. }
  56785. #else
  56786. static int test_extra_alerts_skip_hs(void)
  56787. {
  56788. return TEST_SKIPPED;
  56789. }
  56790. #endif
  56791. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  56792. defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  56793. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  56794. unsigned char* key, unsigned int key_max_len)
  56795. {
  56796. (void)ssl;
  56797. (void)id;
  56798. (void)key_max_len;
  56799. /* zero means error */
  56800. key[0] = 0x10;
  56801. return 1;
  56802. }
  56803. static int test_extra_alerts_bad_psk(void)
  56804. {
  56805. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56806. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56807. struct test_memio_ctx test_ctx;
  56808. WOLFSSL_ALERT_HISTORY h;
  56809. int ret, err;
  56810. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56811. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56812. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  56813. if (ret != 0)
  56814. return TEST_FAIL;
  56815. ret = wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256");
  56816. if (ret != WOLFSSL_SUCCESS)
  56817. return TEST_FAIL;
  56818. ret = wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256");
  56819. if (ret != WOLFSSL_SUCCESS)
  56820. return TEST_FAIL;
  56821. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  56822. ret = wolfSSL_connect(ssl_c);
  56823. err = wolfSSL_get_error(ssl_c, ret);
  56824. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  56825. return TEST_FAIL;
  56826. ret = wolfSSL_accept(ssl_s);
  56827. err = wolfSSL_get_error(ssl_s, ret);
  56828. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  56829. return TEST_FAIL;
  56830. ret = wolfSSL_connect(ssl_c);
  56831. err = wolfSSL_get_error(ssl_c, ret);
  56832. if (ret == WOLFSSL_SUCCESS || err == WOLFSSL_ERROR_WANT_READ)
  56833. return TEST_FAIL;
  56834. ret = wolfSSL_get_alert_history(ssl_c, &h);
  56835. if (ret != WOLFSSL_SUCCESS)
  56836. return TEST_FAIL;
  56837. if (h.last_tx.code != handshake_failure)
  56838. return TEST_FAIL;
  56839. if (h.last_tx.level != alert_fatal)
  56840. return TEST_FAIL;
  56841. wolfSSL_free(ssl_c);
  56842. wolfSSL_CTX_free(ctx_c);
  56843. wolfSSL_free(ssl_s);
  56844. wolfSSL_CTX_free(ctx_s);
  56845. return TEST_SUCCESS;
  56846. }
  56847. #else
  56848. static int test_extra_alerts_bad_psk(void)
  56849. {
  56850. return TEST_SKIPPED;
  56851. }
  56852. #endif
  56853. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  56854. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56855. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  56856. int sz, void *ctx)
  56857. {
  56858. static int sentServerHello = FALSE;
  56859. if (!sentServerHello) {
  56860. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  56861. size_t i;
  56862. if (sz < (int)sizeof(renegExt))
  56863. return WOLFSSL_CBIO_ERR_GENERAL;
  56864. /* Remove SCR from ServerHello */
  56865. for (i = 0; i < sz - sizeof(renegExt); i++) {
  56866. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  56867. /* Found the extension. Change it to something unrecognized. */
  56868. buf[i+1] = 0x11;
  56869. break;
  56870. }
  56871. }
  56872. sentServerHello = TRUE;
  56873. }
  56874. return EmbedSend(ssl, buf, sz, ctx);
  56875. }
  56876. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  56877. {
  56878. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  56879. }
  56880. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  56881. {
  56882. WOLFSSL_ALERT_HISTORY h;
  56883. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  56884. AssertIntEQ(h.last_rx.code, handshake_failure);
  56885. AssertIntEQ(h.last_rx.level, alert_fatal);
  56886. }
  56887. static int test_harden_no_secure_renegotiation(void)
  56888. {
  56889. callback_functions client_cbs, server_cbs;
  56890. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56891. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56892. client_cbs.method = wolfTLSv1_2_client_method;
  56893. server_cbs.method = wolfTLSv1_2_server_method;
  56894. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  56895. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  56896. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56897. AssertIntEQ(client_cbs.return_code, TEST_FAIL);
  56898. AssertIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  56899. AssertIntEQ(server_cbs.return_code, TEST_FAIL);
  56900. AssertIntEQ(server_cbs.last_err, SOCKET_ERROR_E);
  56901. return TEST_RES_CHECK(1);
  56902. }
  56903. #else
  56904. static int test_harden_no_secure_renegotiation(void)
  56905. {
  56906. return TEST_SKIPPED;
  56907. }
  56908. #endif
  56909. #if defined(HAVE_OCSP) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56910. static int test_override_alt_cert_chain_cert_cb(int preverify,
  56911. WOLFSSL_X509_STORE_CTX* store)
  56912. {
  56913. printf("preverify: %d\n", preverify);
  56914. printf("store->error: %d\n", store->error);
  56915. printf("error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  56916. if (store->error == OCSP_INVALID_STATUS) {
  56917. printf("Overriding OCSP error\n");
  56918. return 1;
  56919. }
  56920. #ifndef WOLFSSL_ALT_CERT_CHAINS
  56921. else if ((store->error == ASN_NO_SIGNER_E ||
  56922. store->error == ASN_SELF_SIGNED_E
  56923. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  56924. defined(HAVE_WEBSERVER)
  56925. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  56926. #endif
  56927. ) && store->error_depth == store->totalCerts - 1) {
  56928. printf("Overriding no signer error only for root cert\n");
  56929. return 1;
  56930. }
  56931. #endif
  56932. else
  56933. return preverify;
  56934. }
  56935. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  56936. int urlSz, unsigned char* request, int requestSz,
  56937. unsigned char** response)
  56938. {
  56939. (void)ioCtx;
  56940. (void)url;
  56941. (void)urlSz;
  56942. (void)request;
  56943. (void)requestSz;
  56944. (void)response;
  56945. return -1;
  56946. }
  56947. static void test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  56948. {
  56949. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  56950. test_override_alt_cert_chain_cert_cb);
  56951. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  56952. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  56953. AssertIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  56954. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  56955. AssertIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  56956. WOLFSSL_SUCCESS);
  56957. }
  56958. static void test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  56959. {
  56960. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56961. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  56962. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  56963. AssertIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  56964. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  56965. AssertIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  56966. WOLFSSL_SUCCESS);
  56967. }
  56968. static void test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  56969. {
  56970. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  56971. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  56972. }
  56973. static int test_override_alt_cert_chain(void)
  56974. {
  56975. size_t i;
  56976. struct test_params {
  56977. ctx_callback client_ctx_cb;
  56978. ctx_callback server_ctx_cb;
  56979. int result;
  56980. } params[] = {
  56981. {test_override_alt_cert_chain_client_ctx_ready,
  56982. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  56983. {test_override_alt_cert_chain_client_ctx_ready2,
  56984. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  56985. };
  56986. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56987. callback_functions client_cbs, server_cbs;
  56988. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56989. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56990. printf("test config: %d\n", (int)i);
  56991. client_cbs.ctx_ready = params[i].client_ctx_cb;
  56992. server_cbs.ctx_ready = params[i].server_ctx_cb;
  56993. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56994. AssertIntEQ(client_cbs.return_code, params[i].result);
  56995. AssertIntEQ(server_cbs.return_code, params[i].result);
  56996. }
  56997. return TEST_RES_CHECK(1);
  56998. }
  56999. #else
  57000. static int test_override_alt_cert_chain(void)
  57001. {
  57002. return TEST_SKIPPED;
  57003. }
  57004. #endif
  57005. /*----------------------------------------------------------------------------*
  57006. | Main
  57007. *----------------------------------------------------------------------------*/
  57008. typedef int (*TEST_FUNC)(void);
  57009. typedef struct {
  57010. const char *name;
  57011. TEST_FUNC func;
  57012. byte run:1;
  57013. } TEST_CASE;
  57014. #define TEST_DECL(func) { #func, func, 0 }
  57015. int testAll = 1;
  57016. TEST_CASE testCases[] = {
  57017. TEST_DECL(test_fileAccess),
  57018. TEST_DECL(test_wolfSSL_Init),
  57019. TEST_DECL(test_wolfSSL_Method_Allocators),
  57020. #ifndef NO_WOLFSSL_SERVER
  57021. TEST_DECL(test_wolfSSL_CTX_new),
  57022. #endif
  57023. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  57024. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  57025. TEST_DECL(test_for_double_Free),
  57026. #endif
  57027. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57028. TEST_DECL(test_wolfSSL_get_finished),
  57029. TEST_DECL(test_wolfSSL_CTX_add_session),
  57030. TEST_DECL(test_wolfSSL_CTX_add_session_ext),
  57031. #endif
  57032. TEST_DECL(test_SSL_CIPHER_get_xxx),
  57033. TEST_DECL(test_wolfSSL_ERR_strings),
  57034. TEST_DECL(test_wolfSSL_EVP_shake128),
  57035. TEST_DECL(test_wolfSSL_EVP_shake256),
  57036. TEST_DECL(test_EVP_blake2),
  57037. TEST_DECL(test_EVP_MD_do_all),
  57038. TEST_DECL(test_OBJ_NAME_do_all),
  57039. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  57040. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  57041. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  57042. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  57043. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  57044. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  57045. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  57046. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  57047. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  57048. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  57049. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  57050. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  57051. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  57052. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  57053. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  57054. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  57055. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  57056. TEST_DECL(test_wolfSSL_FPKI),
  57057. TEST_DECL(test_wolfSSL_OtherName),
  57058. TEST_DECL(test_wolfSSL_CertRsaPss),
  57059. TEST_DECL(test_wolfSSL_CertManagerCRL),
  57060. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  57061. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  57062. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  57063. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  57064. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  57065. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  57066. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  57067. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  57068. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  57069. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  57070. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  57071. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  57072. TEST_DECL(test_server_wolfSSL_new),
  57073. TEST_DECL(test_client_wolfSSL_new),
  57074. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  57075. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  57076. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  57077. TEST_DECL(test_SetTmpEC_DHE_Sz),
  57078. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  57079. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  57080. TEST_DECL(test_wolfSSL_dtls_plaintext),
  57081. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57082. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57083. TEST_DECL(test_wolfSSL_read_write),
  57084. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  57085. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
  57086. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  57087. TEST_DECL(test_wolfSSL_dtls_export),
  57088. TEST_DECL(test_wolfSSL_tls_export),
  57089. #endif
  57090. TEST_DECL(test_wolfSSL_SetMinVersion),
  57091. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  57092. /* TLS extensions tests */
  57093. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57094. #ifdef HAVE_SNI
  57095. TEST_DECL(test_wolfSSL_UseSNI_params),
  57096. TEST_DECL(test_wolfSSL_UseSNI_connection),
  57097. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  57098. #endif /* HAVE_SNI */
  57099. #endif
  57100. TEST_DECL(test_wolfSSL_UseTrustedCA),
  57101. TEST_DECL(test_wolfSSL_UseMaxFragment),
  57102. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  57103. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  57104. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57105. TEST_DECL(test_wolfSSL_UseALPN_connection),
  57106. TEST_DECL(test_wolfSSL_UseALPN_params),
  57107. #endif
  57108. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  57109. TEST_DECL(test_wolfSSL_set_alpn_protos),
  57110. #endif
  57111. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  57112. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  57113. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  57114. TEST_DECL(test_tls_ext_duplicate),
  57115. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  57116. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  57117. TEST_DECL(test_wolfSSL_Tls13_ECH),
  57118. #endif
  57119. /* X509 tests */
  57120. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  57121. TEST_DECL(test_wolfSSL_PKCS12),
  57122. TEST_DECL(test_wolfSSL_no_password_cb),
  57123. TEST_DECL(test_wolfSSL_PKCS8),
  57124. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  57125. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  57126. TEST_DECL(test_wolfSSL_PKCS5),
  57127. TEST_DECL(test_wolfSSL_URI),
  57128. TEST_DECL(test_wolfSSL_TBS),
  57129. TEST_DECL(test_wolfSSL_X509_verify),
  57130. TEST_DECL(test_wolfSSL_X509_TLS_version),
  57131. TEST_DECL(test_wc_PemToDer),
  57132. TEST_DECL(test_wc_AllocDer),
  57133. TEST_DECL(test_wc_CertPemToDer),
  57134. TEST_DECL(test_wc_KeyPemToDer),
  57135. TEST_DECL(test_wc_PubKeyPemToDer),
  57136. TEST_DECL(test_wc_PemPubKeyToDer),
  57137. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  57138. TEST_DECL(test_wc_CheckCertSigPubKey),
  57139. /* OCSP Stapling */
  57140. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  57141. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  57142. /* Multicast */
  57143. TEST_DECL(test_wolfSSL_mcast),
  57144. /* ASN.1 compatibility API tests */
  57145. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  57146. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  57147. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  57148. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  57149. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  57150. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  57151. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  57152. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  57153. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  57154. TEST_DECL(test_wolfSSL_ASN1_get_object),
  57155. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  57156. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  57157. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  57158. TEST_DECL(test_wolfSSL_ASN1_STRING),
  57159. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  57160. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  57161. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  57162. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  57163. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  57164. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  57165. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  57166. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  57167. TEST_DECL(test_wolfSSL_ASN1_TIME),
  57168. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  57169. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  57170. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  57171. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  57172. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  57173. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  57174. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  57175. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  57176. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  57177. /* compatibility tests */
  57178. TEST_DECL(test_wolfSSL_lhash),
  57179. TEST_DECL(test_wolfSSL_X509_NAME),
  57180. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  57181. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  57182. #ifndef NO_BIO
  57183. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  57184. TEST_DECL(test_wolfSSL_X509_INFO),
  57185. #endif
  57186. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  57187. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  57188. TEST_DECL(test_wolfSSL_X509_check_host),
  57189. TEST_DECL(test_wolfSSL_X509_check_email),
  57190. TEST_DECL(test_wolfSSL_DES),
  57191. TEST_DECL(test_wolfSSL_certs),
  57192. TEST_DECL(test_wolfSSL_X509_check_private_key),
  57193. TEST_DECL(test_wolfSSL_private_keys),
  57194. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  57195. #ifndef NO_BIO
  57196. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  57197. #endif
  57198. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  57199. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  57200. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  57201. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  57202. #ifndef NO_BIO
  57203. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  57204. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  57205. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  57206. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  57207. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  57208. #endif
  57209. TEST_DECL(test_DSA_do_sign_verify),
  57210. TEST_DECL(test_wolfSSL_tmp_dh),
  57211. TEST_DECL(test_wolfSSL_ctrl),
  57212. TEST_DECL(test_wolfSSL_EVP_MD_size),
  57213. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  57214. TEST_DECL(test_wolfSSL_EVP_Digest),
  57215. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  57216. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  57217. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  57218. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  57219. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  57220. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  57221. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  57222. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  57223. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  57224. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  57225. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  57226. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  57227. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  57228. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  57229. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  57230. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  57231. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  57232. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  57233. #endif
  57234. #ifndef NO_BIO
  57235. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  57236. TEST_DECL(test_wolfSSL_GetLoggingCb),
  57237. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  57238. TEST_DECL(test_wc_ERR_remove_state),
  57239. TEST_DECL(test_wc_ERR_print_errors_fp),
  57240. #endif
  57241. TEST_DECL(test_wolfSSL_set_options),
  57242. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  57243. TEST_DECL(test_wolfSSL_set1_curves_list),
  57244. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  57245. TEST_DECL(test_wolfSSL_PKCS7_certs),
  57246. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  57247. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  57248. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  57249. TEST_DECL(test_wolfSSL_msgCb),
  57250. TEST_DECL(test_wolfSSL_either_side),
  57251. TEST_DECL(test_wolfSSL_DTLS_either_side),
  57252. TEST_DECL(test_wolfSSL_dtls_fragments),
  57253. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  57254. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  57255. TEST_DECL(test_wolfSSL_dtls_bad_record),
  57256. TEST_DECL(test_wolfSSL_dtls_stateless),
  57257. TEST_DECL(test_generate_cookie),
  57258. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  57259. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  57260. TEST_DECL(test_wolfSSL_X509_Name_canon),
  57261. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  57262. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  57263. TEST_DECL(test_wolfSSL_X509_NID),
  57264. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  57265. TEST_DECL(test_wolfSSL_get0_param),
  57266. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  57267. TEST_DECL(test_wolfSSL_set1_host),
  57268. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  57269. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  57270. TEST_DECL(test_wolfSSL_X509_STORE),
  57271. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  57272. TEST_DECL(test_X509_STORE_get0_objects),
  57273. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  57274. /* RAND compatability API */
  57275. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  57276. TEST_DECL(test_wolfSSL_RAND_bytes),
  57277. /* BN compatability API */
  57278. TEST_DECL(test_wolfSSL_BN_CTX),
  57279. TEST_DECL(test_wolfSSL_BN),
  57280. TEST_DECL(test_wolfSSL_BN_init),
  57281. TEST_DECL(test_wolfSSL_BN_enc_dec),
  57282. TEST_DECL(test_wolfSSL_BN_word),
  57283. TEST_DECL(test_wolfSSL_BN_bits),
  57284. TEST_DECL(test_wolfSSL_BN_shift),
  57285. TEST_DECL(test_wolfSSL_BN_math),
  57286. TEST_DECL(test_wolfSSL_BN_math_mod),
  57287. TEST_DECL(test_wolfSSL_BN_math_other),
  57288. TEST_DECL(test_wolfSSL_BN_rand),
  57289. TEST_DECL(test_wolfSSL_BN_prime),
  57290. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  57291. #ifndef NO_BIO
  57292. TEST_DECL(test_wolfSSL_PEM_read_bio),
  57293. TEST_DECL(test_wolfSSL_BIO),
  57294. #endif
  57295. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  57296. TEST_DECL(test_wolfSSL_X509),
  57297. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  57298. TEST_DECL(test_wolfSSL_X509_sign),
  57299. TEST_DECL(test_wolfSSL_X509_sign2),
  57300. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  57301. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  57302. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57303. TEST_DECL(test_wolfSSL_check_domain),
  57304. #endif
  57305. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  57306. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  57307. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  57308. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  57309. TEST_DECL(test_wolfSSL_RAND),
  57310. TEST_DECL(test_wolfSSL_BUF),
  57311. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  57312. TEST_DECL(test_wolfSSL_X509_cmp_time),
  57313. TEST_DECL(test_wolfSSL_X509_time_adj),
  57314. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  57315. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  57316. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  57317. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  57318. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  57319. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  57320. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  57321. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  57322. TEST_DECL(test_wolfSSL_Tls13_postauth),
  57323. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  57324. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  57325. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  57326. TEST_DECL(test_wolfSSL_THREADID_hash),
  57327. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  57328. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  57329. TEST_DECL(test_error_queue_per_thread),
  57330. TEST_DECL(test_wolfSSL_ERR_put_error),
  57331. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  57332. #ifndef NO_BIO
  57333. TEST_DECL(test_wolfSSL_ERR_print_errors),
  57334. #endif
  57335. TEST_DECL(test_wolfSSL_HMAC),
  57336. TEST_DECL(test_wolfSSL_CMAC),
  57337. TEST_DECL(test_wolfSSL_OBJ),
  57338. TEST_DECL(test_wolfSSL_OBJ_cmp),
  57339. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  57340. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  57341. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  57342. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  57343. TEST_DECL(test_GENERAL_NAME_set0_othername),
  57344. TEST_DECL(test_othername_and_SID_ext),
  57345. TEST_DECL(test_wolfSSL_X509_set_name),
  57346. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  57347. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  57348. TEST_DECL(test_wolfSSL_X509_set_version),
  57349. #ifndef NO_BIO
  57350. TEST_DECL(test_wolfSSL_BIO_gets),
  57351. TEST_DECL(test_wolfSSL_BIO_puts),
  57352. TEST_DECL(test_wolfSSL_BIO_dump),
  57353. TEST_DECL(test_wolfSSL_BIO_should_retry),
  57354. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  57355. TEST_DECL(test_wolfSSL_BIO_write),
  57356. TEST_DECL(test_wolfSSL_BIO_connect),
  57357. TEST_DECL(test_wolfSSL_BIO_accept),
  57358. TEST_DECL(test_wolfSSL_BIO_printf),
  57359. TEST_DECL(test_wolfSSL_BIO_f_md),
  57360. TEST_DECL(test_wolfSSL_BIO_up_ref),
  57361. TEST_DECL(test_wolfSSL_BIO_tls),
  57362. TEST_DECL(test_wolfSSL_BIO_reset),
  57363. #endif
  57364. TEST_DECL(test_wolfSSL_cert_cb),
  57365. TEST_DECL(test_wolfSSL_SESSION),
  57366. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  57367. TEST_DECL(test_wolfSSL_ticket_keys),
  57368. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  57369. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  57370. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  57371. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  57372. TEST_DECL(test_wolfSSL_MD4),
  57373. TEST_DECL(test_wolfSSL_verify_mode),
  57374. TEST_DECL(test_wolfSSL_verify_depth),
  57375. TEST_DECL(test_wolfSSL_HMAC_CTX),
  57376. TEST_DECL(test_wolfSSL_msg_callback),
  57377. TEST_DECL(test_wolfSSL_SHA),
  57378. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  57379. TEST_DECL(test_wolfSSL_MD5),
  57380. TEST_DECL(test_wolfSSL_MD5_Transform),
  57381. TEST_DECL(test_wolfSSL_SHA_Transform),
  57382. TEST_DECL(test_wolfSSL_SHA256),
  57383. TEST_DECL(test_wolfSSL_SHA256_Transform),
  57384. TEST_DECL(test_wolfSSL_SHA224),
  57385. TEST_DECL(test_wolfSSL_SHA512_Transform),
  57386. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  57387. TEST_DECL(test_wolfSSL_X509_CRL),
  57388. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  57389. TEST_DECL(test_wolfSSL_PEM_read_X509),
  57390. TEST_DECL(test_wolfSSL_PEM_read),
  57391. #ifndef NO_BIO
  57392. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  57393. #endif
  57394. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  57395. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  57396. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  57397. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  57398. TEST_DECL(test_wolfSSL_X509_check_ca),
  57399. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  57400. TEST_DECL(test_wolfSSL_make_cert),
  57401. TEST_DECL(test_wolfSSL_DES_ncbc),
  57402. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  57403. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  57404. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  57405. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  57406. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  57407. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  57408. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  57409. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  57410. TEST_DECL(test_wolfssl_EVP_chacha20),
  57411. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  57412. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  57413. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  57414. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  57415. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  57416. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  57417. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  57418. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  57419. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  57420. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  57421. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  57422. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  57423. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  57424. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  57425. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  57426. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  57427. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  57428. TEST_DECL(test_CONF_modules_xxx),
  57429. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  57430. TEST_DECL(test_CRYPTO_THREADID_xxx),
  57431. TEST_DECL(test_ENGINE_cleanup),
  57432. #ifdef OPENSSL_ALL
  57433. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  57434. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  57435. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  57436. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  57437. TEST_DECL(test_wolfSSL_DSA_SIG),
  57438. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  57439. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  57440. TEST_DECL(test_wolfSSL_CTX_ctrl),
  57441. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  57442. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  57443. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  57444. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  57445. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  57446. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  57447. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  57448. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  57449. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  57450. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  57451. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  57452. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  57453. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  57454. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  57455. TEST_DECL(test_wolfSSL_EVP_rc4),
  57456. TEST_DECL(test_wolfSSL_EVP_enc_null),
  57457. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  57458. TEST_DECL(test_wolfSSL_EVP_mdc2),
  57459. TEST_DECL(test_wolfSSL_EVP_md4),
  57460. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  57461. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  57462. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  57463. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  57464. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  57465. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  57466. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  57467. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  57468. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  57469. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  57470. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  57471. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  57472. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  57473. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  57474. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  57475. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  57476. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  57477. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  57478. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  57479. TEST_DECL(test_evp_cipher_aes_gcm),
  57480. TEST_DECL(test_wolfSSL_OBJ_ln),
  57481. TEST_DECL(test_wolfSSL_OBJ_sn),
  57482. TEST_DECL(test_wolfSSL_TXT_DB),
  57483. TEST_DECL(test_wolfSSL_NCONF),
  57484. #endif /* OPENSSL_ALL */
  57485. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  57486. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  57487. #ifndef NO_BIO
  57488. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  57489. #endif /* !NO_BIO */
  57490. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  57491. TEST_DECL(test_wolfSSL_X509_CA_num),
  57492. TEST_DECL(test_wolfSSL_X509_get_version),
  57493. #ifndef NO_BIO
  57494. TEST_DECL(test_wolfSSL_X509_print),
  57495. TEST_DECL(test_wolfSSL_X509_CRL_print),
  57496. TEST_DECL(test_wolfSSL_BIO_get_len),
  57497. #endif
  57498. TEST_DECL(test_wolfSSL_RSA),
  57499. TEST_DECL(test_wolfSSL_RSA_DER),
  57500. TEST_DECL(test_wolfSSL_RSA_print),
  57501. #ifndef NO_RSA
  57502. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  57503. #endif
  57504. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  57505. TEST_DECL(test_wolfSSL_RSA_get0_key),
  57506. TEST_DECL(test_wolfSSL_RSA_meth),
  57507. TEST_DECL(test_wolfSSL_RSA_verify),
  57508. TEST_DECL(test_wolfSSL_RSA_sign),
  57509. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  57510. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  57511. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  57512. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  57513. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  57514. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  57515. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  57516. TEST_DECL(test_wolfSSL_RSA_ex_data),
  57517. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  57518. TEST_DECL(test_wolfSSL_RSA_To_Der),
  57519. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  57520. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  57521. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  57522. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  57523. TEST_DECL(test_wolfSSL_DH),
  57524. TEST_DECL(test_wolfSSL_DH_dup),
  57525. TEST_DECL(test_wolfSSL_DH_check),
  57526. TEST_DECL(test_wolfSSL_DH_prime),
  57527. TEST_DECL(test_wolfSSL_DH_1536_prime),
  57528. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  57529. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  57530. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  57531. TEST_DECL(test_wolfSSL_d2i_DHparams),
  57532. TEST_DECL(test_wolfSSL_DH_LoadDer),
  57533. TEST_DECL(test_wolfSSL_i2d_DHparams),
  57534. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  57535. TEST_DECL(test_wolfSSL_EC_GROUP),
  57536. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  57537. TEST_DECL(test_wolfSSL_EC_POINT),
  57538. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  57539. TEST_DECL(test_EC_i2d),
  57540. TEST_DECL(test_wolfSSL_EC_curve),
  57541. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  57542. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  57543. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  57544. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  57545. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  57546. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  57547. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  57548. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  57549. TEST_DECL(test_ECDSA_size_sign),
  57550. TEST_DECL(test_ECDH_compute_key),
  57551. #endif
  57552. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  57553. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  57554. TEST_DECL(test_wolfSSL_X509V3_EXT),
  57555. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  57556. TEST_DECL(test_wolfSSL_X509_get_ext),
  57557. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  57558. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  57559. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  57560. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  57561. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  57562. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  57563. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  57564. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  57565. TEST_DECL(test_wolfSSL_X509_cmp),
  57566. TEST_DECL(test_openssl_generate_key_and_cert),
  57567. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  57568. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  57569. /* test the no op functions for compatibility */
  57570. TEST_DECL(test_no_op_functions),
  57571. /* OpenSSL EVP_PKEY API tests */
  57572. TEST_DECL(test_EVP_PKEY_rsa),
  57573. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  57574. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  57575. TEST_DECL(test_EVP_PKEY_ec),
  57576. TEST_DECL(test_EVP_PKEY_cmp),
  57577. /* OpenSSL error API tests */
  57578. TEST_DECL(test_ERR_load_crypto_strings),
  57579. /* OpenSSL sk_X509 API test */
  57580. TEST_DECL(test_sk_X509),
  57581. /* OpenSSL sk_X509_CRL API test */
  57582. TEST_DECL(test_sk_X509_CRL),
  57583. /* OpenSSL X509 API test */
  57584. TEST_DECL(test_X509_get_signature_nid),
  57585. /* OpenSSL X509 REQ API test */
  57586. TEST_DECL(test_X509_REQ),
  57587. /* OpenSSL PKCS7 API test */
  57588. TEST_DECL(test_wolfssl_PKCS7),
  57589. TEST_DECL(test_wolfSSL_PKCS7_sign),
  57590. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  57591. #ifndef NO_BIO
  57592. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  57593. #ifdef HAVE_SMIME
  57594. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  57595. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  57596. #endif /* HAVE_SMIME */
  57597. #endif /* !NO_BIO */
  57598. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  57599. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  57600. TEST_DECL(test_X509_LOOKUP_add_dir),
  57601. /* wolfCrypt ASN tests */
  57602. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  57603. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  57604. TEST_DECL(test_wc_SetSubjectRaw),
  57605. TEST_DECL(test_wc_GetSubjectRaw),
  57606. TEST_DECL(test_wc_SetIssuerRaw),
  57607. TEST_DECL(test_wc_SetIssueBuffer),
  57608. TEST_DECL(test_wc_SetSubjectKeyId),
  57609. TEST_DECL(test_wc_SetSubject),
  57610. TEST_DECL(test_CheckCertSignature),
  57611. TEST_DECL(test_wc_ParseCert),
  57612. TEST_DECL(test_wc_ParseCert_Error),
  57613. TEST_DECL(test_MakeCertWithPathLen),
  57614. /* wolfCrypt ECC tests */
  57615. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  57616. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  57617. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  57618. #ifdef WOLFSSL_TLS13
  57619. /* TLS v1.3 API tests */
  57620. TEST_DECL(test_tls13_apis),
  57621. TEST_DECL(test_tls13_cipher_suites),
  57622. #endif
  57623. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  57624. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  57625. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  57626. /* Bad certificate signature tests */
  57627. TEST_DECL(test_EccSigFailure_cm),
  57628. TEST_DECL(test_RsaSigFailure_cm),
  57629. #endif /* NO_CERTS */
  57630. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  57631. !defined(NO_OLD_TLS))
  57632. TEST_DECL(test_DhCallbacks),
  57633. #endif
  57634. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57635. TEST_DECL(test_export_keying_material),
  57636. #endif
  57637. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  57638. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57639. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57640. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57641. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  57642. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  57643. #endif
  57644. TEST_DECL(test_wolfSSL_security_level),
  57645. TEST_DECL(test_wolfSSL_SSL_in_init),
  57646. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  57647. TEST_DECL(test_wolfSSL_OpenSSL_version),
  57648. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  57649. TEST_DECL(test_ticket_and_psk_mixing),
  57650. TEST_DECL(test_prioritize_psk),
  57651. TEST_DECL(test_CONF_CTX_FILE),
  57652. TEST_DECL(test_CONF_CTX_CMDLINE),
  57653. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  57654. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  57655. /* wolfcrypt */
  57656. TEST_DECL(test_wolfCrypt_Init),
  57657. TEST_DECL(test_wc_InitMd5),
  57658. TEST_DECL(test_wc_Md5Update),
  57659. TEST_DECL(test_wc_Md5Final),
  57660. TEST_DECL(test_wc_InitSha),
  57661. TEST_DECL(test_wc_ShaUpdate),
  57662. TEST_DECL(test_wc_ShaFinal),
  57663. TEST_DECL(test_wc_InitSha256),
  57664. TEST_DECL(test_wc_Sha256Update),
  57665. TEST_DECL(test_wc_Sha256Final),
  57666. TEST_DECL(test_wc_Sha256FinalRaw),
  57667. TEST_DECL(test_wc_Sha256GetFlags),
  57668. TEST_DECL(test_wc_Sha256Free),
  57669. TEST_DECL(test_wc_Sha256GetHash),
  57670. TEST_DECL(test_wc_Sha256Copy),
  57671. TEST_DECL(test_wc_InitSha512),
  57672. TEST_DECL(test_wc_Sha512Update),
  57673. TEST_DECL(test_wc_Sha512Final),
  57674. TEST_DECL(test_wc_Sha512GetFlags),
  57675. TEST_DECL(test_wc_Sha512FinalRaw),
  57676. TEST_DECL(test_wc_Sha512Free),
  57677. TEST_DECL(test_wc_Sha512GetHash),
  57678. TEST_DECL(test_wc_Sha512Copy),
  57679. TEST_DECL(test_wc_InitSha512_224),
  57680. TEST_DECL(test_wc_Sha512_224Update),
  57681. TEST_DECL(test_wc_Sha512_224Final),
  57682. TEST_DECL(test_wc_Sha512_224GetFlags),
  57683. TEST_DECL(test_wc_Sha512_224FinalRaw),
  57684. TEST_DECL(test_wc_Sha512_224Free),
  57685. TEST_DECL(test_wc_Sha512_224GetHash),
  57686. TEST_DECL(test_wc_Sha512_224Copy),
  57687. TEST_DECL(test_wc_InitSha512_256),
  57688. TEST_DECL(test_wc_Sha512_256Update),
  57689. TEST_DECL(test_wc_Sha512_256Final),
  57690. TEST_DECL(test_wc_Sha512_256GetFlags),
  57691. TEST_DECL(test_wc_Sha512_256FinalRaw),
  57692. TEST_DECL(test_wc_Sha512_256Free),
  57693. TEST_DECL(test_wc_Sha512_256GetHash),
  57694. TEST_DECL(test_wc_Sha512_256Copy),
  57695. TEST_DECL(test_wc_InitSha384),
  57696. TEST_DECL(test_wc_Sha384Update),
  57697. TEST_DECL(test_wc_Sha384Final),
  57698. TEST_DECL(test_wc_Sha384GetFlags),
  57699. TEST_DECL(test_wc_Sha384FinalRaw),
  57700. TEST_DECL(test_wc_Sha384Free),
  57701. TEST_DECL(test_wc_Sha384GetHash),
  57702. TEST_DECL(test_wc_Sha384Copy),
  57703. TEST_DECL(test_wc_InitSha224),
  57704. TEST_DECL(test_wc_Sha224Update),
  57705. TEST_DECL(test_wc_Sha224Final),
  57706. TEST_DECL(test_wc_Sha224SetFlags),
  57707. TEST_DECL(test_wc_Sha224GetFlags),
  57708. TEST_DECL(test_wc_Sha224Free),
  57709. TEST_DECL(test_wc_Sha224GetHash),
  57710. TEST_DECL(test_wc_Sha224Copy),
  57711. TEST_DECL(test_wc_InitBlake2b),
  57712. TEST_DECL(test_wc_InitBlake2b_WithKey),
  57713. TEST_DECL(test_wc_InitBlake2s_WithKey),
  57714. TEST_DECL(test_wc_InitRipeMd),
  57715. TEST_DECL(test_wc_RipeMdUpdate),
  57716. TEST_DECL(test_wc_RipeMdFinal),
  57717. TEST_DECL(test_wc_InitSha3),
  57718. TEST_DECL(testing_wc_Sha3_Update),
  57719. TEST_DECL(test_wc_Sha3_224_Final),
  57720. TEST_DECL(test_wc_Sha3_256_Final),
  57721. TEST_DECL(test_wc_Sha3_384_Final),
  57722. TEST_DECL(test_wc_Sha3_512_Final),
  57723. TEST_DECL(test_wc_Sha3_224_Copy),
  57724. TEST_DECL(test_wc_Sha3_256_Copy),
  57725. TEST_DECL(test_wc_Sha3_384_Copy),
  57726. TEST_DECL(test_wc_Sha3_512_Copy),
  57727. TEST_DECL(test_wc_Sha3_GetFlags),
  57728. TEST_DECL(test_wc_InitShake256),
  57729. TEST_DECL(testing_wc_Shake256_Update),
  57730. TEST_DECL(test_wc_Shake256_Final),
  57731. TEST_DECL(test_wc_Shake256_Copy),
  57732. TEST_DECL(test_wc_Shake256Hash),
  57733. TEST_DECL(test_wc_Md5HmacSetKey),
  57734. TEST_DECL(test_wc_Md5HmacUpdate),
  57735. TEST_DECL(test_wc_Md5HmacFinal),
  57736. TEST_DECL(test_wc_ShaHmacSetKey),
  57737. TEST_DECL(test_wc_ShaHmacUpdate),
  57738. TEST_DECL(test_wc_ShaHmacFinal),
  57739. TEST_DECL(test_wc_Sha224HmacSetKey),
  57740. TEST_DECL(test_wc_Sha224HmacUpdate),
  57741. TEST_DECL(test_wc_Sha224HmacFinal),
  57742. TEST_DECL(test_wc_Sha256HmacSetKey),
  57743. TEST_DECL(test_wc_Sha256HmacUpdate),
  57744. TEST_DECL(test_wc_Sha256HmacFinal),
  57745. TEST_DECL(test_wc_Sha384HmacSetKey),
  57746. TEST_DECL(test_wc_Sha384HmacUpdate),
  57747. TEST_DECL(test_wc_Sha384HmacFinal),
  57748. TEST_DECL(test_wc_HashInit),
  57749. TEST_DECL(test_wc_HashSetFlags),
  57750. TEST_DECL(test_wc_HashGetFlags),
  57751. TEST_DECL(test_wc_InitCmac),
  57752. TEST_DECL(test_wc_CmacUpdate),
  57753. TEST_DECL(test_wc_CmacFinal),
  57754. TEST_DECL(test_wc_AesCmacGenerate),
  57755. TEST_DECL(test_wc_AesGcmStream),
  57756. TEST_DECL(test_wc_Des3_SetIV),
  57757. TEST_DECL(test_wc_Des3_SetKey),
  57758. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  57759. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  57760. TEST_DECL(test_wc_Des3_EcbEncrypt),
  57761. TEST_DECL(test_wc_Chacha_SetKey),
  57762. TEST_DECL(test_wc_Chacha_Process),
  57763. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  57764. TEST_DECL(test_wc_Poly1305SetKey),
  57765. TEST_DECL(test_wc_CamelliaSetKey),
  57766. TEST_DECL(test_wc_CamelliaSetIV),
  57767. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  57768. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  57769. TEST_DECL(test_wc_Arc4SetKey),
  57770. TEST_DECL(test_wc_Arc4Process),
  57771. TEST_DECL(test_wc_Rc2SetKey),
  57772. TEST_DECL(test_wc_Rc2SetIV),
  57773. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  57774. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  57775. TEST_DECL(test_wc_AesSetKey),
  57776. TEST_DECL(test_wc_AesSetIV),
  57777. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  57778. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  57779. TEST_DECL(test_wc_AesGcmSetKey),
  57780. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  57781. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  57782. TEST_DECL(test_wc_GmacSetKey),
  57783. TEST_DECL(test_wc_GmacUpdate),
  57784. TEST_DECL(test_wc_InitRsaKey),
  57785. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  57786. TEST_DECL(test_wc_RsaPublicKeyDecode),
  57787. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  57788. TEST_DECL(test_wc_MakeRsaKey),
  57789. TEST_DECL(test_wc_SetKeyUsage),
  57790. TEST_DECL(test_wc_CheckProbablePrime),
  57791. TEST_DECL(test_wc_RsaPSS_Verify),
  57792. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  57793. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  57794. TEST_DECL(test_wc_SetMutexCb),
  57795. TEST_DECL(test_wc_LockMutex_ex),
  57796. TEST_DECL(test_wc_RsaKeyToDer),
  57797. TEST_DECL(test_wc_RsaKeyToPublicDer),
  57798. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  57799. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  57800. TEST_DECL(test_wc_RsaEncryptSize),
  57801. TEST_DECL(test_wc_RsaSSL_SignVerify),
  57802. TEST_DECL(test_wc_RsaFlattenPublicKey),
  57803. TEST_DECL(test_RsaDecryptBoundsCheck),
  57804. TEST_DECL(test_wc_AesCcmSetKey),
  57805. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  57806. TEST_DECL(test_wc_InitDsaKey),
  57807. TEST_DECL(test_wc_DsaSignVerify),
  57808. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  57809. TEST_DECL(test_wc_MakeDsaKey),
  57810. TEST_DECL(test_wc_DsaKeyToDer),
  57811. TEST_DECL(test_wc_DsaKeyToPublicDer),
  57812. TEST_DECL(test_wc_DsaImportParamsRaw),
  57813. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  57814. TEST_DECL(test_wc_DsaExportParamsRaw),
  57815. TEST_DECL(test_wc_DsaExportKeyRaw),
  57816. TEST_DECL(test_wc_SignatureGetSize_ecc),
  57817. TEST_DECL(test_wc_SignatureGetSize_rsa),
  57818. /*
  57819. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  57820. * avoid memory leaks.
  57821. */
  57822. TEST_DECL(test_wolfCrypt_Cleanup),
  57823. #ifdef OPENSSL_EXTRA
  57824. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  57825. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  57826. TEST_DECL(test_ED25519),
  57827. TEST_DECL(test_ED448),
  57828. #endif
  57829. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  57830. !defined(HAVE_SELFTEST) && \
  57831. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  57832. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  57833. #endif
  57834. #ifdef HAVE_HASHDRBG
  57835. #ifdef TEST_RESEED_INTERVAL
  57836. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  57837. #endif
  57838. TEST_DECL(test_wc_RNG_GenerateBlock),
  57839. #endif
  57840. TEST_DECL(test_get_rand_digit),
  57841. TEST_DECL(test_get_digit_count),
  57842. TEST_DECL(test_mp_cond_copy),
  57843. TEST_DECL(test_mp_rand),
  57844. TEST_DECL(test_get_digit),
  57845. TEST_DECL(test_wc_export_int),
  57846. TEST_DECL(test_wc_InitRngNonce),
  57847. TEST_DECL(test_wc_InitRngNonce_ex),
  57848. TEST_DECL(test_wc_ed25519_make_key),
  57849. TEST_DECL(test_wc_ed25519_init),
  57850. TEST_DECL(test_wc_ed25519_sign_msg),
  57851. TEST_DECL(test_wc_ed25519_import_public),
  57852. TEST_DECL(test_wc_ed25519_import_private_key),
  57853. TEST_DECL(test_wc_ed25519_export),
  57854. TEST_DECL(test_wc_ed25519_size),
  57855. TEST_DECL(test_wc_ed25519_exportKey),
  57856. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  57857. TEST_DECL(test_wc_curve25519_init),
  57858. TEST_DECL(test_wc_curve25519_size),
  57859. TEST_DECL(test_wc_curve25519_export_key_raw),
  57860. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  57861. TEST_DECL(test_wc_curve25519_make_key),
  57862. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  57863. TEST_DECL(test_wc_curve25519_make_pub),
  57864. TEST_DECL(test_wc_curve25519_export_public_ex),
  57865. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  57866. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  57867. TEST_DECL(test_wc_curve25519_import_private),
  57868. TEST_DECL(test_wc_ed448_make_key),
  57869. TEST_DECL(test_wc_ed448_init),
  57870. TEST_DECL(test_wc_ed448_sign_msg),
  57871. TEST_DECL(test_wc_ed448_import_public),
  57872. TEST_DECL(test_wc_ed448_import_private_key),
  57873. TEST_DECL(test_wc_ed448_export),
  57874. TEST_DECL(test_wc_ed448_size),
  57875. TEST_DECL(test_wc_ed448_exportKey),
  57876. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  57877. TEST_DECL(test_wc_curve448_make_key),
  57878. TEST_DECL(test_wc_curve448_shared_secret_ex),
  57879. TEST_DECL(test_wc_curve448_export_public_ex),
  57880. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  57881. TEST_DECL(test_wc_curve448_export_key_raw),
  57882. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  57883. TEST_DECL(test_wc_curve448_import_private),
  57884. TEST_DECL(test_wc_curve448_init),
  57885. TEST_DECL(test_wc_curve448_size),
  57886. TEST_DECL(test_wc_ecc_make_key),
  57887. TEST_DECL(test_wc_ecc_init),
  57888. TEST_DECL(test_wc_ecc_check_key),
  57889. TEST_DECL(test_wc_ecc_get_generator),
  57890. TEST_DECL(test_wc_ecc_size),
  57891. TEST_DECL(test_wc_ecc_params),
  57892. TEST_DECL(test_wc_ecc_signVerify_hash),
  57893. TEST_DECL(test_wc_ecc_shared_secret),
  57894. TEST_DECL(test_wc_ecc_export_x963),
  57895. TEST_DECL(test_wc_ecc_export_x963_ex),
  57896. TEST_DECL(test_wc_ecc_import_x963),
  57897. TEST_DECL(ecc_import_private_key),
  57898. TEST_DECL(test_wc_ecc_export_private_only),
  57899. TEST_DECL(test_wc_ecc_rs_to_sig),
  57900. TEST_DECL(test_wc_ecc_import_raw),
  57901. TEST_DECL(test_wc_ecc_import_unsigned),
  57902. TEST_DECL(test_wc_ecc_sig_size),
  57903. TEST_DECL(test_wc_ecc_ctx_new),
  57904. TEST_DECL(test_wc_ecc_ctx_reset),
  57905. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  57906. TEST_DECL(test_wc_ecc_ctx_set_info),
  57907. TEST_DECL(test_wc_ecc_encryptDecrypt),
  57908. TEST_DECL(test_wc_ecc_del_point),
  57909. TEST_DECL(test_wc_ecc_pointFns),
  57910. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  57911. TEST_DECL(test_wc_ecc_verify_hash_ex),
  57912. TEST_DECL(test_wc_ecc_mulmod),
  57913. TEST_DECL(test_wc_ecc_is_valid_idx),
  57914. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  57915. TEST_DECL(test_wc_ecc_sig_size_calc),
  57916. TEST_DECL(test_ToTraditional),
  57917. TEST_DECL(test_wc_EccPrivateKeyToDer),
  57918. TEST_DECL(test_wc_DhPublicKeyDecode),
  57919. TEST_DECL(test_wc_Ed25519KeyToDer),
  57920. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  57921. TEST_DECL(test_wc_Ed448KeyToDer),
  57922. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  57923. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  57924. TEST_DECL(test_wc_SetSubjectBuffer),
  57925. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  57926. TEST_DECL(test_wc_PKCS7_New),
  57927. TEST_DECL(test_wc_PKCS7_Init),
  57928. TEST_DECL(test_wc_PKCS7_InitWithCert),
  57929. TEST_DECL(test_wc_PKCS7_EncodeData),
  57930. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  57931. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  57932. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  57933. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  57934. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  57935. TEST_DECL(test_wc_PKCS7_Degenerate),
  57936. TEST_DECL(test_wc_PKCS7_BER),
  57937. TEST_DECL(test_PKCS7_signed_enveloped),
  57938. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  57939. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  57940. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  57941. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  57942. TEST_DECL(test_wc_i2d_PKCS12),
  57943. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  57944. TEST_DECL(test_openssl_FIPS_drbg),
  57945. TEST_DECL(test_wc_CryptoCb),
  57946. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  57947. TEST_DECL(test_wolfSSL_FIPS_mode),
  57948. #ifdef WOLFSSL_DTLS
  57949. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  57950. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  57951. #endif
  57952. #if !defined(NO_FILESYSTEM) && \
  57953. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57954. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  57955. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  57956. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  57957. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  57958. #ifdef HAVE_MAX_FRAGMENT
  57959. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  57960. #endif /* HAVE_MAX_FRAGMENT */
  57961. TEST_DECL(test_wolfSSL_dtls_stateless2),
  57962. #if !defined(NO_OLD_TLS)
  57963. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  57964. #endif /* !defined(NO_OLD_TLS) */
  57965. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57966. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  57967. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  57968. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  57969. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  57970. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  57971. TEST_DECL(test_ForceZero),
  57972. TEST_DECL(test_wolfSSL_Cleanup),
  57973. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  57974. && defined(WOLFSSL_TLS13) && \
  57975. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  57976. TEST_DECL(test_ticket_nonce_malloc),
  57977. #endif
  57978. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  57979. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  57980. !defined(WOLFSSL_NO_CLIENT_AUTH))
  57981. TEST_DECL(test_various_pathlen_chains),
  57982. #endif
  57983. TEST_DECL(test_ticket_ret_create),
  57984. TEST_DECL(test_extra_alerts_wrong_cs),
  57985. TEST_DECL(test_extra_alerts_skip_hs),
  57986. TEST_DECL(test_extra_alerts_bad_psk),
  57987. TEST_DECL(test_harden_no_secure_renegotiation),
  57988. TEST_DECL(test_override_alt_cert_chain),
  57989. /* If at some point a stub get implemented this test should fail indicating
  57990. * a need to implement a new test case
  57991. */
  57992. TEST_DECL(test_stubs_are_stubs)
  57993. };
  57994. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  57995. static void TestSetup(void)
  57996. {
  57997. /* Stub, for now. Add common test setup code here. */
  57998. }
  57999. static void TestCleanup(void)
  58000. {
  58001. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  58002. /* Clear any errors added to the error queue during the test run. */
  58003. wolfSSL_ERR_clear_error();
  58004. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  58005. }
  58006. /* Print out all API test cases with numeric identifier.
  58007. */
  58008. void ApiTest_PrintTestCases(void)
  58009. {
  58010. int i;
  58011. printf("All Test Cases:\n");
  58012. for (i = 0; i < TEST_CASE_CNT; i++) {
  58013. printf("%3d: %s\n", i + 1, testCases[i].name);
  58014. }
  58015. }
  58016. /* Add test case with index to the list to run.
  58017. *
  58018. * @param [in] idx Index of test case to run starting at 1.
  58019. * @return 0 on success.
  58020. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  58021. */
  58022. int ApiTest_RunIdx(int idx)
  58023. {
  58024. if (idx < 1 || idx > TEST_CASE_CNT) {
  58025. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  58026. return BAD_FUNC_ARG;
  58027. }
  58028. testAll = 0;
  58029. testCases[idx-1].run = 1;
  58030. return 0;
  58031. }
  58032. /* Add test case with name to the list to run.
  58033. *
  58034. * @param [in] name Name of test case to run.
  58035. * @return 0 on success.
  58036. * @return BAD_FUNC_ARG when name is not a known test case name.
  58037. */
  58038. int ApiTest_RunName(char* name)
  58039. {
  58040. int i;
  58041. for (i = 0; i < TEST_CASE_CNT; i++) {
  58042. if (XSTRCMP(testCases[i].name, name) == 0) {
  58043. testAll = 0;
  58044. testCases[i].run = 1;
  58045. return 0;
  58046. }
  58047. }
  58048. printf("Test case name not found: %s\n", name);
  58049. printf("Use --list to see all test case names.\n");
  58050. return BAD_FUNC_ARG;
  58051. }
  58052. /* Converts the result code to a string.
  58053. *
  58054. * @param [in] res Test result code.
  58055. * @return String describing test result.
  58056. */
  58057. static const char* apitest_res_string(int res)
  58058. {
  58059. const char* str = "invalid result";
  58060. switch (res) {
  58061. case TEST_SUCCESS:
  58062. str = "passed";
  58063. break;
  58064. case TEST_FAIL:
  58065. str = "failed";
  58066. break;
  58067. case TEST_SKIPPED:
  58068. str = "skipped";
  58069. break;
  58070. }
  58071. return str;
  58072. }
  58073. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58074. static double gettime_secs(void)
  58075. {
  58076. struct timeval tv;
  58077. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  58078. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  58079. }
  58080. #endif
  58081. void ApiTest(void)
  58082. {
  58083. int i;
  58084. int ret;
  58085. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58086. double timeDiff;
  58087. #endif
  58088. printf(" Begin API Tests\n");
  58089. fflush(stdout);
  58090. /* we must perform init and cleanup if not all tests are running */
  58091. if (!testAll) {
  58092. #ifdef WOLFCRYPT_ONLY
  58093. wolfCrypt_Init();
  58094. #else
  58095. wolfSSL_Init();
  58096. #endif
  58097. }
  58098. for (i = 0; i < TEST_CASE_CNT; ++i) {
  58099. /* When not testing all cases then skip if not marked for running. */
  58100. if (!testAll && !testCases[i].run) {
  58101. continue;
  58102. }
  58103. TestSetup();
  58104. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  58105. fflush(stdout);
  58106. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58107. timeDiff = gettime_secs();
  58108. #endif
  58109. ret = testCases[i].func();
  58110. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58111. timeDiff = gettime_secs() - timeDiff;
  58112. #endif
  58113. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58114. if (ret != TEST_SKIPPED) {
  58115. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  58116. }
  58117. else
  58118. #endif
  58119. {
  58120. printf(" %s\n", apitest_res_string(ret));
  58121. }
  58122. fflush(stdout);
  58123. /* if return code is < 0 and not skipped then assert error */
  58124. Assert((ret > 0 || ret == TEST_SKIPPED),
  58125. ("Test failed\n"),
  58126. ("ret %d", ret));
  58127. TestCleanup();
  58128. }
  58129. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  58130. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  58131. wc_ecc_fp_free(); /* free per thread cache */
  58132. #endif
  58133. if (!testAll) {
  58134. #ifdef WOLFCRYPT_ONLY
  58135. wolfCrypt_Cleanup();
  58136. #else
  58137. wolfSSL_Cleanup();
  58138. #endif
  58139. }
  58140. (void)testDevId;
  58141. printf(" End API Tests\n");
  58142. fflush(stdout);
  58143. }