api.c 2.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110
  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)
  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. | Setup
  421. *----------------------------------------------------------------------------*/
  422. static int test_wolfSSL_Init(void)
  423. {
  424. int result;
  425. result = wolfSSL_Init();
  426. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  427. return result;
  428. }
  429. static int test_wolfSSL_Cleanup(void)
  430. {
  431. int result;
  432. result = wolfSSL_Cleanup();
  433. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  434. return result;
  435. }
  436. /* Initialize the wolfCrypt state.
  437. * POST: 0 success.
  438. */
  439. static int test_wolfCrypt_Init(void)
  440. {
  441. int result;
  442. result = wolfCrypt_Init();
  443. result = TEST_RES_CHECK(result == 0);
  444. return result;
  445. } /* END test_wolfCrypt_Init */
  446. static int test_wolfCrypt_Cleanup(void)
  447. {
  448. int result;
  449. result = wolfCrypt_Cleanup();
  450. result = TEST_RES_CHECK(result == 0);
  451. return result;
  452. }
  453. /*----------------------------------------------------------------------------*
  454. | Platform dependent function test
  455. *----------------------------------------------------------------------------*/
  456. static int test_fileAccess(void)
  457. {
  458. int res = TEST_SKIPPED;
  459. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  460. const char *fname[] = {
  461. svrCertFile, svrKeyFile, caCertFile,
  462. eccCertFile, eccKeyFile, eccRsaCertFile,
  463. cliCertFile, cliCertDerFile, cliKeyFile,
  464. dhParamFile,
  465. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  466. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  467. NULL
  468. };
  469. const char derfile[] = "./certs/server-cert.der";
  470. XFILE f;
  471. size_t sz;
  472. byte *buff;
  473. int i;
  474. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  475. for (i=0; fname[i] != NULL ; i++) {
  476. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  477. XFCLOSE(f);
  478. }
  479. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  480. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  481. sz = (size_t) XFTELL(f);
  482. XREWIND(f);
  483. AssertTrue(sz == sizeof_server_cert_der_2048);
  484. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  485. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  486. XMEMCMP(server_cert_der_2048, buff, sz);
  487. res = TEST_RES_CHECK(1);
  488. #endif
  489. return res;
  490. }
  491. /*----------------------------------------------------------------------------*
  492. | Method Allocators
  493. *----------------------------------------------------------------------------*/
  494. static int test_wolfSSL_Method_Allocators(void)
  495. {
  496. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  497. do { \
  498. WOLFSSL_METHOD *method; \
  499. condition(method = allocator()); \
  500. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  501. } while(0)
  502. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  503. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  504. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  505. TEST_METHOD_ALLOCATOR(a, AssertNull)
  506. #ifndef NO_OLD_TLS
  507. #ifdef WOLFSSL_ALLOW_SSLV3
  508. #ifndef NO_WOLFSSL_SERVER
  509. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  510. #endif
  511. #ifndef NO_WOLFSSL_CLIENT
  512. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  513. #endif
  514. #endif
  515. #ifdef WOLFSSL_ALLOW_TLSV10
  516. #ifndef NO_WOLFSSL_SERVER
  517. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  518. #endif
  519. #ifndef NO_WOLFSSL_CLIENT
  520. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  521. #endif
  522. #endif
  523. #ifndef NO_WOLFSSL_SERVER
  524. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  525. #endif
  526. #ifndef NO_WOLFSSL_CLIENT
  527. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  528. #endif
  529. #endif /* !NO_OLD_TLS */
  530. #ifndef WOLFSSL_NO_TLS12
  531. #ifndef NO_WOLFSSL_SERVER
  532. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  533. #endif
  534. #ifndef NO_WOLFSSL_CLIENT
  535. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  536. #endif
  537. #endif /* !WOLFSSL_NO_TLS12 */
  538. #ifdef WOLFSSL_TLS13
  539. #ifndef NO_WOLFSSL_SERVER
  540. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  541. #endif
  542. #ifndef NO_WOLFSSL_CLIENT
  543. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  544. #endif
  545. #endif /* WOLFSSL_TLS13 */
  546. #ifndef NO_WOLFSSL_SERVER
  547. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  548. #endif
  549. #ifndef NO_WOLFSSL_CLIENT
  550. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  551. #endif
  552. #ifdef WOLFSSL_DTLS
  553. #ifndef NO_OLD_TLS
  554. #ifndef NO_WOLFSSL_SERVER
  555. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  556. #endif
  557. #ifndef NO_WOLFSSL_CLIENT
  558. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  559. #endif
  560. #endif
  561. #ifndef WOLFSSL_NO_TLS12
  562. #ifndef NO_WOLFSSL_SERVER
  563. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  564. #endif
  565. #ifndef NO_WOLFSSL_CLIENT
  566. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  567. #endif
  568. #endif
  569. #endif /* WOLFSSL_DTLS */
  570. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  571. /* Stubs */
  572. #ifndef NO_WOLFSSL_SERVER
  573. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  574. #endif
  575. #ifndef NO_WOLFSSL_CLIENT
  576. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  577. #endif
  578. #endif
  579. /* Test Either Method (client or server) */
  580. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  581. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  582. #ifndef NO_OLD_TLS
  583. #ifdef WOLFSSL_ALLOW_TLSV10
  584. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  585. #endif
  586. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  587. #endif /* !NO_OLD_TLS */
  588. #ifndef WOLFSSL_NO_TLS12
  589. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  590. #endif /* !WOLFSSL_NO_TLS12 */
  591. #ifdef WOLFSSL_TLS13
  592. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  593. #endif /* WOLFSSL_TLS13 */
  594. #ifdef WOLFSSL_DTLS
  595. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  596. #ifndef NO_OLD_TLS
  597. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  598. #endif /* !NO_OLD_TLS */
  599. #ifndef WOLFSSL_NO_TLS12
  600. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  601. #endif /* !WOLFSSL_NO_TLS12 */
  602. #endif /* WOLFSSL_DTLS */
  603. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  604. return TEST_SUCCESS;
  605. }
  606. /*----------------------------------------------------------------------------*
  607. | Context
  608. *----------------------------------------------------------------------------*/
  609. #ifndef NO_WOLFSSL_SERVER
  610. static int test_wolfSSL_CTX_new(void)
  611. {
  612. WOLFSSL_CTX *ctx;
  613. WOLFSSL_METHOD* method;
  614. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  615. AssertNotNull(method = wolfSSLv23_server_method());
  616. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  617. wolfSSL_CTX_free(ctx);
  618. return TEST_RES_CHECK(1);
  619. }
  620. #endif
  621. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  622. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  623. static int test_for_double_Free(void)
  624. {
  625. WOLFSSL_CTX* ctx;
  626. WOLFSSL* ssl;
  627. int skipTest = 0;
  628. const char* testCertFile;
  629. const char* testKeyFile;
  630. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  631. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  632. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  633. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  634. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  635. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  636. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  637. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  638. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  639. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  640. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  641. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  642. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  643. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  644. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  645. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  646. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  647. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  648. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  649. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  650. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  651. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  652. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  653. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  654. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  655. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  656. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  657. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  658. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  659. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  660. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  661. #ifdef OPENSSL_EXTRA
  662. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  663. "!SRP:!kRSA:!aNULL:!eNULL";
  664. #endif
  665. #ifndef NO_RSA
  666. testCertFile = svrCertFile;
  667. testKeyFile = svrKeyFile;
  668. #elif defined(HAVE_ECC)
  669. testCertFile = eccCertFile;
  670. testKeyFile = eccKeyFile;
  671. #else
  672. skipTest = 1;
  673. #endif
  674. if (skipTest != 1) {
  675. #ifndef NO_WOLFSSL_SERVER
  676. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  677. AssertNotNull(ctx);
  678. #else
  679. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  680. AssertNotNull(ctx);
  681. #endif
  682. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  683. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  684. ssl = wolfSSL_new(ctx);
  685. AssertNotNull(ssl);
  686. /* First test freeing SSL, then CTX */
  687. wolfSSL_free(ssl);
  688. wolfSSL_CTX_free(ctx);
  689. #ifndef NO_WOLFSSL_CLIENT
  690. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  691. AssertNotNull(ctx);
  692. #else
  693. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  694. AssertNotNull(ctx);
  695. #endif
  696. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  697. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  698. ssl = wolfSSL_new(ctx);
  699. AssertNotNull(ssl);
  700. /* Next test freeing CTX then SSL */
  701. wolfSSL_CTX_free(ctx);
  702. wolfSSL_free(ssl);
  703. #ifndef NO_WOLFSSL_SERVER
  704. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  705. AssertNotNull(ctx);
  706. #else
  707. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  708. AssertNotNull(ctx);
  709. #endif
  710. /* Test setting ciphers at ctx level */
  711. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  712. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  713. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  714. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  715. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  716. /* only update TLSv13 suites */
  717. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  718. #endif
  719. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  720. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  721. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  722. /* only update pre-TLSv13 suites */
  723. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  724. #endif
  725. #ifdef OPENSSL_EXTRA
  726. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  727. #endif
  728. AssertNotNull(ssl = wolfSSL_new(ctx));
  729. wolfSSL_CTX_free(ctx);
  730. wolfSSL_free(ssl);
  731. #ifndef NO_WOLFSSL_CLIENT
  732. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  733. AssertNotNull(ctx);
  734. #else
  735. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  736. AssertNotNull(ctx);
  737. #endif
  738. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  739. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  740. ssl = wolfSSL_new(ctx);
  741. AssertNotNull(ssl);
  742. /* test setting ciphers at SSL level */
  743. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  744. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  745. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  746. /* only update TLSv13 suites */
  747. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  748. #endif
  749. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  750. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  751. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  752. /* only update pre-TLSv13 suites */
  753. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  754. #endif
  755. wolfSSL_CTX_free(ctx);
  756. wolfSSL_free(ssl);
  757. }
  758. return TEST_RES_CHECK(1);
  759. }
  760. #endif
  761. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  762. {
  763. int res = TEST_SKIPPED;
  764. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  765. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  766. (!defined(NO_RSA) || defined(HAVE_ECC))
  767. const char* testCertFile;
  768. const char* testKeyFile;
  769. WOLFSSL_CTX* ctx;
  770. WOLFSSL* ssl;
  771. const byte cipherList[] =
  772. {
  773. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  774. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  775. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  776. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  777. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  778. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  779. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  780. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  781. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  782. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  783. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  784. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  785. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  786. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  787. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  788. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  789. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  790. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  791. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  792. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  793. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  794. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  795. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  796. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  797. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  798. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  799. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  800. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  801. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  802. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  803. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  804. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  805. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  806. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  807. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  808. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  809. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  810. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  811. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  812. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  813. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  814. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  815. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  816. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  817. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  818. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  819. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  820. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  821. /* SHA256 */
  822. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  823. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  824. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  825. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  826. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  827. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  828. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  829. /* SHA384 */
  830. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  831. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  832. /* AES-GCM */
  833. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  834. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  835. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  836. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  837. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  838. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  839. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  840. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  841. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  842. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  843. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  844. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  845. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  846. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  847. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  848. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  849. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  850. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  851. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  852. * also, in some of the other AES-CCM suites
  853. * there will be second byte number conflicts
  854. * with non-ECC AES-GCM */
  855. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  856. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  857. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  858. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  859. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  860. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  861. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  862. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  863. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  864. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  865. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  866. /* Camellia */
  867. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  868. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  869. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  870. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  871. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  872. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  873. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  874. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  875. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  876. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  877. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  878. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  879. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  880. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  881. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  882. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  883. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  884. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  885. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  886. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  887. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  888. /* TLS v1.3 cipher suites */
  889. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  890. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  891. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  892. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  893. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  894. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  895. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  896. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  897. };
  898. #ifndef NO_RSA
  899. testCertFile = svrCertFile;
  900. testKeyFile = svrKeyFile;
  901. #elif defined(HAVE_ECC)
  902. testCertFile = eccCertFile;
  903. testKeyFile = eccKeyFile;
  904. #endif
  905. #ifndef NO_WOLFSSL_SERVER
  906. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  907. AssertNotNull(ctx);
  908. #else
  909. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  910. AssertNotNull(ctx);
  911. #endif
  912. AssertTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  913. sizeof(cipherList)));
  914. wolfSSL_CTX_free(ctx);
  915. #ifndef NO_WOLFSSL_SERVER
  916. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  917. AssertNotNull(ctx);
  918. #else
  919. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  920. AssertNotNull(ctx);
  921. #endif
  922. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  923. WOLFSSL_FILETYPE_PEM));
  924. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  925. WOLFSSL_FILETYPE_PEM));
  926. ssl = wolfSSL_new(ctx);
  927. AssertNotNull(ssl);
  928. AssertTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  929. sizeof(cipherList)));
  930. wolfSSL_free(ssl);
  931. wolfSSL_CTX_free(ctx);
  932. res = TEST_RES_CHECK(1);
  933. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  934. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  935. return res;
  936. }
  937. static int test_wolfSSL_CTX_use_certificate_file(void)
  938. {
  939. int res = TEST_SKIPPED;
  940. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  941. WOLFSSL_CTX *ctx;
  942. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  943. /* invalid context */
  944. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  945. WOLFSSL_FILETYPE_PEM));
  946. /* invalid cert file */
  947. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  948. WOLFSSL_FILETYPE_PEM));
  949. /* invalid cert type */
  950. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  951. #ifdef NO_RSA
  952. /* rsa needed */
  953. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  954. #else
  955. /* success */
  956. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  957. #endif
  958. wolfSSL_CTX_free(ctx);
  959. res = TEST_RES_CHECK(1);
  960. #endif
  961. return res;
  962. }
  963. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  964. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  965. {
  966. int res = TEST_SKIPPED;
  967. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  968. WOLFSSL_CTX* ctx;
  969. int ret;
  970. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  971. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  972. server_cert_der_2048);
  973. wolfSSL_CTX_free(ctx);
  974. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  975. #endif
  976. return res;
  977. }
  978. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  979. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  980. * context using buffer.
  981. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  982. * --enable-testcert flag.
  983. */
  984. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  985. {
  986. int res = TEST_SKIPPED;
  987. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  988. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  989. WOLFSSL_CTX* ctx;
  990. int ret;
  991. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  992. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  993. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  994. wolfSSL_CTX_free(ctx);
  995. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  996. #endif
  997. return res;
  998. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  999. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1000. {
  1001. int res = TEST_SKIPPED;
  1002. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1003. WOLFSSL_CTX *ctx;
  1004. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1005. /* invalid context */
  1006. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1007. WOLFSSL_FILETYPE_PEM));
  1008. /* invalid key file */
  1009. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1010. WOLFSSL_FILETYPE_PEM));
  1011. /* invalid key type */
  1012. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1013. /* success */
  1014. #ifdef NO_RSA
  1015. /* rsa needed */
  1016. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1017. #else
  1018. /* success */
  1019. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1020. #endif
  1021. wolfSSL_CTX_free(ctx);
  1022. res = TEST_RES_CHECK(1);
  1023. #endif
  1024. return res;
  1025. }
  1026. /* test both file and buffer versions along with unloading trusted peer certs */
  1027. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1028. {
  1029. int res = TEST_SKIPPED;
  1030. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1031. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1032. WOLFSSL_CTX *ctx;
  1033. WOLFSSL* ssl;
  1034. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1035. AssertNotNull(ssl = wolfSSL_new(ctx));
  1036. #if !defined(NO_FILESYSTEM)
  1037. /* invalid file */
  1038. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1039. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1040. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1041. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1042. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1043. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1044. /* success */
  1045. AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1046. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1047. /* unload cert */
  1048. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1049. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1050. /* invalid file */
  1051. AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1052. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1053. AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1054. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1055. AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1056. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1057. /* success */
  1058. AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1059. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1060. #ifdef WOLFSSL_LOCAL_X509_STORE
  1061. /* unload cert */
  1062. AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1063. AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1064. #endif
  1065. #endif
  1066. /* Test of loading certs from buffers */
  1067. /* invalid buffer */
  1068. AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1069. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1070. /* success */
  1071. #ifdef USE_CERT_BUFFERS_1024
  1072. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1073. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1074. #endif
  1075. #ifdef USE_CERT_BUFFERS_2048
  1076. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1077. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1078. #endif
  1079. /* unload cert */
  1080. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1081. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1082. wolfSSL_free(ssl);
  1083. wolfSSL_CTX_free(ctx);
  1084. res = TEST_RES_CHECK(1);
  1085. #endif
  1086. return res;
  1087. }
  1088. static int test_wolfSSL_CTX_load_verify_locations(void)
  1089. {
  1090. int res = TEST_SKIPPED;
  1091. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1092. WOLFSSL_CTX *ctx;
  1093. #ifndef NO_RSA
  1094. WOLFSSL_CERT_MANAGER* cm;
  1095. #ifdef PERSIST_CERT_CACHE
  1096. int cacheSz;
  1097. #endif
  1098. #endif
  1099. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1100. const char* load_certs_path = "./certs/external";
  1101. const char* load_no_certs_path = "./examples";
  1102. const char* load_expired_path = "./certs/test/expired";
  1103. #endif
  1104. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1105. /* invalid arguments */
  1106. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  1107. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  1108. /* invalid ca file */
  1109. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1110. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1111. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1112. (defined(WOLFSSL_QT) && \
  1113. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1114. /* invalid path */
  1115. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1116. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1117. #endif
  1118. /* load ca cert */
  1119. #ifdef NO_RSA
  1120. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1121. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1122. #else /* Skip the following test without RSA certs. */
  1123. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1124. #ifdef PERSIST_CERT_CACHE
  1125. /* Get cert cache size */
  1126. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  1127. #endif
  1128. /* Test unloading CA's */
  1129. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1130. #ifdef PERSIST_CERT_CACHE
  1131. /* Verify no certs (result is less than cacheSz) */
  1132. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1133. #endif
  1134. /* load ca cert again */
  1135. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1136. /* Test getting CERT_MANAGER */
  1137. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1138. /* Test unloading CA's using CM */
  1139. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1140. #ifdef PERSIST_CERT_CACHE
  1141. /* Verify no certs (result is less than cacheSz) */
  1142. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1143. #endif
  1144. #endif
  1145. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1146. /* Test loading CA certificates using a path */
  1147. #ifdef NO_RSA
  1148. /* failure here okay since certs in external directory are RSA */
  1149. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1150. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1151. #else
  1152. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1153. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1154. #endif
  1155. /* Test loading path with no files */
  1156. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  1157. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1158. /* Test loading expired CA certificates */
  1159. #ifdef NO_RSA
  1160. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1161. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1162. WOLFSSL_SUCCESS);
  1163. #else
  1164. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1165. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1166. WOLFSSL_SUCCESS);
  1167. #endif
  1168. /* Test loading CA certificates and ignoring all errors */
  1169. #ifdef NO_RSA
  1170. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1171. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1172. #else
  1173. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1174. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1175. #endif
  1176. #endif
  1177. wolfSSL_CTX_free(ctx);
  1178. res = TEST_RES_CHECK(1);
  1179. #endif
  1180. return res;
  1181. }
  1182. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1183. {
  1184. int res = TEST_SKIPPED;
  1185. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1186. (!defined(NO_RSA) || defined(HAVE_ECC))
  1187. WOLFSSL_CTX* ctx;
  1188. byte dirValid = 0;
  1189. int ret = 0;
  1190. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1191. if (ctx == NULL) {
  1192. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1193. ret = -1;
  1194. }
  1195. if (ret == 0) {
  1196. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1197. dirValid = 1;
  1198. #else
  1199. word32 numDirs;
  1200. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1201. if (caDirs == NULL || numDirs == 0) {
  1202. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1203. ret = -1;
  1204. }
  1205. else {
  1206. ReadDirCtx dirCtx;
  1207. word32 i;
  1208. for (i = 0; i < numDirs; ++i) {
  1209. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1210. /* Directory isn't empty. */
  1211. dirValid = 1;
  1212. wc_ReadDirClose(&dirCtx);
  1213. break;
  1214. }
  1215. }
  1216. }
  1217. #endif
  1218. }
  1219. /*
  1220. * If the directory isn't empty, we should be able to load CA
  1221. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1222. * usable.
  1223. */
  1224. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1225. WOLFSSL_SUCCESS) {
  1226. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1227. ret = -1;
  1228. }
  1229. #ifdef OPENSSL_EXTRA
  1230. if (ret == 0 &&
  1231. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1232. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1233. ret = -1;
  1234. }
  1235. #endif /* OPENSSL_EXTRA */
  1236. wolfSSL_CTX_free(ctx);
  1237. res = TEST_RES_CHECK(ret == 0);
  1238. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1239. return res;
  1240. }
  1241. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1242. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  1243. {
  1244. int ret;
  1245. WOLFSSL_CERT_MANAGER* cm;
  1246. cm = wolfSSL_CertManagerNew();
  1247. if (cm == NULL) {
  1248. fprintf(stderr, "test_cm_load_ca failed\n");
  1249. return -1;
  1250. }
  1251. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1252. wolfSSL_CertManagerFree(cm);
  1253. return ret;
  1254. }
  1255. static int test_cm_load_ca_file(const char* ca_cert_file)
  1256. {
  1257. int ret = 0;
  1258. byte* cert_buf = NULL;
  1259. size_t cert_sz = 0;
  1260. #if defined(WOLFSSL_PEM_TO_DER)
  1261. DerBuffer* pDer = NULL;
  1262. #endif
  1263. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1264. if (ret == 0) {
  1265. /* normal test */
  1266. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1267. if (ret == WOLFSSL_SUCCESS) {
  1268. /* test including null terminator in length */
  1269. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1270. if (tmp == NULL) {
  1271. ret = MEMORY_E;
  1272. }
  1273. else {
  1274. cert_buf = tmp;
  1275. cert_buf[cert_sz] = '\0';
  1276. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1277. WOLFSSL_FILETYPE_PEM);
  1278. }
  1279. }
  1280. #if defined(WOLFSSL_PEM_TO_DER)
  1281. if (ret == WOLFSSL_SUCCESS) {
  1282. /* test loading DER */
  1283. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1284. if (ret == 0 && pDer != NULL) {
  1285. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1286. WOLFSSL_FILETYPE_ASN1);
  1287. wc_FreeDer(&pDer);
  1288. }
  1289. }
  1290. #endif
  1291. }
  1292. free(cert_buf);
  1293. return ret;
  1294. }
  1295. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1296. int file_type, word32 flags)
  1297. {
  1298. int ret;
  1299. WOLFSSL_CERT_MANAGER* cm;
  1300. cm = wolfSSL_CertManagerNew();
  1301. if (cm == NULL) {
  1302. fprintf(stderr, "test_cm_load_ca failed\n");
  1303. return -1;
  1304. }
  1305. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1306. 0, flags);
  1307. wolfSSL_CertManagerFree(cm);
  1308. return ret;
  1309. }
  1310. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1311. {
  1312. int ret = 0;
  1313. byte* cert_buf = NULL;
  1314. size_t cert_sz = 0;
  1315. #if defined(WOLFSSL_PEM_TO_DER)
  1316. DerBuffer* pDer = NULL;
  1317. #endif
  1318. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1319. if (ret == 0) {
  1320. /* normal test */
  1321. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1322. WOLFSSL_FILETYPE_PEM, flags);
  1323. if (ret == WOLFSSL_SUCCESS) {
  1324. /* test including null terminator in length */
  1325. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1326. if (tmp == NULL) {
  1327. ret = MEMORY_E;
  1328. }
  1329. else {
  1330. cert_buf = tmp;
  1331. cert_buf[cert_sz] = '\0';
  1332. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1333. WOLFSSL_FILETYPE_PEM, flags);
  1334. }
  1335. }
  1336. #if defined(WOLFSSL_PEM_TO_DER)
  1337. if (ret == WOLFSSL_SUCCESS) {
  1338. /* test loading DER */
  1339. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1340. if (ret == 0 && pDer != NULL) {
  1341. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1342. WOLFSSL_FILETYPE_ASN1, flags);
  1343. wc_FreeDer(&pDer);
  1344. }
  1345. }
  1346. #endif
  1347. }
  1348. free(cert_buf);
  1349. return ret;
  1350. }
  1351. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1352. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1353. {
  1354. int res = TEST_SKIPPED;
  1355. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1356. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1357. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1358. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1359. defined(HAVE_LIGHTY)
  1360. WOLFSSL_CERT_MANAGER* cm = NULL;
  1361. /* Raw OCSP response bytes captured using the following setup:
  1362. * - Run responder with
  1363. * openssl ocsp -port 9999 -ndays 9999
  1364. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1365. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1366. * -rkey certs/ocsp/ocsp-responder-key.pem
  1367. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1368. * - Run client with
  1369. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1370. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1371. * -cert certs/ocsp/server1-cert.pem
  1372. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1373. * - Copy raw response from Wireshark.
  1374. */
  1375. byte response[] = {
  1376. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1377. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1378. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1379. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1380. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1381. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1382. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1383. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1384. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1385. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1386. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1387. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1388. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1389. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1390. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1391. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1392. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1393. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1394. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1395. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1396. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1397. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1398. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1399. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1400. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1401. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1402. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1403. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1404. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1405. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1406. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1407. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1408. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1409. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1410. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1411. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1412. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1413. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1414. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1415. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1416. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1417. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1418. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1419. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1420. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1421. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1422. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1423. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1424. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1425. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1426. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1427. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1428. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1429. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1430. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1431. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1432. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1433. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1434. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1435. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1436. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1437. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1438. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1439. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1440. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1441. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1442. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1443. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1444. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1445. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1446. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1447. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1448. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1449. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1450. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1451. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1452. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1453. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1454. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1455. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1456. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1457. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1458. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1459. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1460. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1461. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1462. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1463. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1464. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1465. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1466. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1467. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1468. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1469. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1470. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1471. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1472. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1473. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1474. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1475. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1476. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1477. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1478. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1479. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1480. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1481. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1482. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1483. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1484. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1485. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1486. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1487. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1488. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1489. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1490. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1491. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1492. 0x59, 0x59, 0xa0, 0x07
  1493. };
  1494. OcspEntry entry[1];
  1495. CertStatus status[1];
  1496. OcspRequest* request;
  1497. byte serial[] = {0x05};
  1498. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1499. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1500. XMEMSET(entry, 0, sizeof(OcspEntry));
  1501. XMEMSET(status, 0, sizeof(CertStatus));
  1502. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1503. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1504. DYNAMIC_TYPE_OCSP_REQUEST);
  1505. AssertNotNull(request->serial);
  1506. request->serialSz = sizeof(serial);
  1507. XMEMCPY(request->serial, serial, sizeof(serial));
  1508. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1509. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1510. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1511. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1512. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1513. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1514. /* Response should be valid. */
  1515. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1516. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1517. /* Flip a byte in the request serial number, response should be invalid
  1518. * now. */
  1519. request->serial[0] ^= request->serial[0];
  1520. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1521. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1522. wolfSSL_OCSP_REQUEST_free(request);
  1523. wolfSSL_CertManagerFree(cm);
  1524. res = TEST_RES_CHECK(1);
  1525. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1526. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1527. #endif /* HAVE_OCSP */
  1528. return res;
  1529. }
  1530. static int test_wolfSSL_CheckOCSPResponse(void)
  1531. {
  1532. int result = TEST_SKIPPED;
  1533. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  1534. const char* responseFile = "./certs/ocsp/test-response.der";
  1535. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  1536. const char* responseNoInternFile = "./certs/ocsp/test-response-nointern.der";
  1537. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  1538. OcspResponse* res = NULL;
  1539. byte data[4096];
  1540. const unsigned char* pt;
  1541. int dataSz;
  1542. XFILE f;
  1543. WOLFSSL_OCSP_BASICRESP* bs;
  1544. WOLFSSL_X509_STORE* st;
  1545. WOLFSSL_X509* issuer;
  1546. f = XFOPEN(responseFile, "rb");
  1547. AssertTrue(f != XBADFILE);
  1548. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1549. AssertIntGT(dataSz, 0);
  1550. XFCLOSE(f);
  1551. pt = data;
  1552. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1553. AssertNotNull(res);
  1554. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1555. AssertNotNull(issuer);
  1556. st = wolfSSL_X509_STORE_new();
  1557. AssertNotNull(st);
  1558. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1559. bs = wolfSSL_OCSP_response_get1_basic(res);
  1560. AssertNotNull(bs);
  1561. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1562. wolfSSL_OCSP_BASICRESP_free(bs);
  1563. wolfSSL_OCSP_RESPONSE_free(res);
  1564. wolfSSL_X509_STORE_free(st);
  1565. wolfSSL_X509_free(issuer);
  1566. /* check loading a response with optional certs */
  1567. f = XFOPEN(responseNoInternFile, "rb");
  1568. AssertTrue(f != XBADFILE);
  1569. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1570. AssertIntGT(dataSz, 0);
  1571. XFCLOSE(f);
  1572. pt = data;
  1573. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1574. AssertNotNull(res);
  1575. wolfSSL_OCSP_RESPONSE_free(res);
  1576. /* check loading a response with multiple certs */
  1577. {
  1578. WOLFSSL_CERT_MANAGER* cm = NULL;
  1579. OcspEntry *entry;
  1580. CertStatus* status;
  1581. OcspRequest* request;
  1582. byte serial1[] = {0x01};
  1583. byte serial[] = {0x02};
  1584. byte issuerHash[] = {
  1585. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  1586. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  1587. 0x37, 0x15, 0x8A, 0x88
  1588. };
  1589. byte issuerKeyHash[] = {
  1590. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  1591. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  1592. 0x7E, 0x72, 0x15, 0x21
  1593. };
  1594. entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  1595. DYNAMIC_TYPE_OPENSSL);
  1596. AssertNotNull(entry);
  1597. status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  1598. DYNAMIC_TYPE_OPENSSL);
  1599. AssertNotNull(status);
  1600. XMEMSET(entry, 0, sizeof(OcspEntry));
  1601. XMEMSET(status, 0, sizeof(CertStatus));
  1602. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1603. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1604. DYNAMIC_TYPE_OCSP_REQUEST);
  1605. AssertNotNull(request->serial);
  1606. request->serialSz = sizeof(serial);
  1607. XMEMCPY(request->serial, serial, sizeof(serial));
  1608. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1609. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1610. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1611. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1612. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  1613. WOLFSSL_SUCCESS);
  1614. f = XFOPEN(responseMultiFile, "rb");
  1615. AssertTrue(f != XBADFILE);
  1616. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1617. AssertIntGT(dataSz, 0);
  1618. XFCLOSE(f);
  1619. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1620. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1621. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1622. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1623. AssertNotNull(entry->status);
  1624. XMEMCPY(request->serial, serial1, sizeof(serial1));
  1625. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1626. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1627. /* store both status's in the entry to check that "next" is not
  1628. * overwritten */
  1629. status->next = entry->status;
  1630. entry->status = status;
  1631. XMEMCPY(request->serial, serial, sizeof(serial));
  1632. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1633. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1634. AssertNotNull(entry->status->next);
  1635. /* compare the status found */
  1636. AssertIntEQ(status->serialSz, entry->status->serialSz);
  1637. AssertIntEQ(XMEMCMP(status->serial, entry->status->serial,
  1638. status->serialSz), 0);
  1639. wolfSSL_OCSP_CERTID_free(entry);
  1640. wolfSSL_OCSP_REQUEST_free(request);
  1641. wolfSSL_CertManagerFree(cm);
  1642. }
  1643. #if defined(WC_RSA_PSS)
  1644. {
  1645. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  1646. /* check loading a response with RSA-PSS signature */
  1647. f = XFOPEN(responsePssFile, "rb");
  1648. AssertTrue(f != XBADFILE);
  1649. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1650. AssertIntGT(dataSz, 0);
  1651. XFCLOSE(f);
  1652. pt = data;
  1653. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1654. AssertNotNull(res);
  1655. /* try to verify the response */
  1656. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1657. AssertNotNull(issuer);
  1658. st = wolfSSL_X509_STORE_new();
  1659. AssertNotNull(st);
  1660. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1661. bs = wolfSSL_OCSP_response_get1_basic(res);
  1662. AssertNotNull(bs);
  1663. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1664. wolfSSL_OCSP_BASICRESP_free(bs);
  1665. wolfSSL_OCSP_RESPONSE_free(res);
  1666. wolfSSL_X509_STORE_free(st);
  1667. wolfSSL_X509_free(issuer);
  1668. }
  1669. #endif
  1670. result = TEST_RES_CHECK(1);
  1671. #endif /* HAVE_OCSP */
  1672. return result;
  1673. }
  1674. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1675. {
  1676. int res = TEST_SKIPPED;
  1677. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1678. const char* ca_cert = "./certs/ca-cert.pem";
  1679. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1680. int ret;
  1681. ret = test_cm_load_ca_file(ca_cert);
  1682. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1683. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1684. #elif defined(NO_RSA)
  1685. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1686. #else
  1687. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1688. #endif
  1689. ret = test_cm_load_ca_file(ca_expired_cert);
  1690. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1691. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1692. res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
  1693. #elif defined(NO_RSA)
  1694. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1695. res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
  1696. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1697. !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  1698. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1699. res = TEST_RES_CHECK(ret == ASN_AFTER_DATE_E);
  1700. #else
  1701. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1702. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1703. #endif
  1704. #endif
  1705. return res;
  1706. }
  1707. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1708. {
  1709. int res = TEST_SKIPPED;
  1710. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1711. const char* ca_cert = "./certs/ca-cert.pem";
  1712. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1713. int ret;
  1714. ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE);
  1715. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1716. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1717. #elif defined(NO_RSA)
  1718. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1719. #else
  1720. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1721. #endif
  1722. ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1723. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY);
  1724. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1725. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1726. res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
  1727. #elif defined(NO_RSA)
  1728. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1729. res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
  1730. #else
  1731. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1732. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1733. #endif
  1734. #endif
  1735. return res;
  1736. }
  1737. static int test_wolfSSL_CertManagerGetCerts(void)
  1738. {
  1739. int res = TEST_SKIPPED;
  1740. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1741. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1742. defined(WOLFSSL_SIGNER_DER_CERT)
  1743. WOLFSSL_CERT_MANAGER* cm = NULL;
  1744. WOLFSSL_STACK* sk = NULL;
  1745. X509* x509 = NULL;
  1746. X509* cert1 = NULL;
  1747. FILE* file1 = NULL;
  1748. #ifdef DEBUG_WOLFSSL_VERBOSE
  1749. WOLFSSL_BIO* bio = NULL;
  1750. #endif
  1751. int i = 0;
  1752. int ret = 0;
  1753. const byte* der;
  1754. int derSz = 0;
  1755. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1756. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1757. fclose(file1);
  1758. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1759. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1760. AssertNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1761. ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz, WOLFSSL_FILETYPE_ASN1);
  1762. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1763. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1764. * and full OpenSSL compatibility */
  1765. AssertIntEQ(ret, ASN_SELF_SIGNED_E);
  1766. #else
  1767. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  1768. #endif
  1769. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1770. "./certs/ca-cert.pem", NULL));
  1771. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1772. for (i = 0; i < sk_X509_num(sk); i++) {
  1773. x509 = sk_X509_value(sk, i);
  1774. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1775. #ifdef DEBUG_WOLFSSL_VERBOSE
  1776. bio = BIO_new(wolfSSL_BIO_s_file());
  1777. if (bio != NULL) {
  1778. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1779. X509_print(bio, x509);
  1780. BIO_free(bio);
  1781. }
  1782. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1783. }
  1784. wolfSSL_X509_free(cert1);
  1785. sk_X509_pop_free(sk, NULL);
  1786. wolfSSL_CertManagerFree(cm);
  1787. res = TEST_RES_CHECK(1);
  1788. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1789. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1790. defined(WOLFSSL_SIGNER_DER_CERT) */
  1791. return res;
  1792. }
  1793. static int test_wolfSSL_CertManagerSetVerify(void)
  1794. {
  1795. int res = TEST_SKIPPED;
  1796. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1797. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1798. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1799. int ret = 0;
  1800. WOLFSSL_CERT_MANAGER* cm;
  1801. int tmp = myVerifyAction;
  1802. const char* ca_cert = "./certs/ca-cert.pem";
  1803. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1804. cm = wolfSSL_CertManagerNew();
  1805. AssertNotNull(cm);
  1806. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1807. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1808. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1809. AssertIntEQ(ret, -1);
  1810. #else
  1811. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1812. #endif
  1813. /* Use the test CB that always accepts certs */
  1814. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1815. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1816. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1817. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1818. {
  1819. const char* verifyCert = "./certs/server-cert.pem";
  1820. /* Use the test CB that always fails certs */
  1821. myVerifyAction = VERIFY_FORCE_FAIL;
  1822. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1823. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1824. }
  1825. #endif
  1826. wolfSSL_CertManagerFree(cm);
  1827. myVerifyAction = tmp;
  1828. res = TEST_RES_CHECK(1);
  1829. #endif
  1830. return res;
  1831. }
  1832. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1833. defined(DEBUG_UNIT_TEST_CERTS)
  1834. /* Used when debugging name constraint tests. Not static to allow use in
  1835. * multiple locations with complex define guards. */
  1836. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1837. {
  1838. BIO* out = BIO_new_file(fileName, "wb");
  1839. if (out != NULL) {
  1840. PEM_write_bio_X509(out, x509);
  1841. BIO_free(out);
  1842. }
  1843. }
  1844. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1845. {
  1846. BIO* out = BIO_new_file(fileName, "wb");
  1847. if (out != NULL) {
  1848. BIO_write(out, der, derSz);
  1849. BIO_free(out);
  1850. }
  1851. }
  1852. #else
  1853. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1854. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1855. #endif
  1856. static int test_wolfSSL_CertManagerNameConstraint(void)
  1857. {
  1858. int res = TEST_SKIPPED;
  1859. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1860. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1861. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1862. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1863. !defined(NO_SHA256)
  1864. WOLFSSL_CERT_MANAGER* cm;
  1865. WOLFSSL_EVP_PKEY *priv;
  1866. WOLFSSL_X509_NAME* name;
  1867. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1868. const char* server_cert = "./certs/test/server-goodcn.pem";
  1869. int i = 0;
  1870. static const byte extNameConsOid[] = {85, 29, 30};
  1871. RsaKey key;
  1872. WC_RNG rng;
  1873. byte *der;
  1874. int derSz;
  1875. word32 idx = 0;
  1876. byte *pt;
  1877. WOLFSSL_X509 *x509, *ca;
  1878. wc_InitRng(&rng);
  1879. /* load in CA private key for signing */
  1880. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1881. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1882. sizeof_server_key_der_2048), 0);
  1883. /* get ca certificate then alter it */
  1884. AssertNotNull(der =
  1885. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1886. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1887. WOLFSSL_FILETYPE_ASN1));
  1888. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1889. XMEMCPY(der, pt, derSz);
  1890. /* find the name constraint extension and alter it */
  1891. pt = der;
  1892. for (i = 0; i < derSz - 3; i++) {
  1893. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1894. pt += 3;
  1895. break;
  1896. }
  1897. pt++;
  1898. }
  1899. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1900. /* go to the length value and set it to 0 */
  1901. while (i < derSz && *pt != 0x81) {
  1902. pt++;
  1903. i++;
  1904. }
  1905. AssertIntNE(i, derSz); /* did not place to alter */
  1906. pt++;
  1907. *pt = 0x00;
  1908. /* resign the altered certificate */
  1909. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1910. FOURK_BUF, &key, NULL, &rng)), 0);
  1911. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1912. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1913. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1914. wolfSSL_CertManagerFree(cm);
  1915. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1916. wolfSSL_X509_free(x509);
  1917. wc_FreeRsaKey(&key);
  1918. wc_FreeRng(&rng);
  1919. /* add email alt name to satisfy constraint */
  1920. pt = (byte*)server_key_der_2048;
  1921. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1922. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1923. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1924. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1925. WOLFSSL_FILETYPE_ASN1));
  1926. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1927. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1928. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1929. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1930. /* Good cert test with proper alt email name */
  1931. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1932. WOLFSSL_FILETYPE_PEM));
  1933. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1934. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1935. AssertNotNull(name = X509_NAME_new());
  1936. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1937. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1938. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1939. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1940. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1941. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1942. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1943. X509_NAME_free(name);
  1944. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1945. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1946. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1947. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1948. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1949. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1950. wolfSSL_X509_free(x509);
  1951. /* Cert with bad alt name list */
  1952. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1953. WOLFSSL_FILETYPE_PEM));
  1954. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1955. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1956. AssertNotNull(name = X509_NAME_new());
  1957. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1958. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1959. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1960. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1961. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1962. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1963. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1964. X509_NAME_free(name);
  1965. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1966. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1967. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1968. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1969. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1970. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1971. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1972. wolfSSL_CertManagerFree(cm);
  1973. wolfSSL_X509_free(x509);
  1974. wolfSSL_X509_free(ca);
  1975. wolfSSL_EVP_PKEY_free(priv);
  1976. res = TEST_RES_CHECK(1);
  1977. #endif
  1978. return res;
  1979. }
  1980. static int test_wolfSSL_CertManagerNameConstraint2(void)
  1981. {
  1982. int res = TEST_SKIPPED;
  1983. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1984. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1985. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1986. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1987. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1988. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1989. const char* server_cert = "./certs/server-cert.pem";
  1990. WOLFSSL_CERT_MANAGER* cm;
  1991. WOLFSSL_X509 *x509, *ca;
  1992. const unsigned char *der;
  1993. const unsigned char *pt;
  1994. WOLFSSL_EVP_PKEY *priv;
  1995. WOLFSSL_X509_NAME* name;
  1996. int derSz;
  1997. /* C=US*/
  1998. char altName[] = {
  1999. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2000. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2001. };
  2002. /* C=ID */
  2003. char altNameFail[] = {
  2004. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2005. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2006. };
  2007. /* C=US ST=California*/
  2008. char altNameExc[] = {
  2009. 0x30, 0x22,
  2010. 0x31, 0x0B,
  2011. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2012. 0x31, 0x13,
  2013. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2014. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2015. };
  2016. /* load in CA private key for signing */
  2017. pt = ca_key_der_2048;
  2018. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2019. sizeof_ca_key_der_2048));
  2020. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2021. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2022. WOLFSSL_FILETYPE_ASN1));
  2023. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2024. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2025. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2026. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2027. WOLFSSL_FILETYPE_PEM));
  2028. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2029. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2030. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2031. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2032. #else
  2033. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2034. #endif
  2035. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2036. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2037. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2038. /* add in matching DIR alt name and resign */
  2039. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2040. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2041. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2042. #else
  2043. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2044. #endif
  2045. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2046. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2047. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2048. wolfSSL_X509_free(x509);
  2049. /* check verify fail */
  2050. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2051. WOLFSSL_FILETYPE_PEM));
  2052. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2053. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2054. /* add in miss matching DIR alt name and resign */
  2055. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2056. ASN_DIR_TYPE);
  2057. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2058. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2059. #else
  2060. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2061. #endif
  2062. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2063. #ifndef WOLFSSL_NO_ASN_STRICT
  2064. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2065. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2066. #else
  2067. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2068. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2069. #endif
  2070. /* check that it still fails if one bad altname and one good altname is in
  2071. * the certificate */
  2072. wolfSSL_X509_free(x509);
  2073. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2074. WOLFSSL_FILETYPE_PEM));
  2075. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2076. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2077. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2078. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2079. ASN_DIR_TYPE);
  2080. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2081. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2082. #else
  2083. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2084. #endif
  2085. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2086. #ifndef WOLFSSL_NO_ASN_STRICT
  2087. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2088. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2089. #else
  2090. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2091. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2092. #endif
  2093. /* check it fails with switching position of bad altname */
  2094. wolfSSL_X509_free(x509);
  2095. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2096. WOLFSSL_FILETYPE_PEM));
  2097. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2098. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2099. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2100. ASN_DIR_TYPE);
  2101. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2102. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2103. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2104. #else
  2105. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2106. #endif
  2107. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2108. #ifndef WOLFSSL_NO_ASN_STRICT
  2109. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2110. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2111. #else
  2112. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2113. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2114. #endif
  2115. wolfSSL_CertManagerFree(cm);
  2116. wolfSSL_X509_free(x509);
  2117. wolfSSL_X509_free(ca);
  2118. /* now test with excluded name constraint */
  2119. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2120. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2121. WOLFSSL_FILETYPE_ASN1));
  2122. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2123. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2124. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2125. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2126. WOLFSSL_FILETYPE_PEM));
  2127. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2128. ASN_DIR_TYPE);
  2129. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2130. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2131. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2132. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2133. #else
  2134. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2135. #endif
  2136. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2137. #ifndef WOLFSSL_NO_ASN_STRICT
  2138. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2139. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2140. #else
  2141. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2142. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2143. #endif
  2144. wolfSSL_CertManagerFree(cm);
  2145. wolfSSL_X509_free(x509);
  2146. wolfSSL_X509_free(ca);
  2147. wolfSSL_EVP_PKEY_free(priv);
  2148. res = TEST_RES_CHECK(1);
  2149. #endif
  2150. return res;
  2151. }
  2152. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2153. {
  2154. int res = TEST_SKIPPED;
  2155. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2156. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2157. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2158. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2159. !defined(NO_SHA256)
  2160. WOLFSSL_CERT_MANAGER* cm;
  2161. WOLFSSL_EVP_PKEY *priv;
  2162. WOLFSSL_X509_NAME* name;
  2163. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2164. const char* server_cert = "./certs/test/server-goodcn.pem";
  2165. byte *der;
  2166. int derSz;
  2167. byte *pt;
  2168. WOLFSSL_X509 *x509, *ca;
  2169. pt = (byte*)server_key_der_2048;
  2170. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2171. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2172. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2173. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2174. WOLFSSL_FILETYPE_ASN1));
  2175. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2176. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2177. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2178. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2179. /* check satisfying .wolfssl.com constraint passes */
  2180. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2181. WOLFSSL_FILETYPE_PEM));
  2182. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2183. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2184. AssertNotNull(name = X509_NAME_new());
  2185. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2186. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2187. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2188. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2189. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2190. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2191. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2192. X509_NAME_free(name);
  2193. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2194. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2195. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2196. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2197. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2198. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2199. wolfSSL_X509_free(x509);
  2200. /* check satisfying .random.com constraint passes */
  2201. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2202. WOLFSSL_FILETYPE_PEM));
  2203. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2204. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2205. AssertNotNull(name = X509_NAME_new());
  2206. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2207. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2208. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2209. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2210. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2211. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2212. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2213. X509_NAME_free(name);
  2214. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2215. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2216. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2217. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2218. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2219. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2220. wolfSSL_X509_free(x509);
  2221. /* check fail case when neither constraint is matched */
  2222. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2223. WOLFSSL_FILETYPE_PEM));
  2224. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2225. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2226. AssertNotNull(name = X509_NAME_new());
  2227. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2228. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2229. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2230. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2231. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2232. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2233. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2234. X509_NAME_free(name);
  2235. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2236. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2237. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2238. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2239. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2240. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2241. wolfSSL_CertManagerFree(cm);
  2242. wolfSSL_X509_free(x509);
  2243. wolfSSL_X509_free(ca);
  2244. wolfSSL_EVP_PKEY_free(priv);
  2245. res = TEST_RES_CHECK(1);
  2246. #endif
  2247. return res;
  2248. }
  2249. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2250. {
  2251. int res = TEST_SKIPPED;
  2252. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2253. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2254. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2255. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2256. !defined(NO_SHA256)
  2257. WOLFSSL_CERT_MANAGER* cm;
  2258. WOLFSSL_EVP_PKEY *priv;
  2259. WOLFSSL_X509_NAME* name;
  2260. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2261. const char* server_cert = "./certs/test/server-goodcn.pem";
  2262. byte *der;
  2263. int derSz;
  2264. byte *pt;
  2265. WOLFSSL_X509 *x509, *ca;
  2266. pt = (byte*)server_key_der_2048;
  2267. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2268. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2269. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2270. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2271. WOLFSSL_FILETYPE_ASN1));
  2272. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2273. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2274. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2275. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2276. /* check satisfying wolfssl.com constraint passes */
  2277. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2278. WOLFSSL_FILETYPE_PEM));
  2279. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2280. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2281. AssertNotNull(name = X509_NAME_new());
  2282. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2283. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2284. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2285. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2286. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2287. X509_NAME_free(name);
  2288. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2289. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2290. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2291. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2292. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2293. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2294. wolfSSL_X509_free(x509);
  2295. /* check satisfying example.com constraint passes */
  2296. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2297. WOLFSSL_FILETYPE_PEM));
  2298. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2299. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2300. AssertNotNull(name = X509_NAME_new());
  2301. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2302. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2303. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2304. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2305. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2306. X509_NAME_free(name);
  2307. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2308. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2309. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2310. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2311. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2312. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2313. wolfSSL_X509_free(x509);
  2314. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2315. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2316. WOLFSSL_FILETYPE_PEM));
  2317. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2318. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2319. AssertNotNull(name = X509_NAME_new());
  2320. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2321. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2322. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2323. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2324. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2325. X509_NAME_free(name);
  2326. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2327. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2328. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2329. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2330. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2331. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2332. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2333. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2334. wolfSSL_X509_free(x509);
  2335. /* check fail when one DNS in the list is bad */
  2336. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2337. WOLFSSL_FILETYPE_PEM));
  2338. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2339. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2340. AssertNotNull(name = X509_NAME_new());
  2341. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2342. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2343. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2344. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2345. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2346. X509_NAME_free(name);
  2347. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2348. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2349. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2350. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2351. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2352. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2353. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2354. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2355. wolfSSL_X509_free(x509);
  2356. /* check fail case when neither constraint is matched */
  2357. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2358. WOLFSSL_FILETYPE_PEM));
  2359. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2360. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2361. AssertNotNull(name = X509_NAME_new());
  2362. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2363. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2364. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2365. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2366. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2367. X509_NAME_free(name);
  2368. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2369. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2370. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2371. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2372. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2373. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2374. wolfSSL_CertManagerFree(cm);
  2375. wolfSSL_X509_free(x509);
  2376. wolfSSL_X509_free(ca);
  2377. wolfSSL_EVP_PKEY_free(priv);
  2378. res = TEST_RES_CHECK(1);
  2379. #endif
  2380. return res;
  2381. }
  2382. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2383. {
  2384. int res = TEST_SKIPPED;
  2385. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2386. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2387. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2388. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2389. !defined(NO_SHA256)
  2390. WOLFSSL_CERT_MANAGER* cm;
  2391. WOLFSSL_EVP_PKEY *priv;
  2392. WOLFSSL_X509_NAME* name;
  2393. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2394. const char* server_cert = "./certs/test/server-goodcn.pem";
  2395. byte *der;
  2396. int derSz;
  2397. byte *pt;
  2398. WOLFSSL_X509 *x509, *ca;
  2399. pt = (byte*)server_key_der_2048;
  2400. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2401. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2402. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2403. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2404. WOLFSSL_FILETYPE_ASN1));
  2405. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2406. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2407. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2408. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2409. /* check satisfying wolfssl.com constraint passes */
  2410. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2411. WOLFSSL_FILETYPE_PEM));
  2412. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2413. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2414. AssertNotNull(name = X509_NAME_new());
  2415. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2416. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2417. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2418. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2419. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2420. X509_NAME_free(name);
  2421. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2422. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2423. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2424. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2425. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2426. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2427. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2428. wolfSSL_X509_free(x509);
  2429. /* fail with DNS check because of common name */
  2430. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2431. WOLFSSL_FILETYPE_PEM));
  2432. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2433. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2434. AssertNotNull(name = X509_NAME_new());
  2435. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2436. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2437. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2438. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2439. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2440. X509_NAME_free(name);
  2441. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2442. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2443. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2444. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2445. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2446. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2447. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2448. wolfSSL_X509_free(x509);
  2449. /* fail on permitted DNS name constraint */
  2450. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2451. WOLFSSL_FILETYPE_PEM));
  2452. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2453. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2454. AssertNotNull(name = X509_NAME_new());
  2455. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2456. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2457. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2458. X509_NAME_free(name);
  2459. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2460. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2461. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2462. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2463. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2464. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2465. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2466. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2467. wolfSSL_X509_free(x509);
  2468. /* fail on permitted email name constraint */
  2469. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2470. WOLFSSL_FILETYPE_PEM));
  2471. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2472. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2473. AssertNotNull(name = X509_NAME_new());
  2474. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2475. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2476. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2477. X509_NAME_free(name);
  2478. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2479. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2480. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2481. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2482. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2483. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2484. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2485. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2486. wolfSSL_X509_free(x509);
  2487. /* success with empty email name */
  2488. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2489. WOLFSSL_FILETYPE_PEM));
  2490. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2491. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2492. AssertNotNull(name = X509_NAME_new());
  2493. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2494. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2495. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2496. X509_NAME_free(name);
  2497. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2498. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2499. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2500. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2501. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2502. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2503. wolfSSL_X509_free(x509);
  2504. wolfSSL_CertManagerFree(cm);
  2505. wolfSSL_X509_free(ca);
  2506. wolfSSL_EVP_PKEY_free(priv);
  2507. res = TEST_RES_CHECK(1);
  2508. #endif
  2509. return res;
  2510. }
  2511. static int test_wolfSSL_FPKI(void)
  2512. {
  2513. int res = TEST_SKIPPED;
  2514. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2515. XFILE f;
  2516. const char* fpkiCert = "./certs/fpki-cert.der";
  2517. DecodedCert cert;
  2518. byte buf[4096];
  2519. byte* uuid;
  2520. byte* fascn;
  2521. word32 fascnSz;
  2522. word32 uuidSz;
  2523. int bytes;
  2524. f = XFOPEN(fpkiCert, "rb");
  2525. AssertTrue((f != XBADFILE));
  2526. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2527. XFCLOSE(f);
  2528. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2529. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2530. AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2531. fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2532. AssertNotNull(fascn);
  2533. AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2534. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2535. AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2536. uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2537. AssertNotNull(uuid);
  2538. AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2539. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2540. wc_FreeDecodedCert(&cert);
  2541. res = TEST_RES_CHECK(1);
  2542. #endif
  2543. return res;
  2544. }
  2545. /* use RID in confuncture with other names to test parsing of unknown other
  2546. * names */
  2547. static int test_wolfSSL_OtherName(void)
  2548. {
  2549. int res = TEST_SKIPPED;
  2550. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2551. XFILE f;
  2552. const char* ridCert = "./certs/rid-cert.der";
  2553. DecodedCert cert;
  2554. byte buf[4096];
  2555. int bytes;
  2556. f = XFOPEN(ridCert, "rb");
  2557. AssertTrue((f != XBADFILE));
  2558. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2559. XFCLOSE(f);
  2560. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2561. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2562. wc_FreeDecodedCert(&cert);
  2563. res = TEST_RES_CHECK(1);
  2564. #endif
  2565. return res;
  2566. }
  2567. static int test_wolfSSL_CertRsaPss(void)
  2568. {
  2569. int res = TEST_SKIPPED;
  2570. /* FIPS v2 and below don't support long salts. */
  2571. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2572. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2573. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2574. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2575. XFILE f;
  2576. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2577. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2578. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2579. RSA_MAX_SIZE >= 3072
  2580. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2581. #endif
  2582. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2583. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2584. #endif
  2585. DecodedCert cert;
  2586. byte buf[4096];
  2587. int bytes;
  2588. WOLFSSL_CERT_MANAGER* cm;
  2589. cm = wolfSSL_CertManagerNew();
  2590. AssertNotNull(cm);
  2591. AssertIntEQ(WOLFSSL_SUCCESS,
  2592. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2593. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2594. AssertIntEQ(WOLFSSL_SUCCESS,
  2595. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2596. #endif
  2597. f = XFOPEN(rsaPssSha256Cert, "rb");
  2598. AssertTrue((f != XBADFILE));
  2599. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2600. XFCLOSE(f);
  2601. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2602. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2603. wc_FreeDecodedCert(&cert);
  2604. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2605. RSA_MAX_SIZE >= 3072
  2606. f = XFOPEN(rsaPssSha384Cert, "rb");
  2607. AssertTrue((f != XBADFILE));
  2608. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2609. XFCLOSE(f);
  2610. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2611. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2612. wc_FreeDecodedCert(&cert);
  2613. #endif
  2614. wolfSSL_CertManagerFree(cm);
  2615. res = TEST_RES_CHECK(1);
  2616. #endif
  2617. return res;
  2618. }
  2619. static int test_wolfSSL_CertManagerCRL(void)
  2620. {
  2621. int res = TEST_SKIPPED;
  2622. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2623. !defined(NO_RSA)
  2624. const char* ca_cert = "./certs/ca-cert.pem";
  2625. const char* crl1 = "./certs/crl/crl.pem";
  2626. const char* crl2 = "./certs/crl/crl2.pem";
  2627. WOLFSSL_CERT_MANAGER* cm = NULL;
  2628. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2629. AssertIntEQ(WOLFSSL_SUCCESS,
  2630. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2631. AssertIntEQ(WOLFSSL_SUCCESS,
  2632. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2633. AssertIntEQ(WOLFSSL_SUCCESS,
  2634. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2635. wolfSSL_CertManagerFreeCRL(cm);
  2636. AssertIntEQ(WOLFSSL_SUCCESS,
  2637. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2638. AssertIntEQ(WOLFSSL_SUCCESS,
  2639. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2640. wolfSSL_CertManagerFree(cm);
  2641. res = TEST_RES_CHECK(1);
  2642. #endif
  2643. return res;
  2644. }
  2645. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2646. {
  2647. int res = TEST_SKIPPED;
  2648. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2649. !defined(NO_WOLFSSL_CLIENT)
  2650. WOLFSSL_CTX* ctx;
  2651. const char* ca_cert = "./certs/ca-cert.pem";
  2652. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2653. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2654. AssertNotNull(ctx);
  2655. /* test good CA */
  2656. AssertTrue(WOLFSSL_SUCCESS ==
  2657. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2658. WOLFSSL_LOAD_FLAG_NONE));
  2659. /* test expired CA */
  2660. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  2661. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2662. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2663. #else
  2664. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2665. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2666. #endif
  2667. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2668. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2669. wolfSSL_CTX_free(ctx);
  2670. res = TEST_RES_CHECK(1);
  2671. #endif
  2672. return res;
  2673. }
  2674. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2675. {
  2676. int res = TEST_SKIPPED;
  2677. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2678. defined(USE_CERT_BUFFERS_2048)
  2679. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2680. WOLFSSL_CTX* ctx;
  2681. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2682. byte ca_expired_cert[TWOK_BUF];
  2683. word32 sizeof_ca_expired_cert;
  2684. XFILE fp;
  2685. #ifndef NO_WOLFSSL_CLIENT
  2686. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2687. #else
  2688. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2689. #endif
  2690. AssertNotNull(ctx);
  2691. /* test good CA */
  2692. AssertTrue(WOLFSSL_SUCCESS ==
  2693. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2694. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2695. WOLFSSL_LOAD_FLAG_NONE));
  2696. /* load expired CA */
  2697. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2698. fp = XFOPEN(ca_expired_cert_file, "rb");
  2699. AssertTrue(fp != XBADFILE);
  2700. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2701. sizeof(ca_expired_cert), fp);
  2702. XFCLOSE(fp);
  2703. /* test expired CA failure */
  2704. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  2705. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2706. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2707. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2708. #else
  2709. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2710. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2711. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2712. #endif
  2713. /* test expired CA success */
  2714. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2715. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2716. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2717. wolfSSL_CTX_free(ctx);
  2718. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2719. res = TEST_RES_CHECK(1);
  2720. #endif
  2721. return res;
  2722. }
  2723. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2724. {
  2725. int res = TEST_SKIPPED;
  2726. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2727. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2728. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2729. WOLFSSL_CTX* ctx;
  2730. #ifndef NO_WOLFSSL_CLIENT
  2731. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2732. #else
  2733. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2734. #endif
  2735. AssertTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  2736. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  2737. WOLFSSL_FILETYPE_ASN1));
  2738. wolfSSL_CTX_free(ctx);
  2739. res = TEST_RES_CHECK(1);
  2740. #endif
  2741. return res;
  2742. }
  2743. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2744. {
  2745. int res = TEST_SKIPPED;
  2746. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2747. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2748. WOLFSSL_CTX* ctx;
  2749. WOLFSSL* ssl;
  2750. const char *certChain[] = {
  2751. "./certs/intermediate/client-int-cert.pem",
  2752. "./certs/intermediate/ca-int2-cert.pem",
  2753. "./certs/intermediate/ca-int-cert.pem",
  2754. "./certs/ca-cert.pem",
  2755. NULL
  2756. };
  2757. const char** cert;
  2758. WOLFSSL_X509* x509;
  2759. WOLF_STACK_OF(X509)* chain = NULL;
  2760. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2761. AssertNotNull(ssl = wolfSSL_new(ctx));
  2762. for (cert = certChain; *cert != NULL; cert++) {
  2763. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2764. AssertNotNull(x509);
  2765. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2766. X509_free(x509);
  2767. }
  2768. for (cert = certChain; *cert != NULL; cert++) {
  2769. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2770. AssertNotNull(x509);
  2771. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2772. X509_free(x509);
  2773. }
  2774. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2775. AssertIntEQ(sk_X509_num(chain), 3);
  2776. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2777. AssertIntEQ(sk_X509_num(chain), 3);
  2778. SSL_free(ssl);
  2779. SSL_CTX_free(ctx);
  2780. res = TEST_RES_CHECK(1);
  2781. #endif
  2782. return res;
  2783. }
  2784. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2785. {
  2786. int res = TEST_SKIPPED;
  2787. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2788. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2789. const char* server_chain_der = "./certs/server-cert-chain.der";
  2790. const char* client_single_pem = "./certs/client-cert.pem";
  2791. WOLFSSL_CTX* ctx;
  2792. int ret = 0;
  2793. (void)server_chain_der;
  2794. (void)client_single_pem;
  2795. (void)ctx;
  2796. #ifndef NO_WOLFSSL_CLIENT
  2797. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2798. AssertNotNull(ctx);
  2799. #else
  2800. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2801. AssertNotNull(ctx);
  2802. #endif
  2803. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2804. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2805. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2806. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2807. wolfSSL_CTX_free(ctx);
  2808. res = TEST_RES_CHECK(ret == 0);
  2809. #endif
  2810. return res;
  2811. }
  2812. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  2813. {
  2814. int res = TEST_SKIPPED;
  2815. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2816. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2817. WOLFSSL_CTX *ctx;
  2818. (void)ctx;
  2819. #ifndef NO_WOLFSSL_CLIENT
  2820. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2821. #else
  2822. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2823. #endif
  2824. /* invalid context */
  2825. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2826. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2827. /* invalid dhParamFile file */
  2828. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2829. NULL, WOLFSSL_FILETYPE_PEM));
  2830. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2831. bogusFile, WOLFSSL_FILETYPE_PEM));
  2832. /* success */
  2833. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2834. WOLFSSL_FILETYPE_PEM));
  2835. wolfSSL_CTX_free(ctx);
  2836. res = TEST_RES_CHECK(1);
  2837. #endif
  2838. return res;
  2839. }
  2840. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2841. {
  2842. int res = TEST_SKIPPED;
  2843. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2844. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2845. WOLFSSL_CTX *ctx;
  2846. (void)ctx;
  2847. #ifndef NO_WOLFSSL_CLIENT
  2848. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2849. #else
  2850. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2851. #endif
  2852. /* invalid context */
  2853. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2854. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2855. /* invalid dhParamFile file */
  2856. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2857. 0, WOLFSSL_FILETYPE_ASN1));
  2858. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2859. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2860. /* success */
  2861. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2862. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2863. wolfSSL_CTX_free(ctx);
  2864. res = TEST_RES_CHECK(1);
  2865. #endif
  2866. return res;
  2867. }
  2868. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2869. {
  2870. int res = TEST_SKIPPED;
  2871. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2872. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2873. WOLFSSL_CTX *ctx;
  2874. (void)ctx;
  2875. #ifndef NO_WOLFSSL_CLIENT
  2876. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2877. AssertNotNull(ctx);
  2878. #else
  2879. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2880. AssertNotNull(ctx);
  2881. #endif
  2882. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2883. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2884. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2885. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2886. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2887. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2888. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2889. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2890. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2891. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2892. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2893. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2894. wolfSSL_CTX_free(ctx);
  2895. res = TEST_RES_CHECK(1);
  2896. #endif
  2897. return res;
  2898. }
  2899. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  2900. {
  2901. int res = TEST_SKIPPED;
  2902. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  2903. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2904. WOLFSSL_CTX* ctx = NULL;
  2905. const char* derCert = "./certs/server-cert.der";
  2906. const char* nullPath = NULL;
  2907. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2908. const char* emptyPath = "";
  2909. /* der load Case 1 ctx NULL */
  2910. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2911. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2912. #ifndef NO_WOLFSSL_CLIENT
  2913. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2914. #else
  2915. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2916. #endif
  2917. /* Case 2 filePath NULL */
  2918. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2919. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2920. /* Case 3 invalid format */
  2921. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2922. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2923. /* Case 4 filePath not valid */
  2924. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2925. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2926. /* Case 5 filePath empty */
  2927. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2928. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2929. #ifndef NO_RSA
  2930. /* Case 6 success case */
  2931. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2932. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2933. #endif
  2934. wolfSSL_CTX_free(ctx);
  2935. res = TEST_RES_CHECK(1);
  2936. #endif
  2937. return res;
  2938. }
  2939. static int test_wolfSSL_CTX_enable_disable(void)
  2940. {
  2941. int res = TEST_SKIPPED;
  2942. #ifndef NO_CERTS
  2943. WOLFSSL_CTX* ctx = NULL;
  2944. #ifdef HAVE_CRL
  2945. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2946. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2947. #endif
  2948. #ifdef HAVE_OCSP
  2949. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2950. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2951. #endif
  2952. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2953. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2954. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2955. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2956. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2957. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2958. #endif
  2959. #ifndef NO_WOLFSSL_CLIENT
  2960. #ifdef HAVE_EXTENDED_MASTER
  2961. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  2962. #endif
  2963. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2964. AssertNotNull(ctx);
  2965. #ifdef HAVE_EXTENDED_MASTER
  2966. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  2967. #endif
  2968. #elif !defined(NO_WOLFSSL_SERVER)
  2969. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2970. #else
  2971. return TEST_SUCCESS;
  2972. #endif
  2973. #ifdef HAVE_CRL
  2974. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  2975. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  2976. #endif
  2977. #ifdef HAVE_OCSP
  2978. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  2979. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  2980. WOLFSSL_SUCCESS);
  2981. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  2982. WOLFSSL_SUCCESS);
  2983. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  2984. WOLFSSL_SUCCESS);
  2985. #endif
  2986. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2987. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2988. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2989. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2990. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2991. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2992. #endif
  2993. wolfSSL_CTX_free(ctx);
  2994. res = TEST_RES_CHECK(1);
  2995. #endif /* NO_CERTS */
  2996. return res;
  2997. }
  2998. static int test_wolfSSL_CTX_ticket_API(void)
  2999. {
  3000. int res = TEST_SKIPPED;
  3001. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3002. WOLFSSL_CTX* ctx = NULL;
  3003. void *userCtx = (void*)"this is my ctx";
  3004. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3005. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3006. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3007. wolfSSL_CTX_free(ctx);
  3008. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3009. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3010. res = TEST_RES_CHECK(1);
  3011. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3012. return res;
  3013. }
  3014. static int test_wolfSSL_set_minmax_proto_version(void)
  3015. {
  3016. int res = TEST_SKIPPED;
  3017. #ifdef OPENSSL_EXTRA
  3018. WOLFSSL_CTX *ctx;
  3019. WOLFSSL *ssl;
  3020. int ret;
  3021. (void)ret;
  3022. (void)ssl;
  3023. #ifndef NO_WOLFSSL_CLIENT
  3024. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3025. AssertNotNull(ssl = wolfSSL_new(ctx));
  3026. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3027. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3028. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3029. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3030. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3031. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3032. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3033. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3034. wolfSSL_free(ssl);
  3035. wolfSSL_CTX_free(ctx);
  3036. #endif
  3037. #ifndef NO_WOLFSSL_SERVER
  3038. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3039. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3040. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3041. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3042. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3043. wolfSSL_CTX_free(ctx);
  3044. #endif
  3045. res = TEST_RES_CHECK(1);
  3046. #endif
  3047. return res;
  3048. }
  3049. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3050. defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  3051. static void test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3052. {
  3053. AssertStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3054. }
  3055. static void test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3056. {
  3057. /* Set TLS 1.2 */
  3058. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3059. WOLFSSL_SUCCESS);
  3060. }
  3061. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3062. * what was set at ctx creation. */
  3063. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3064. {
  3065. callback_functions client_cbs, server_cbs;
  3066. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3067. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3068. client_cbs.method = wolfTLS_client_method;
  3069. server_cbs.method = wolfTLS_server_method;
  3070. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3071. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3072. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3073. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  3074. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  3075. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  3076. return TEST_RES_CHECK(1);
  3077. }
  3078. #else
  3079. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3080. {
  3081. return TEST_SKIPPED;
  3082. }
  3083. #endif
  3084. /*----------------------------------------------------------------------------*
  3085. | SSL
  3086. *----------------------------------------------------------------------------*/
  3087. static int test_server_wolfSSL_new(void)
  3088. {
  3089. int res = TEST_SKIPPED;
  3090. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3091. !defined(NO_WOLFSSL_SERVER)
  3092. WOLFSSL_CTX *ctx;
  3093. WOLFSSL_CTX *ctx_nocert;
  3094. WOLFSSL *ssl;
  3095. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3096. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3097. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  3098. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  3099. /* invalid context */
  3100. AssertNull(ssl = wolfSSL_new(NULL));
  3101. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  3102. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  3103. #endif
  3104. /* success */
  3105. AssertNotNull(ssl = wolfSSL_new(ctx));
  3106. wolfSSL_free(ssl);
  3107. wolfSSL_CTX_free(ctx);
  3108. wolfSSL_CTX_free(ctx_nocert);
  3109. res = TEST_RES_CHECK(1);
  3110. #endif
  3111. return res;
  3112. }
  3113. static int test_client_wolfSSL_new(void)
  3114. {
  3115. int res = TEST_SKIPPED;
  3116. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3117. !defined(NO_WOLFSSL_CLIENT)
  3118. WOLFSSL_CTX *ctx;
  3119. WOLFSSL_CTX *ctx_nocert;
  3120. WOLFSSL *ssl;
  3121. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3122. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3123. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3124. /* invalid context */
  3125. AssertNull(ssl = wolfSSL_new(NULL));
  3126. /* success */
  3127. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  3128. wolfSSL_free(ssl);
  3129. /* success */
  3130. AssertNotNull(ssl = wolfSSL_new(ctx));
  3131. wolfSSL_free(ssl);
  3132. wolfSSL_CTX_free(ctx);
  3133. wolfSSL_CTX_free(ctx_nocert);
  3134. res = TEST_RES_CHECK(1);
  3135. #endif
  3136. return res;
  3137. }
  3138. static int test_wolfSSL_SetTmpDH_file(void)
  3139. {
  3140. int res = TEST_SKIPPED;
  3141. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3142. !defined(NO_WOLFSSL_SERVER)
  3143. WOLFSSL_CTX *ctx;
  3144. WOLFSSL *ssl;
  3145. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3146. #ifndef NO_RSA
  3147. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3148. WOLFSSL_FILETYPE_PEM));
  3149. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3150. WOLFSSL_FILETYPE_PEM));
  3151. #elif defined(HAVE_ECC)
  3152. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3153. WOLFSSL_FILETYPE_PEM));
  3154. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3155. WOLFSSL_FILETYPE_PEM));
  3156. #elif defined(HAVE_ED25519)
  3157. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3158. WOLFSSL_FILETYPE_PEM));
  3159. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3160. WOLFSSL_FILETYPE_PEM));
  3161. #elif defined(HAVE_ED448)
  3162. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3163. WOLFSSL_FILETYPE_PEM));
  3164. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3165. WOLFSSL_FILETYPE_PEM));
  3166. #endif
  3167. AssertNotNull(ssl = wolfSSL_new(ctx));
  3168. /* invalid ssl */
  3169. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3170. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3171. /* invalid dhParamFile file */
  3172. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3173. NULL, WOLFSSL_FILETYPE_PEM));
  3174. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3175. bogusFile, WOLFSSL_FILETYPE_PEM));
  3176. /* success */
  3177. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3178. WOLFSSL_FILETYPE_PEM));
  3179. wolfSSL_free(ssl);
  3180. wolfSSL_CTX_free(ctx);
  3181. res = TEST_RES_CHECK(1);
  3182. #endif
  3183. return res;
  3184. }
  3185. static int test_wolfSSL_SetTmpDH_buffer(void)
  3186. {
  3187. int res = TEST_SKIPPED;
  3188. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3189. WOLFSSL_CTX *ctx;
  3190. WOLFSSL *ssl;
  3191. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3192. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3193. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3194. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3195. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3196. AssertNotNull(ssl = wolfSSL_new(ctx));
  3197. /* invalid ssl */
  3198. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3199. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3200. /* invalid dhParamFile file */
  3201. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3202. 0, WOLFSSL_FILETYPE_ASN1));
  3203. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3204. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3205. /* success */
  3206. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3207. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3208. wolfSSL_free(ssl);
  3209. wolfSSL_CTX_free(ctx);
  3210. res = TEST_RES_CHECK(1);
  3211. #endif
  3212. return res;
  3213. }
  3214. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3215. {
  3216. int res = TEST_SKIPPED;
  3217. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3218. WOLFSSL_CTX *ctx, *ctx2;
  3219. WOLFSSL *ssl, *ssl2;
  3220. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3221. AssertNotNull(ctx);
  3222. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3223. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3224. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3225. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3226. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3227. ssl = wolfSSL_new(ctx);
  3228. AssertNotNull(ssl);
  3229. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3230. AssertNotNull(ctx2);
  3231. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3232. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3233. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3234. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3235. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3236. ssl2 = wolfSSL_new(ctx2);
  3237. AssertNotNull(ssl2);
  3238. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3239. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3240. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3241. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3242. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3243. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3244. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3245. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3246. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3247. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3248. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3249. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3250. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3251. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3252. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3253. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3254. wolfSSL_free(ssl2);
  3255. wolfSSL_CTX_free(ctx2);
  3256. wolfSSL_free(ssl);
  3257. wolfSSL_CTX_free(ctx);
  3258. res = TEST_RES_CHECK(1);
  3259. #endif
  3260. return res;
  3261. }
  3262. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3263. * allowed.
  3264. * POST: return 1 on success.
  3265. */
  3266. static int test_wolfSSL_SetMinVersion(void)
  3267. {
  3268. int res = TEST_SKIPPED;
  3269. #ifndef NO_WOLFSSL_CLIENT
  3270. int failFlag = WOLFSSL_SUCCESS;
  3271. WOLFSSL_CTX* ctx;
  3272. WOLFSSL* ssl;
  3273. int itr;
  3274. #ifndef NO_OLD_TLS
  3275. const int versions[] = {
  3276. #ifdef WOLFSSL_ALLOW_TLSV10
  3277. WOLFSSL_TLSV1,
  3278. #endif
  3279. WOLFSSL_TLSV1_1,
  3280. WOLFSSL_TLSV1_2};
  3281. #elif !defined(WOLFSSL_NO_TLS12)
  3282. const int versions[] = { WOLFSSL_TLSV1_2 };
  3283. #else
  3284. const int versions[] = { WOLFSSL_TLSV1_3 };
  3285. #endif
  3286. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3287. ssl = wolfSSL_new(ctx);
  3288. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3289. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3290. failFlag = WOLFSSL_FAILURE;
  3291. }
  3292. }
  3293. wolfSSL_free(ssl);
  3294. wolfSSL_CTX_free(ctx);
  3295. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3296. #endif
  3297. return res;
  3298. } /* END test_wolfSSL_SetMinVersion */
  3299. #ifdef OPENSSL_EXTRA
  3300. static int test_ED25519(void)
  3301. {
  3302. int res = TEST_SKIPPED;
  3303. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3304. defined(WOLFSSL_KEY_GEN)
  3305. byte priv[ED25519_PRV_KEY_SIZE];
  3306. unsigned int privSz = (unsigned int)sizeof(priv);
  3307. byte pub[ED25519_PUB_KEY_SIZE];
  3308. unsigned int pubSz = (unsigned int)sizeof(pub);
  3309. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3310. const char* msg = TEST_STRING;
  3311. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3312. byte sig[ED25519_SIG_SIZE];
  3313. unsigned int sigSz = (unsigned int)sizeof(sig);
  3314. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3315. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3316. WOLFSSL_SUCCESS);
  3317. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3318. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3319. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3320. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3321. &sigSz), WOLFSSL_SUCCESS);
  3322. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3323. #ifdef HAVE_ED25519_VERIFY
  3324. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3325. sigSz), WOLFSSL_SUCCESS);
  3326. #endif /* HAVE_ED25519_VERIFY */
  3327. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3328. res = TEST_RES_CHECK(1);
  3329. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3330. return res;
  3331. }
  3332. static int test_ED448(void)
  3333. {
  3334. int res = TEST_SKIPPED;
  3335. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3336. defined(WOLFSSL_KEY_GEN)
  3337. byte priv[ED448_PRV_KEY_SIZE];
  3338. unsigned int privSz = (unsigned int)sizeof(priv);
  3339. byte pub[ED448_PUB_KEY_SIZE];
  3340. unsigned int pubSz = (unsigned int)sizeof(pub);
  3341. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3342. const char* msg = TEST_STRING;
  3343. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3344. byte sig[ED448_SIG_SIZE];
  3345. unsigned int sigSz = (unsigned int)sizeof(sig);
  3346. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3347. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3348. WOLFSSL_SUCCESS);
  3349. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3350. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3351. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3352. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3353. &sigSz), WOLFSSL_SUCCESS);
  3354. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3355. #ifdef HAVE_ED448_VERIFY
  3356. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3357. sigSz), WOLFSSL_SUCCESS);
  3358. #endif /* HAVE_ED448_VERIFY */
  3359. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3360. res = TEST_RES_CHECK(1);
  3361. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3362. return res;
  3363. }
  3364. #endif /* OPENSSL_EXTRA */
  3365. #include <wolfssl/openssl/pem.h>
  3366. /*----------------------------------------------------------------------------*
  3367. | EVP
  3368. *----------------------------------------------------------------------------*/
  3369. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3370. {
  3371. int res = TEST_SKIPPED;
  3372. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3373. WOLFSSL_BIO* rbio = NULL;
  3374. WOLFSSL_BIO* wbio = NULL;
  3375. WOLFSSL_EVP_PKEY* pkey = NULL;
  3376. char line[256] = { 0 };
  3377. char line1[256] = { 0 };
  3378. int i;
  3379. /* test error cases */
  3380. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3381. /*
  3382. * test RSA public key print
  3383. * in this test, pass '3' for indent
  3384. */
  3385. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3386. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3387. sizeof_client_keypub_der_1024);
  3388. AssertNotNull(rbio);
  3389. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3390. AssertNotNull(pkey);
  3391. wbio = BIO_new(BIO_s_mem());
  3392. AssertNotNull(wbio);
  3393. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3394. BIO_gets(wbio, line, sizeof(line));
  3395. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3396. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3397. BIO_gets(wbio, line, sizeof(line));
  3398. strcpy(line1, " Modulus:\n");
  3399. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3400. BIO_gets(wbio, line, sizeof(line));
  3401. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  3402. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3403. /* skip to the end of modulus element*/
  3404. for (i = 0; i < 8 ;i++) {
  3405. BIO_gets(wbio, line, sizeof(line));
  3406. }
  3407. BIO_gets(wbio, line, sizeof(line));
  3408. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3409. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3410. /* should reach EOF */
  3411. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3412. EVP_PKEY_free(pkey);
  3413. pkey = NULL;
  3414. BIO_free(rbio);
  3415. BIO_free(wbio);
  3416. rbio = NULL;
  3417. wbio = NULL;
  3418. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3419. /*
  3420. * test DSA public key print
  3421. */
  3422. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3423. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3424. sizeof_dsa_pub_key_der_2048);
  3425. AssertNotNull(rbio);
  3426. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3427. AssertNotNull(pkey);
  3428. wbio = BIO_new(BIO_s_mem());
  3429. AssertNotNull(wbio);
  3430. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3431. BIO_gets(wbio, line, sizeof(line));
  3432. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3433. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3434. BIO_gets(wbio, line, sizeof(line));
  3435. strcpy(line1, "pub:\n");
  3436. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3437. BIO_gets(wbio, line, sizeof(line));
  3438. strcpy(line1,
  3439. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3440. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3441. /* skip to the end of pub element*/
  3442. for (i = 0; i < 17 ;i++) {
  3443. BIO_gets(wbio, line, sizeof(line));
  3444. }
  3445. BIO_gets(wbio, line, sizeof(line));
  3446. strcpy(line1, "P:\n");
  3447. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3448. /* skip to the end of P element*/
  3449. for (i = 0; i < 18 ;i++) {
  3450. BIO_gets(wbio, line, sizeof(line));
  3451. }
  3452. BIO_gets(wbio, line, sizeof(line));
  3453. strcpy(line1, "Q:\n");
  3454. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3455. /* skip to the end of Q element*/
  3456. for (i = 0; i < 3 ;i++) {
  3457. BIO_gets(wbio, line, sizeof(line));
  3458. }
  3459. BIO_gets(wbio, line, sizeof(line));
  3460. strcpy(line1, "G:\n");
  3461. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3462. /* skip to the end of G element*/
  3463. for (i = 0; i < 18 ;i++) {
  3464. BIO_gets(wbio, line, sizeof(line));
  3465. }
  3466. /* should reach EOF */
  3467. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3468. EVP_PKEY_free(pkey);
  3469. pkey = NULL;
  3470. BIO_free(rbio);
  3471. BIO_free(wbio);
  3472. rbio = NULL;
  3473. wbio = NULL;
  3474. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3475. /*
  3476. * test ECC public key print
  3477. */
  3478. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3479. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3480. sizeof_ecc_clikeypub_der_256);
  3481. AssertNotNull(rbio);
  3482. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3483. AssertNotNull(pkey);
  3484. wbio = BIO_new(BIO_s_mem());
  3485. AssertNotNull(wbio);
  3486. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3487. BIO_gets(wbio, line, sizeof(line));
  3488. strcpy(line1, "Public-Key: (256 bit)\n");
  3489. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3490. BIO_gets(wbio, line, sizeof(line));
  3491. strcpy(line1, "pub:\n");
  3492. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3493. BIO_gets(wbio, line, sizeof(line));
  3494. strcpy(line1,
  3495. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3496. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3497. /* skip to the end of pub element*/
  3498. for (i = 0; i < 4 ;i++) {
  3499. BIO_gets(wbio, line, sizeof(line));
  3500. }
  3501. BIO_gets(wbio, line, sizeof(line));
  3502. strcpy(line1, "ASN1 OID: prime256v1\n");
  3503. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3504. BIO_gets(wbio, line, sizeof(line));
  3505. strcpy(line1, "NIST CURVE: P-256\n");
  3506. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3507. /* should reach EOF */
  3508. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3509. EVP_PKEY_free(pkey);
  3510. pkey = NULL;
  3511. BIO_free(rbio);
  3512. BIO_free(wbio);
  3513. rbio = NULL;
  3514. wbio = NULL;
  3515. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3516. /*
  3517. * test DH public key print
  3518. */
  3519. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3520. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3521. sizeof_dh_pub_key_der_2048);
  3522. AssertNotNull(rbio);
  3523. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3524. AssertNotNull(pkey);
  3525. wbio = BIO_new(BIO_s_mem());
  3526. AssertNotNull(wbio);
  3527. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3528. BIO_gets(wbio, line, sizeof(line));
  3529. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3530. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3531. BIO_gets(wbio, line, sizeof(line));
  3532. strcpy(line1, "public-key:\n");
  3533. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3534. BIO_gets(wbio, line, sizeof(line));
  3535. strcpy(line1,
  3536. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3537. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3538. /* skip to the end of public-key element*/
  3539. for (i = 0; i < 17 ;i++) {
  3540. BIO_gets(wbio, line, sizeof(line));
  3541. }
  3542. BIO_gets(wbio, line, sizeof(line));
  3543. strcpy(line1, "prime:\n");
  3544. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3545. BIO_gets(wbio, line, sizeof(line));
  3546. strcpy(line1,
  3547. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3548. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3549. /* skip to the end of prime element*/
  3550. for (i = 0; i < 17 ;i++) {
  3551. BIO_gets(wbio, line, sizeof(line));
  3552. }
  3553. BIO_gets(wbio, line, sizeof(line));
  3554. strcpy(line1, "generator: 2 (0x02)\n");
  3555. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3556. /* should reach EOF */
  3557. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3558. EVP_PKEY_free(pkey);
  3559. pkey = NULL;
  3560. BIO_free(rbio);
  3561. BIO_free(wbio);
  3562. rbio = NULL;
  3563. wbio = NULL;
  3564. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3565. /* to prevent "unused variable" warning */
  3566. (void)pkey;
  3567. (void)wbio;
  3568. (void)rbio;
  3569. (void)line;
  3570. (void)line1;
  3571. (void)i;
  3572. res = TEST_RES_CHECK(1);
  3573. #endif /* OPENSSL_EXTRA */
  3574. return res;
  3575. }
  3576. /* Test functions for base64 encode/decode */
  3577. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3578. {
  3579. int res = TEST_SKIPPED;
  3580. #if defined(OPENSSL_EXTRA) && \
  3581. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3582. EVP_ENCODE_CTX* ctx = NULL;
  3583. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3584. AssertIntEQ( ctx->remaining,0);
  3585. AssertIntEQ( ctx->data[0],0);
  3586. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3587. EVP_ENCODE_CTX_free(ctx);
  3588. res = TEST_RES_CHECK(1);
  3589. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3590. return res;
  3591. }
  3592. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3593. {
  3594. int res = TEST_SKIPPED;
  3595. #if defined(OPENSSL_EXTRA) && \
  3596. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3597. EVP_ENCODE_CTX* ctx = NULL;
  3598. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3599. EVP_ENCODE_CTX_free(ctx);
  3600. res = TEST_RES_CHECK(1);
  3601. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3602. return res;
  3603. }
  3604. static int test_wolfSSL_EVP_EncodeInit(void)
  3605. {
  3606. int res = TEST_SKIPPED;
  3607. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3608. EVP_ENCODE_CTX* ctx = NULL;
  3609. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3610. AssertIntEQ( ctx->remaining,0);
  3611. AssertIntEQ( ctx->data[0],0);
  3612. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3613. /* make ctx dirty */
  3614. ctx->remaining = 10;
  3615. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3616. EVP_EncodeInit(ctx);
  3617. AssertIntEQ( ctx->remaining,0);
  3618. AssertIntEQ( ctx->data[0],0);
  3619. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3620. EVP_ENCODE_CTX_free(ctx);
  3621. res = TEST_RES_CHECK(1);
  3622. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3623. return res;
  3624. }
  3625. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3626. {
  3627. int res = TEST_SKIPPED;
  3628. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3629. int outl;
  3630. int total;
  3631. const unsigned char plain0[] = {"Th"};
  3632. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3633. const unsigned char plain2[] = {"This is additional data."};
  3634. const unsigned char enc0[] = {"VGg=\n"};
  3635. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3636. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3637. const unsigned char enc2[] =
  3638. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3639. unsigned char encOutBuff[300];
  3640. EVP_ENCODE_CTX* ctx = NULL;
  3641. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3642. EVP_EncodeInit(ctx);
  3643. /* illegal parameter test */
  3644. AssertIntEQ(
  3645. EVP_EncodeUpdate(
  3646. NULL, /* pass NULL as ctx */
  3647. encOutBuff,
  3648. &outl,
  3649. plain1,
  3650. sizeof(plain1)-1),
  3651. 0 /* expected result code 0: fail */
  3652. );
  3653. AssertIntEQ(
  3654. EVP_EncodeUpdate(
  3655. ctx,
  3656. NULL, /* pass NULL as out buff */
  3657. &outl,
  3658. plain1,
  3659. sizeof(plain1)-1),
  3660. 0 /* expected result code 0: fail */
  3661. );
  3662. AssertIntEQ(
  3663. EVP_EncodeUpdate(
  3664. ctx,
  3665. encOutBuff,
  3666. NULL, /* pass NULL as outl */
  3667. plain1,
  3668. sizeof(plain1)-1),
  3669. 0 /* expected result code 0: fail */
  3670. );
  3671. AssertIntEQ(
  3672. EVP_EncodeUpdate(
  3673. ctx,
  3674. encOutBuff,
  3675. &outl,
  3676. NULL, /* pass NULL as in */
  3677. sizeof(plain1)-1),
  3678. 0 /* expected result code 0: fail */
  3679. );
  3680. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3681. /* meaningless parameter test */
  3682. AssertIntEQ(
  3683. EVP_EncodeUpdate(
  3684. ctx,
  3685. encOutBuff,
  3686. &outl,
  3687. plain1,
  3688. 0), /* pass zero input */
  3689. 1 /* expected result code 1: success */
  3690. );
  3691. /* very small data encoding test */
  3692. EVP_EncodeInit(ctx);
  3693. AssertIntEQ(
  3694. EVP_EncodeUpdate(
  3695. ctx,
  3696. encOutBuff,
  3697. &outl,
  3698. plain0,
  3699. sizeof(plain0)-1),
  3700. 1 /* expected result code 1: success */
  3701. );
  3702. AssertIntEQ(outl,0);
  3703. EVP_EncodeFinal(
  3704. ctx,
  3705. encOutBuff + outl,
  3706. &outl);
  3707. AssertIntEQ( outl, sizeof(enc0)-1);
  3708. AssertIntEQ(
  3709. XSTRNCMP(
  3710. (const char*)encOutBuff,
  3711. (const char*)enc0,sizeof(enc0) ),
  3712. 0);
  3713. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3714. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3715. sizeof(enc0)-1);
  3716. AssertIntEQ(
  3717. XSTRNCMP(
  3718. (const char*)encOutBuff,
  3719. (const char*)enc0,sizeof(enc0) ),
  3720. 0);
  3721. /* pass small size( < 48bytes ) input, then make sure they are not
  3722. * encoded and just stored in ctx
  3723. */
  3724. EVP_EncodeInit(ctx);
  3725. total = 0;
  3726. outl = 0;
  3727. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3728. AssertIntEQ(
  3729. EVP_EncodeUpdate(
  3730. ctx,
  3731. encOutBuff, /* buffer for output */
  3732. &outl, /* size of output */
  3733. plain1, /* input */
  3734. sizeof(plain1)-1), /* size of input */
  3735. 1); /* expected result code 1:success */
  3736. total += outl;
  3737. AssertIntEQ(outl, 0); /* no output expected */
  3738. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3739. AssertTrue(
  3740. XSTRNCMP((const char*)(ctx->data),
  3741. (const char*)plain1,
  3742. ctx->remaining) ==0 );
  3743. AssertTrue(encOutBuff[0] == 0);
  3744. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3745. * the stored data and the new input together
  3746. */
  3747. AssertIntEQ(
  3748. EVP_EncodeUpdate(
  3749. ctx,
  3750. encOutBuff + outl, /* buffer for output */
  3751. &outl, /* size of output */
  3752. plain2, /* additional input */
  3753. sizeof(plain2) -1), /* size of additional input */
  3754. 1); /* expected result code 1:success */
  3755. total += outl;
  3756. AssertIntNE(outl, 0); /* some output is expected this time*/
  3757. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3758. AssertIntEQ(
  3759. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3760. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3761. EVP_EncodeFinal(
  3762. ctx,
  3763. encOutBuff + outl,
  3764. &outl);
  3765. total += outl;
  3766. AssertIntNE(total,0);
  3767. AssertIntNE(outl,0);
  3768. AssertIntEQ(XSTRNCMP(
  3769. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3770. /* test with illeagal parameters */
  3771. outl = 1;
  3772. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3773. AssertIntEQ(outl, 0);
  3774. outl = 1;
  3775. EVP_EncodeFinal(ctx, NULL, &outl);
  3776. AssertIntEQ(outl, 0);
  3777. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3778. EVP_EncodeFinal(NULL, NULL, NULL);
  3779. EVP_ENCODE_CTX_free(ctx);
  3780. res = TEST_RES_CHECK(1);
  3781. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3782. return res;
  3783. }
  3784. static int test_wolfSSL_EVP_EncodeFinal(void)
  3785. {
  3786. int res = TEST_SKIPPED;
  3787. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3788. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3789. * test_wolfSSL_EVP_EncodeUpdate
  3790. */
  3791. res = TEST_RES_CHECK(1);
  3792. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3793. return res;
  3794. }
  3795. static int test_wolfSSL_EVP_DecodeInit(void)
  3796. {
  3797. int res = TEST_SKIPPED;
  3798. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3799. EVP_ENCODE_CTX* ctx = NULL;
  3800. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3801. AssertIntEQ( ctx->remaining,0);
  3802. AssertIntEQ( ctx->data[0],0);
  3803. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3804. /* make ctx dirty */
  3805. ctx->remaining = 10;
  3806. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3807. EVP_DecodeInit(ctx);
  3808. AssertIntEQ( ctx->remaining,0);
  3809. AssertIntEQ( ctx->data[0],0);
  3810. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3811. EVP_ENCODE_CTX_free(ctx);
  3812. res = TEST_RES_CHECK(1);
  3813. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3814. return res;
  3815. }
  3816. static int test_wolfSSL_EVP_DecodeUpdate(void)
  3817. {
  3818. int res = TEST_SKIPPED;
  3819. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3820. int outl;
  3821. unsigned char decOutBuff[300];
  3822. EVP_ENCODE_CTX* ctx;
  3823. static const unsigned char enc1[] =
  3824. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3825. /* const unsigned char plain1[] =
  3826. {"This is a base64 decoding test."} */
  3827. ctx = EVP_ENCODE_CTX_new();
  3828. EVP_DecodeInit(ctx);
  3829. /* illegal parameter tests */
  3830. /* pass NULL as ctx */
  3831. AssertIntEQ(
  3832. EVP_DecodeUpdate(
  3833. NULL, /* pass NULL as ctx */
  3834. decOutBuff,
  3835. &outl,
  3836. enc1,
  3837. sizeof(enc1)-1),
  3838. -1 /* expected result code -1: fail */
  3839. );
  3840. AssertIntEQ( outl, 0);
  3841. /* pass NULL as output */
  3842. AssertIntEQ(
  3843. EVP_DecodeUpdate(
  3844. ctx,
  3845. NULL, /* pass NULL as out buff */
  3846. &outl,
  3847. enc1,
  3848. sizeof(enc1)-1),
  3849. -1 /* expected result code -1: fail */
  3850. );
  3851. AssertIntEQ( outl, 0);
  3852. /* pass NULL as outl */
  3853. AssertIntEQ(
  3854. EVP_DecodeUpdate(
  3855. ctx,
  3856. decOutBuff,
  3857. NULL, /* pass NULL as outl */
  3858. enc1,
  3859. sizeof(enc1)-1),
  3860. -1 /* expected result code -1: fail */
  3861. );
  3862. /* pass NULL as input */
  3863. AssertIntEQ(
  3864. EVP_DecodeUpdate(
  3865. ctx,
  3866. decOutBuff,
  3867. &outl,
  3868. NULL, /* pass NULL as in */
  3869. sizeof(enc1)-1),
  3870. -1 /* expected result code -1: fail */
  3871. );
  3872. AssertIntEQ( outl, 0);
  3873. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  3874. /* pass zero length input */
  3875. AssertIntEQ(
  3876. EVP_DecodeUpdate(
  3877. ctx,
  3878. decOutBuff,
  3879. &outl,
  3880. enc1,
  3881. 0), /* pass zero as input len */
  3882. 1 /* expected result code 1: success */
  3883. );
  3884. /* decode correct base64 string */
  3885. {
  3886. static const unsigned char enc2[] =
  3887. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3888. static const unsigned char plain2[] =
  3889. {"This is a base64 decoding test."};
  3890. EVP_EncodeInit(ctx);
  3891. AssertIntEQ(
  3892. EVP_DecodeUpdate(
  3893. ctx,
  3894. decOutBuff,
  3895. &outl,
  3896. enc2,
  3897. sizeof(enc2)-1),
  3898. 0 /* expected result code 0: success */
  3899. );
  3900. AssertIntEQ(outl,sizeof(plain2) -1);
  3901. AssertIntEQ(
  3902. EVP_DecodeFinal(
  3903. ctx,
  3904. decOutBuff + outl,
  3905. &outl),
  3906. 1 /* expected result code 1: success */
  3907. );
  3908. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  3909. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3910. sizeof(plain2) -1 ),0);
  3911. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  3912. sizeof(plain2)-1);
  3913. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3914. sizeof(plain2) -1 ),0);
  3915. }
  3916. /* decode correct base64 string which does not have '\n' in its last*/
  3917. {
  3918. static const unsigned char enc3[] =
  3919. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  3920. static const unsigned char plain3[] =
  3921. {"This is a base64 decoding test."}; /* 31 chars */
  3922. EVP_EncodeInit(ctx);
  3923. AssertIntEQ(
  3924. EVP_DecodeUpdate(
  3925. ctx,
  3926. decOutBuff,
  3927. &outl,
  3928. enc3,
  3929. sizeof(enc3)-1),
  3930. 0 /* expected result code 0: success */
  3931. );
  3932. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  3933. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3934. sizeof(plain3) -1 ),0);
  3935. AssertIntEQ(
  3936. EVP_DecodeFinal(
  3937. ctx,
  3938. decOutBuff + outl,
  3939. &outl),
  3940. 1 /* expected result code 1: success */
  3941. );
  3942. AssertIntEQ(outl,0 );
  3943. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  3944. sizeof(plain3)-1);
  3945. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3946. sizeof(plain3) -1 ),0);
  3947. }
  3948. /* decode string which has a padding char ('=') in the illegal position*/
  3949. {
  3950. static const unsigned char enc4[] =
  3951. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  3952. EVP_EncodeInit(ctx);
  3953. AssertIntEQ(
  3954. EVP_DecodeUpdate(
  3955. ctx,
  3956. decOutBuff,
  3957. &outl,
  3958. enc4,
  3959. sizeof(enc4)-1),
  3960. -1 /* expected result code -1: error */
  3961. );
  3962. AssertIntEQ(outl,0);
  3963. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  3964. }
  3965. /* small data decode test */
  3966. {
  3967. static const unsigned char enc00[] = {"VG"};
  3968. static const unsigned char enc01[] = {"g=\n"};
  3969. static const unsigned char plain4[] = {"Th"};
  3970. EVP_EncodeInit(ctx);
  3971. AssertIntEQ(
  3972. EVP_DecodeUpdate(
  3973. ctx,
  3974. decOutBuff,
  3975. &outl,
  3976. enc00,
  3977. sizeof(enc00)-1),
  3978. 1 /* expected result code 1: success */
  3979. );
  3980. AssertIntEQ(outl,0);
  3981. AssertIntEQ(
  3982. EVP_DecodeUpdate(
  3983. ctx,
  3984. decOutBuff + outl,
  3985. &outl,
  3986. enc01,
  3987. sizeof(enc01)-1),
  3988. 0 /* expected result code 0: success */
  3989. );
  3990. AssertIntEQ(outl,sizeof(plain4)-1);
  3991. /* test with illegal parameters */
  3992. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  3993. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  3994. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  3995. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  3996. EVP_DecodeFinal(
  3997. ctx,
  3998. decOutBuff + outl,
  3999. &outl);
  4000. AssertIntEQ( outl, 0);
  4001. AssertIntEQ(
  4002. XSTRNCMP(
  4003. (const char*)decOutBuff,
  4004. (const char*)plain4,sizeof(plain4)-1 ),
  4005. 0);
  4006. }
  4007. EVP_ENCODE_CTX_free(ctx);
  4008. res = TEST_RES_CHECK(1);
  4009. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4010. return res;
  4011. }
  4012. static int test_wolfSSL_EVP_DecodeFinal(void)
  4013. {
  4014. int res = TEST_SKIPPED;
  4015. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4016. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4017. * test_wolfSSL_EVP_DecodeUpdate
  4018. */
  4019. res = TEST_RES_CHECK(1);
  4020. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4021. return res;
  4022. }
  4023. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4024. */
  4025. #ifdef OPENSSL_EXTRA
  4026. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4027. {
  4028. #ifndef NO_AES
  4029. const WOLFSSL_EVP_CIPHER* c;
  4030. c = wolfSSL_EVP_get_cipherbynid(419);
  4031. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4032. defined(WOLFSSL_AES_128)
  4033. AssertNotNull(c);
  4034. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  4035. #else
  4036. AssertNull(c);
  4037. #endif
  4038. c = wolfSSL_EVP_get_cipherbynid(423);
  4039. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4040. defined(WOLFSSL_AES_192)
  4041. AssertNotNull(c);
  4042. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  4043. #else
  4044. AssertNull(c);
  4045. #endif
  4046. c = wolfSSL_EVP_get_cipherbynid(427);
  4047. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4048. defined(WOLFSSL_AES_256)
  4049. AssertNotNull(c);
  4050. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  4051. #else
  4052. AssertNull(c);
  4053. #endif
  4054. c = wolfSSL_EVP_get_cipherbynid(904);
  4055. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4056. AssertNotNull(c);
  4057. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  4058. #else
  4059. AssertNull(c);
  4060. #endif
  4061. c = wolfSSL_EVP_get_cipherbynid(905);
  4062. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4063. AssertNotNull(c);
  4064. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  4065. #else
  4066. AssertNull(c);
  4067. #endif
  4068. c = wolfSSL_EVP_get_cipherbynid(906);
  4069. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4070. AssertNotNull(c);
  4071. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  4072. #else
  4073. AssertNull(c);
  4074. #endif
  4075. c = wolfSSL_EVP_get_cipherbynid(418);
  4076. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4077. AssertNotNull(c);
  4078. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  4079. #else
  4080. AssertNull(c);
  4081. #endif
  4082. c = wolfSSL_EVP_get_cipherbynid(422);
  4083. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4084. AssertNotNull(c);
  4085. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  4086. #else
  4087. AssertNull(c);
  4088. #endif
  4089. c = wolfSSL_EVP_get_cipherbynid(426);
  4090. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4091. AssertNotNull(c);
  4092. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  4093. #else
  4094. AssertNull(c);
  4095. #endif
  4096. #endif /* !NO_AES */
  4097. #ifndef NO_DES3
  4098. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4099. #ifdef WOLFSSL_DES_ECB
  4100. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4101. #endif
  4102. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4103. #ifdef WOLFSSL_DES_ECB
  4104. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4105. #endif
  4106. #endif /* !NO_DES3 */
  4107. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4108. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4109. #endif
  4110. /* test for nid is out of range */
  4111. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  4112. return TEST_RES_CHECK(1);
  4113. }
  4114. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4115. {
  4116. int res = TEST_SKIPPED;
  4117. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4118. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4119. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4120. const EVP_CIPHER *test;
  4121. byte key[AES_BLOCK_SIZE] = {0};
  4122. byte iv[AES_BLOCK_SIZE] = {0};
  4123. AssertNotNull(ctx);
  4124. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4125. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4126. test = EVP_CIPHER_CTX_cipher(ctx);
  4127. AssertTrue(init == test);
  4128. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4129. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4130. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4131. EVP_CIPHER_CTX_free(ctx);
  4132. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4133. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4134. res = TEST_RES_CHECK(1);
  4135. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4136. return res;
  4137. }
  4138. #endif /* OPENSSL_EXTRA */
  4139. /*----------------------------------------------------------------------------*
  4140. | IO
  4141. *----------------------------------------------------------------------------*/
  4142. /* helper functions */
  4143. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  4144. #ifdef WOLFSSL_SESSION_EXPORT
  4145. #ifdef WOLFSSL_DTLS
  4146. /* set up function for sending session information */
  4147. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  4148. {
  4149. WOLFSSL_CTX* ctx = NULL;
  4150. WOLFSSL* ssl = NULL;
  4151. AssertNotNull(inSsl);
  4152. AssertNotNull(buf);
  4153. AssertIntNE(0, sz);
  4154. /* Set ctx to DTLS 1.2 */
  4155. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  4156. AssertNotNull(ctx);
  4157. ssl = wolfSSL_new(ctx);
  4158. AssertNotNull(ssl);
  4159. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  4160. wolfSSL_free(ssl);
  4161. wolfSSL_CTX_free(ctx);
  4162. (void)userCtx;
  4163. return 0;
  4164. }
  4165. #endif
  4166. /* returns negative value on fail and positive (including 0) on success */
  4167. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  4168. {
  4169. int ret, err, loop_count, count, timeout = 10;
  4170. char msg[] = "I hear you fa shizzle!";
  4171. char input[1024];
  4172. loop_count = ((func_args*)args)->argc;
  4173. #ifdef WOLFSSL_ASYNC_CRYPT
  4174. err = 0; /* Reset error */
  4175. #endif
  4176. do {
  4177. #ifdef WOLFSSL_ASYNC_CRYPT
  4178. if (err == WC_PENDING_E) {
  4179. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4180. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4181. }
  4182. #endif
  4183. ret = wolfSSL_accept(ssl);
  4184. err = wolfSSL_get_error(ssl, 0);
  4185. if (err == WOLFSSL_ERROR_WANT_READ ||
  4186. err == WOLFSSL_ERROR_WANT_WRITE) {
  4187. int select_ret;
  4188. err = WC_PENDING_E;
  4189. select_ret = tcp_select(*sockfd, timeout);
  4190. if (select_ret == TEST_TIMEOUT) {
  4191. return WOLFSSL_FATAL_ERROR;
  4192. }
  4193. }
  4194. } while (err == WC_PENDING_E);
  4195. if (ret != WOLFSSL_SUCCESS) {
  4196. char buff[WOLFSSL_MAX_ERROR_SZ];
  4197. fprintf(stderr, "error = %d, %s\n", err,
  4198. wolfSSL_ERR_error_string(err, buff));
  4199. return ret;
  4200. }
  4201. for (count = 0; count < loop_count; count++) {
  4202. int select_ret;
  4203. select_ret = tcp_select(*sockfd, timeout);
  4204. if (select_ret == TEST_TIMEOUT) {
  4205. ret = WOLFSSL_FATAL_ERROR;
  4206. break;
  4207. }
  4208. do {
  4209. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4210. if (ret > 0) {
  4211. input[ret] = '\0';
  4212. fprintf(stderr, "Client message: %s\n", input);
  4213. }
  4214. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4215. do {
  4216. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  4217. return WOLFSSL_FATAL_ERROR;
  4218. }
  4219. err = wolfSSL_get_error(ssl, ret);
  4220. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4221. }
  4222. return ret;
  4223. }
  4224. #endif /* WOLFSSL_SESSION_EXPORT */
  4225. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4226. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4227. defined(HAVE_AES_CBC)
  4228. typedef struct openssl_key_ctx {
  4229. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4230. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4231. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4232. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4233. } openssl_key_ctx;
  4234. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4235. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4236. static WC_INLINE int OpenSSLTicketInit(void)
  4237. {
  4238. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4239. if (ret != 0) return ret;
  4240. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4241. sizeof(myOpenSSLKey_ctx.name));
  4242. if (ret != 0) return ret;
  4243. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4244. sizeof(myOpenSSLKey_ctx.key));
  4245. if (ret != 0) return ret;
  4246. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4247. sizeof(myOpenSSLKey_ctx.hmacKey));
  4248. if (ret != 0) return ret;
  4249. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4250. sizeof(myOpenSSLKey_ctx.iv));
  4251. if (ret != 0) return ret;
  4252. return 0;
  4253. }
  4254. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4255. byte name[WOLFSSL_TICKET_NAME_SZ],
  4256. byte iv[WOLFSSL_TICKET_IV_SZ],
  4257. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4258. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4259. (void)ssl;
  4260. if (enc) {
  4261. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4262. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4263. }
  4264. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4265. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4266. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4267. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4268. return 0;
  4269. }
  4270. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4271. if (enc)
  4272. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4273. else
  4274. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4275. return 1;
  4276. }
  4277. static WC_INLINE void OpenSSLTicketCleanup(void)
  4278. {
  4279. wc_FreeRng(&myOpenSSLKey_rng);
  4280. }
  4281. #endif
  4282. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4283. #ifdef WC_SHA512_DIGEST_SIZE
  4284. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4285. #else
  4286. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4287. #endif
  4288. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4289. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4290. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4291. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4292. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4293. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  4294. {
  4295. SOCKET_T sockfd = 0;
  4296. SOCKET_T clientfd = 0;
  4297. word16 port;
  4298. callback_functions* cbf;
  4299. WOLFSSL_CTX* ctx = 0;
  4300. WOLFSSL* ssl = 0;
  4301. func_args* opts = (func_args*)args;
  4302. char msg[] = "I hear you fa shizzle!";
  4303. char input[1024];
  4304. int idx;
  4305. int ret, err = 0;
  4306. int sharedCtx = 0;
  4307. int doUdp = 0;
  4308. SOCKADDR_IN_T cliAddr;
  4309. socklen_t cliLen;
  4310. const char* certFile = svrCertFile;
  4311. const char* keyFile = svrKeyFile;
  4312. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4313. size_t msg_len = 0;
  4314. #endif
  4315. #ifdef WOLFSSL_TIRTOS
  4316. fdOpenSession(Task_self());
  4317. #endif
  4318. opts->return_code = TEST_FAIL;
  4319. cbf = opts->callbacks;
  4320. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4321. if (cbf != NULL && cbf->ctx) {
  4322. ctx = cbf->ctx;
  4323. sharedCtx = 1;
  4324. }
  4325. else
  4326. #endif
  4327. {
  4328. WOLFSSL_METHOD* method = NULL;
  4329. if (cbf != NULL && cbf->method != NULL) {
  4330. method = cbf->method();
  4331. }
  4332. else {
  4333. method = wolfSSLv23_server_method();
  4334. }
  4335. ctx = wolfSSL_CTX_new(method);
  4336. }
  4337. if (ctx == NULL) {
  4338. goto done;
  4339. }
  4340. if (cbf == NULL || !cbf->ticNoInit) {
  4341. #if defined(HAVE_SESSION_TICKET) && \
  4342. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4343. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4344. OpenSSLTicketInit();
  4345. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4346. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4347. TicketInit();
  4348. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4349. #endif
  4350. #endif
  4351. }
  4352. #if defined(USE_WINDOWS_API)
  4353. port = opts->signal->port;
  4354. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4355. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4356. /* Let tcp_listen assign port */
  4357. port = 0;
  4358. #else
  4359. /* Use default port */
  4360. port = wolfSSLPort;
  4361. #endif
  4362. if (cbf != NULL)
  4363. doUdp = cbf->doUdp;
  4364. /* do it here to detect failure */
  4365. tcp_accept(
  4366. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4367. if (doUdp) {
  4368. cliLen = sizeof(cliAddr);
  4369. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4370. (struct sockaddr*)&cliAddr, &cliLen);
  4371. AssertIntGT(idx, 0);
  4372. }
  4373. else {
  4374. CloseSocket(sockfd);
  4375. }
  4376. wolfSSL_CTX_set_verify(ctx,
  4377. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4378. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4379. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4380. #endif
  4381. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4382. != WOLFSSL_SUCCESS) {
  4383. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4384. goto done;
  4385. }
  4386. if (cbf != NULL && cbf->certPemFile != NULL)
  4387. certFile = cbf->certPemFile;
  4388. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4389. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  4390. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4391. #else
  4392. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  4393. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4394. #endif
  4395. /*err_sys("can't load server cert chain file, "
  4396. "Please run from wolfSSL home dir");*/
  4397. goto done;
  4398. }
  4399. if (cbf != NULL && cbf->keyPemFile != NULL)
  4400. keyFile = cbf->keyPemFile;
  4401. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4402. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  4403. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4404. #else
  4405. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  4406. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4407. #endif
  4408. /*err_sys("can't load server key file, "
  4409. "Please run from wolfSSL home dir");*/
  4410. goto done;
  4411. }
  4412. #ifdef HAVE_CRL
  4413. if (cbf != NULL && cbf->crlPemFile != NULL) {
  4414. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  4415. goto done;
  4416. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  4417. != WOLFSSL_SUCCESS)
  4418. goto done;
  4419. }
  4420. #endif
  4421. /* call ctx setup callback */
  4422. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4423. cbf->ctx_ready(ctx);
  4424. }
  4425. ssl = wolfSSL_new(ctx);
  4426. if (ssl == NULL) {
  4427. goto done;
  4428. }
  4429. if (doUdp) {
  4430. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4431. if (err != WOLFSSL_SUCCESS)
  4432. goto done;
  4433. }
  4434. #ifdef WOLFSSL_SESSION_EXPORT
  4435. /* only add in more complex nonblocking case with session export tests */
  4436. if (args && opts->argc > 0) {
  4437. /* set as nonblock and time out for waiting on read/write */
  4438. tcp_set_nonblocking(&clientfd);
  4439. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  4440. }
  4441. #endif
  4442. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4443. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  4444. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4445. #else
  4446. if (wolfSSL_use_certificate_file(ssl, certFile,
  4447. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4448. #endif
  4449. /*err_sys("can't load server cert chain file, "
  4450. "Please run from wolfSSL home dir");*/
  4451. goto done;
  4452. }
  4453. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4454. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  4455. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4456. #else
  4457. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  4458. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4459. #endif
  4460. /*err_sys("can't load server key file, "
  4461. "Please run from wolfSSL home dir");*/
  4462. goto done;
  4463. }
  4464. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4465. /*err_sys("SSL_set_fd failed");*/
  4466. goto done;
  4467. }
  4468. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4469. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4470. #elif !defined(NO_DH)
  4471. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4472. #endif
  4473. /* call ssl setup callback */
  4474. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4475. cbf->ssl_ready(ssl);
  4476. }
  4477. #ifdef WOLFSSL_SESSION_EXPORT
  4478. /* only add in more complex nonblocking case with session export tests */
  4479. if (opts->argc > 0) {
  4480. ret = nonblocking_accept_read(args, ssl, &clientfd);
  4481. if (ret >= 0) {
  4482. opts->return_code = TEST_SUCCESS;
  4483. }
  4484. #ifdef WOLFSSL_TIRTOS
  4485. Task_yield();
  4486. #endif
  4487. goto done;
  4488. }
  4489. #endif
  4490. #ifdef WOLFSSL_ASYNC_CRYPT
  4491. err = 0; /* Reset error */
  4492. #endif
  4493. do {
  4494. #ifdef WOLFSSL_ASYNC_CRYPT
  4495. if (err == WC_PENDING_E) {
  4496. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4497. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4498. }
  4499. #endif
  4500. ret = wolfSSL_accept(ssl);
  4501. err = wolfSSL_get_error(ssl, 0);
  4502. } while (err == WC_PENDING_E);
  4503. if (ret != WOLFSSL_SUCCESS) {
  4504. char buff[WOLFSSL_MAX_ERROR_SZ];
  4505. fprintf(stderr, "error = %d, %s\n", err,
  4506. wolfSSL_ERR_error_string(err, buff));
  4507. /*err_sys("SSL_accept failed");*/
  4508. goto done;
  4509. }
  4510. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4511. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4512. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  4513. AssertIntGE(msg_len, 0);
  4514. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4515. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  4516. AssertIntGE(msg_len, 0);
  4517. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4518. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4519. if (idx > 0) {
  4520. input[idx] = '\0';
  4521. fprintf(stderr, "Client message: %s\n", input);
  4522. }
  4523. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4524. /*err_sys("SSL_write failed");*/
  4525. #ifdef WOLFSSL_TIRTOS
  4526. return;
  4527. #else
  4528. return 0;
  4529. #endif
  4530. }
  4531. if (cbf != NULL && cbf->on_result != NULL)
  4532. cbf->on_result(ssl);
  4533. #ifdef WOLFSSL_TIRTOS
  4534. Task_yield();
  4535. #endif
  4536. opts->return_code = TEST_SUCCESS;
  4537. done:
  4538. if (cbf != NULL)
  4539. cbf->last_err = err;
  4540. if (cbf != NULL && cbf->on_cleanup != NULL)
  4541. cbf->on_cleanup(ssl);
  4542. wolfSSL_shutdown(ssl);
  4543. wolfSSL_free(ssl);
  4544. if (!sharedCtx)
  4545. wolfSSL_CTX_free(ctx);
  4546. CloseSocket(clientfd);
  4547. #ifdef WOLFSSL_TIRTOS
  4548. fdCloseSession(Task_self());
  4549. #endif
  4550. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4551. && defined(HAVE_THREAD_LS)
  4552. wc_ecc_fp_free(); /* free per thread cache */
  4553. #endif
  4554. if (cbf == NULL || !cbf->ticNoInit) {
  4555. #if defined(HAVE_SESSION_TICKET) && \
  4556. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4557. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4558. OpenSSLTicketCleanup();
  4559. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4560. TicketCleanup();
  4561. #endif
  4562. #endif
  4563. }
  4564. #ifndef WOLFSSL_TIRTOS
  4565. return 0;
  4566. #endif
  4567. }
  4568. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4569. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4570. {
  4571. SOCKET_T sockfd = 0;
  4572. SOCKET_T clientfd = 0;
  4573. word16 port;
  4574. callback_functions* cbf;
  4575. WOLFSSL_CTX* ctx = 0;
  4576. WOLFSSL* ssl = 0;
  4577. char msg[] = "I hear you fa shizzle!";
  4578. char input[1024];
  4579. int idx;
  4580. int ret, err = 0;
  4581. int sharedCtx = 0;
  4582. int loop_count = ((func_args*)args)->argc;
  4583. int count = 0;
  4584. #ifdef WOLFSSL_TIRTOS
  4585. fdOpenSession(Task_self());
  4586. #endif
  4587. ((func_args*)args)->return_code = TEST_FAIL;
  4588. cbf = ((func_args*)args)->callbacks;
  4589. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4590. if (cbf != NULL && cbf->ctx) {
  4591. ctx = cbf->ctx;
  4592. sharedCtx = 1;
  4593. }
  4594. else
  4595. #endif
  4596. {
  4597. WOLFSSL_METHOD* method = NULL;
  4598. if (cbf != NULL && cbf->method != NULL) {
  4599. method = cbf->method();
  4600. }
  4601. else {
  4602. method = wolfSSLv23_server_method();
  4603. }
  4604. ctx = wolfSSL_CTX_new(method);
  4605. }
  4606. #if defined(USE_WINDOWS_API)
  4607. port = ((func_args*)args)->signal->port;
  4608. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4609. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4610. /* Let tcp_listen assign port */
  4611. port = 0;
  4612. #else
  4613. /* Use default port */
  4614. port = wolfSSLPort;
  4615. #endif
  4616. wolfSSL_CTX_set_verify(ctx,
  4617. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4618. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4619. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4620. #endif
  4621. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4622. != WOLFSSL_SUCCESS) {
  4623. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4624. goto done;
  4625. }
  4626. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4627. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4628. /*err_sys("can't load server cert chain file, "
  4629. "Please run from wolfSSL home dir");*/
  4630. goto done;
  4631. }
  4632. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4633. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4634. /*err_sys("can't load server key file, "
  4635. "Please run from wolfSSL home dir");*/
  4636. goto done;
  4637. }
  4638. /* call ctx setup callback */
  4639. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4640. cbf->ctx_ready(ctx);
  4641. }
  4642. while (count != loop_count) {
  4643. ssl = wolfSSL_new(ctx);
  4644. if (ssl == NULL) {
  4645. goto done;
  4646. }
  4647. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4648. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4649. /*err_sys("can't load server cert chain file, "
  4650. "Please run from wolfSSL home dir");*/
  4651. goto done;
  4652. }
  4653. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4654. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4655. /*err_sys("can't load server key file, "
  4656. "Please run from wolfSSL home dir");*/
  4657. goto done;
  4658. }
  4659. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4660. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4661. #elif !defined(NO_DH)
  4662. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4663. #endif
  4664. /* call ssl setup callback */
  4665. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4666. cbf->ssl_ready(ssl);
  4667. }
  4668. /* do it here to detect failure */
  4669. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4670. CloseSocket(sockfd);
  4671. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4672. /*err_sys("SSL_set_fd failed");*/
  4673. goto done;
  4674. }
  4675. #ifdef WOLFSSL_ASYNC_CRYPT
  4676. err = 0; /* Reset error */
  4677. #endif
  4678. do {
  4679. #ifdef WOLFSSL_ASYNC_CRYPT
  4680. if (err == WC_PENDING_E) {
  4681. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4682. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4683. }
  4684. #endif
  4685. ret = wolfSSL_accept(ssl);
  4686. err = wolfSSL_get_error(ssl, 0);
  4687. } while (err == WC_PENDING_E);
  4688. if (ret != WOLFSSL_SUCCESS) {
  4689. char buff[WOLFSSL_MAX_ERROR_SZ];
  4690. fprintf(stderr, "error = %d, %s\n", err,
  4691. wolfSSL_ERR_error_string(err, buff));
  4692. /*err_sys("SSL_accept failed");*/
  4693. goto done;
  4694. }
  4695. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4696. if (idx > 0) {
  4697. input[idx] = '\0';
  4698. fprintf(stderr, "Client message: %s\n", input);
  4699. }
  4700. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4701. /*err_sys("SSL_write failed");*/
  4702. #ifdef WOLFSSL_TIRTOS
  4703. return;
  4704. #else
  4705. return 0;
  4706. #endif
  4707. }
  4708. /* free ssl for this connection */
  4709. wolfSSL_shutdown(ssl);
  4710. wolfSSL_free(ssl); ssl = NULL;
  4711. CloseSocket(clientfd);
  4712. count++;
  4713. }
  4714. #ifdef WOLFSSL_TIRTOS
  4715. Task_yield();
  4716. #endif
  4717. ((func_args*)args)->return_code = TEST_SUCCESS;
  4718. done:
  4719. if (ssl != NULL) {
  4720. wolfSSL_shutdown(ssl);
  4721. wolfSSL_free(ssl);
  4722. }
  4723. if (!sharedCtx)
  4724. wolfSSL_CTX_free(ctx);
  4725. CloseSocket(clientfd);
  4726. #ifdef WOLFSSL_TIRTOS
  4727. fdCloseSession(Task_self());
  4728. #endif
  4729. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4730. && defined(HAVE_THREAD_LS)
  4731. wc_ecc_fp_free(); /* free per thread cache */
  4732. #endif
  4733. #ifndef WOLFSSL_TIRTOS
  4734. return 0;
  4735. #endif
  4736. }
  4737. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4738. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4739. static int test_client_nofail(void* args, cbType cb)
  4740. {
  4741. #if !defined(NO_WOLFSSL_CLIENT)
  4742. SOCKET_T sockfd = 0;
  4743. callback_functions* cbf;
  4744. WOLFSSL_CTX* ctx = 0;
  4745. WOLFSSL* ssl = 0;
  4746. WOLFSSL_CIPHER* cipher;
  4747. char msg[64] = "hello wolfssl!";
  4748. char reply[1024];
  4749. int input;
  4750. int msgSz = (int)XSTRLEN(msg);
  4751. int ret, err = 0;
  4752. int cipherSuite;
  4753. int sharedCtx = 0;
  4754. int doUdp = 0;
  4755. const char* cipherName1, *cipherName2;
  4756. #ifdef WOLFSSL_TIRTOS
  4757. fdOpenSession(Task_self());
  4758. #endif
  4759. ((func_args*)args)->return_code = TEST_FAIL;
  4760. cbf = ((func_args*)args)->callbacks;
  4761. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4762. if (cbf != NULL && cbf->ctx) {
  4763. ctx = cbf->ctx;
  4764. sharedCtx = cbf->isSharedCtx;
  4765. }
  4766. else
  4767. #endif
  4768. {
  4769. WOLFSSL_METHOD* method = NULL;
  4770. if (cbf != NULL && cbf->method != NULL) {
  4771. method = cbf->method();
  4772. }
  4773. else {
  4774. method = wolfSSLv23_client_method();
  4775. }
  4776. ctx = wolfSSL_CTX_new(method);
  4777. }
  4778. if (cbf != NULL)
  4779. doUdp = cbf->doUdp;
  4780. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4781. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4782. #endif
  4783. /* Do connect here so server detects failures */
  4784. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4785. doUdp, 0, NULL);
  4786. /* Connect the socket so that we don't have to set the peer later on */
  4787. if (doUdp)
  4788. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  4789. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4790. {
  4791. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4792. goto done;
  4793. }
  4794. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4795. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4796. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4797. #else
  4798. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4799. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4800. #endif
  4801. /*err_sys("can't load client cert file, "
  4802. "Please run from wolfSSL home dir");*/
  4803. goto done;
  4804. }
  4805. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4806. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4807. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4808. #else
  4809. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4810. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4811. #endif
  4812. /*err_sys("can't load client key file, "
  4813. "Please run from wolfSSL home dir");*/
  4814. goto done;
  4815. }
  4816. #ifdef HAVE_CRL
  4817. if (cbf != NULL && cbf->crlPemFile != NULL) {
  4818. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  4819. goto done;
  4820. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  4821. != WOLFSSL_SUCCESS)
  4822. goto done;
  4823. }
  4824. #endif
  4825. /* call ctx setup callback */
  4826. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4827. cbf->ctx_ready(ctx);
  4828. }
  4829. ssl = wolfSSL_new(ctx);
  4830. if (ssl == NULL) {
  4831. goto done;
  4832. }
  4833. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4834. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4835. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4836. #else
  4837. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4838. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4839. #endif
  4840. /*err_sys("can't load client cert file, "
  4841. "Please run from wolfSSL home dir");*/
  4842. goto done;
  4843. }
  4844. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4845. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4846. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4847. #else
  4848. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4849. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4850. #endif
  4851. /*err_sys("can't load client key file, "
  4852. "Please run from wolfSSL home dir");*/
  4853. goto done;
  4854. }
  4855. if (!doUdp) {
  4856. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4857. /*err_sys("SSL_set_fd failed");*/
  4858. goto done;
  4859. }
  4860. }
  4861. else {
  4862. #ifdef WOLFSSL_DTLS
  4863. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4864. /*err_sys("SSL_set_fd failed");*/
  4865. goto done;
  4866. }
  4867. #else
  4868. goto done;
  4869. #endif
  4870. }
  4871. /* call ssl setup callback */
  4872. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4873. cbf->ssl_ready(ssl);
  4874. }
  4875. #ifdef WOLFSSL_ASYNC_CRYPT
  4876. err = 0; /* Reset error */
  4877. #endif
  4878. do {
  4879. #ifdef WOLFSSL_ASYNC_CRYPT
  4880. if (err == WC_PENDING_E) {
  4881. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4882. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4883. }
  4884. #endif
  4885. ret = wolfSSL_connect(ssl);
  4886. err = wolfSSL_get_error(ssl, 0);
  4887. } while (err == WC_PENDING_E);
  4888. if (ret != WOLFSSL_SUCCESS) {
  4889. char buff[WOLFSSL_MAX_ERROR_SZ];
  4890. fprintf(stderr, "error = %d, %s\n", err,
  4891. wolfSSL_ERR_error_string(err, buff));
  4892. /*err_sys("SSL_connect failed");*/
  4893. goto done;
  4894. }
  4895. /* test the various get cipher methods */
  4896. /* Internal cipher suite names */
  4897. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  4898. cipherName1 = wolfSSL_get_cipher_name(ssl);
  4899. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  4900. (cipherSuite >> 8), cipherSuite & 0xFF);
  4901. AssertStrEQ(cipherName1, cipherName2);
  4902. /* IANA Cipher Suites Names */
  4903. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  4904. then it's the internal cipher suite name */
  4905. cipher = wolfSSL_get_current_cipher(ssl);
  4906. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  4907. cipherName2 = wolfSSL_get_cipher(ssl);
  4908. AssertStrEQ(cipherName1, cipherName2);
  4909. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  4910. !defined(WOLFSSL_QT)
  4911. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  4912. (cipherSuite >> 8), cipherSuite & 0xFF);
  4913. AssertStrEQ(cipherName1, cipherName2);
  4914. #endif
  4915. if (cb != NULL)
  4916. (cb)(ctx, ssl);
  4917. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4918. /*err_sys("SSL_write failed");*/
  4919. goto done;
  4920. }
  4921. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4922. if (input > 0) {
  4923. reply[input] = '\0';
  4924. fprintf(stderr, "Server response: %s\n", reply);
  4925. }
  4926. if (cbf != NULL && cbf->on_result != NULL)
  4927. cbf->on_result(ssl);
  4928. ((func_args*)args)->return_code = TEST_SUCCESS;
  4929. done:
  4930. if (cbf != NULL)
  4931. cbf->last_err = err;
  4932. if (cbf != NULL && cbf->on_cleanup != NULL)
  4933. cbf->on_cleanup(ssl);
  4934. wolfSSL_free(ssl);
  4935. if (!sharedCtx)
  4936. wolfSSL_CTX_free(ctx);
  4937. CloseSocket(sockfd);
  4938. #ifdef WOLFSSL_TIRTOS
  4939. fdCloseSession(Task_self());
  4940. #endif
  4941. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4942. && defined(HAVE_THREAD_LS)
  4943. wc_ecc_fp_free(); /* free per thread cache */
  4944. #endif
  4945. #else
  4946. (void)args;
  4947. (void)cb;
  4948. #endif /* !NO_WOLFSSL_CLIENT */
  4949. return 0;
  4950. }
  4951. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  4952. callback_functions* server_cb)
  4953. {
  4954. func_args client_args;
  4955. func_args server_args;
  4956. tcp_ready ready;
  4957. THREAD_TYPE serverThread;
  4958. XMEMSET(&client_args, 0, sizeof(func_args));
  4959. XMEMSET(&server_args, 0, sizeof(func_args));
  4960. #ifdef WOLFSSL_TIRTOS
  4961. fdOpenSession(Task_self());
  4962. #endif
  4963. StartTCP();
  4964. InitTcpReady(&ready);
  4965. #if defined(USE_WINDOWS_API)
  4966. /* use RNG to get random port if using windows */
  4967. ready.port = GetRandomPort();
  4968. #endif
  4969. server_args.signal = &ready;
  4970. server_args.callbacks = server_cb;
  4971. client_args.signal = &ready;
  4972. client_args.callbacks = client_cb;
  4973. start_thread(test_server_nofail, &server_args, &serverThread);
  4974. wait_tcp_ready(&server_args);
  4975. test_client_nofail(&client_args, NULL);
  4976. join_thread(serverThread);
  4977. client_cb->return_code = client_args.return_code;
  4978. server_cb->return_code = server_args.return_code;
  4979. FreeTcpReady(&ready);
  4980. #ifdef WOLFSSL_TIRTOS
  4981. fdOpenSession(Task_self());
  4982. #endif
  4983. }
  4984. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  4985. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
  4986. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  4987. {
  4988. SOCKET_T sockfd = 0;
  4989. callback_functions* cbf;
  4990. WOLFSSL_CTX* ctx = 0;
  4991. WOLFSSL* ssl = 0;
  4992. WOLFSSL_SESSION* session = NULL;
  4993. char msg[64] = "hello wolfssl!";
  4994. char reply[1024];
  4995. int input;
  4996. int msgSz = (int)XSTRLEN(msg);
  4997. int ret, err = 0;
  4998. int sharedCtx = 0;
  4999. #ifdef WOLFSSL_TIRTOS
  5000. fdOpenSession(Task_self());
  5001. #endif
  5002. ((func_args*)args)->return_code = TEST_FAIL;
  5003. cbf = ((func_args*)args)->callbacks;
  5004. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5005. if (cbf != NULL && cbf->ctx) {
  5006. ctx = cbf->ctx;
  5007. sharedCtx = 1;
  5008. }
  5009. else
  5010. #endif
  5011. {
  5012. WOLFSSL_METHOD* method = NULL;
  5013. if (cbf != NULL && cbf->method != NULL) {
  5014. method = cbf->method();
  5015. }
  5016. else {
  5017. method = wolfSSLv23_client_method();
  5018. }
  5019. ctx = wolfSSL_CTX_new(method);
  5020. }
  5021. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5022. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5023. #endif
  5024. /* Do connect here so server detects failures */
  5025. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5026. 0, 0, NULL);
  5027. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5028. {
  5029. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5030. goto done;
  5031. }
  5032. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5033. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5034. /*err_sys("can't load client cert file, "
  5035. "Please run from wolfSSL home dir");*/
  5036. goto done;
  5037. }
  5038. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5039. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5040. /*err_sys("can't load client key file, "
  5041. "Please run from wolfSSL home dir");*/
  5042. goto done;
  5043. }
  5044. /* call ctx setup callback */
  5045. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5046. cbf->ctx_ready(ctx);
  5047. }
  5048. ssl = wolfSSL_new(ctx);
  5049. if (ssl == NULL) {
  5050. goto done;
  5051. }
  5052. /* keep handshakre resources for re-using WOLFSSL obj */
  5053. wolfSSL_KeepArrays(ssl);
  5054. if (wolfSSL_KeepHandshakeResources(ssl)) {
  5055. /* err_sys("SSL_KeepHandshakeResources failed"); */
  5056. goto done;
  5057. }
  5058. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5059. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5060. /*err_sys("can't load client cert file, "
  5061. "Please run from wolfSSL home dir");*/
  5062. goto done;
  5063. }
  5064. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5065. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5066. /*err_sys("can't load client key file, "
  5067. "Please run from wolfSSL home dir");*/
  5068. goto done;
  5069. }
  5070. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5071. /*err_sys("SSL_set_fd failed");*/
  5072. goto done;
  5073. }
  5074. /* call ssl setup callback */
  5075. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5076. cbf->ssl_ready(ssl);
  5077. }
  5078. #ifdef WOLFSSL_ASYNC_CRYPT
  5079. err = 0; /* Reset error */
  5080. #endif
  5081. do {
  5082. #ifdef WOLFSSL_ASYNC_CRYPT
  5083. if (err == WC_PENDING_E) {
  5084. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5085. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5086. }
  5087. #endif
  5088. ret = wolfSSL_connect(ssl);
  5089. err = wolfSSL_get_error(ssl, 0);
  5090. } while (err == WC_PENDING_E);
  5091. if (ret != WOLFSSL_SUCCESS) {
  5092. char buff[WOLFSSL_MAX_ERROR_SZ];
  5093. fprintf(stderr, "error = %d, %s\n", err,
  5094. wolfSSL_ERR_error_string(err, buff));
  5095. /*err_sys("SSL_connect failed");*/
  5096. goto done;
  5097. }
  5098. /* Build first session */
  5099. if (cb != NULL)
  5100. ((cbType)cb)(ctx, ssl);
  5101. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5102. /*err_sys("SSL_write failed");*/
  5103. goto done;
  5104. }
  5105. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5106. if (input > 0) {
  5107. reply[input] = '\0';
  5108. fprintf(stderr, "Server response: %s\n", reply);
  5109. }
  5110. /* Session Resumption by re-using WOLFSSL object */
  5111. wolfSSL_set_quiet_shutdown(ssl, 1);
  5112. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  5113. /* err_sys ("SSL shutdown failed"); */
  5114. goto done;
  5115. }
  5116. session = wolfSSL_get1_session(ssl);
  5117. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  5118. /* err_sys ("SSL_clear failed"); */
  5119. goto done;
  5120. }
  5121. wolfSSL_set_session(ssl, session);
  5122. wolfSSL_SESSION_free(session);
  5123. session = NULL;
  5124. /* close socket once */
  5125. CloseSocket(sockfd);
  5126. sockfd = 0;
  5127. /* wait until server ready */
  5128. wait_tcp_ready((func_args*)server_args);
  5129. fprintf(stderr, "session resumption\n");
  5130. /* Do re-connect */
  5131. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5132. 0, 0, NULL);
  5133. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5134. /*err_sys("SSL_set_fd failed");*/
  5135. goto done;
  5136. }
  5137. #ifdef WOLFSSL_ASYNC_CRYPT
  5138. err = 0; /* Reset error */
  5139. #endif
  5140. do {
  5141. #ifdef WOLFSSL_ASYNC_CRYPT
  5142. if (err == WC_PENDING_E) {
  5143. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5144. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5145. }
  5146. #endif
  5147. ret = wolfSSL_connect(ssl);
  5148. err = wolfSSL_get_error(ssl, 0);
  5149. } while (err == WC_PENDING_E);
  5150. if (ret != WOLFSSL_SUCCESS) {
  5151. char buff[WOLFSSL_MAX_ERROR_SZ];
  5152. fprintf(stderr, "error = %d, %s\n", err,
  5153. wolfSSL_ERR_error_string(err, buff));
  5154. /*err_sys("SSL_connect failed");*/
  5155. goto done;
  5156. }
  5157. /* Build first session */
  5158. if (cb != NULL)
  5159. ((cbType)cb)(ctx, ssl);
  5160. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5161. /*err_sys("SSL_write failed");*/
  5162. goto done;
  5163. }
  5164. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5165. if (input > 0) {
  5166. reply[input] = '\0';
  5167. fprintf(stderr, "Server response: %s\n", reply);
  5168. }
  5169. ((func_args*)args)->return_code = TEST_SUCCESS;
  5170. done:
  5171. wolfSSL_free(ssl);
  5172. if (!sharedCtx)
  5173. wolfSSL_CTX_free(ctx);
  5174. CloseSocket(sockfd);
  5175. #ifdef WOLFSSL_TIRTOS
  5176. fdCloseSession(Task_self());
  5177. #endif
  5178. return;
  5179. }
  5180. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  5181. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  5182. static int test_client_verifyDepth(void* args)
  5183. {
  5184. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5185. SOCKET_T sockfd = 0;
  5186. callback_functions* cbf;
  5187. WOLFSSL_CTX* ctx = 0;
  5188. WOLFSSL* ssl = 0;
  5189. char msg[64] = "hello wolfssl!";
  5190. char reply[1024];
  5191. int input;
  5192. int msgSz = (int)XSTRLEN(msg);
  5193. int ret, err = 0;
  5194. int verify_depth = ((func_args*)args)->argc;
  5195. ((func_args*)args)->return_code = TEST_FAIL;
  5196. cbf = ((func_args*)args)->callbacks;
  5197. {
  5198. WOLFSSL_METHOD* method = NULL;
  5199. if (cbf != NULL && cbf->method != NULL) {
  5200. method = cbf->method();
  5201. }
  5202. else {
  5203. method = wolfSSLv23_client_method();
  5204. }
  5205. ctx = wolfSSL_CTX_new(method);
  5206. }
  5207. /* Do connect here so server detects failures */
  5208. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5209. 0, 0, NULL);
  5210. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  5211. != WOLFSSL_SUCCESS)
  5212. {
  5213. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5214. goto done;
  5215. }
  5216. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5217. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5218. /*err_sys("can't load client cert file, "
  5219. "Please run from wolfSSL home dir");*/
  5220. goto done;
  5221. }
  5222. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5223. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5224. /*err_sys("can't load client key file, "
  5225. "Please run from wolfSSL home dir");*/
  5226. goto done;
  5227. }
  5228. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  5229. /* set verify depth */
  5230. if (verify_depth == 0) {
  5231. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  5232. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5233. }
  5234. else if (verify_depth == -1) {
  5235. myVerifyAction = VERIFY_USE_PREVERFIY;
  5236. SSL_CTX_set_verify_depth(ctx, 0);
  5237. }
  5238. else if (verify_depth > 0) {
  5239. myVerifyAction = VERIFY_USE_PREVERFIY;
  5240. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5241. }
  5242. ssl = wolfSSL_new(ctx);
  5243. if (ssl == NULL) {
  5244. goto done;
  5245. }
  5246. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5247. /*err_sys("SSL_set_fd failed");*/
  5248. goto done;
  5249. }
  5250. #ifdef WOLFSSL_ASYNC_CRYPT
  5251. err = 0; /* Reset error */
  5252. #endif
  5253. do {
  5254. #ifdef WOLFSSL_ASYNC_CRYPT
  5255. if (err == WC_PENDING_E) {
  5256. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5257. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5258. }
  5259. #endif
  5260. ret = wolfSSL_connect(ssl);
  5261. err = wolfSSL_get_error(ssl, 0);
  5262. } while (err == WC_PENDING_E);
  5263. if (ret != WOLFSSL_SUCCESS) {
  5264. char buff[WOLFSSL_MAX_ERROR_SZ];
  5265. fprintf(stderr, "error = %d, %s\n", err,
  5266. wolfSSL_ERR_error_string(err, buff));
  5267. goto done;
  5268. }
  5269. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5270. goto done;
  5271. }
  5272. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5273. if (input > 0) {
  5274. reply[input] = '\0';
  5275. fprintf(stderr, "Server response: %s\n", reply);
  5276. }
  5277. ((func_args*)args)->return_code = TEST_SUCCESS;
  5278. done:
  5279. wolfSSL_free(ssl);
  5280. wolfSSL_CTX_free(ctx);
  5281. CloseSocket(sockfd);
  5282. #else
  5283. (void)args;
  5284. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  5285. return 0;
  5286. }
  5287. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5288. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  5289. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  5290. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  5291. #define HAVE_ALPN_PROTOS_SUPPORT
  5292. #endif
  5293. /* Generic TLS client / server with callbacks for API unit tests
  5294. * Used by SNI / ALPN / crypto callback helper functions */
  5295. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5296. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  5297. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  5298. #define ENABLE_TLS_CALLBACK_TEST
  5299. #endif
  5300. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  5301. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  5302. /* TLS server for API unit testing - generic */
  5303. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  5304. {
  5305. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5306. WOLFSSL_CTX* ctx = NULL;
  5307. WOLFSSL* ssl = NULL;
  5308. SOCKET_T sfd = 0;
  5309. SOCKET_T cfd = 0;
  5310. word16 port;
  5311. char msg[] = "I hear you fa shizzle!";
  5312. int len = (int) XSTRLEN(msg);
  5313. char input[1024];
  5314. int idx;
  5315. int ret, err = 0;
  5316. ((func_args*)args)->return_code = TEST_FAIL;
  5317. #ifdef WOLFSSL_STATIC_MEMORY
  5318. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5319. callbacks->memSz > 0) {
  5320. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5321. callbacks->mem, callbacks->memSz, 0, 1);
  5322. if (ret != WOLFSSL_SUCCESS) {
  5323. fprintf(stderr, "CTX static new failed %d\n", ret);
  5324. return 0;
  5325. }
  5326. }
  5327. #else
  5328. if (ctx == NULL) {
  5329. ctx = wolfSSL_CTX_new(callbacks->method());
  5330. }
  5331. if (ctx == NULL) {
  5332. fprintf(stderr, "CTX new failed\n");
  5333. return 0;
  5334. }
  5335. #endif
  5336. /* set defaults */
  5337. if (callbacks->caPemFile == NULL)
  5338. callbacks->caPemFile = cliCertFile;
  5339. if (callbacks->certPemFile == NULL)
  5340. callbacks->certPemFile = svrCertFile;
  5341. if (callbacks->keyPemFile == NULL)
  5342. callbacks->keyPemFile = svrKeyFile;
  5343. #ifdef WOLFSSL_TIRTOS
  5344. fdOpenSession(Task_self());
  5345. #endif
  5346. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5347. #if defined(USE_WINDOWS_API)
  5348. port = ((func_args*)args)->signal->port;
  5349. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5350. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5351. /* Let tcp_listen assign port */
  5352. port = 0;
  5353. #else
  5354. /* Use default port */
  5355. port = wolfSSLPort;
  5356. #endif
  5357. wolfSSL_CTX_set_verify(ctx,
  5358. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5359. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5360. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5361. #endif
  5362. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  5363. if (callbacks->method == wolfDTLSv1_2_server_method) {
  5364. AssertIntEQ(WOLFSSL_SUCCESS,
  5365. wolfSSL_CTX_dtls_set_export(ctx, test_export));
  5366. }
  5367. #endif
  5368. AssertIntEQ(WOLFSSL_SUCCESS,
  5369. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5370. AssertIntEQ(WOLFSSL_SUCCESS,
  5371. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5372. WOLFSSL_FILETYPE_PEM));
  5373. AssertIntEQ(WOLFSSL_SUCCESS,
  5374. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5375. WOLFSSL_FILETYPE_PEM));
  5376. #ifdef HAVE_CRL
  5377. if (callbacks->crlPemFile != NULL) {
  5378. AssertIntEQ(WOLFSSL_SUCCESS,
  5379. wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  5380. WOLFSSL_FILETYPE_PEM));
  5381. }
  5382. #endif
  5383. if (callbacks->ctx_ready)
  5384. callbacks->ctx_ready(ctx);
  5385. ssl = wolfSSL_new(ctx);
  5386. if (ssl == NULL) {
  5387. fprintf(stderr, "SSL new failed\n");
  5388. wolfSSL_CTX_free(ctx);
  5389. return 0;
  5390. }
  5391. if (wolfSSL_dtls(ssl)) {
  5392. SOCKADDR_IN_T cliAddr;
  5393. socklen_t cliLen;
  5394. cliLen = sizeof(cliAddr);
  5395. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  5396. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  5397. (struct sockaddr*)&cliAddr, &cliLen);
  5398. AssertIntGT(idx, 0);
  5399. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5400. }
  5401. else {
  5402. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5403. CloseSocket(sfd);
  5404. }
  5405. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  5406. if (callbacks->loadToSSL) {
  5407. wolfSSL_SetDevId(ssl, callbacks->devId);
  5408. AssertIntEQ(WOLFSSL_SUCCESS,
  5409. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5410. WOLFSSL_FILETYPE_PEM));
  5411. AssertIntEQ(WOLFSSL_SUCCESS,
  5412. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5413. WOLFSSL_FILETYPE_PEM));
  5414. }
  5415. #ifdef NO_PSK
  5416. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5417. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5418. #elif !defined(NO_DH)
  5419. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5420. #endif
  5421. #endif
  5422. if (callbacks->ssl_ready)
  5423. callbacks->ssl_ready(ssl);
  5424. #ifdef WOLFSSL_ASYNC_CRYPT
  5425. err = 0; /* Reset error */
  5426. #endif
  5427. do {
  5428. #ifdef WOLFSSL_ASYNC_CRYPT
  5429. if (err == WC_PENDING_E) {
  5430. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5431. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5432. }
  5433. #endif
  5434. ret = wolfSSL_accept(ssl);
  5435. err = wolfSSL_get_error(ssl, ret);
  5436. } while (err == WC_PENDING_E);
  5437. if (ret != WOLFSSL_SUCCESS) {
  5438. char buff[WOLFSSL_MAX_ERROR_SZ];
  5439. fprintf(stderr, "accept error = %d, %s\n", err,
  5440. wolfSSL_ERR_error_string(err, buff));
  5441. /*err_sys("SSL_accept failed");*/
  5442. }
  5443. else {
  5444. #ifdef WOLFSSL_ASYNC_CRYPT
  5445. err = 0; /* Reset error */
  5446. #endif
  5447. do {
  5448. #ifdef WOLFSSL_ASYNC_CRYPT
  5449. if (err == WC_PENDING_E) {
  5450. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5451. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5452. }
  5453. #endif
  5454. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5455. err = wolfSSL_get_error(ssl, idx);
  5456. } while (err == WC_PENDING_E);
  5457. if (idx > 0) {
  5458. input[idx] = 0;
  5459. fprintf(stderr, "Client message: %s\n", input);
  5460. }
  5461. #ifdef WOLFSSL_ASYNC_CRYPT
  5462. err = 0; /* Reset error */
  5463. #endif
  5464. do {
  5465. #ifdef WOLFSSL_ASYNC_CRYPT
  5466. if (err == WC_PENDING_E) {
  5467. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5468. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5469. }
  5470. #endif
  5471. ret = wolfSSL_write(ssl, msg, len);
  5472. err = wolfSSL_get_error(ssl, ret);
  5473. } while (err == WC_PENDING_E);
  5474. AssertIntEQ(len, ret);
  5475. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  5476. defined(WOLFSSL_DTLS)
  5477. if (wolfSSL_dtls(ssl)) {
  5478. byte* import;
  5479. word32 sz;
  5480. wolfSSL_dtls_export(ssl, NULL, &sz);
  5481. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5482. AssertNotNull(import);
  5483. idx = wolfSSL_dtls_export(ssl, import, &sz);
  5484. AssertIntGE(idx, 0);
  5485. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  5486. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5487. }
  5488. #endif
  5489. #ifdef WOLFSSL_TIRTOS
  5490. Task_yield();
  5491. #endif
  5492. ((func_args*)args)->return_code = TEST_SUCCESS;
  5493. }
  5494. if (callbacks->on_result)
  5495. callbacks->on_result(ssl);
  5496. wolfSSL_shutdown(ssl);
  5497. wolfSSL_free(ssl);
  5498. wolfSSL_CTX_free(ctx);
  5499. CloseSocket(cfd);
  5500. #ifdef WOLFSSL_TIRTOS
  5501. fdCloseSession(Task_self());
  5502. #endif
  5503. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5504. && defined(HAVE_THREAD_LS)
  5505. wc_ecc_fp_free(); /* free per thread cache */
  5506. #endif
  5507. #ifndef WOLFSSL_TIRTOS
  5508. return 0;
  5509. #endif
  5510. }
  5511. /* TLS Client for API unit testing - generic */
  5512. static void run_wolfssl_client(void* args)
  5513. {
  5514. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5515. WOLFSSL_CTX* ctx = NULL;
  5516. WOLFSSL* ssl = NULL;
  5517. SOCKET_T sfd = 0;
  5518. char msg[] = "hello wolfssl server!";
  5519. int len = (int) XSTRLEN(msg);
  5520. char input[1024];
  5521. int ret, err = 0;
  5522. ((func_args*)args)->return_code = TEST_FAIL;
  5523. /* set defaults */
  5524. if (callbacks->caPemFile == NULL)
  5525. callbacks->caPemFile = caCertFile;
  5526. if (callbacks->certPemFile == NULL)
  5527. callbacks->certPemFile = cliCertFile;
  5528. if (callbacks->keyPemFile == NULL)
  5529. callbacks->keyPemFile = cliKeyFile;
  5530. #ifdef WOLFSSL_STATIC_MEMORY
  5531. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5532. callbacks->memSz > 0) {
  5533. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5534. callbacks->mem, callbacks->memSz, 0, 1);
  5535. if (ret != WOLFSSL_SUCCESS) {
  5536. fprintf(stderr, "CTX static new failed %d\n", ret);
  5537. return;
  5538. }
  5539. }
  5540. #else
  5541. if (ctx == NULL) {
  5542. ctx = wolfSSL_CTX_new(callbacks->method());
  5543. }
  5544. if (ctx == NULL) {
  5545. fprintf(stderr, "CTX new failed\n");
  5546. return;
  5547. }
  5548. #endif
  5549. #ifdef WOLFSSL_TIRTOS
  5550. fdOpenSession(Task_self());
  5551. #endif
  5552. if (!callbacks->loadToSSL) {
  5553. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5554. }
  5555. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5556. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5557. #endif
  5558. AssertIntEQ(WOLFSSL_SUCCESS,
  5559. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5560. if (!callbacks->loadToSSL) {
  5561. AssertIntEQ(WOLFSSL_SUCCESS,
  5562. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5563. WOLFSSL_FILETYPE_PEM));
  5564. AssertIntEQ(WOLFSSL_SUCCESS,
  5565. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5566. WOLFSSL_FILETYPE_PEM));
  5567. }
  5568. #ifdef HAVE_CRL
  5569. if (callbacks->crlPemFile != NULL) {
  5570. AssertIntEQ(WOLFSSL_SUCCESS,
  5571. wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  5572. WOLFSSL_FILETYPE_PEM));
  5573. }
  5574. #endif
  5575. if (callbacks->ctx_ready)
  5576. callbacks->ctx_ready(ctx);
  5577. ssl = wolfSSL_new(ctx);
  5578. if (wolfSSL_dtls(ssl)) {
  5579. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5580. 1, 0, ssl);
  5581. }
  5582. else {
  5583. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5584. 0, 0, ssl);
  5585. }
  5586. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  5587. if (callbacks->loadToSSL) {
  5588. wolfSSL_SetDevId(ssl, callbacks->devId);
  5589. AssertIntEQ(WOLFSSL_SUCCESS,
  5590. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5591. WOLFSSL_FILETYPE_PEM));
  5592. AssertIntEQ(WOLFSSL_SUCCESS,
  5593. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5594. WOLFSSL_FILETYPE_PEM));
  5595. }
  5596. if (callbacks->ssl_ready)
  5597. callbacks->ssl_ready(ssl);
  5598. #ifdef WOLFSSL_ASYNC_CRYPT
  5599. err = 0; /* Reset error */
  5600. #endif
  5601. do {
  5602. #ifdef WOLFSSL_ASYNC_CRYPT
  5603. if (err == WC_PENDING_E) {
  5604. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5605. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5606. }
  5607. #endif
  5608. ret = wolfSSL_connect(ssl);
  5609. err = wolfSSL_get_error(ssl, ret);
  5610. } while (err == WC_PENDING_E);
  5611. if (ret != WOLFSSL_SUCCESS) {
  5612. char buff[WOLFSSL_MAX_ERROR_SZ];
  5613. fprintf(stderr, "error = %d, %s\n", err,
  5614. wolfSSL_ERR_error_string(err, buff));
  5615. /*err_sys("SSL_connect failed");*/
  5616. }
  5617. else {
  5618. #ifdef WOLFSSL_ASYNC_CRYPT
  5619. err = 0; /* Reset error */
  5620. #endif
  5621. do {
  5622. #ifdef WOLFSSL_ASYNC_CRYPT
  5623. if (err == WC_PENDING_E) {
  5624. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5625. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5626. }
  5627. #endif
  5628. ret = wolfSSL_write(ssl, msg, len);
  5629. err = wolfSSL_get_error(ssl, ret);
  5630. } while (err == WC_PENDING_E);
  5631. AssertIntEQ(len, ret);
  5632. #ifdef WOLFSSL_ASYNC_CRYPT
  5633. err = 0; /* Reset error */
  5634. #endif
  5635. do {
  5636. #ifdef WOLFSSL_ASYNC_CRYPT
  5637. if (err == WC_PENDING_E) {
  5638. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5639. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5640. }
  5641. #endif
  5642. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5643. err = wolfSSL_get_error(ssl, ret);
  5644. } while (err == WC_PENDING_E);
  5645. if (ret > 0) {
  5646. input[ret] = '\0'; /* null term */
  5647. fprintf(stderr, "Server response: %s\n", input);
  5648. }
  5649. ((func_args*)args)->return_code = TEST_SUCCESS;
  5650. }
  5651. if (callbacks->on_result)
  5652. callbacks->on_result(ssl);
  5653. wolfSSL_free(ssl);
  5654. wolfSSL_CTX_free(ctx);
  5655. CloseSocket(sfd);
  5656. #ifdef WOLFSSL_TIRTOS
  5657. fdCloseSession(Task_self());
  5658. #endif
  5659. }
  5660. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5661. static int test_wolfSSL_read_write(void)
  5662. {
  5663. /* The unit testing for read and write shall happen simultaneously, since
  5664. * one can't do anything with one without the other. (Except for a failure
  5665. * test case.) This function will call all the others that will set up,
  5666. * execute, and report their test findings.
  5667. *
  5668. * Set up the success case first. This function will become the template
  5669. * for the other tests. This should eventually be renamed
  5670. *
  5671. * The success case isn't interesting, how can this fail?
  5672. * - Do not give the client context a CA certificate. The connect should
  5673. * fail. Do not need server for this?
  5674. * - Using NULL for the ssl object on server. Do not need client for this.
  5675. * - Using NULL for the ssl object on client. Do not need server for this.
  5676. * - Good ssl objects for client and server. Client write() without server
  5677. * read().
  5678. * - Good ssl objects for client and server. Server write() without client
  5679. * read().
  5680. * - Forgetting the password callback?
  5681. */
  5682. tcp_ready ready;
  5683. func_args client_args;
  5684. func_args server_args;
  5685. THREAD_TYPE serverThread;
  5686. XMEMSET(&client_args, 0, sizeof(func_args));
  5687. XMEMSET(&server_args, 0, sizeof(func_args));
  5688. #ifdef WOLFSSL_TIRTOS
  5689. fdOpenSession(Task_self());
  5690. #endif
  5691. StartTCP();
  5692. InitTcpReady(&ready);
  5693. #if defined(USE_WINDOWS_API)
  5694. /* use RNG to get random port if using windows */
  5695. ready.port = GetRandomPort();
  5696. #endif
  5697. server_args.signal = &ready;
  5698. client_args.signal = &ready;
  5699. start_thread(test_server_nofail, &server_args, &serverThread);
  5700. wait_tcp_ready(&server_args);
  5701. test_client_nofail(&client_args, NULL);
  5702. join_thread(serverThread);
  5703. AssertTrue(client_args.return_code);
  5704. AssertTrue(server_args.return_code);
  5705. FreeTcpReady(&ready);
  5706. #ifdef WOLFSSL_TIRTOS
  5707. fdOpenSession(Task_self());
  5708. #endif
  5709. return TEST_RES_CHECK(1);
  5710. }
  5711. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  5712. {
  5713. int res = TEST_SKIPPED;
  5714. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5715. !defined(WOLFSSL_TLS13)
  5716. /* The unit test for session resumption by re-using WOLFSSL object.
  5717. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5718. * for second connection.
  5719. */
  5720. tcp_ready ready;
  5721. func_args client_args;
  5722. func_args server_args;
  5723. THREAD_TYPE serverThread;
  5724. XMEMSET(&client_args, 0, sizeof(func_args));
  5725. XMEMSET(&server_args, 0, sizeof(func_args));
  5726. #ifdef WOLFSSL_TIRTOS
  5727. fdOpenSession(Task_self());
  5728. #endif
  5729. StartTCP();
  5730. InitTcpReady(&ready);
  5731. #if defined(USE_WINDOWS_API)
  5732. /* use RNG to get random port if using windows */
  5733. ready.port = GetRandomPort();
  5734. #endif
  5735. server_args.signal = &ready;
  5736. client_args.signal = &ready;
  5737. /* the var is used for loop number */
  5738. server_args.argc = 2;
  5739. start_thread(test_server_loop, &server_args, &serverThread);
  5740. wait_tcp_ready(&server_args);
  5741. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5742. join_thread(serverThread);
  5743. AssertTrue(client_args.return_code);
  5744. AssertTrue(server_args.return_code);
  5745. FreeTcpReady(&ready);
  5746. #ifdef WOLFSSL_TIRTOS
  5747. fdOpenSession(Task_self());
  5748. #endif
  5749. res = TEST_RES_CHECK(1);
  5750. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5751. return res;
  5752. }
  5753. static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5754. {
  5755. int res = TEST_SKIPPED;
  5756. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5757. /* This unit test is to check set verify Depth */
  5758. tcp_ready ready;
  5759. func_args client_args;
  5760. func_args server_args;
  5761. THREAD_TYPE serverThread;
  5762. callback_functions client_cbf;
  5763. XMEMSET(&client_args, 0, sizeof(func_args));
  5764. XMEMSET(&server_args, 0, sizeof(func_args));
  5765. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5766. #ifdef WOLFSSL_TLS13
  5767. client_cbf.method = wolfTLSv1_3_client_method;
  5768. #endif /* WOLFSSL_TLS13 */
  5769. client_args.callbacks = &client_cbf;
  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 = 1;
  5780. /* test case 1 verify depth is equal to peer chain */
  5781. {
  5782. start_thread(test_server_nofail, &server_args, &serverThread);
  5783. wait_tcp_ready(&server_args);
  5784. /* the var is used for verify depth */
  5785. client_args.argc = 2;
  5786. test_client_verifyDepth(&client_args);
  5787. join_thread(serverThread);
  5788. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5789. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5790. }
  5791. /* test case 2
  5792. * verify depth is zero, number of peer's chain is 2.
  5793. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5794. * callback.
  5795. */
  5796. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5797. {
  5798. start_thread(test_server_nofail, &server_args, &serverThread);
  5799. wait_tcp_ready(&server_args);
  5800. client_args.argc = 0;
  5801. test_client_verifyDepth(&client_args);
  5802. join_thread(serverThread);
  5803. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5804. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5805. }
  5806. /* test case 3
  5807. * verify depth is zero, number of peer's chain is 2
  5808. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5809. * therefore, handshake becomes failure.
  5810. */
  5811. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5812. {
  5813. start_thread(test_server_nofail, &server_args, &serverThread);
  5814. wait_tcp_ready(&server_args);
  5815. client_args.argc = -1;
  5816. test_client_verifyDepth(&client_args);
  5817. join_thread(serverThread);
  5818. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5819. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5820. }
  5821. FreeTcpReady(&ready);
  5822. res = TEST_RES_CHECK(1);
  5823. #else
  5824. (void)test_client_verifyDepth;
  5825. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5826. return res;
  5827. }
  5828. static int test_wolfSSL_CTX_set_cipher_list(void)
  5829. {
  5830. int res = TEST_SKIPPED;
  5831. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5832. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  5833. && !defined(NO_SHA256) && defined(HAVE_ECC)
  5834. WOLFSSL_CTX* ctx;
  5835. WOLFSSL_CTX* ctxClient;
  5836. WOLFSSL* sslClient;
  5837. tcp_ready ready;
  5838. func_args client_args;
  5839. func_args server_args;
  5840. callback_functions client_cb;
  5841. callback_functions server_cb;
  5842. THREAD_TYPE serverThread;
  5843. XMEMSET(&client_args, 0, sizeof(func_args));
  5844. XMEMSET(&server_args, 0, sizeof(func_args));
  5845. StartTCP();
  5846. InitTcpReady(&ready);
  5847. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5848. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5849. AssertNotNull((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())));
  5850. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  5851. AssertIntEQ(WOLFSSL_SUCCESS,
  5852. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5853. AssertIntEQ(WOLFSSL_SUCCESS,
  5854. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5855. AssertIntEQ(WOLFSSL_SUCCESS,
  5856. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5857. AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  5858. AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE-RSA-AES128-SHA256"));
  5859. client_cb.ctx = ctxClient;
  5860. server_cb.ctx = ctx;
  5861. /* we are responsible for free'ing WOLFSSL_CTX */
  5862. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  5863. server_args.signal = &ready;
  5864. server_args.callbacks = &server_cb;
  5865. client_args.signal = &ready;
  5866. client_args.callbacks = &client_cb;
  5867. client_args.return_code = TEST_FAIL;
  5868. start_thread(test_server_nofail, &server_args, &serverThread);
  5869. wait_tcp_ready(&server_args);
  5870. test_client_nofail(&client_args, NULL);
  5871. join_thread(serverThread);
  5872. wolfSSL_CTX_free(client_cb.ctx);
  5873. wolfSSL_CTX_free(server_cb.ctx);
  5874. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5875. FreeTcpReady(&ready);
  5876. /* check with cipher string that has '+' */
  5877. AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  5878. AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  5879. AssertNotNull((sslClient = wolfSSL_new(ctxClient)));
  5880. /* check for the existance of an ECDHE ECDSA cipher suite */
  5881. {
  5882. int i = 0;
  5883. int found = 0;
  5884. const char* suite;
  5885. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk;
  5886. WOLFSSL_CIPHER* current;
  5887. AssertNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  5888. do {
  5889. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  5890. if (current) {
  5891. suite = wolfSSL_CIPHER_get_name(current);
  5892. if (suite && XSTRSTR(suite, "ECDSA")) {
  5893. found = 1;
  5894. break;
  5895. }
  5896. }
  5897. } while (current);
  5898. AssertIntEQ(found, 1);
  5899. }
  5900. wolfSSL_free(sslClient);
  5901. wolfSSL_CTX_free(ctxClient);
  5902. res = TEST_RES_CHECK(1);
  5903. #endif
  5904. return res;
  5905. }
  5906. static int test_client_get_finished(void* args, cbType cb)
  5907. {
  5908. #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
  5909. SOCKET_T sockfd = 0;
  5910. callback_functions* cbf;
  5911. WOLFSSL_CTX* ctx = 0;
  5912. WOLFSSL* ssl = 0;
  5913. char msg[64] = "hello wolfssl!";
  5914. char reply[1024];
  5915. int msgSz = (int)XSTRLEN(msg);
  5916. int ret, err = 0;
  5917. WOLFSSL_METHOD* method = NULL;
  5918. size_t msg_len = 0;
  5919. (void) args;
  5920. (void) cb;
  5921. ((func_args*)args)->return_code = TEST_FAIL;
  5922. cbf = ((func_args*)args)->callbacks;
  5923. if (cbf != NULL && cbf->method != NULL) {
  5924. method = cbf->method();
  5925. }
  5926. else {
  5927. method = wolfSSLv23_client_method();
  5928. }
  5929. ctx = wolfSSL_CTX_new(method);
  5930. /* Do connect here so server detects failures */
  5931. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5932. 0, 0, NULL);
  5933. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5934. {
  5935. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5936. goto done;
  5937. }
  5938. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5939. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5940. goto done;
  5941. }
  5942. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5943. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5944. goto done;
  5945. }
  5946. /* call ctx setup callback */
  5947. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5948. cbf->ctx_ready(ctx);
  5949. }
  5950. ssl = wolfSSL_new(ctx);
  5951. if (ssl == NULL) {
  5952. goto done;
  5953. }
  5954. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5955. goto done;
  5956. }
  5957. /* call ssl setup callback */
  5958. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5959. cbf->ssl_ready(ssl);
  5960. }
  5961. #ifdef WOLFSSL_ASYNC_CRYPT
  5962. err = 0; /* Reset error */
  5963. #endif
  5964. do {
  5965. #ifdef WOLFSSL_ASYNC_CRYPT
  5966. if (err == WC_PENDING_E) {
  5967. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5968. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5969. }
  5970. #endif
  5971. ret = wolfSSL_connect(ssl);
  5972. err = wolfSSL_get_error(ssl, 0);
  5973. } while (err == WC_PENDING_E);
  5974. if (ret != WOLFSSL_SUCCESS) {
  5975. char buff[WOLFSSL_MAX_ERROR_SZ];
  5976. fprintf(stderr, "error = %d, %s\n", err,
  5977. wolfSSL_ERR_error_string(err, buff));
  5978. goto done;
  5979. }
  5980. /* get_finished test */
  5981. /* 1. get own sent message */
  5982. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  5983. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  5984. AssertIntGE(msg_len, 0);
  5985. /* 2. get peer message */
  5986. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  5987. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  5988. AssertIntGE(msg_len, 0);
  5989. if (cb != NULL)
  5990. (cb)(ctx, ssl);
  5991. #ifdef WOLFSSL_ASYNC_CRYPT
  5992. err = 0; /* Reset error */
  5993. #endif
  5994. do {
  5995. #ifdef WOLFSSL_ASYNC_CRYPT
  5996. if (err == WC_PENDING_E) {
  5997. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5998. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5999. }
  6000. #endif
  6001. ret = wolfSSL_write(ssl, msg, msgSz);
  6002. err = wolfSSL_get_error(ssl, 0);
  6003. } while (err == WC_PENDING_E);
  6004. if (ret != msgSz) {
  6005. /*err_sys("SSL_write failed");*/
  6006. goto done;
  6007. }
  6008. #ifdef WOLFSSL_ASYNC_CRYPT
  6009. err = 0; /* Reset error */
  6010. #endif
  6011. do {
  6012. #ifdef WOLFSSL_ASYNC_CRYPT
  6013. if (err == WC_PENDING_E) {
  6014. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6015. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6016. }
  6017. #endif
  6018. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6019. err = wolfSSL_get_error(ssl, 0);
  6020. } while (err == WC_PENDING_E);
  6021. if (ret > 0) {
  6022. reply[ret] = '\0';
  6023. fprintf(stderr, "Server response: %s\n", reply);
  6024. }
  6025. ((func_args*)args)->return_code = TEST_SUCCESS;
  6026. done:
  6027. wolfSSL_free(ssl);
  6028. wolfSSL_CTX_free(ctx);
  6029. CloseSocket(sockfd);
  6030. #else
  6031. (void)args;
  6032. (void)cb;
  6033. #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
  6034. return 0;
  6035. }
  6036. static int test_wolfSSL_get_finished(void)
  6037. {
  6038. int res = TEST_SKIPPED;
  6039. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6040. tcp_ready ready;
  6041. func_args client_args;
  6042. func_args server_args;
  6043. THREAD_TYPE serverThread;
  6044. XMEMSET(&client_args, 0, sizeof(func_args));
  6045. XMEMSET(&server_args, 0, sizeof(func_args));
  6046. StartTCP();
  6047. InitTcpReady(&ready);
  6048. #if defined(USE_WINDOWS_API)
  6049. /* use RNG to get random port if using windows */
  6050. ready.port = GetRandomPort();
  6051. #endif
  6052. server_args.signal = &ready;
  6053. client_args.signal = &ready;
  6054. start_thread(test_server_nofail, &server_args, &serverThread);
  6055. wait_tcp_ready(&server_args);
  6056. test_client_get_finished(&client_args, NULL);
  6057. join_thread(serverThread);
  6058. AssertTrue(client_args.return_code);
  6059. AssertTrue(server_args.return_code);
  6060. /* test received msg vs sent msg */
  6061. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6062. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6063. FreeTcpReady(&ready);
  6064. res = TEST_RES_CHECK(1);
  6065. #else
  6066. (void)test_client_get_finished;
  6067. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  6068. return res;
  6069. }
  6070. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6071. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6072. !defined(NO_SESSION_CACHE)
  6073. /* Sessions to restore/store */
  6074. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6075. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6076. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6077. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6078. {
  6079. /* Don't store sessions. Lookup is still enabled. */
  6080. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6081. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6082. #ifdef OPENSSL_EXTRA
  6083. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6084. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6085. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6086. #endif
  6087. /* Require both peers to provide certs */
  6088. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6089. }
  6090. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6091. {
  6092. WOLFSSL_SESSION** sess;
  6093. if (wolfSSL_is_server(ssl))
  6094. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6095. else
  6096. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6097. if (*sess == NULL) {
  6098. #ifdef NO_SESSION_CACHE_REF
  6099. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6100. #else
  6101. /* Test for backwards compatibility */
  6102. if (wolfSSL_is_server(ssl)) {
  6103. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6104. }
  6105. else {
  6106. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6107. }
  6108. #endif
  6109. /* Now save the session in the internal store to make it available
  6110. * for lookup. For TLS 1.3, we can't save the session without
  6111. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6112. * session from cache. */
  6113. if (wolfSSL_is_server(ssl)
  6114. #ifndef WOLFSSL_TICKET_HAVE_ID
  6115. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6116. #endif
  6117. )
  6118. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6119. *sess), WOLFSSL_SUCCESS);
  6120. }
  6121. else {
  6122. /* If we have a session retrieved then remaining connections should be
  6123. * resuming on that session */
  6124. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6125. }
  6126. /* Save CTX to be able to decrypt tickets */
  6127. if (wolfSSL_is_server(ssl) &&
  6128. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6129. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6130. = wolfSSL_get_SSL_CTX(ssl));
  6131. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6132. WOLFSSL_SUCCESS);
  6133. }
  6134. #ifdef SESSION_CERTS
  6135. #ifndef WOLFSSL_TICKET_HAVE_ID
  6136. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6137. wolfSSL_session_reused(ssl))
  6138. #endif
  6139. {
  6140. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6141. * for all connections. TLS 1.3 only has tickets so if we don't
  6142. * include the session id in the ticket then the certificates
  6143. * will not be available on resumption. */
  6144. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6145. AssertNotNull(peer);
  6146. wolfSSL_X509_free(peer);
  6147. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6148. #ifdef OPENSSL_EXTRA
  6149. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  6150. #endif
  6151. }
  6152. #endif /* SESSION_CERTS */
  6153. }
  6154. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  6155. {
  6156. /* Set the session to reuse for the client */
  6157. AssertIntEQ(wolfSSL_set_session(ssl,
  6158. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  6159. }
  6160. #endif
  6161. static int test_wolfSSL_CTX_add_session(void)
  6162. {
  6163. int res = TEST_SKIPPED;
  6164. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6165. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6166. !defined(NO_SESSION_CACHE)
  6167. tcp_ready ready;
  6168. func_args client_args;
  6169. func_args server_args;
  6170. THREAD_TYPE serverThread;
  6171. callback_functions client_cb;
  6172. callback_functions server_cb;
  6173. method_provider methods[][2] = {
  6174. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6175. !defined(NO_DES3))
  6176. /* Without AES there are almost no ciphersuites available. This leads
  6177. * to no ciphersuites being available and an error. */
  6178. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  6179. #endif
  6180. #ifndef WOLFSSL_NO_TLS12
  6181. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  6182. #endif
  6183. /* Needs the default ticket callback since it is tied to the
  6184. * connection context and this makes it easy to carry over the ticket
  6185. * crypto context between connections */
  6186. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6187. defined(HAVE_SESSION_TICKET)
  6188. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  6189. #endif
  6190. };
  6191. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  6192. size_t i, j;
  6193. for (i = 0; i < methodsLen; i++) {
  6194. /* First run creates a connection while the second+ run will attempt
  6195. * to resume the connection. The trick is that the internal cache
  6196. * is turned off. wolfSSL_CTX_add_session should put the session in
  6197. * the cache anyway. */
  6198. test_wolfSSL_CTX_add_session_client_sess = NULL;
  6199. test_wolfSSL_CTX_add_session_server_sess = NULL;
  6200. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  6201. for (j = 0; j < 5; j++) {
  6202. #ifdef WOLFSSL_TIRTOS
  6203. fdOpenSession(Task_self());
  6204. #endif
  6205. StartTCP();
  6206. InitTcpReady(&ready);
  6207. XMEMSET(&client_args, 0, sizeof(func_args));
  6208. XMEMSET(&server_args, 0, sizeof(func_args));
  6209. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6210. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6211. client_cb.method = methods[i][0];
  6212. server_cb.method = methods[i][1];
  6213. server_args.signal = &ready;
  6214. server_args.callbacks = &server_cb;
  6215. client_args.signal = &ready;
  6216. client_args.callbacks = &client_cb;
  6217. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  6218. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  6219. server_cb.isSharedCtx = 1;
  6220. }
  6221. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6222. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6223. if (j != 0)
  6224. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  6225. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6226. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6227. server_cb.ticNoInit = 1; /* Use default builtin */
  6228. start_thread(test_server_nofail, &server_args, &serverThread);
  6229. wait_tcp_ready(&server_args);
  6230. test_client_nofail(&client_args, NULL);
  6231. join_thread(serverThread);
  6232. AssertTrue(client_args.return_code);
  6233. AssertTrue(server_args.return_code);
  6234. FreeTcpReady(&ready);
  6235. }
  6236. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  6237. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  6238. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  6239. }
  6240. res = TEST_RES_CHECK(1);
  6241. #endif
  6242. return res;
  6243. }
  6244. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6245. /* canned export of a session using older version 3 */
  6246. static unsigned char version_3[] = {
  6247. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  6248. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  6249. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6250. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  6251. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6252. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6253. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  6254. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  6255. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  6256. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6257. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6258. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  6259. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6260. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  6261. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  6262. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  6263. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  6264. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6265. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  6266. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  6267. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6268. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6269. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6270. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6271. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6272. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6273. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6274. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6275. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6276. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6277. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6278. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  6279. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  6280. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  6281. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  6282. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  6283. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  6284. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  6285. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  6286. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  6287. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  6288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6289. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  6290. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  6291. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6292. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  6293. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  6294. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  6295. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  6296. 0x2E, 0x31, 0xED, 0x4F
  6297. };
  6298. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  6299. static int test_wolfSSL_dtls_export(void)
  6300. {
  6301. int res = TEST_SKIPPED;
  6302. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6303. tcp_ready ready;
  6304. func_args client_args;
  6305. func_args server_args;
  6306. THREAD_TYPE serverThread;
  6307. callback_functions server_cbf;
  6308. callback_functions client_cbf;
  6309. #ifdef WOLFSSL_TIRTOS
  6310. fdOpenSession(Task_self());
  6311. #endif
  6312. InitTcpReady(&ready);
  6313. #if defined(USE_WINDOWS_API)
  6314. /* use RNG to get random port if using windows */
  6315. ready.port = GetRandomPort();
  6316. #endif
  6317. /* set using dtls */
  6318. XMEMSET(&client_args, 0, sizeof(func_args));
  6319. XMEMSET(&server_args, 0, sizeof(func_args));
  6320. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6321. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6322. server_cbf.method = wolfDTLSv1_2_server_method;
  6323. client_cbf.method = wolfDTLSv1_2_client_method;
  6324. server_args.callbacks = &server_cbf;
  6325. client_args.callbacks = &client_cbf;
  6326. server_args.signal = &ready;
  6327. client_args.signal = &ready;
  6328. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6329. wait_tcp_ready(&server_args);
  6330. run_wolfssl_client(&client_args);
  6331. join_thread(serverThread);
  6332. AssertTrue(client_args.return_code);
  6333. AssertTrue(server_args.return_code);
  6334. FreeTcpReady(&ready);
  6335. #ifdef WOLFSSL_TIRTOS
  6336. fdOpenSession(Task_self());
  6337. #endif
  6338. {
  6339. SOCKET_T sockfd = 0;
  6340. WOLFSSL_CTX* ctx;
  6341. WOLFSSL* ssl;
  6342. char msg[64] = "hello wolfssl!";
  6343. char reply[1024];
  6344. int msgSz = (int)XSTRLEN(msg);
  6345. byte *session, *window;
  6346. unsigned int sessionSz, windowSz;
  6347. #ifndef TEST_IPV6
  6348. struct sockaddr_in peerAddr;
  6349. #else
  6350. struct sockaddr_in6 peerAddr;
  6351. #endif /* TEST_IPV6 */
  6352. int i;
  6353. /* Set ctx to DTLS 1.2 */
  6354. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  6355. AssertNotNull(ssl = wolfSSL_new(ctx));
  6356. /* test importing version 3 */
  6357. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6358. /* test importing bad length and bad version */
  6359. version_3[2] += 1;
  6360. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6361. version_3[2] -= 1; version_3[1] = 0XA0;
  6362. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6363. wolfSSL_free(ssl);
  6364. wolfSSL_CTX_free(ctx);
  6365. /* check storing client state after connection and storing window only */
  6366. #ifdef WOLFSSL_TIRTOS
  6367. fdOpenSession(Task_self());
  6368. #endif
  6369. InitTcpReady(&ready);
  6370. #if defined(USE_WINDOWS_API)
  6371. /* use RNG to get random port if using windows */
  6372. ready.port = GetRandomPort();
  6373. #endif
  6374. /* set using dtls */
  6375. XMEMSET(&server_args, 0, sizeof(func_args));
  6376. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6377. server_cbf.method = wolfDTLSv1_2_server_method;
  6378. server_cbf.doUdp = 1;
  6379. server_args.callbacks = &server_cbf;
  6380. server_args.argc = 3; /* set loop_count to 3 */
  6381. server_args.signal = &ready;
  6382. start_thread(test_server_nofail, &server_args, &serverThread);
  6383. wait_tcp_ready(&server_args);
  6384. /* create and connect with client */
  6385. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  6386. AssertIntEQ(WOLFSSL_SUCCESS,
  6387. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  6388. AssertIntEQ(WOLFSSL_SUCCESS,
  6389. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  6390. AssertIntEQ(WOLFSSL_SUCCESS,
  6391. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  6392. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  6393. AssertNotNull(ssl = wolfSSL_new(ctx));
  6394. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6395. /* store server information connected too */
  6396. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  6397. #ifndef TEST_IPV6
  6398. peerAddr.sin_family = AF_INET;
  6399. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  6400. peerAddr.sin_port = XHTONS(server_args.signal->port);
  6401. #else
  6402. peerAddr.sin6_family = AF_INET6;
  6403. AssertIntEQ(
  6404. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  6405. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  6406. #endif
  6407. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6408. WOLFSSL_SUCCESS);
  6409. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  6410. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  6411. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6412. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  6413. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6414. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6415. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  6416. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6417. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6418. wolfSSL_free(ssl);
  6419. for (i = 1; i < server_args.argc; i++) {
  6420. /* restore state */
  6421. AssertNotNull(ssl = wolfSSL_new(ctx));
  6422. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  6423. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  6424. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6425. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6426. WOLFSSL_SUCCESS);
  6427. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6428. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6429. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6430. wolfSSL_free(ssl);
  6431. }
  6432. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6433. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6434. wolfSSL_CTX_free(ctx);
  6435. fprintf(stderr, "done and waiting for server\n");
  6436. join_thread(serverThread);
  6437. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6438. FreeTcpReady(&ready);
  6439. #ifdef WOLFSSL_TIRTOS
  6440. fdOpenSession(Task_self());
  6441. #endif
  6442. }
  6443. res = TEST_RES_CHECK(1);
  6444. #endif
  6445. return res;
  6446. }
  6447. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6448. #ifdef WOLFSSL_TLS13
  6449. static const byte canned_client_tls13_session[] = {
  6450. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  6451. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  6452. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  6453. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6454. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6455. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6456. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  6457. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  6458. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  6459. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6460. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6461. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  6462. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  6463. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  6464. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  6465. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6466. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6467. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6468. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6469. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6470. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6471. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6472. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6473. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6474. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6475. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6476. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6477. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6478. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6479. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  6480. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  6481. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  6482. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6483. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6484. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6485. 0x00, 0x03
  6486. };
  6487. static const byte canned_server_tls13_session[] = {
  6488. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  6489. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  6490. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  6491. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6492. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6493. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6494. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  6495. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  6496. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  6497. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6498. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6499. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  6500. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  6501. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  6502. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  6503. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6504. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6505. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6506. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6507. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6508. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6509. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6510. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6511. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6512. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6513. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6514. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6515. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6516. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6517. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  6518. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  6519. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  6520. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6521. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6522. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6523. 0x00, 0x04
  6524. };
  6525. #endif /* WOLFSSL_TLS13 */
  6526. static const byte canned_client_session[] = {
  6527. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6528. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6529. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  6530. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6531. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6532. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6533. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6534. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  6535. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6536. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6537. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6538. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  6539. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6540. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6541. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6542. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6543. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6544. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6545. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6546. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6547. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6548. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6549. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6550. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6551. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6552. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6553. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6554. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6555. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6556. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6557. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6558. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6559. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6560. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6561. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6562. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6563. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6564. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6565. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6566. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6567. 0x00, 0x03
  6568. };
  6569. static const byte canned_server_session[] = {
  6570. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6571. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6572. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6573. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6574. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6575. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6576. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6577. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  6578. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6579. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6580. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6581. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  6582. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6583. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6584. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6585. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6586. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6587. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6588. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6589. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6590. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6591. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6592. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6593. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6594. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6595. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6596. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6597. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6598. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6599. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6600. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6601. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6602. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6603. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6604. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6605. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6606. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6607. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6608. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6609. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6610. 0x00, 0x04
  6611. };
  6612. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  6613. {
  6614. SOCKET_T sockfd = 0;
  6615. SOCKET_T clientfd = 0;
  6616. word16 port;
  6617. callback_functions* cbf;
  6618. WOLFSSL_CTX* ctx = 0;
  6619. WOLFSSL* ssl = 0;
  6620. char msg[] = "I hear you fa shizzle!";
  6621. char input[1024];
  6622. int idx;
  6623. #ifdef WOLFSSL_TIRTOS
  6624. fdOpenSession(Task_self());
  6625. #endif
  6626. ((func_args*)args)->return_code = TEST_FAIL;
  6627. cbf = ((func_args*)args)->callbacks;
  6628. {
  6629. WOLFSSL_METHOD* method = NULL;
  6630. if (cbf != NULL && cbf->method != NULL) {
  6631. method = cbf->method();
  6632. }
  6633. else {
  6634. method = wolfTLSv1_2_server_method();
  6635. }
  6636. ctx = wolfSSL_CTX_new(method);
  6637. }
  6638. if (ctx == NULL) {
  6639. goto done;
  6640. }
  6641. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6642. #if defined(USE_WINDOWS_API)
  6643. port = ((func_args*)args)->signal->port;
  6644. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6645. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6646. /* Let tcp_listen assign port */
  6647. port = 0;
  6648. #else
  6649. /* Use default port */
  6650. port = wolfSSLPort;
  6651. #endif
  6652. /* do it here to detect failure */
  6653. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6654. CloseSocket(sockfd);
  6655. /* call ctx setup callback */
  6656. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6657. cbf->ctx_ready(ctx);
  6658. }
  6659. ssl = wolfSSL_new(ctx);
  6660. if (ssl == NULL) {
  6661. goto done;
  6662. }
  6663. wolfSSL_set_fd(ssl, clientfd);
  6664. /* call ssl setup callback */
  6665. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6666. cbf->ssl_ready(ssl);
  6667. }
  6668. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6669. if (idx > 0) {
  6670. input[idx] = '\0';
  6671. fprintf(stderr, "Client message export/import: %s\n", input);
  6672. }
  6673. else {
  6674. fprintf(stderr, "ret = %d error = %d\n", idx,
  6675. wolfSSL_get_error(ssl, idx));
  6676. goto done;
  6677. }
  6678. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6679. /*err_sys("SSL_write failed");*/
  6680. #ifdef WOLFSSL_TIRTOS
  6681. return;
  6682. #else
  6683. return 0;
  6684. #endif
  6685. }
  6686. #ifdef WOLFSSL_TIRTOS
  6687. Task_yield();
  6688. #endif
  6689. ((func_args*)args)->return_code = TEST_SUCCESS;
  6690. done:
  6691. wolfSSL_shutdown(ssl);
  6692. wolfSSL_free(ssl);
  6693. wolfSSL_CTX_free(ctx);
  6694. CloseSocket(clientfd);
  6695. #ifdef WOLFSSL_TIRTOS
  6696. fdCloseSession(Task_self());
  6697. #endif
  6698. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6699. && defined(HAVE_THREAD_LS)
  6700. wc_ecc_fp_free(); /* free per thread cache */
  6701. #endif
  6702. #if defined(HAVE_SESSION_TICKET) && \
  6703. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6704. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  6705. OpenSSLTicketCleanup();
  6706. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6707. TicketCleanup();
  6708. #endif
  6709. #endif
  6710. #ifndef WOLFSSL_TIRTOS
  6711. return 0;
  6712. #endif
  6713. }
  6714. static void load_tls12_canned_server(WOLFSSL* ssl)
  6715. {
  6716. int clientfd = wolfSSL_get_fd(ssl);
  6717. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  6718. sizeof(canned_server_session)), sizeof(canned_server_session));
  6719. wolfSSL_set_fd(ssl, clientfd);
  6720. }
  6721. #ifdef WOLFSSL_TLS13
  6722. static void load_tls13_canned_server(WOLFSSL* ssl)
  6723. {
  6724. int clientfd = wolfSSL_get_fd(ssl);
  6725. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  6726. sizeof(canned_server_tls13_session)),
  6727. sizeof(canned_server_tls13_session));
  6728. wolfSSL_set_fd(ssl, clientfd);
  6729. }
  6730. #endif
  6731. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  6732. static int test_wolfSSL_tls_export_run(int v)
  6733. {
  6734. SOCKET_T sockfd = 0;
  6735. WOLFSSL_CTX* ctx = 0;
  6736. WOLFSSL* ssl = 0;
  6737. char msg[64] = "hello wolfssl!";
  6738. char reply[1024];
  6739. word32 replySz;
  6740. int msgSz = (int)XSTRLEN(msg);
  6741. const byte* clientSession = NULL;
  6742. int clientSessionSz = 0;
  6743. tcp_ready ready;
  6744. func_args server_args;
  6745. THREAD_TYPE serverThread;
  6746. callback_functions server_cbf;
  6747. #ifdef WOLFSSL_TIRTOS
  6748. fdOpenSession(Task_self());
  6749. #endif
  6750. InitTcpReady(&ready);
  6751. #if defined(USE_WINDOWS_API)
  6752. /* use RNG to get random port if using windows */
  6753. ready.port = GetRandomPort();
  6754. #endif
  6755. XMEMSET(&server_args, 0, sizeof(func_args));
  6756. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6757. switch (v) {
  6758. case WOLFSSL_TLSV1_2:
  6759. server_cbf.method = wolfTLSv1_2_server_method;
  6760. server_cbf.ssl_ready = load_tls12_canned_server;
  6761. /* setup the client side */
  6762. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  6763. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6764. clientSession = canned_client_session;
  6765. clientSessionSz = sizeof(canned_client_session);
  6766. break;
  6767. #ifdef WOLFSSL_TLS13
  6768. case WOLFSSL_TLSV1_3:
  6769. server_cbf.method = wolfTLSv1_3_server_method;
  6770. server_cbf.ssl_ready = load_tls13_canned_server;
  6771. /* setup the client side */
  6772. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  6773. clientSession = canned_client_tls13_session;
  6774. clientSessionSz = sizeof(canned_client_tls13_session);
  6775. break;
  6776. #endif
  6777. }
  6778. server_args.callbacks = &server_cbf;
  6779. server_args.signal = &ready;
  6780. start_thread(tls_export_server, &server_args, &serverThread);
  6781. wait_tcp_ready(&server_args);
  6782. #ifdef WOLFSSL_TIRTOS
  6783. fdOpenSession(Task_self());
  6784. #endif
  6785. AssertNotNull(ssl = wolfSSL_new(ctx));
  6786. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  6787. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  6788. clientSessionSz);
  6789. replySz = sizeof(reply);
  6790. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  6791. #if !defined(NO_PSK) && defined(HAVE_ANON)
  6792. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  6793. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  6794. #endif
  6795. wolfSSL_set_fd(ssl, sockfd);
  6796. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6797. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  6798. wolfSSL_free(ssl);
  6799. wolfSSL_CTX_free(ctx);
  6800. CloseSocket(sockfd);
  6801. #ifdef WOLFSSL_TIRTOS
  6802. fdCloseSession(Task_self());
  6803. #endif
  6804. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6805. && defined(HAVE_THREAD_LS)
  6806. wc_ecc_fp_free(); /* free per thread cache */
  6807. #endif
  6808. join_thread(serverThread);
  6809. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6810. FreeTcpReady(&ready);
  6811. #ifdef WOLFSSL_TIRTOS
  6812. fdOpenSession(Task_self());
  6813. #endif
  6814. return TEST_RES_CHECK(1);
  6815. }
  6816. #endif
  6817. static int test_wolfSSL_tls_export(void)
  6818. {
  6819. int res = TEST_SKIPPED;
  6820. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6821. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  6822. #ifdef WOLFSSL_TLS13
  6823. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  6824. #endif
  6825. res = TEST_RES_CHECK(1);
  6826. #endif
  6827. return res;
  6828. }
  6829. /*----------------------------------------------------------------------------*
  6830. | TLS extensions tests
  6831. *----------------------------------------------------------------------------*/
  6832. #ifdef ENABLE_TLS_CALLBACK_TEST
  6833. /* Connection test runner - generic */
  6834. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  6835. callback_functions* server_callbacks)
  6836. {
  6837. tcp_ready ready;
  6838. func_args client_args;
  6839. func_args server_args;
  6840. THREAD_TYPE serverThread;
  6841. XMEMSET(&client_args, 0, sizeof(func_args));
  6842. XMEMSET(&server_args, 0, sizeof(func_args));
  6843. StartTCP();
  6844. client_args.callbacks = client_callbacks;
  6845. server_args.callbacks = server_callbacks;
  6846. #ifdef WOLFSSL_TIRTOS
  6847. fdOpenSession(Task_self());
  6848. #endif
  6849. /* RUN Server side */
  6850. InitTcpReady(&ready);
  6851. #if defined(USE_WINDOWS_API)
  6852. /* use RNG to get random port if using windows */
  6853. ready.port = GetRandomPort();
  6854. #endif
  6855. server_args.signal = &ready;
  6856. client_args.signal = &ready;
  6857. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6858. wait_tcp_ready(&server_args);
  6859. /* RUN Client side */
  6860. run_wolfssl_client(&client_args);
  6861. join_thread(serverThread);
  6862. FreeTcpReady(&ready);
  6863. #ifdef WOLFSSL_TIRTOS
  6864. fdCloseSession(Task_self());
  6865. #endif
  6866. client_callbacks->return_code = client_args.return_code;
  6867. server_callbacks->return_code = server_args.return_code;
  6868. }
  6869. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6870. #ifdef HAVE_SNI
  6871. static int test_wolfSSL_UseSNI_params(void)
  6872. {
  6873. int res = TEST_SKIPPED;
  6874. #if !defined(NO_WOLFSSL_CLIENT)
  6875. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6876. WOLFSSL *ssl = wolfSSL_new(ctx);
  6877. AssertNotNull(ctx);
  6878. AssertNotNull(ssl);
  6879. /* invalid [ctx|ssl] */
  6880. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  6881. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  6882. /* invalid type */
  6883. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  6884. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  6885. /* invalid data */
  6886. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  6887. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  6888. /* success case */
  6889. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  6890. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  6891. wolfSSL_free(ssl);
  6892. wolfSSL_CTX_free(ctx);
  6893. res = TEST_RES_CHECK(1);
  6894. #endif /* !NO_WOLFSSL_CLIENT */
  6895. return res;
  6896. }
  6897. /* BEGIN of connection tests callbacks */
  6898. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6899. {
  6900. AssertIntEQ(WOLFSSL_SUCCESS,
  6901. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6902. }
  6903. static void use_SNI_at_ssl(WOLFSSL* ssl)
  6904. {
  6905. AssertIntEQ(WOLFSSL_SUCCESS,
  6906. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6907. }
  6908. static void different_SNI_at_ssl(WOLFSSL* ssl)
  6909. {
  6910. AssertIntEQ(WOLFSSL_SUCCESS,
  6911. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  6912. }
  6913. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  6914. {
  6915. use_SNI_at_ssl(ssl);
  6916. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6917. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  6918. }
  6919. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  6920. {
  6921. use_SNI_at_ssl(ssl);
  6922. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6923. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  6924. }
  6925. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6926. {
  6927. use_SNI_at_ctx(ctx);
  6928. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6929. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6930. }
  6931. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  6932. {
  6933. use_SNI_at_ssl(ssl);
  6934. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6935. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6936. }
  6937. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6938. {
  6939. use_SNI_at_ctx(ctx);
  6940. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6941. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6942. }
  6943. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  6944. {
  6945. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  6946. }
  6947. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  6948. {
  6949. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  6950. }
  6951. static void verify_SNI_no_matching(WOLFSSL* ssl)
  6952. {
  6953. byte type = WOLFSSL_SNI_HOST_NAME;
  6954. void* request = (void*) &type; /* to be overwritten */
  6955. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  6956. AssertNotNull(request);
  6957. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6958. AssertNull(request);
  6959. }
  6960. static void verify_SNI_real_matching(WOLFSSL* ssl)
  6961. {
  6962. byte type = WOLFSSL_SNI_HOST_NAME;
  6963. void* request = NULL;
  6964. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  6965. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6966. AssertNotNull(request);
  6967. AssertStrEQ("www.wolfssl.com", (char*)request);
  6968. }
  6969. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  6970. {
  6971. byte type = WOLFSSL_SNI_HOST_NAME;
  6972. void* request = NULL;
  6973. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  6974. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6975. AssertNotNull(request);
  6976. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  6977. }
  6978. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6979. {
  6980. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  6981. }
  6982. /* END of connection tests callbacks */
  6983. static int test_wolfSSL_UseSNI_connection(void)
  6984. {
  6985. int res = TEST_SKIPPED;
  6986. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  6987. callback_functions client_cb;
  6988. callback_functions server_cb;
  6989. size_t i;
  6990. struct {
  6991. method_provider client_meth;
  6992. method_provider server_meth;
  6993. } methods[] = {
  6994. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  6995. {wolfSSLv23_client_method, wolfSSLv23_server_method},
  6996. #endif
  6997. #ifndef WOLFSSL_NO_TLS12
  6998. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
  6999. #endif
  7000. #ifdef WOLFSSL_TLS13
  7001. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
  7002. #endif
  7003. };
  7004. for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
  7005. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7006. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7007. client_cb.method = methods[i].client_meth;
  7008. server_cb.method = methods[i].server_meth;
  7009. client_cb.devId = testDevId;
  7010. server_cb.devId = testDevId;
  7011. /* success case at ctx */
  7012. printf("success case at ctx\n");
  7013. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7014. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7015. test_wolfSSL_client_server(&client_cb, &server_cb);
  7016. /* success case at ssl */
  7017. printf("success case at ssl\n");
  7018. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  7019. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  7020. test_wolfSSL_client_server(&client_cb, &server_cb);
  7021. /* default mismatch behavior */
  7022. printf("default mismatch behavior\n");
  7023. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7024. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  7025. test_wolfSSL_client_server(&client_cb, &server_cb);
  7026. /* continue on mismatch */
  7027. printf("continue on mismatch\n");
  7028. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7029. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  7030. test_wolfSSL_client_server(&client_cb, &server_cb);
  7031. /* fake answer on mismatch */
  7032. printf("fake answer on mismatch\n");
  7033. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7034. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  7035. test_wolfSSL_client_server(&client_cb, &server_cb);
  7036. /* sni abort - success */
  7037. printf("sni abort - success\n");
  7038. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7039. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7040. test_wolfSSL_client_server(&client_cb, &server_cb);
  7041. /* sni abort - abort when absent (ctx) */
  7042. printf("sni abort - abort when absent (ctx)\n");
  7043. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7044. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7045. test_wolfSSL_client_server(&client_cb, &server_cb);
  7046. /* sni abort - abort when absent (ssl) */
  7047. printf("sni abort - abort when absent (ssl)\n");
  7048. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7049. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7050. test_wolfSSL_client_server(&client_cb, &server_cb);
  7051. /* sni abort - success when overwritten */
  7052. printf("sni abort - success when overwritten\n");
  7053. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7054. 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;
  7055. test_wolfSSL_client_server(&client_cb, &server_cb);
  7056. /* sni abort - success when allowing mismatches */
  7057. printf("sni abort - success when allowing mismatches\n");
  7058. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7059. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  7060. test_wolfSSL_client_server(&client_cb, &server_cb);
  7061. }
  7062. res = TEST_RES_CHECK(1);
  7063. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7064. return res;
  7065. }
  7066. static int test_wolfSSL_SNI_GetFromBuffer(void)
  7067. {
  7068. byte buff[] = { /* www.paypal.com */
  7069. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  7070. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  7071. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  7072. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7073. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7074. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  7075. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  7076. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  7077. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7078. };
  7079. byte buff2[] = { /* api.textmate.org */
  7080. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  7081. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  7082. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  7083. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  7084. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  7085. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  7086. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  7087. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  7088. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  7089. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  7090. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  7091. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  7092. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  7093. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  7094. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  7095. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  7096. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  7097. };
  7098. byte buff3[] = { /* no sni extension */
  7099. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  7100. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  7101. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  7102. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7103. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7104. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  7105. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7106. };
  7107. byte buff4[] = { /* last extension has zero size */
  7108. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  7109. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7110. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7111. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7112. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  7113. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  7114. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  7115. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  7116. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  7117. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  7118. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  7119. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  7120. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  7121. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  7122. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  7123. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  7124. 0x12, 0x00, 0x00
  7125. };
  7126. byte buff5[] = { /* SSL v2.0 client hello */
  7127. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  7128. /* dummy bytes bellow, just to pass size check */
  7129. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7130. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7131. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7132. };
  7133. byte result[32] = {0};
  7134. word32 length = 32;
  7135. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  7136. 0, result, &length));
  7137. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  7138. 0, result, &length));
  7139. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7140. 1, result, &length));
  7141. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7142. 0, result, &length));
  7143. buff[0] = 0x16;
  7144. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7145. 0, result, &length));
  7146. buff[1] = 0x03;
  7147. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  7148. sizeof(buff), 0, result, &length));
  7149. buff[2] = 0x03;
  7150. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  7151. sizeof(buff), 0, result, &length));
  7152. buff[4] = 0x64;
  7153. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7154. 0, result, &length));
  7155. result[length] = 0;
  7156. AssertStrEQ("www.paypal.com", (const char*) result);
  7157. length = 32;
  7158. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7159. 0, result, &length));
  7160. result[length] = 0;
  7161. AssertStrEQ("api.textmate.org", (const char*) result);
  7162. /* SSL v2.0 tests */
  7163. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  7164. sizeof(buff5), 0, result, &length));
  7165. buff5[2] = 0x02;
  7166. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7167. sizeof(buff5), 0, result, &length));
  7168. buff5[2] = 0x01; buff5[6] = 0x08;
  7169. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7170. sizeof(buff5), 0, result, &length));
  7171. buff5[6] = 0x09; buff5[8] = 0x01;
  7172. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7173. sizeof(buff5), 0, result, &length));
  7174. return TEST_RES_CHECK(1);
  7175. }
  7176. #endif /* HAVE_SNI */
  7177. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  7178. static int test_wolfSSL_UseTrustedCA(void)
  7179. {
  7180. int res = TEST_SKIPPED;
  7181. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  7182. && !defined(NO_RSA)
  7183. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7184. WOLFSSL_CTX *ctx;
  7185. WOLFSSL *ssl;
  7186. byte id[20];
  7187. #ifndef NO_WOLFSSL_SERVER
  7188. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  7189. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7190. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7191. #else
  7192. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  7193. #endif
  7194. AssertNotNull((ssl = wolfSSL_new(ctx)));
  7195. XMEMSET(id, 0, sizeof(id));
  7196. /* error cases */
  7197. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  7198. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7199. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  7200. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7201. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  7202. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7203. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  7204. #ifdef NO_SHA
  7205. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7206. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7207. #endif
  7208. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7209. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  7210. /* success cases */
  7211. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7212. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  7213. #ifndef NO_SHA
  7214. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7215. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7216. #endif
  7217. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7218. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  7219. wolfSSL_free(ssl);
  7220. wolfSSL_CTX_free(ctx);
  7221. res = TEST_RES_CHECK(1);
  7222. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7223. #endif /* HAVE_TRUSTED_CA */
  7224. return res;
  7225. }
  7226. static int test_wolfSSL_UseMaxFragment(void)
  7227. {
  7228. int res = TEST_SKIPPED;
  7229. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  7230. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7231. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7232. #ifndef NO_WOLFSSL_SERVER
  7233. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7234. #else
  7235. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7236. #endif
  7237. WOLFSSL *ssl;
  7238. #ifdef OPENSSL_EXTRA
  7239. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  7240. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  7241. #else
  7242. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  7243. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  7244. #endif
  7245. #ifndef NO_WOLFSSL_SERVER
  7246. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7247. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7248. #endif
  7249. AssertNotNull(ctx);
  7250. ssl = wolfSSL_new(ctx);
  7251. AssertNotNull(ssl);
  7252. #ifdef OPENSSL_EXTRA
  7253. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  7254. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  7255. #else
  7256. UseMaxFragment = wolfSSL_UseMaxFragment;
  7257. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  7258. #endif
  7259. /* error cases */
  7260. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  7261. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  7262. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  7263. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  7264. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  7265. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  7266. /* success case */
  7267. #ifdef OPENSSL_EXTRA
  7268. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7269. #else
  7270. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7271. #endif
  7272. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  7273. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  7274. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  7275. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  7276. #ifdef OPENSSL_EXTRA
  7277. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7278. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7279. #else
  7280. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7281. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7282. #endif
  7283. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  7284. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  7285. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  7286. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  7287. #ifdef OPENSSL_EXTRA
  7288. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7289. #else
  7290. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7291. #endif
  7292. wolfSSL_free(ssl);
  7293. wolfSSL_CTX_free(ctx);
  7294. res = TEST_RES_CHECK(1);
  7295. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7296. #endif
  7297. return res;
  7298. }
  7299. static int test_wolfSSL_UseTruncatedHMAC(void)
  7300. {
  7301. int res = TEST_SKIPPED;
  7302. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  7303. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7304. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7305. #ifndef NO_WOLFSSL_SERVER
  7306. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7307. #else
  7308. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7309. #endif
  7310. WOLFSSL *ssl;
  7311. AssertNotNull(ctx);
  7312. #ifndef NO_WOLFSSL_SERVER
  7313. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7314. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7315. #endif
  7316. ssl = wolfSSL_new(ctx);
  7317. AssertNotNull(ssl);
  7318. /* error cases */
  7319. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  7320. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  7321. /* success case */
  7322. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  7323. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  7324. wolfSSL_free(ssl);
  7325. wolfSSL_CTX_free(ctx);
  7326. res = TEST_RES_CHECK(1);
  7327. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7328. #endif
  7329. return res;
  7330. }
  7331. static int test_wolfSSL_UseSupportedCurve(void)
  7332. {
  7333. int res = TEST_SKIPPED;
  7334. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  7335. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7336. WOLFSSL *ssl = wolfSSL_new(ctx);
  7337. AssertNotNull(ctx);
  7338. AssertNotNull(ssl);
  7339. /* error cases */
  7340. AssertIntNE(WOLFSSL_SUCCESS,
  7341. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7342. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  7343. AssertIntNE(WOLFSSL_SUCCESS,
  7344. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7345. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  7346. /* success case */
  7347. AssertIntEQ(WOLFSSL_SUCCESS,
  7348. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  7349. AssertIntEQ(WOLFSSL_SUCCESS,
  7350. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  7351. wolfSSL_free(ssl);
  7352. wolfSSL_CTX_free(ctx);
  7353. res = TEST_RES_CHECK(1);
  7354. #endif
  7355. return res;
  7356. }
  7357. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  7358. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7359. {
  7360. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  7361. }
  7362. static void use_ALPN_all(WOLFSSL* ssl)
  7363. {
  7364. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7365. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7366. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7367. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7368. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7369. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7370. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7371. }
  7372. static void use_ALPN_all_continue(WOLFSSL* ssl)
  7373. {
  7374. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7375. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7376. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7377. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7378. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7379. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7380. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7381. }
  7382. static void use_ALPN_one(WOLFSSL* ssl)
  7383. {
  7384. /* spdy/2 */
  7385. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7386. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7387. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7388. }
  7389. static void use_ALPN_unknown(WOLFSSL* ssl)
  7390. {
  7391. /* http/2.0 */
  7392. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7393. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7394. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7395. }
  7396. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  7397. {
  7398. /* http/2.0 */
  7399. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7400. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7401. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7402. }
  7403. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  7404. {
  7405. /* spdy/3 */
  7406. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7407. char *proto = NULL;
  7408. word16 protoSz = 0;
  7409. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7410. /* check value */
  7411. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  7412. if (proto) {
  7413. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  7414. }
  7415. }
  7416. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  7417. {
  7418. char *proto = NULL;
  7419. word16 protoSz = 0;
  7420. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  7421. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7422. /* check value */
  7423. AssertIntEQ(1, (0 == protoSz));
  7424. AssertIntEQ(1, (NULL == proto));
  7425. }
  7426. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  7427. {
  7428. /* http/1.1 */
  7429. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7430. char *proto;
  7431. word16 protoSz = 0;
  7432. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7433. /* check value */
  7434. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7435. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7436. }
  7437. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  7438. {
  7439. /* spdy/2 */
  7440. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7441. char *proto;
  7442. word16 protoSz = 0;
  7443. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7444. /* check value */
  7445. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7446. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7447. }
  7448. static void verify_ALPN_client_list(WOLFSSL* ssl)
  7449. {
  7450. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7451. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7452. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7453. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7454. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7455. char *clist = NULL;
  7456. word16 clistSz = 0;
  7457. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  7458. &clistSz));
  7459. /* check value */
  7460. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  7461. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  7462. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  7463. }
  7464. static int test_wolfSSL_UseALPN_connection(void)
  7465. {
  7466. int res = TEST_SKIPPED;
  7467. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7468. callback_functions client_cb;
  7469. callback_functions server_cb;
  7470. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7471. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7472. client_cb.method = wolfSSLv23_client_method;
  7473. server_cb.method = wolfSSLv23_server_method;
  7474. client_cb.devId = testDevId;
  7475. server_cb.devId = testDevId;
  7476. /* success case same list */
  7477. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7478. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  7479. test_wolfSSL_client_server(&client_cb, &server_cb);
  7480. /* success case only one for server */
  7481. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7482. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  7483. test_wolfSSL_client_server(&client_cb, &server_cb);
  7484. /* success case only one for client */
  7485. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  7486. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  7487. test_wolfSSL_client_server(&client_cb, &server_cb);
  7488. /* success case none for client */
  7489. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7490. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  7491. test_wolfSSL_client_server(&client_cb, &server_cb);
  7492. /* success case mismatch behavior but option 'continue' set */
  7493. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  7494. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  7495. test_wolfSSL_client_server(&client_cb, &server_cb);
  7496. /* success case read protocol send by client */
  7497. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7498. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  7499. test_wolfSSL_client_server(&client_cb, &server_cb);
  7500. /* mismatch behavior with same list
  7501. * the first and only this one must be taken */
  7502. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7503. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  7504. test_wolfSSL_client_server(&client_cb, &server_cb);
  7505. /* default mismatch behavior */
  7506. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7507. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  7508. test_wolfSSL_client_server(&client_cb, &server_cb);
  7509. res = TEST_RES_CHECK(1);
  7510. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7511. return res;
  7512. }
  7513. static int test_wolfSSL_UseALPN_params(void)
  7514. {
  7515. int res = TEST_SKIPPED;
  7516. #ifndef NO_WOLFSSL_CLIENT
  7517. /* "http/1.1" */
  7518. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7519. /* "spdy/1" */
  7520. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  7521. /* "spdy/2" */
  7522. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7523. /* "spdy/3" */
  7524. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7525. char buff[256];
  7526. word32 idx;
  7527. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7528. WOLFSSL *ssl = wolfSSL_new(ctx);
  7529. AssertNotNull(ctx);
  7530. AssertNotNull(ssl);
  7531. /* error cases */
  7532. AssertIntNE(WOLFSSL_SUCCESS,
  7533. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  7534. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7535. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  7536. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7537. /* success case */
  7538. /* http1 only */
  7539. AssertIntEQ(WOLFSSL_SUCCESS,
  7540. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  7541. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7542. /* http1, spdy1 */
  7543. XMEMCPY(buff, http1, sizeof(http1));
  7544. idx = sizeof(http1);
  7545. buff[idx++] = ',';
  7546. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7547. idx += sizeof(spdy1);
  7548. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7549. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7550. /* http1, spdy2, spdy1 */
  7551. XMEMCPY(buff, http1, sizeof(http1));
  7552. idx = sizeof(http1);
  7553. buff[idx++] = ',';
  7554. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7555. idx += sizeof(spdy2);
  7556. buff[idx++] = ',';
  7557. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7558. idx += sizeof(spdy1);
  7559. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7560. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7561. /* spdy3, http1, spdy2, spdy1 */
  7562. XMEMCPY(buff, spdy3, sizeof(spdy3));
  7563. idx = sizeof(spdy3);
  7564. buff[idx++] = ',';
  7565. XMEMCPY(buff+idx, http1, sizeof(http1));
  7566. idx += sizeof(http1);
  7567. buff[idx++] = ',';
  7568. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7569. idx += sizeof(spdy2);
  7570. buff[idx++] = ',';
  7571. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7572. idx += sizeof(spdy1);
  7573. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7574. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7575. wolfSSL_free(ssl);
  7576. wolfSSL_CTX_free(ctx);
  7577. res = TEST_RES_CHECK(1);
  7578. #endif
  7579. return res;
  7580. }
  7581. #endif /* HAVE_ALPN */
  7582. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7583. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  7584. {
  7585. unsigned char p[] = {
  7586. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7587. 6, 's', 'p', 'd', 'y', '/', '2',
  7588. 6, 's', 'p', 'd', 'y', '/', '1',
  7589. };
  7590. unsigned char p_len = sizeof(p);
  7591. int ret;
  7592. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  7593. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7594. AssertIntEQ(ret, 0);
  7595. #else
  7596. AssertIntEQ(ret, SSL_SUCCESS);
  7597. #endif
  7598. }
  7599. static void set_alpn_protos(SSL* ssl)
  7600. {
  7601. unsigned char p[] = {
  7602. 6, 's', 'p', 'd', 'y', '/', '3',
  7603. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7604. 6, 's', 'p', 'd', 'y', '/', '2',
  7605. 6, 's', 'p', 'd', 'y', '/', '1',
  7606. };
  7607. unsigned char p_len = sizeof(p);
  7608. int ret;
  7609. ret = SSL_set_alpn_protos(ssl, p, p_len);
  7610. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7611. AssertIntEQ(ret, 0);
  7612. #else
  7613. AssertIntEQ(ret, SSL_SUCCESS);
  7614. #endif
  7615. }
  7616. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  7617. {
  7618. /* "spdy/3" */
  7619. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7620. const unsigned char *proto;
  7621. unsigned int protoSz = 0;
  7622. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7623. /* check value */
  7624. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7625. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7626. }
  7627. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  7628. {
  7629. /* "http/1.1" */
  7630. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7631. const unsigned char *proto;
  7632. unsigned int protoSz = 0;
  7633. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7634. /* check value */
  7635. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7636. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7637. }
  7638. static int test_wolfSSL_set_alpn_protos(void)
  7639. {
  7640. int res = TEST_SKIPPED;
  7641. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7642. callback_functions client_cb;
  7643. callback_functions server_cb;
  7644. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7645. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7646. client_cb.method = wolfSSLv23_client_method;
  7647. server_cb.method = wolfSSLv23_server_method;
  7648. client_cb.devId = testDevId;
  7649. server_cb.devId = testDevId;
  7650. /* use CTX_alpn_protos */
  7651. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7652. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  7653. test_wolfSSL_client_server(&client_cb, &server_cb);
  7654. /* use set_alpn_protos */
  7655. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  7656. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  7657. test_wolfSSL_client_server(&client_cb, &server_cb);
  7658. res = TEST_RES_CHECK(1);
  7659. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7660. return res;
  7661. }
  7662. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  7663. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  7664. {
  7665. int res = TEST_SKIPPED;
  7666. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  7667. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7668. WOLFSSL *ssl = wolfSSL_new(ctx);
  7669. AssertNotNull(ctx);
  7670. AssertNotNull(ssl);
  7671. /* error cases */
  7672. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  7673. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  7674. /* success cases */
  7675. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  7676. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  7677. wolfSSL_free(ssl);
  7678. wolfSSL_CTX_free(ctx);
  7679. res = TEST_RES_CHECK(1);
  7680. #endif
  7681. return res;
  7682. }
  7683. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  7684. {
  7685. int res = TEST_SKIPPED;
  7686. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  7687. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7688. WOLFSSL *ssl = wolfSSL_new(ctx);
  7689. AssertNotNull(ctx);
  7690. AssertNotNull(ssl);
  7691. /* error cases */
  7692. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  7693. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  7694. /* success cases */
  7695. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  7696. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  7697. wolfSSL_free(ssl);
  7698. wolfSSL_CTX_free(ctx);
  7699. res = TEST_RES_CHECK(1);
  7700. #endif
  7701. return res;
  7702. }
  7703. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  7704. static int test_wolfSSL_SCR_Reconnect(void)
  7705. {
  7706. int res = TEST_SKIPPED;
  7707. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  7708. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  7709. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256)
  7710. struct test_memio_ctx test_ctx;
  7711. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  7712. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  7713. byte data;
  7714. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  7715. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  7716. test_ctx.s_ciphers =
  7717. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  7718. AssertIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  7719. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  7720. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  7721. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  7722. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  7723. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  7724. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  7725. /* WOLFSSL_FATAL_ERROR since it will block */
  7726. AssertIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  7727. AssertIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  7728. WOLFSSL_ERROR_WANT_READ);
  7729. AssertIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  7730. AssertIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  7731. WOLFSSL_ERROR_WANT_READ);
  7732. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  7733. wolfSSL_free(ssl_c);
  7734. ssl_c = NULL;
  7735. wolfSSL_free(ssl_s);
  7736. ssl_s = NULL;
  7737. wolfSSL_CTX_free(ctx_c);
  7738. ctx_c = NULL;
  7739. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  7740. AssertIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  7741. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  7742. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  7743. wolfSSL_free(ssl_s);
  7744. wolfSSL_free(ssl_c);
  7745. wolfSSL_CTX_free(ctx_s);
  7746. wolfSSL_CTX_free(ctx_c);
  7747. res = TEST_RES_CHECK(1);
  7748. #endif
  7749. return res;
  7750. }
  7751. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  7752. (!defined(NO_RSA) || defined(HAVE_ECC))
  7753. /* Called when writing. */
  7754. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  7755. {
  7756. (void)ssl;
  7757. (void)buf;
  7758. (void)sz;
  7759. (void)ctx;
  7760. /* Force error return from wolfSSL_accept_TLSv13(). */
  7761. return WANT_WRITE;
  7762. }
  7763. /* Called when reading. */
  7764. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  7765. {
  7766. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  7767. int len = (int)msg->length;
  7768. (void)ssl;
  7769. (void)sz;
  7770. /* Pass back as much of message as will fit in buffer. */
  7771. if (len > sz)
  7772. len = sz;
  7773. XMEMCPY(buf, msg->buffer, len);
  7774. /* Move over returned data. */
  7775. msg->buffer += len;
  7776. msg->length -= len;
  7777. /* Amount actually copied. */
  7778. return len;
  7779. }
  7780. #endif
  7781. /* Test the detection of duplicate known TLS extensions.
  7782. * Specifically in a ClientHello.
  7783. */
  7784. static int test_tls_ext_duplicate(void)
  7785. {
  7786. int res = TEST_SKIPPED;
  7787. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  7788. !defined(NO_FILESYSTEM)
  7789. const unsigned char clientHelloDupTlsExt[] = {
  7790. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  7791. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  7792. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  7793. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  7794. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  7795. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  7796. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  7797. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  7798. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7799. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  7800. 0x00, 0x9e, 0x01, 0x00,
  7801. /* Extensions - duplicate signature algorithms. */
  7802. 0x00, 0x19, 0x00, 0x0d,
  7803. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  7804. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  7805. /* Supported Versions extension for TLS 1.3. */
  7806. 0x00, 0x2b,
  7807. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  7808. };
  7809. WOLFSSL_BUFFER_INFO msg;
  7810. const char* testCertFile;
  7811. const char* testKeyFile;
  7812. WOLFSSL_CTX *ctx;
  7813. WOLFSSL *ssl;
  7814. #ifndef NO_RSA
  7815. testCertFile = svrCertFile;
  7816. testKeyFile = svrKeyFile;
  7817. #elif defined(HAVE_ECC)
  7818. testCertFile = eccCertFile;
  7819. testKeyFile = eccKeyFile;
  7820. #endif
  7821. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7822. AssertNotNull(ctx);
  7823. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  7824. WOLFSSL_FILETYPE_PEM));
  7825. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  7826. WOLFSSL_FILETYPE_PEM));
  7827. /* Read from 'msg'. */
  7828. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  7829. /* No where to send to - dummy sender. */
  7830. wolfSSL_SetIOSend(ctx, DummySend);
  7831. ssl = wolfSSL_new(ctx);
  7832. AssertNotNull(ssl);
  7833. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  7834. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  7835. wolfSSL_SetIOReadCtx(ssl, &msg);
  7836. AssertIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  7837. AssertIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  7838. wolfSSL_free(ssl);
  7839. wolfSSL_CTX_free(ctx);
  7840. res = TEST_RES_CHECK(1);
  7841. #endif
  7842. return res;
  7843. }
  7844. /*----------------------------------------------------------------------------*
  7845. | X509 Tests
  7846. *----------------------------------------------------------------------------*/
  7847. static int test_wolfSSL_X509_NAME_get_entry(void)
  7848. {
  7849. int res = TEST_SKIPPED;
  7850. #if !defined(NO_CERTS) && !defined(NO_RSA)
  7851. #if defined(OPENSSL_ALL) || \
  7852. (defined(OPENSSL_EXTRA) && \
  7853. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  7854. /* use openssl like name to test mapping */
  7855. X509_NAME_ENTRY* ne;
  7856. X509_NAME* name;
  7857. X509* x509;
  7858. #ifndef NO_FILESYSTEM
  7859. ASN1_STRING* asn;
  7860. char* subCN = NULL;
  7861. #endif
  7862. int idx;
  7863. ASN1_OBJECT *object = NULL;
  7864. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  7865. defined(WOLFSSL_NGINX)
  7866. #ifndef NO_BIO
  7867. BIO* bio;
  7868. #endif
  7869. #endif
  7870. #ifndef NO_FILESYSTEM
  7871. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7872. WOLFSSL_FILETYPE_PEM);
  7873. AssertNotNull(x509);
  7874. name = X509_get_subject_name(x509);
  7875. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7876. AssertIntGE(idx, 0);
  7877. ne = X509_NAME_get_entry(name, idx);
  7878. AssertNotNull(ne);
  7879. asn = X509_NAME_ENTRY_get_data(ne);
  7880. AssertNotNull(asn);
  7881. subCN = (char*)ASN1_STRING_data(asn);
  7882. AssertNotNull(subCN);
  7883. wolfSSL_FreeX509(x509);
  7884. #endif
  7885. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7886. WOLFSSL_FILETYPE_PEM);
  7887. AssertNotNull(x509);
  7888. name = X509_get_subject_name(x509);
  7889. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7890. AssertIntGE(idx, 0);
  7891. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  7892. defined(WOLFSSL_NGINX)
  7893. #ifndef NO_BIO
  7894. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7895. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  7896. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7897. AssertIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  7898. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7899. BIO_free(bio);
  7900. #endif
  7901. #endif
  7902. ne = X509_NAME_get_entry(name, idx);
  7903. AssertNotNull(ne);
  7904. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  7905. wolfSSL_FreeX509(x509);
  7906. res = TEST_RES_CHECK(1);
  7907. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  7908. #endif /* !NO_CERTS && !NO_RSA */
  7909. return res;
  7910. }
  7911. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  7912. static int test_wolfSSL_PKCS12(void)
  7913. {
  7914. int res = TEST_SKIPPED;
  7915. /* .p12 file is encrypted with DES3 */
  7916. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  7917. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  7918. * Password Key
  7919. */
  7920. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  7921. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  7922. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  7923. byte buf[6000];
  7924. char file[] = "./certs/test-servercert.p12";
  7925. char order[] = "./certs/ecc-rsa-server.p12";
  7926. #ifdef WC_RC2
  7927. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  7928. #endif
  7929. char pass[] = "a password";
  7930. const char goodPsw[] = "wolfSSL test";
  7931. const char badPsw[] = "bad";
  7932. #ifdef HAVE_ECC
  7933. WOLFSSL_X509_NAME* subject;
  7934. WOLFSSL_X509 *x509;
  7935. #endif
  7936. XFILE f;
  7937. int bytes, ret, goodPswLen, badPswLen;
  7938. WOLFSSL_BIO *bio;
  7939. WOLFSSL_EVP_PKEY *pkey;
  7940. WC_PKCS12 *pkcs12;
  7941. WC_PKCS12 *pkcs12_2;
  7942. WOLFSSL_X509 *cert;
  7943. WOLFSSL_X509 *tmp;
  7944. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  7945. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7946. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7947. WOLFSSL_CTX *ctx;
  7948. WOLFSSL *ssl;
  7949. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  7950. #endif
  7951. f = XFOPEN(file, "rb");
  7952. AssertTrue((f != XBADFILE));
  7953. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7954. XFCLOSE(f);
  7955. goodPswLen = (int)XSTRLEN(goodPsw);
  7956. badPswLen = (int)XSTRLEN(badPsw);
  7957. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  7958. AssertNotNull(bio);
  7959. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  7960. d2i_PKCS12_bio(bio, &pkcs12);
  7961. AssertNotNull(pkcs12);
  7962. BIO_free(bio);
  7963. /* check verify MAC directly */
  7964. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  7965. AssertIntEQ(ret, 1);
  7966. /* check verify MAC fail case directly */
  7967. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  7968. AssertIntEQ(ret, 0);
  7969. /* check verify MAC fail case */
  7970. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7971. AssertIntEQ(ret, 0);
  7972. AssertNull(pkey);
  7973. AssertNull(cert);
  7974. /* check parse with no extra certs kept */
  7975. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7976. AssertIntEQ(ret, 1);
  7977. AssertNotNull(pkey);
  7978. AssertNotNull(cert);
  7979. wolfSSL_EVP_PKEY_free(pkey);
  7980. wolfSSL_X509_free(cert);
  7981. /* check parse with extra certs kept */
  7982. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7983. AssertIntEQ(ret, 1);
  7984. AssertNotNull(pkey);
  7985. AssertNotNull(cert);
  7986. AssertNotNull(ca);
  7987. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7988. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7989. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  7990. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7991. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7992. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7993. #else
  7994. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7995. #endif
  7996. /* Copy stack structure */
  7997. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  7998. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  7999. /* CTX now owns the tmp_ca stack structure */
  8000. tmp_ca = NULL;
  8001. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  8002. AssertNotNull(tmp_ca);
  8003. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  8004. /* Check that the main cert is also set */
  8005. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  8006. AssertNotNull(ssl = SSL_new(ctx));
  8007. AssertNotNull(SSL_get_certificate(ssl));
  8008. SSL_free(ssl);
  8009. SSL_CTX_free(ctx);
  8010. #endif
  8011. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8012. /* should be 2 other certs on stack */
  8013. tmp = sk_X509_pop(ca);
  8014. AssertNotNull(tmp);
  8015. X509_free(tmp);
  8016. tmp = sk_X509_pop(ca);
  8017. AssertNotNull(tmp);
  8018. X509_free(tmp);
  8019. AssertNull(sk_X509_pop(ca));
  8020. EVP_PKEY_free(pkey);
  8021. X509_free(cert);
  8022. sk_X509_pop_free(ca, X509_free);
  8023. /* check PKCS12_create */
  8024. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  8025. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  8026. SSL_SUCCESS);
  8027. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8028. -1, -1, 100, -1, 0)));
  8029. EVP_PKEY_free(pkey);
  8030. X509_free(cert);
  8031. sk_X509_pop_free(ca, NULL);
  8032. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8033. SSL_SUCCESS);
  8034. PKCS12_free(pkcs12_2);
  8035. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8036. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8037. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8038. 2000, 1, 0)));
  8039. EVP_PKEY_free(pkey);
  8040. X509_free(cert);
  8041. sk_X509_pop_free(ca, NULL);
  8042. /* convert to DER then back and parse */
  8043. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  8044. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  8045. PKCS12_free(pkcs12_2);
  8046. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  8047. BIO_free(bio);
  8048. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8049. SSL_SUCCESS);
  8050. /* should be 2 other certs on stack */
  8051. tmp = sk_X509_pop(ca);
  8052. AssertNotNull(tmp);
  8053. X509_free(tmp);
  8054. tmp = sk_X509_pop(ca);
  8055. AssertNotNull(tmp);
  8056. X509_free(tmp);
  8057. AssertNull(sk_X509_pop(ca));
  8058. #ifndef NO_RC4
  8059. PKCS12_free(pkcs12_2);
  8060. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  8061. NID_pbe_WithSHA1And128BitRC4,
  8062. NID_pbe_WithSHA1And128BitRC4,
  8063. 2000, 1, 0)));
  8064. EVP_PKEY_free(pkey);
  8065. X509_free(cert);
  8066. sk_X509_pop_free(ca, NULL);
  8067. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8068. SSL_SUCCESS);
  8069. #endif /* NO_RC4 */
  8070. EVP_PKEY_free(pkey);
  8071. X509_free(cert);
  8072. PKCS12_free(pkcs12);
  8073. PKCS12_free(pkcs12_2);
  8074. sk_X509_pop_free(ca, NULL);
  8075. #ifdef HAVE_ECC
  8076. /* test order of parsing */
  8077. f = XFOPEN(order, "rb");
  8078. AssertTrue(f != XBADFILE);
  8079. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8080. XFCLOSE(f);
  8081. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8082. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8083. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  8084. WOLFSSL_SUCCESS);
  8085. /* check use of pkey after parse */
  8086. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8087. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8088. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8089. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8090. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8091. #else
  8092. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8093. #endif
  8094. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  8095. SSL_CTX_free(ctx);
  8096. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8097. #endif
  8098. AssertNotNull(pkey);
  8099. AssertNotNull(cert);
  8100. AssertNotNull(ca);
  8101. /* compare subject lines of certificates */
  8102. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8103. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  8104. SSL_FILETYPE_PEM));
  8105. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8106. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8107. X509_free(x509);
  8108. /* test expected fail case */
  8109. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8110. SSL_FILETYPE_PEM));
  8111. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8112. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8113. X509_free(x509);
  8114. X509_free(cert);
  8115. /* get subject line from ca stack */
  8116. AssertNotNull(cert = sk_X509_pop(ca));
  8117. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8118. /* compare subject from certificate in ca to expected */
  8119. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8120. SSL_FILETYPE_PEM));
  8121. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8122. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8123. EVP_PKEY_free(pkey);
  8124. X509_free(x509);
  8125. X509_free(cert);
  8126. BIO_free(bio);
  8127. PKCS12_free(pkcs12);
  8128. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  8129. /* test order of parsing */
  8130. f = XFOPEN(file, "rb");
  8131. AssertTrue(f != XBADFILE);
  8132. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8133. XFCLOSE(f);
  8134. /* check verify MAC fail case */
  8135. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8136. AssertIntEQ(ret, 0);
  8137. AssertNull(pkey);
  8138. AssertNull(cert);
  8139. /* check parse with no extra certs kept */
  8140. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8141. AssertIntEQ(ret, 1);
  8142. AssertNotNull(pkey);
  8143. AssertNotNull(cert);
  8144. wolfSSL_EVP_PKEY_free(pkey);
  8145. wolfSSL_X509_free(cert);
  8146. /* check parse with extra certs kept */
  8147. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8148. AssertIntEQ(ret, 1);
  8149. AssertNotNull(pkey);
  8150. AssertNotNull(cert);
  8151. AssertNotNull(ca);
  8152. wolfSSL_EVP_PKEY_free(pkey);
  8153. wolfSSL_X509_free(cert);
  8154. sk_X509_pop_free(ca, NULL);
  8155. PKCS12_free(pkcs12);
  8156. #endif /* HAVE_ECC */
  8157. #ifdef WC_RC2
  8158. /* test PKCS#12 with RC2 encryption */
  8159. f = XFOPEN(rc2p12, "rb");
  8160. AssertTrue(f != XBADFILE);
  8161. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8162. XFCLOSE(f);
  8163. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8164. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8165. /* check verify MAC fail case */
  8166. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8167. AssertIntEQ(ret, 0);
  8168. AssertNull(pkey);
  8169. AssertNull(cert);
  8170. /* check parse iwth not extra certs kept */
  8171. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8172. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8173. AssertNotNull(pkey);
  8174. AssertNotNull(cert);
  8175. /* check parse with extra certs kept */
  8176. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8177. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8178. AssertNotNull(pkey);
  8179. AssertNotNull(cert);
  8180. AssertNotNull(ca);
  8181. wolfSSL_EVP_PKEY_free(pkey);
  8182. wolfSSL_X509_free(cert);
  8183. sk_X509_pop_free(ca, NULL);
  8184. BIO_free(bio);
  8185. PKCS12_free(pkcs12);
  8186. #endif /* WC_RC2 */
  8187. /* Test i2d_PKCS12_bio */
  8188. f = XFOPEN(file, "rb");
  8189. AssertTrue((f != XBADFILE));
  8190. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8191. XFCLOSE(f);
  8192. bio = BIO_new(BIO_s_mem());
  8193. AssertNotNull(bio);
  8194. ret = i2d_PKCS12_bio(bio, pkcs12);
  8195. AssertIntEQ(ret, 1);
  8196. ret = i2d_PKCS12_bio(NULL, pkcs12);
  8197. AssertIntEQ(ret, 0);
  8198. ret = i2d_PKCS12_bio(bio, NULL);
  8199. AssertIntEQ(ret, 0);
  8200. PKCS12_free(pkcs12);
  8201. BIO_free(bio);
  8202. (void)order;
  8203. res = TEST_RES_CHECK(1);
  8204. #endif /* OPENSSL_EXTRA */
  8205. #endif /* HAVE_FIPS */
  8206. return res;
  8207. }
  8208. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  8209. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  8210. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  8211. #define TEST_PKCS8_ENC
  8212. #endif
  8213. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8214. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8215. /* used to keep track if FailTestCallback was called */
  8216. static int failTestCallbackCalled = 0;
  8217. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  8218. {
  8219. (void)passwd;
  8220. (void)sz;
  8221. (void)rw;
  8222. (void)userdata;
  8223. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  8224. failTestCallbackCalled = 1;
  8225. return -1;
  8226. }
  8227. #endif
  8228. static int test_wolfSSL_no_password_cb(void)
  8229. {
  8230. int res = TEST_SKIPPED;
  8231. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8232. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8233. WOLFSSL_CTX* ctx;
  8234. byte buff[FOURK_BUF];
  8235. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8236. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8237. XFILE f;
  8238. int bytes;
  8239. #ifndef NO_WOLFSSL_CLIENT
  8240. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  8241. #else
  8242. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  8243. #endif
  8244. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  8245. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  8246. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8247. XFCLOSE(f);
  8248. AssertIntLE(bytes, sizeof(buff));
  8249. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8250. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8251. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  8252. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8253. XFCLOSE(f);
  8254. AssertIntLE(bytes, sizeof(buff));
  8255. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8256. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8257. wolfSSL_CTX_free(ctx);
  8258. if (failTestCallbackCalled != 0) {
  8259. Fail(("Password callback should not be called by default"),
  8260. ("Password callback was called without attempting "
  8261. "to first decipher private key without password."));
  8262. }
  8263. res = TEST_RES_CHECK(1);
  8264. #endif
  8265. return res;
  8266. }
  8267. #ifdef TEST_PKCS8_ENC
  8268. /* for PKCS8 test case */
  8269. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  8270. {
  8271. int flag = 0;
  8272. (void)rw;
  8273. if (userdata != NULL) {
  8274. flag = *((int*)userdata); /* user set data */
  8275. }
  8276. switch (flag) {
  8277. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  8278. * can be anything the user wishes to be passed to the callback
  8279. * associated with the WOLFSSL_CTX */
  8280. XSTRNCPY(passwd, "yassl123", sz);
  8281. return 8;
  8282. default:
  8283. return BAD_FUNC_ARG;
  8284. }
  8285. }
  8286. #endif /* TEST_PKCS8_ENC */
  8287. /* Testing functions dealing with PKCS8 */
  8288. static int test_wolfSSL_PKCS8(void)
  8289. {
  8290. int res = TEST_SKIPPED;
  8291. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  8292. !defined(WOLFCRYPT_ONLY)
  8293. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8294. byte buff[FOURK_BUF];
  8295. byte der[FOURK_BUF];
  8296. #ifndef NO_RSA
  8297. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  8298. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  8299. #endif
  8300. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8301. #ifdef HAVE_ECC
  8302. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8303. #endif
  8304. XFILE f;
  8305. int bytes;
  8306. WOLFSSL_CTX* ctx;
  8307. #if defined(HAVE_ECC) && !defined(NO_CODING)
  8308. int ret;
  8309. ecc_key key;
  8310. word32 x = 0;
  8311. #endif
  8312. #ifdef TEST_PKCS8_ENC
  8313. #if !defined(NO_RSA) && !defined(NO_SHA)
  8314. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  8315. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  8316. #endif
  8317. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8318. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  8319. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  8320. #endif
  8321. int flag;
  8322. #endif
  8323. (void)der;
  8324. #ifndef NO_WOLFSSL_CLIENT
  8325. #ifndef WOLFSSL_NO_TLS12
  8326. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8327. #else
  8328. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8329. #endif
  8330. #else
  8331. #ifndef WOLFSSL_NO_TLS12
  8332. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  8333. #else
  8334. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  8335. #endif
  8336. #endif
  8337. #ifdef TEST_PKCS8_ENC
  8338. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  8339. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  8340. flag = 1; /* used by password callback as return code */
  8341. #if !defined(NO_RSA) && !defined(NO_SHA)
  8342. /* test loading PEM PKCS8 encrypted file */
  8343. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  8344. AssertTrue((f != XBADFILE));
  8345. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8346. XFCLOSE(f);
  8347. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8348. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8349. /* this next case should fail because of password callback return code */
  8350. flag = 0; /* used by password callback as return code */
  8351. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8352. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8353. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8354. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8355. "yassl123"), 0);
  8356. /* test that error value is returned with a bad password */
  8357. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8358. "bad"), 0);
  8359. /* test loading PEM PKCS8 encrypted file */
  8360. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  8361. AssertTrue((f != XBADFILE));
  8362. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8363. XFCLOSE(f);
  8364. flag = 1; /* used by password callback as return code */
  8365. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8366. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8367. /* this next case should fail because of password callback return code */
  8368. flag = 0; /* used by password callback as return code */
  8369. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8370. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8371. #endif /* !NO_RSA && !NO_SHA */
  8372. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8373. /* test loading PEM PKCS8 encrypted ECC Key file */
  8374. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  8375. AssertTrue((f != XBADFILE));
  8376. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8377. XFCLOSE(f);
  8378. flag = 1; /* used by password callback as return code */
  8379. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8380. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8381. /* this next case should fail because of password callback return code */
  8382. flag = 0; /* used by password callback as return code */
  8383. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8384. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8385. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8386. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8387. "yassl123"), 0);
  8388. /* test that error value is returned with a bad password */
  8389. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8390. "bad"), 0);
  8391. /* test loading DER PKCS8 encrypted ECC Key file */
  8392. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  8393. AssertTrue((f != XBADFILE));
  8394. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8395. XFCLOSE(f);
  8396. flag = 1; /* used by password callback as return code */
  8397. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8398. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8399. /* this next case should fail because of password callback return code */
  8400. flag = 0; /* used by password callback as return code */
  8401. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8402. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8403. /* leave flag as "okay" */
  8404. flag = 1;
  8405. #endif /* HAVE_ECC && !NO_SHA */
  8406. #endif /* TEST_PKCS8_ENC */
  8407. #ifndef NO_RSA
  8408. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  8409. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  8410. AssertTrue((f != XBADFILE));
  8411. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8412. XFCLOSE(f);
  8413. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8414. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8415. /* test loading PEM PKCS8 private key file (not encrypted) */
  8416. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  8417. AssertTrue((f != XBADFILE));
  8418. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8419. XFCLOSE(f);
  8420. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8421. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8422. #endif /* !NO_RSA */
  8423. /* Test PKCS8 PEM ECC key no crypt */
  8424. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  8425. AssertTrue((f != XBADFILE));
  8426. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8427. XFCLOSE(f);
  8428. #ifdef HAVE_ECC
  8429. /* Test PKCS8 PEM ECC key no crypt */
  8430. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8431. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8432. #ifndef NO_CODING
  8433. /* decrypt PKCS8 PEM to key in DER format */
  8434. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  8435. (word32)sizeof(der), NULL)), 0);
  8436. ret = wc_ecc_init(&key);
  8437. if (ret == 0) {
  8438. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  8439. wc_ecc_free(&key);
  8440. }
  8441. AssertIntEQ(ret, 0);
  8442. #endif
  8443. /* Test PKCS8 DER ECC key no crypt */
  8444. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  8445. AssertTrue((f != XBADFILE));
  8446. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8447. XFCLOSE(f);
  8448. /* Test using a PKCS8 ECC PEM */
  8449. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8450. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8451. #else
  8452. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  8453. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  8454. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  8455. #endif /* HAVE_ECC */
  8456. wolfSSL_CTX_free(ctx);
  8457. res = TEST_RES_CHECK(1);
  8458. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8459. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  8460. return res;
  8461. }
  8462. static int test_wolfSSL_PKCS8_ED25519(void)
  8463. {
  8464. int res = TEST_SKIPPED;
  8465. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  8466. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  8467. defined(HAVE_ED25519_KEY_IMPORT)
  8468. const byte encPrivKey[] = \
  8469. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  8470. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  8471. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  8472. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  8473. "2L9W4v7swXkV+X+a1ww=\n"
  8474. "-----END ENCRYPTED PRIVATE KEY-----\n";
  8475. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  8476. byte der[FOURK_BUF];
  8477. WOLFSSL_CTX* ctx;
  8478. int bytes;
  8479. XMEMSET(der, 0, sizeof(der));
  8480. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  8481. (word32)sizeof(der), password)), 0);
  8482. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8483. #ifndef NO_WOLFSSL_SERVER
  8484. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8485. #else
  8486. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8487. #endif
  8488. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  8489. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8490. wolfSSL_CTX_free(ctx);
  8491. res = TEST_RES_CHECK(1);
  8492. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8493. #endif
  8494. return res;
  8495. }
  8496. static int test_wolfSSL_PKCS8_ED448(void)
  8497. {
  8498. int res = TEST_SKIPPED;
  8499. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  8500. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  8501. defined(HAVE_ED448_KEY_IMPORT)
  8502. const byte encPrivKey[] = \
  8503. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  8504. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  8505. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  8506. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  8507. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  8508. "-----END ENCRYPTED PRIVATE KEY-----\n";
  8509. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  8510. byte der[FOURK_BUF];
  8511. WOLFSSL_CTX* ctx;
  8512. int bytes;
  8513. XMEMSET(der, 0, sizeof(der));
  8514. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  8515. (word32)sizeof(der), password)), 0);
  8516. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8517. #ifndef NO_WOLFSSL_SERVER
  8518. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8519. #else
  8520. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8521. #endif
  8522. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  8523. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8524. wolfSSL_CTX_free(ctx);
  8525. res = TEST_RES_CHECK(1);
  8526. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8527. #endif
  8528. return res;
  8529. }
  8530. /* Testing functions dealing with PKCS5 */
  8531. static int test_wolfSSL_PKCS5(void)
  8532. {
  8533. int res = TEST_SKIPPED;
  8534. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  8535. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  8536. const char* passwd = "myfipsPa$$W0rd";
  8537. #else
  8538. const char *passwd = "pass1234";
  8539. #endif
  8540. const unsigned char *salt = (unsigned char *)"salt1234";
  8541. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  8542. DYNAMIC_TYPE_TMP_BUFFER);
  8543. int ret = 0;
  8544. AssertNotNull(out);
  8545. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  8546. (int)XSTRLEN((const char *) salt), 10,
  8547. WC_SHA_DIGEST_SIZE,out);
  8548. AssertIntEQ(ret, SSL_SUCCESS);
  8549. #ifdef WOLFSSL_SHA512
  8550. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  8551. (int)XSTRLEN((const char *) salt), 10,
  8552. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  8553. AssertIntEQ(ret, SSL_SUCCESS);
  8554. #endif
  8555. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8556. res = TEST_RES_CHECK(1);
  8557. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  8558. return res;
  8559. }
  8560. /* test parsing URI from certificate */
  8561. static int test_wolfSSL_URI(void)
  8562. {
  8563. int res = TEST_SKIPPED;
  8564. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8565. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  8566. defined(OPENSSL_EXTRA))
  8567. WOLFSSL_X509* x509;
  8568. const char uri[] = "./certs/client-uri-cert.pem";
  8569. const char badUri[] = "./certs/client-relative-uri.pem";
  8570. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  8571. AssertNotNull(x509);
  8572. wolfSSL_FreeX509(x509);
  8573. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  8574. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  8575. && !defined(WOLFSSL_FPKI)
  8576. AssertNull(x509);
  8577. #else
  8578. AssertNotNull(x509);
  8579. wolfSSL_FreeX509(x509);
  8580. #endif
  8581. res = TEST_RES_CHECK(1);
  8582. #endif
  8583. return res;
  8584. }
  8585. static int test_wolfSSL_TBS(void)
  8586. {
  8587. int res = TEST_SKIPPED;
  8588. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8589. && defined(OPENSSL_EXTRA)
  8590. WOLFSSL_X509* x509;
  8591. const unsigned char* tbs;
  8592. int tbsSz;
  8593. AssertNotNull(x509 =
  8594. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  8595. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  8596. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  8597. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  8598. AssertIntEQ(tbsSz, 1003);
  8599. wolfSSL_FreeX509(x509);
  8600. res = TEST_RES_CHECK(1);
  8601. #endif
  8602. return res;
  8603. }
  8604. static int test_wolfSSL_X509_verify(void)
  8605. {
  8606. int res = TEST_SKIPPED;
  8607. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8608. && defined(OPENSSL_EXTRA)
  8609. WOLFSSL_X509* ca;
  8610. WOLFSSL_X509* serv;
  8611. WOLFSSL_EVP_PKEY* pkey;
  8612. unsigned char buf[2048];
  8613. const unsigned char* pt = NULL;
  8614. int bufSz;
  8615. AssertNotNull(ca =
  8616. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  8617. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  8618. WOLFSSL_SUCCESS);
  8619. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  8620. WOLFSSL_SUCCESS);
  8621. AssertIntEQ(bufSz, 294);
  8622. bufSz = 2048;
  8623. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  8624. WOLFSSL_SUCCESS);
  8625. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  8626. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  8627. AssertNotNull(serv =
  8628. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  8629. /* success case */
  8630. pt = buf;
  8631. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  8632. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  8633. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  8634. wolfSSL_EVP_PKEY_free(pkey);
  8635. /* fail case */
  8636. bufSz = 2048;
  8637. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  8638. WOLFSSL_SUCCESS);
  8639. pt = buf;
  8640. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  8641. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  8642. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  8643. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  8644. wolfSSL_EVP_PKEY_free(pkey);
  8645. wolfSSL_FreeX509(ca);
  8646. wolfSSL_FreeX509(serv);
  8647. res = TEST_RES_CHECK(1);
  8648. #endif
  8649. return res;
  8650. }
  8651. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8652. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8653. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8654. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) \
  8655. && !defined(NO_ASN_TIME)
  8656. /* create certificate with version 2 */
  8657. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  8658. {
  8659. WOLFSSL_X509 *x509, *x509v2;
  8660. WOLFSSL_EVP_PKEY *priv, *pub;
  8661. unsigned char *der = NULL, *key = NULL, *pt;
  8662. char *header, *name;
  8663. int derSz;
  8664. long keySz;
  8665. XFILE fp;
  8666. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  8667. time_t t;
  8668. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8669. WOLFSSL_FILETYPE_PEM));
  8670. fp = XFOPEN(cliKeyFile, "rb");
  8671. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  8672. WOLFSSL_SUCCESS);
  8673. XFCLOSE(fp);
  8674. pt = key;
  8675. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  8676. (const unsigned char**)&pt, keySz));
  8677. /* create the version 2 certificate */
  8678. AssertNotNull(x509v2 = X509_new());
  8679. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  8680. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  8681. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  8682. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  8683. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  8684. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  8685. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  8686. t = time(NULL);
  8687. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  8688. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  8689. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  8690. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  8691. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  8692. derSz = wolfSSL_i2d_X509(x509v2, &der);
  8693. AssertIntGT(derSz, 0);
  8694. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  8695. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8696. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  8697. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8698. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8699. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8700. wolfSSL_X509_free(x509);
  8701. wolfSSL_X509_free(x509v2);
  8702. wolfSSL_EVP_PKEY_free(priv);
  8703. wolfSSL_EVP_PKEY_free(pub);
  8704. wolfSSL_ASN1_TIME_free(notBefore);
  8705. wolfSSL_ASN1_TIME_free(notAfter);
  8706. }
  8707. /* override certificate version error */
  8708. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  8709. {
  8710. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8711. AssertIntEQ(store->error, ASN_VERSION_E);
  8712. #else
  8713. AssertIntEQ(store->error, 0);
  8714. #endif
  8715. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  8716. (void)preverify;
  8717. return 1;
  8718. }
  8719. /* set verify callback that will override bad certificate version */
  8720. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  8721. {
  8722. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  8723. }
  8724. #endif
  8725. static int test_wolfSSL_X509_TLS_version(void)
  8726. {
  8727. int res = TEST_SKIPPED;
  8728. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8729. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8730. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8731. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) \
  8732. && !defined(NO_ASN_TIME)
  8733. tcp_ready ready;
  8734. func_args server_args;
  8735. func_args client_args;
  8736. THREAD_TYPE serverThread;
  8737. callback_functions func_cb_client;
  8738. callback_functions func_cb_server;
  8739. /* test server rejects a client certificate that is not version 3 */
  8740. #ifdef WOLFSSL_TIRTOS
  8741. fdOpenSession(Task_self());
  8742. #endif
  8743. XMEMSET(&server_args, 0, sizeof(func_args));
  8744. XMEMSET(&client_args, 0, sizeof(func_args));
  8745. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8746. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8747. StartTCP();
  8748. InitTcpReady(&ready);
  8749. #if defined(USE_WINDOWS_API)
  8750. /* use RNG to get random port if using windows */
  8751. ready.port = GetRandomPort();
  8752. #endif
  8753. server_args.signal = &ready;
  8754. client_args.signal = &ready;
  8755. server_args.return_code = TEST_FAIL;
  8756. client_args.return_code = TEST_FAIL;
  8757. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8758. #ifndef WOLFSSL_NO_TLS12
  8759. func_cb_client.method = wolfTLSv1_2_client_method;
  8760. #else
  8761. func_cb_client.method = wolfTLSv1_3_client_method;
  8762. #endif
  8763. client_args.callbacks = &func_cb_client;
  8764. #ifndef WOLFSSL_NO_TLS12
  8765. func_cb_server.method = wolfTLSv1_2_server_method;
  8766. #else
  8767. func_cb_server.method = wolfTLSv1_3_server_method;
  8768. #endif
  8769. server_args.callbacks = &func_cb_server;
  8770. start_thread(test_server_nofail, &server_args, &serverThread);
  8771. wait_tcp_ready(&server_args);
  8772. test_client_nofail(&client_args, NULL);
  8773. join_thread(serverThread);
  8774. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8775. AssertIntEQ(client_args.return_code, TEST_FAIL);
  8776. AssertIntEQ(server_args.return_code, TEST_FAIL);
  8777. #else
  8778. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8779. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8780. #endif
  8781. FreeTcpReady(&ready);
  8782. #ifdef WOLFSSL_TIRTOS
  8783. fdCloseSession(Task_self());
  8784. #endif
  8785. /* Now re run but override the bad X509 version */
  8786. #ifdef WOLFSSL_TIRTOS
  8787. fdOpenSession(Task_self());
  8788. #endif
  8789. XMEMSET(&server_args, 0, sizeof(func_args));
  8790. XMEMSET(&client_args, 0, sizeof(func_args));
  8791. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8792. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8793. StartTCP();
  8794. InitTcpReady(&ready);
  8795. #if defined(USE_WINDOWS_API)
  8796. /* use RNG to get random port if using windows */
  8797. ready.port = GetRandomPort();
  8798. #endif
  8799. server_args.signal = &ready;
  8800. client_args.signal = &ready;
  8801. server_args.return_code = TEST_FAIL;
  8802. client_args.return_code = TEST_FAIL;
  8803. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8804. func_cb_server.ctx_ready = &test_set_override_x509;
  8805. #ifndef WOLFSSL_NO_TLS12
  8806. func_cb_client.method = wolfTLSv1_2_client_method;
  8807. #else
  8808. func_cb_client.method = wolfTLSv1_3_client_method;
  8809. #endif
  8810. client_args.callbacks = &func_cb_client;
  8811. #ifndef WOLFSSL_NO_TLS12
  8812. func_cb_server.method = wolfTLSv1_2_server_method;
  8813. #else
  8814. func_cb_server.method = wolfTLSv1_3_server_method;
  8815. #endif
  8816. server_args.callbacks = &func_cb_server;
  8817. start_thread(test_server_nofail, &server_args, &serverThread);
  8818. wait_tcp_ready(&server_args);
  8819. test_client_nofail(&client_args, NULL);
  8820. join_thread(serverThread);
  8821. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8822. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8823. FreeTcpReady(&ready);
  8824. #ifdef WOLFSSL_TIRTOS
  8825. fdCloseSession(Task_self());
  8826. #endif
  8827. res = TEST_RES_CHECK(1);
  8828. #endif
  8829. return res;
  8830. }
  8831. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  8832. * version allowed.
  8833. * POST: 1 on success.
  8834. */
  8835. static int test_wolfSSL_CTX_SetMinVersion(void)
  8836. {
  8837. int res = TEST_SKIPPED;
  8838. #ifndef NO_WOLFSSL_CLIENT
  8839. int failFlag = WOLFSSL_SUCCESS;
  8840. WOLFSSL_CTX* ctx;
  8841. int itr;
  8842. #ifndef NO_OLD_TLS
  8843. const int versions[] = {
  8844. #ifdef WOLFSSL_ALLOW_TLSV10
  8845. WOLFSSL_TLSV1,
  8846. #endif
  8847. WOLFSSL_TLSV1_1,
  8848. WOLFSSL_TLSV1_2 };
  8849. #elif !defined(WOLFSSL_NO_TLS12)
  8850. const int versions[] = { WOLFSSL_TLSV1_2 };
  8851. #elif defined(WOLFSSL_TLS13)
  8852. const int versions[] = { WOLFSSL_TLSV1_3 };
  8853. #else
  8854. const int versions[0];
  8855. #endif
  8856. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8857. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  8858. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  8859. != WOLFSSL_SUCCESS) {
  8860. failFlag = WOLFSSL_FAILURE;
  8861. }
  8862. }
  8863. wolfSSL_CTX_free(ctx);
  8864. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  8865. #endif
  8866. return res;
  8867. } /* END test_wolfSSL_CTX_SetMinVersion */
  8868. /*----------------------------------------------------------------------------*
  8869. | OCSP Stapling
  8870. *----------------------------------------------------------------------------*/
  8871. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  8872. * need to contact the CA, lowering the cost of cert revocation checking.
  8873. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  8874. * POST: 1 returned for success.
  8875. */
  8876. static int test_wolfSSL_UseOCSPStapling(void)
  8877. {
  8878. int res = TEST_SKIPPED;
  8879. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  8880. !defined(NO_WOLFSSL_CLIENT)
  8881. int ret;
  8882. WOLFSSL_CTX* ctx;
  8883. WOLFSSL* ssl;
  8884. #ifndef NO_WOLFSSL_CLIENT
  8885. #ifndef WOLFSSL_NO_TLS12
  8886. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8887. #else
  8888. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8889. #endif
  8890. #else
  8891. #ifndef WOLFSSL_NO_TLS12
  8892. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8893. #else
  8894. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8895. #endif
  8896. #endif
  8897. ssl = wolfSSL_new(ctx);
  8898. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  8899. WOLFSSL_CSR2_OCSP_USE_NONCE);
  8900. wolfSSL_free(ssl);
  8901. wolfSSL_CTX_free(ctx);
  8902. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  8903. #endif
  8904. return res;
  8905. } /*END test_wolfSSL_UseOCSPStapling */
  8906. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  8907. * stapling eliminates the need to contact the CA and lowers cert revocation
  8908. * check.
  8909. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  8910. */
  8911. static int test_wolfSSL_UseOCSPStaplingV2(void)
  8912. {
  8913. int res = TEST_SKIPPED;
  8914. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  8915. !defined(NO_WOLFSSL_CLIENT)
  8916. int ret;
  8917. WOLFSSL_CTX* ctx;
  8918. WOLFSSL* ssl;
  8919. #ifndef NO_WOLFSSL_CLIENT
  8920. #ifndef WOLFSSL_NO_TLS12
  8921. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8922. #else
  8923. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8924. #endif
  8925. #else
  8926. #ifndef WOLFSSL_NO_TLS12
  8927. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8928. #else
  8929. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8930. #endif
  8931. #endif
  8932. ssl = wolfSSL_new(ctx);
  8933. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  8934. WOLFSSL_CSR2_OCSP_USE_NONCE );
  8935. wolfSSL_free(ssl);
  8936. wolfSSL_CTX_free(ctx);
  8937. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  8938. #endif
  8939. return res;
  8940. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  8941. /*----------------------------------------------------------------------------*
  8942. | Multicast Tests
  8943. *----------------------------------------------------------------------------*/
  8944. static int test_wolfSSL_mcast(void)
  8945. {
  8946. int res = TEST_SKIPPED;
  8947. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  8948. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  8949. WOLFSSL_CTX* ctx;
  8950. WOLFSSL* ssl;
  8951. int result;
  8952. byte preMasterSecret[512];
  8953. byte clientRandom[32];
  8954. byte serverRandom[32];
  8955. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  8956. byte buf[256];
  8957. word16 newId;
  8958. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  8959. AssertNotNull(ctx);
  8960. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  8961. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8962. ssl = wolfSSL_new(ctx);
  8963. AssertNotNull(ssl);
  8964. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  8965. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  8966. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  8967. result = wolfSSL_set_secret(ssl, 23,
  8968. preMasterSecret, sizeof(preMasterSecret),
  8969. clientRandom, serverRandom, suite);
  8970. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8971. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  8972. AssertIntLE(result, 0);
  8973. AssertIntLE(newId, 100);
  8974. wolfSSL_free(ssl);
  8975. wolfSSL_CTX_free(ctx);
  8976. res = TEST_RES_CHECK(1);
  8977. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  8978. * WOLFSSL_SNIFFER) */
  8979. return res;
  8980. }
  8981. /*----------------------------------------------------------------------------*
  8982. | Wolfcrypt
  8983. *----------------------------------------------------------------------------*/
  8984. /*
  8985. * Unit test for the wc_InitBlake2b()
  8986. */
  8987. static int test_wc_InitBlake2b(void)
  8988. {
  8989. int res = TEST_SKIPPED;
  8990. #ifdef HAVE_BLAKE2
  8991. Blake2b blake;
  8992. int ret = 0;
  8993. /* Test good arg. */
  8994. ret = wc_InitBlake2b(&blake, 64);
  8995. if (ret != 0) {
  8996. ret = WOLFSSL_FATAL_ERROR;
  8997. }
  8998. /* Test bad arg. */
  8999. if (!ret) {
  9000. ret = wc_InitBlake2b(NULL, 64);
  9001. if (ret == 0) {
  9002. ret = WOLFSSL_FATAL_ERROR;
  9003. }
  9004. else {
  9005. ret = 0;
  9006. }
  9007. }
  9008. if (!ret) {
  9009. ret = wc_InitBlake2b(NULL, 128);
  9010. if (ret == 0) {
  9011. ret = WOLFSSL_FATAL_ERROR;
  9012. }
  9013. else {
  9014. ret = 0;
  9015. }
  9016. }
  9017. if (!ret) {
  9018. ret = wc_InitBlake2b(&blake, 128);
  9019. if (ret == 0) {
  9020. ret = WOLFSSL_FATAL_ERROR;
  9021. }
  9022. else {
  9023. ret = 0;
  9024. }
  9025. }
  9026. if (!ret) {
  9027. ret = wc_InitBlake2b(NULL, 0);
  9028. if (ret == 0) {
  9029. ret = WOLFSSL_FATAL_ERROR;
  9030. }
  9031. else {
  9032. ret = 0;
  9033. }
  9034. }
  9035. if (!ret) {
  9036. ret = wc_InitBlake2b(&blake, 0);
  9037. if (ret == 0) {
  9038. ret = WOLFSSL_FATAL_ERROR;
  9039. }
  9040. else {
  9041. ret = 0;
  9042. }
  9043. }
  9044. res = TEST_RES_CHECK(ret == 0);
  9045. #endif
  9046. return res;
  9047. } /*END test_wc_InitBlake2b*/
  9048. /*
  9049. * Unit test for the wc_InitBlake2b_WithKey()
  9050. */
  9051. static int test_wc_InitBlake2b_WithKey(void)
  9052. {
  9053. int res = TEST_SKIPPED;
  9054. #ifdef HAVE_BLAKE2
  9055. Blake2b blake;
  9056. word32 digestSz = BLAKE2B_KEYBYTES;
  9057. byte key[BLAKE2B_KEYBYTES];
  9058. word32 keylen = BLAKE2B_KEYBYTES;
  9059. int ret = 0;
  9060. XMEMSET(key, 0, sizeof(key));
  9061. /* Test good arg. */
  9062. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  9063. if (ret != 0) {
  9064. ret = WOLFSSL_FATAL_ERROR;
  9065. }
  9066. /* Test bad args. */
  9067. if (ret == 0) {
  9068. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  9069. if (ret == BAD_FUNC_ARG) {
  9070. ret = 0;
  9071. }
  9072. }
  9073. if (ret == 0) {
  9074. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  9075. if (ret == BAD_FUNC_ARG) {
  9076. ret = 0;
  9077. }
  9078. }
  9079. if (ret == 0) {
  9080. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  9081. }
  9082. res = TEST_RES_CHECK(ret == 0);
  9083. #endif
  9084. return res;
  9085. } /*END wc_InitBlake2b_WithKey*/
  9086. /*
  9087. * Unit test for the wc_InitBlake2s_WithKey()
  9088. */
  9089. static int test_wc_InitBlake2s_WithKey(void)
  9090. {
  9091. int res = TEST_SKIPPED;
  9092. #ifdef HAVE_BLAKE2S
  9093. Blake2s blake;
  9094. word32 digestSz = BLAKE2S_KEYBYTES;
  9095. byte *key = (byte*)"01234567890123456789012345678901";
  9096. word32 keylen = BLAKE2S_KEYBYTES;
  9097. int ret = 0;
  9098. /* Test good arg. */
  9099. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  9100. if (ret != 0) {
  9101. ret = WOLFSSL_FATAL_ERROR;
  9102. }
  9103. /* Test bad args. */
  9104. if (ret == 0) {
  9105. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  9106. if (ret == BAD_FUNC_ARG) {
  9107. ret = 0;
  9108. }
  9109. }
  9110. if (ret == 0) {
  9111. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  9112. if (ret == BAD_FUNC_ARG) {
  9113. ret = 0;
  9114. }
  9115. }
  9116. if (ret == 0) {
  9117. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  9118. }
  9119. res = TEST_RES_CHECK(ret == 0);
  9120. #endif
  9121. return res;
  9122. } /*END wc_InitBlake2s_WithKey*/
  9123. /*
  9124. * Unit test for the wc_InitMd5()
  9125. */
  9126. static int test_wc_InitMd5(void)
  9127. {
  9128. int res = TEST_SKIPPED;
  9129. #ifndef NO_MD5
  9130. wc_Md5 md5;
  9131. int ret;
  9132. int flag = 0;
  9133. /* Test good arg. */
  9134. ret = wc_InitMd5(&md5);
  9135. if (ret != 0) {
  9136. flag = WOLFSSL_FATAL_ERROR;
  9137. }
  9138. /* Test bad arg. */
  9139. if (!flag) {
  9140. ret = wc_InitMd5(NULL);
  9141. if (ret != BAD_FUNC_ARG) {
  9142. flag = WOLFSSL_FATAL_ERROR;
  9143. }
  9144. }
  9145. wc_Md5Free(&md5);
  9146. res = TEST_RES_CHECK(flag == 0);
  9147. #endif
  9148. return res;
  9149. } /* END test_wc_InitMd5 */
  9150. /*
  9151. * Testing wc_UpdateMd5()
  9152. */
  9153. static int test_wc_Md5Update(void)
  9154. {
  9155. int res = TEST_SKIPPED;
  9156. #ifndef NO_MD5
  9157. wc_Md5 md5;
  9158. byte hash[WC_MD5_DIGEST_SIZE];
  9159. testVector a, b, c;
  9160. int ret;
  9161. int flag = 0;
  9162. ret = wc_InitMd5(&md5);
  9163. if (ret != 0) {
  9164. flag = ret;
  9165. }
  9166. /* Input */
  9167. if (!flag) {
  9168. a.input = "a";
  9169. a.inLen = XSTRLEN(a.input);
  9170. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  9171. if (ret != 0) {
  9172. flag = ret;
  9173. }
  9174. }
  9175. if (!flag) {
  9176. ret = wc_Md5Final(&md5, hash);
  9177. if (ret != 0) {
  9178. flag = ret;
  9179. }
  9180. }
  9181. /* Update input. */
  9182. if (!flag) {
  9183. a.input = "abc";
  9184. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  9185. "\x72";
  9186. a.inLen = XSTRLEN(a.input);
  9187. a.outLen = XSTRLEN(a.output);
  9188. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  9189. if (ret != 0) {
  9190. flag = ret;
  9191. }
  9192. }
  9193. if (!flag) {
  9194. ret = wc_Md5Final(&md5, hash);
  9195. if (ret != 0) {
  9196. flag = ret;
  9197. }
  9198. }
  9199. if (!flag) {
  9200. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  9201. flag = WOLFSSL_FATAL_ERROR;
  9202. }
  9203. }
  9204. /*Pass in bad values. */
  9205. if (!flag) {
  9206. b.input = NULL;
  9207. b.inLen = 0;
  9208. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  9209. if (ret != 0) {
  9210. flag = ret;
  9211. }
  9212. }
  9213. if (!flag) {
  9214. c.input = NULL;
  9215. c.inLen = WC_MD5_DIGEST_SIZE;
  9216. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  9217. if (ret != BAD_FUNC_ARG) {
  9218. flag = WOLFSSL_FATAL_ERROR;
  9219. }
  9220. }
  9221. if (!flag) {
  9222. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  9223. if (ret != BAD_FUNC_ARG) {
  9224. flag = WOLFSSL_FATAL_ERROR;
  9225. }
  9226. }
  9227. wc_Md5Free(&md5);
  9228. res = TEST_RES_CHECK(flag == 0);
  9229. #endif
  9230. return res;
  9231. } /* END test_wc_Md5Update() */
  9232. /*
  9233. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  9234. */
  9235. static int test_wc_Md5Final(void)
  9236. {
  9237. int res = TEST_SKIPPED;
  9238. #ifndef NO_MD5
  9239. /* Instantiate */
  9240. wc_Md5 md5;
  9241. byte* hash_test[3];
  9242. byte hash1[WC_MD5_DIGEST_SIZE];
  9243. byte hash2[2*WC_MD5_DIGEST_SIZE];
  9244. byte hash3[5*WC_MD5_DIGEST_SIZE];
  9245. int times, i, ret;
  9246. int flag = 0;
  9247. /* Initialize */
  9248. ret = wc_InitMd5(&md5);
  9249. if (ret != 0) {
  9250. flag = ret;
  9251. }
  9252. if (!flag) {
  9253. hash_test[0] = hash1;
  9254. hash_test[1] = hash2;
  9255. hash_test[2] = hash3;
  9256. }
  9257. times = sizeof(hash_test)/sizeof(byte*);
  9258. for (i = 0; i < times; i++) {
  9259. if (!flag) {
  9260. ret = wc_Md5Final(&md5, hash_test[i]);
  9261. if (ret != 0) {
  9262. flag = WOLFSSL_FATAL_ERROR;
  9263. }
  9264. }
  9265. }
  9266. /* Test bad args. */
  9267. if (!flag) {
  9268. ret = wc_Md5Final(NULL, NULL);
  9269. if (ret != BAD_FUNC_ARG) {
  9270. flag = WOLFSSL_FATAL_ERROR;
  9271. }
  9272. }
  9273. if (!flag) {
  9274. ret = wc_Md5Final(NULL, hash1);
  9275. if (ret != BAD_FUNC_ARG) {
  9276. flag = WOLFSSL_FATAL_ERROR;
  9277. }
  9278. }
  9279. if (!flag) {
  9280. ret = wc_Md5Final(&md5, NULL);
  9281. if (ret != BAD_FUNC_ARG) {
  9282. flag = WOLFSSL_FATAL_ERROR;
  9283. }
  9284. }
  9285. wc_Md5Free(&md5);
  9286. res = TEST_RES_CHECK(flag == 0);
  9287. #endif
  9288. return res;
  9289. }
  9290. /*
  9291. * Unit test for the wc_InitSha()
  9292. */
  9293. static int test_wc_InitSha(void)
  9294. {
  9295. int res = TEST_SKIPPED;
  9296. #ifndef NO_SHA
  9297. wc_Sha sha;
  9298. int ret;
  9299. int flag = 0;
  9300. /* Test good arg. */
  9301. ret = wc_InitSha(&sha);
  9302. if (ret != 0) {
  9303. flag = WOLFSSL_FATAL_ERROR;
  9304. }
  9305. /* Test bad arg. */
  9306. if (!flag) {
  9307. ret = wc_InitSha(NULL);
  9308. if (ret != BAD_FUNC_ARG) {
  9309. flag = WOLFSSL_FATAL_ERROR;
  9310. }
  9311. }
  9312. wc_ShaFree(&sha);
  9313. res = TEST_RES_CHECK(flag == 0);
  9314. #endif
  9315. return res;
  9316. } /* END test_wc_InitSha */
  9317. /*
  9318. * Tesing wc_ShaUpdate()
  9319. */
  9320. static int test_wc_ShaUpdate(void)
  9321. {
  9322. int res = TEST_SKIPPED;
  9323. #ifndef NO_SHA
  9324. wc_Sha sha;
  9325. byte hash[WC_SHA_DIGEST_SIZE];
  9326. testVector a, b, c;
  9327. int flag = 0;
  9328. int ret;
  9329. ret = wc_InitSha(&sha);
  9330. if (ret != 0) {
  9331. flag = ret;
  9332. }
  9333. /* Input. */
  9334. if (!flag) {
  9335. a.input = "a";
  9336. a.inLen = XSTRLEN(a.input);
  9337. ret = wc_ShaUpdate(&sha, NULL, 0);
  9338. if (ret != 0) {
  9339. flag = ret;
  9340. }
  9341. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  9342. if (ret != 0) {
  9343. flag = ret;
  9344. }
  9345. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9346. if (ret != 0) {
  9347. flag = ret;
  9348. }
  9349. }
  9350. if (!flag) {
  9351. ret = wc_ShaFinal(&sha, hash);
  9352. if (ret != 0) {
  9353. flag = ret;
  9354. }
  9355. }
  9356. /* Update input. */
  9357. if (!flag) {
  9358. a.input = "abc";
  9359. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  9360. "\x6C\x9C\xD0\xD8\x9D";
  9361. a.inLen = XSTRLEN(a.input);
  9362. a.outLen = XSTRLEN(a.output);
  9363. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9364. if (ret != 0) {
  9365. flag = ret;
  9366. }
  9367. }
  9368. if (!flag) {
  9369. ret = wc_ShaFinal(&sha, hash);
  9370. if (ret !=0) {
  9371. flag = ret;
  9372. }
  9373. }
  9374. if (!flag) {
  9375. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  9376. flag = WOLFSSL_FATAL_ERROR;
  9377. }
  9378. }
  9379. /* Try passing in bad values. */
  9380. if (!flag) {
  9381. b.input = NULL;
  9382. b.inLen = 0;
  9383. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  9384. if (ret != 0) {
  9385. flag = ret;
  9386. }
  9387. }
  9388. if (!flag) {
  9389. c.input = NULL;
  9390. c.inLen = WC_SHA_DIGEST_SIZE;
  9391. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  9392. if (ret != BAD_FUNC_ARG) {
  9393. flag = WOLFSSL_FATAL_ERROR;
  9394. }
  9395. }
  9396. if (!flag) {
  9397. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  9398. if (ret != BAD_FUNC_ARG) {
  9399. flag = WOLFSSL_FATAL_ERROR;
  9400. }
  9401. }
  9402. wc_ShaFree(&sha);
  9403. res = TEST_RES_CHECK(flag == 0);
  9404. #endif
  9405. return res;
  9406. } /* END test_wc_ShaUpdate() */
  9407. /*
  9408. * Unit test on wc_ShaFinal
  9409. */
  9410. static int test_wc_ShaFinal(void)
  9411. {
  9412. int res = TEST_SKIPPED;
  9413. #ifndef NO_SHA
  9414. wc_Sha sha;
  9415. byte* hash_test[3];
  9416. byte hash1[WC_SHA_DIGEST_SIZE];
  9417. byte hash2[2*WC_SHA_DIGEST_SIZE];
  9418. byte hash3[5*WC_SHA_DIGEST_SIZE];
  9419. int times, i, ret;
  9420. int flag = 0;
  9421. /*Initialize*/
  9422. ret = wc_InitSha(&sha);
  9423. if (ret) {
  9424. flag = ret;
  9425. }
  9426. if (!flag) {
  9427. hash_test[0] = hash1;
  9428. hash_test[1] = hash2;
  9429. hash_test[2] = hash3;
  9430. }
  9431. times = sizeof(hash_test)/sizeof(byte*);
  9432. for (i = 0; i < times; i++) {
  9433. if (!flag) {
  9434. ret = wc_ShaFinal(&sha, hash_test[i]);
  9435. if (ret != 0) {
  9436. flag = WOLFSSL_FATAL_ERROR;
  9437. }
  9438. }
  9439. }
  9440. /* Test bad args. */
  9441. if (!flag) {
  9442. ret = wc_ShaFinal(NULL, NULL);
  9443. if (ret != BAD_FUNC_ARG) {
  9444. flag = WOLFSSL_FATAL_ERROR;
  9445. }
  9446. }
  9447. if (!flag) {
  9448. ret = wc_ShaFinal(NULL, hash1);
  9449. if (ret != BAD_FUNC_ARG) {
  9450. flag = WOLFSSL_FATAL_ERROR;
  9451. }
  9452. }
  9453. if (!flag) {
  9454. ret = wc_ShaFinal(&sha, NULL);
  9455. if (ret != BAD_FUNC_ARG) {
  9456. flag = WOLFSSL_FATAL_ERROR;
  9457. }
  9458. }
  9459. wc_ShaFree(&sha);
  9460. res = TEST_RES_CHECK(flag == 0);
  9461. #endif
  9462. return res;
  9463. } /* END test_wc_ShaFinal */
  9464. /*
  9465. * Unit test for wc_InitSha256()
  9466. */
  9467. static int test_wc_InitSha256(void)
  9468. {
  9469. int res = TEST_SKIPPED;
  9470. #ifndef NO_SHA256
  9471. wc_Sha256 sha256;
  9472. int ret;
  9473. int flag = 0;
  9474. /* Test good arg. */
  9475. ret = wc_InitSha256(&sha256);
  9476. if (ret != 0) {
  9477. flag = WOLFSSL_FATAL_ERROR;
  9478. }
  9479. /* Test bad arg. */
  9480. if (!flag) {
  9481. ret = wc_InitSha256(NULL);
  9482. if (ret != BAD_FUNC_ARG) {
  9483. flag = WOLFSSL_FATAL_ERROR;
  9484. }
  9485. }
  9486. wc_Sha256Free(&sha256);
  9487. res = TEST_RES_CHECK(flag == 0);
  9488. #endif
  9489. return res;
  9490. } /* END test_wc_InitSha256 */
  9491. /*
  9492. * Unit test for wc_Sha256Update()
  9493. */
  9494. static int test_wc_Sha256Update(void)
  9495. {
  9496. int res = TEST_SKIPPED;
  9497. #ifndef NO_SHA256
  9498. wc_Sha256 sha256;
  9499. byte hash[WC_SHA256_DIGEST_SIZE];
  9500. testVector a, b, c;
  9501. int ret;
  9502. int flag = 0;
  9503. ret = wc_InitSha256(&sha256);
  9504. if (ret != 0) {
  9505. flag = ret;
  9506. }
  9507. /* Input. */
  9508. if (!flag) {
  9509. a.input = "a";
  9510. a.inLen = XSTRLEN(a.input);
  9511. ret = wc_Sha256Update(&sha256, NULL, 0);
  9512. if (ret != 0) {
  9513. flag = ret;
  9514. }
  9515. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  9516. if (ret != 0) {
  9517. flag = ret;
  9518. }
  9519. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  9520. if (ret != 0) {
  9521. flag = ret;
  9522. }
  9523. }
  9524. if (!flag) {
  9525. ret = wc_Sha256Final(&sha256, hash);
  9526. if (ret != 0) {
  9527. flag = ret;
  9528. }
  9529. }
  9530. /* Update input. */
  9531. if (!flag) {
  9532. a.input = "abc";
  9533. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  9534. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  9535. "\x15\xAD";
  9536. a.inLen = XSTRLEN(a.input);
  9537. a.outLen = XSTRLEN(a.output);
  9538. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  9539. if (ret != 0) {
  9540. flag = ret;
  9541. }
  9542. }
  9543. if (!flag) {
  9544. ret = wc_Sha256Final(&sha256, hash);
  9545. if (ret != 0) {
  9546. flag = ret;
  9547. }
  9548. }
  9549. if (!flag) {
  9550. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  9551. flag = WOLFSSL_FATAL_ERROR;
  9552. }
  9553. }
  9554. /* Try passing in bad values */
  9555. if (!flag) {
  9556. b.input = NULL;
  9557. b.inLen = 0;
  9558. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  9559. if (ret != 0) {
  9560. flag = ret;
  9561. }
  9562. }
  9563. if (!flag) {
  9564. c.input = NULL;
  9565. c.inLen = WC_SHA256_DIGEST_SIZE;
  9566. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  9567. if (ret != BAD_FUNC_ARG) {
  9568. flag = WOLFSSL_FATAL_ERROR;
  9569. }
  9570. }
  9571. if (!flag) {
  9572. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  9573. if (ret != BAD_FUNC_ARG) {
  9574. flag = WOLFSSL_FATAL_ERROR;
  9575. }
  9576. }
  9577. wc_Sha256Free(&sha256);
  9578. res = TEST_RES_CHECK(flag == 0);
  9579. #endif
  9580. return res;
  9581. } /* END test_wc_Sha256Update */
  9582. /*
  9583. * Unit test function for wc_Sha256Final()
  9584. */
  9585. static int test_wc_Sha256Final(void)
  9586. {
  9587. int res = TEST_SKIPPED;
  9588. #ifndef NO_SHA256
  9589. wc_Sha256 sha256;
  9590. byte* hash_test[3];
  9591. byte hash1[WC_SHA256_DIGEST_SIZE];
  9592. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  9593. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  9594. int times, i, ret;
  9595. int flag = 0;
  9596. /* Initialize */
  9597. ret = wc_InitSha256(&sha256);
  9598. if (ret != 0) {
  9599. flag = ret;
  9600. }
  9601. if (!flag) {
  9602. hash_test[0] = hash1;
  9603. hash_test[1] = hash2;
  9604. hash_test[2] = hash3;
  9605. }
  9606. times = sizeof(hash_test) / sizeof(byte*);
  9607. for (i = 0; i < times; i++) {
  9608. if (!flag) {
  9609. ret = wc_Sha256Final(&sha256, hash_test[i]);
  9610. if (ret != 0) {
  9611. flag = WOLFSSL_FATAL_ERROR;
  9612. }
  9613. }
  9614. }
  9615. /* Test bad args. */
  9616. if (!flag ) {
  9617. ret = wc_Sha256Final(NULL, NULL);
  9618. if (ret != BAD_FUNC_ARG) {
  9619. flag = WOLFSSL_FATAL_ERROR;
  9620. }
  9621. }
  9622. if (!flag) {
  9623. ret = wc_Sha256Final(NULL, hash1);
  9624. if (ret != BAD_FUNC_ARG) {
  9625. flag = WOLFSSL_FATAL_ERROR;
  9626. }
  9627. }
  9628. if (!flag) {
  9629. ret = wc_Sha256Final(&sha256, NULL);
  9630. if (ret != BAD_FUNC_ARG) {
  9631. flag = WOLFSSL_FATAL_ERROR;
  9632. }
  9633. }
  9634. wc_Sha256Free(&sha256);
  9635. res = TEST_RES_CHECK(flag == 0);
  9636. #endif
  9637. return res;
  9638. } /* END test_wc_Sha256Final */
  9639. /*
  9640. * Unit test function for wc_Sha256FinalRaw()
  9641. */
  9642. static int test_wc_Sha256FinalRaw(void)
  9643. {
  9644. int res = TEST_SKIPPED;
  9645. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  9646. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  9647. !defined(WOLFSSL_NO_HASH_RAW)
  9648. wc_Sha256 sha256;
  9649. byte* hash_test[3];
  9650. byte hash1[WC_SHA256_DIGEST_SIZE];
  9651. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  9652. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  9653. int times, i, ret;
  9654. int flag = 0;
  9655. /* Initialize */
  9656. ret = wc_InitSha256(&sha256);
  9657. if (ret != 0) {
  9658. flag = ret;
  9659. }
  9660. if (!flag) {
  9661. hash_test[0] = hash1;
  9662. hash_test[1] = hash2;
  9663. hash_test[2] = hash3;
  9664. }
  9665. times = sizeof(hash_test) / sizeof(byte*);
  9666. for (i = 0; i < times; i++) {
  9667. if (!flag) {
  9668. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  9669. if (ret != 0) {
  9670. flag = WOLFSSL_FATAL_ERROR;
  9671. }
  9672. }
  9673. }
  9674. /* Test bad args. */
  9675. if (!flag) {
  9676. ret = wc_Sha256FinalRaw(NULL, NULL);
  9677. if (ret != BAD_FUNC_ARG) {
  9678. flag = WOLFSSL_FATAL_ERROR;
  9679. }
  9680. }
  9681. if (!flag) {
  9682. ret = wc_Sha256FinalRaw(NULL, hash1);
  9683. if (ret != BAD_FUNC_ARG) {
  9684. flag = WOLFSSL_FATAL_ERROR;
  9685. }
  9686. }
  9687. if (!flag) {
  9688. ret = wc_Sha256FinalRaw(&sha256, NULL);
  9689. if (ret != BAD_FUNC_ARG) {
  9690. flag = WOLFSSL_FATAL_ERROR;
  9691. }
  9692. }
  9693. wc_Sha256Free(&sha256);
  9694. res = TEST_RES_CHECK(flag == 0);
  9695. #endif
  9696. return res;
  9697. } /* END test_wc_Sha256FinalRaw */
  9698. /*
  9699. * Unit test function for wc_Sha256GetFlags()
  9700. */
  9701. static int test_wc_Sha256GetFlags(void)
  9702. {
  9703. int res = TEST_SKIPPED;
  9704. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  9705. wc_Sha256 sha256;
  9706. word32 flags = 0;
  9707. int flag = 0;
  9708. /* Initialize */
  9709. flag = wc_InitSha256(&sha256);
  9710. if (flag == 0) {
  9711. flag = wc_Sha256GetFlags(&sha256, &flags);
  9712. }
  9713. if (flag == 0) {
  9714. if (flags & WC_HASH_FLAG_ISCOPY) {
  9715. flag = 0;
  9716. }
  9717. }
  9718. wc_Sha256Free(&sha256);
  9719. res = TEST_RES_CHECK(flag == 0);
  9720. #endif
  9721. return res;
  9722. } /* END test_wc_Sha256GetFlags */
  9723. /*
  9724. * Unit test function for wc_Sha256Free()
  9725. */
  9726. static int test_wc_Sha256Free(void)
  9727. {
  9728. int res = TEST_SKIPPED;
  9729. #ifndef NO_SHA256
  9730. wc_Sha256Free(NULL);
  9731. res = TEST_RES_CHECK(1);
  9732. #endif
  9733. return res;
  9734. } /* END test_wc_Sha256Free */
  9735. /*
  9736. * Unit test function for wc_Sha256GetHash()
  9737. */
  9738. static int test_wc_Sha256GetHash(void)
  9739. {
  9740. int res = TEST_SKIPPED;
  9741. #ifndef NO_SHA256
  9742. wc_Sha256 sha256;
  9743. byte hash1[WC_SHA256_DIGEST_SIZE];
  9744. int flag = 0;
  9745. /* Initialize */
  9746. flag = wc_InitSha256(&sha256);
  9747. if (flag == 0) {
  9748. flag = wc_Sha256GetHash(&sha256, hash1);
  9749. }
  9750. /*test bad arguments*/
  9751. if (flag == 0) {
  9752. flag = wc_Sha256GetHash(NULL, NULL);
  9753. if (flag == BAD_FUNC_ARG) {
  9754. flag = 0;
  9755. }
  9756. }
  9757. if (flag == 0) {
  9758. flag = wc_Sha256GetHash(NULL, hash1);
  9759. if (flag == BAD_FUNC_ARG) {
  9760. flag = 0;
  9761. }
  9762. }
  9763. if (flag == 0) {
  9764. flag = wc_Sha256GetHash(&sha256, NULL);
  9765. if (flag == BAD_FUNC_ARG) {
  9766. flag = 0;
  9767. }
  9768. }
  9769. wc_Sha256Free(&sha256);
  9770. res = TEST_RES_CHECK(flag == 0);
  9771. #endif
  9772. return res;
  9773. } /* END test_wc_Sha256GetHash */
  9774. /*
  9775. * Unit test function for wc_Sha256Copy()
  9776. */
  9777. static int test_wc_Sha256Copy(void)
  9778. {
  9779. int res = TEST_SKIPPED;
  9780. #ifndef NO_SHA256
  9781. wc_Sha256 sha256;
  9782. wc_Sha256 temp;
  9783. int flag = 0;
  9784. /* Initialize */
  9785. flag = wc_InitSha256(&sha256);
  9786. if (flag == 0) {
  9787. flag = wc_InitSha256(&temp);
  9788. }
  9789. if (flag == 0) {
  9790. flag = wc_Sha256Copy(&sha256, &temp);
  9791. }
  9792. /*test bad arguments*/
  9793. if (flag == 0) {
  9794. flag = wc_Sha256Copy(NULL, NULL);
  9795. if (flag == BAD_FUNC_ARG) {
  9796. flag = 0;
  9797. }
  9798. }
  9799. if (flag == 0) {
  9800. flag = wc_Sha256Copy(NULL, &temp);
  9801. if (flag == BAD_FUNC_ARG) {
  9802. flag = 0;
  9803. }
  9804. }
  9805. if (flag == 0) {
  9806. flag = wc_Sha256Copy(&sha256, NULL);
  9807. if (flag == BAD_FUNC_ARG) {
  9808. flag = 0;
  9809. }
  9810. }
  9811. wc_Sha256Free(&sha256);
  9812. wc_Sha256Free(&temp);
  9813. res = TEST_RES_CHECK(flag == 0);
  9814. #endif
  9815. return res;
  9816. } /* END test_wc_Sha256Copy */
  9817. /*
  9818. * Testing wc_InitSha512()
  9819. */
  9820. static int test_wc_InitSha512(void)
  9821. {
  9822. int res = TEST_SKIPPED;
  9823. #ifdef WOLFSSL_SHA512
  9824. wc_Sha512 sha512;
  9825. int ret;
  9826. int flag = 0;
  9827. /* Test good arg. */
  9828. ret = wc_InitSha512(&sha512);
  9829. if (ret != 0) {
  9830. flag = WOLFSSL_FATAL_ERROR;
  9831. }
  9832. /* Test bad arg. */
  9833. if (!flag) {
  9834. ret = wc_InitSha512(NULL);
  9835. if (ret != BAD_FUNC_ARG) {
  9836. flag = WOLFSSL_FATAL_ERROR;
  9837. }
  9838. }
  9839. wc_Sha512Free(&sha512);
  9840. res = TEST_RES_CHECK(flag == 0);
  9841. #endif
  9842. return res;
  9843. } /* END test_wc_InitSha512 */
  9844. /*
  9845. * wc_Sha512Update() test.
  9846. */
  9847. static int test_wc_Sha512Update(void)
  9848. {
  9849. int res = TEST_SKIPPED;
  9850. #ifdef WOLFSSL_SHA512
  9851. wc_Sha512 sha512;
  9852. byte hash[WC_SHA512_DIGEST_SIZE];
  9853. testVector a, b, c;
  9854. int ret;
  9855. int flag = 0;
  9856. ret = wc_InitSha512(&sha512);
  9857. if (ret != 0) {
  9858. flag = ret;
  9859. }
  9860. /* Input. */
  9861. if (!flag) {
  9862. a.input = "a";
  9863. a.inLen = XSTRLEN(a.input);
  9864. ret = wc_Sha512Update(&sha512, NULL, 0);
  9865. if (ret != 0) {
  9866. flag = ret;
  9867. }
  9868. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  9869. if (ret != 0) {
  9870. flag = ret;
  9871. }
  9872. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9873. if (ret != 0) {
  9874. flag = ret;
  9875. }
  9876. ret = wc_Sha512Final(&sha512, hash);
  9877. if (ret != 0) {
  9878. flag = ret;
  9879. }
  9880. }
  9881. /* Update input. */
  9882. if (!flag) {
  9883. a.input = "abc";
  9884. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  9885. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  9886. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  9887. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  9888. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  9889. a.inLen = XSTRLEN(a.input);
  9890. a.outLen = XSTRLEN(a.output);
  9891. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9892. if (ret != 0) {
  9893. flag = ret;
  9894. }
  9895. }
  9896. if (!flag) {
  9897. ret = wc_Sha512Final(&sha512, hash);
  9898. if (ret != 0) {
  9899. flag = ret;
  9900. }
  9901. }
  9902. if (!flag) {
  9903. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  9904. flag = WOLFSSL_FATAL_ERROR;
  9905. }
  9906. }
  9907. /* Try passing in bad values */
  9908. if (!flag) {
  9909. b.input = NULL;
  9910. b.inLen = 0;
  9911. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  9912. if (ret != 0) {
  9913. flag = ret;
  9914. }
  9915. }
  9916. if (!flag) {
  9917. c.input = NULL;
  9918. c.inLen = WC_SHA512_DIGEST_SIZE;
  9919. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9920. if (ret != BAD_FUNC_ARG) {
  9921. flag = WOLFSSL_FATAL_ERROR;
  9922. }
  9923. }
  9924. if (!flag) {
  9925. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  9926. if (ret != BAD_FUNC_ARG) {
  9927. flag = WOLFSSL_FATAL_ERROR;
  9928. }
  9929. }
  9930. wc_Sha512Free(&sha512);
  9931. res = TEST_RES_CHECK(flag == 0);
  9932. #endif
  9933. return res;
  9934. } /* END test_wc_Sha512Update */
  9935. #ifdef WOLFSSL_SHA512
  9936. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9937. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9938. /* Perfoms test for
  9939. * - wc_Sha512Final/wc_Sha512FinalRaw
  9940. * - wc_Sha512_224Final/wc_Sha512_224Final
  9941. * - wc_Sha512_256Final/wc_Sha512_256Final
  9942. * parameter:
  9943. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  9944. * WC_HASH_TYPE_SHA512_256
  9945. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  9946. *return 0 on success
  9947. */
  9948. static int test_Sha512_Family_Final(int type, int isRaw)
  9949. {
  9950. wc_Sha512 sha512;
  9951. byte* hash_test[3];
  9952. byte hash1[WC_SHA512_DIGEST_SIZE];
  9953. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9954. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9955. int times, i, ret;
  9956. int(*initFp)(wc_Sha512*);
  9957. int(*finalFp)(wc_Sha512*, byte*);
  9958. void(*freeFp)(wc_Sha512*);
  9959. if (type == WC_HASH_TYPE_SHA512) {
  9960. initFp = wc_InitSha512;
  9961. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9962. !defined(WOLFSSL_NO_HASH_RAW)
  9963. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  9964. #else
  9965. finalFp = (isRaw)? NULL : wc_Sha512Final;
  9966. #endif
  9967. freeFp = wc_Sha512Free;
  9968. }
  9969. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9970. #if !defined(WOLFSSL_NOSHA512_224)
  9971. else if (type == WC_HASH_TYPE_SHA512_224) {
  9972. initFp = wc_InitSha512_224;
  9973. #if !defined(WOLFSSL_NO_HASH_RAW)
  9974. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  9975. #else
  9976. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  9977. #endif
  9978. freeFp = wc_Sha512_224Free;
  9979. }
  9980. #endif
  9981. #if !defined(WOLFSSL_NOSHA512_256)
  9982. else if (type == WC_HASH_TYPE_SHA512_256) {
  9983. initFp = wc_InitSha512_256;
  9984. #if !defined(WOLFSSL_NO_HASH_RAW)
  9985. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  9986. #else
  9987. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  9988. #endif
  9989. freeFp = wc_Sha512_256Free;
  9990. }
  9991. #endif
  9992. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9993. else
  9994. return TEST_FAIL;
  9995. /* Initialize */
  9996. ret = initFp(&sha512);
  9997. if (!ret) {
  9998. hash_test[0] = hash1;
  9999. hash_test[1] = hash2;
  10000. hash_test[2] = hash3;
  10001. }
  10002. times = sizeof(hash_test) / sizeof(byte *);
  10003. /* Good test args. */
  10004. for (i = 0; i < times && ret == 0; i++) {
  10005. ret = finalFp(&sha512, hash_test[i]);
  10006. }
  10007. /* Test bad args. */
  10008. if (!ret) {
  10009. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  10010. ret = WOLFSSL_FATAL_ERROR;
  10011. }
  10012. }
  10013. if (!ret) {
  10014. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  10015. ret = WOLFSSL_FATAL_ERROR;
  10016. }
  10017. }
  10018. if (!ret) {
  10019. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  10020. ret = WOLFSSL_FATAL_ERROR;
  10021. }
  10022. }
  10023. freeFp(&sha512);
  10024. return ret;
  10025. }
  10026. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10027. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10028. #endif /* WOLFSSL_SHA512 */
  10029. /*
  10030. * Unit test function for wc_Sha512Final()
  10031. */
  10032. static int test_wc_Sha512Final(void)
  10033. {
  10034. int res = TEST_SKIPPED;
  10035. #ifdef WOLFSSL_SHA512
  10036. wc_Sha512 sha512;
  10037. byte* hash_test[3];
  10038. byte hash1[WC_SHA512_DIGEST_SIZE];
  10039. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10040. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10041. int times, i, ret;
  10042. int flag = 0;
  10043. /* Initialize */
  10044. ret = wc_InitSha512(&sha512);
  10045. if (ret != 0) {
  10046. flag = ret;
  10047. }
  10048. if (!flag) {
  10049. hash_test[0] = hash1;
  10050. hash_test[1] = hash2;
  10051. hash_test[2] = hash3;
  10052. }
  10053. times = sizeof(hash_test) / sizeof(byte *);
  10054. for (i = 0; i < times; i++) {
  10055. if (!flag) {
  10056. ret = wc_Sha512Final(&sha512, hash_test[i]);
  10057. if (ret != 0) {
  10058. flag = WOLFSSL_FATAL_ERROR;
  10059. }
  10060. }
  10061. }
  10062. /* Test bad args. */
  10063. if (!flag) {
  10064. ret = wc_Sha512Final(NULL, NULL);
  10065. if (ret != BAD_FUNC_ARG) {
  10066. flag = WOLFSSL_FATAL_ERROR;
  10067. }
  10068. }
  10069. if (!flag) {
  10070. ret = wc_Sha512Final(NULL, hash1);
  10071. if (ret != BAD_FUNC_ARG) {
  10072. flag = WOLFSSL_FATAL_ERROR;
  10073. }
  10074. }
  10075. if (!flag) {
  10076. ret = wc_Sha512Final(&sha512, NULL);
  10077. if (ret != BAD_FUNC_ARG) {
  10078. flag = WOLFSSL_FATAL_ERROR;
  10079. }
  10080. }
  10081. wc_Sha512Free(&sha512);
  10082. res = TEST_RES_CHECK(flag == 0);
  10083. #endif
  10084. return res;
  10085. } /* END test_wc_Sha512Final */
  10086. /*
  10087. * Unit test function for wc_Sha512GetFlags()
  10088. */
  10089. static int test_wc_Sha512GetFlags(void)
  10090. {
  10091. int res = TEST_SKIPPED;
  10092. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  10093. wc_Sha512 sha512;
  10094. word32 flags = 0;
  10095. int flag = 0;
  10096. /* Initialize */
  10097. flag = wc_InitSha512(&sha512);
  10098. if (flag == 0) {
  10099. flag = wc_Sha512GetFlags(&sha512, &flags);
  10100. }
  10101. if (flag == 0) {
  10102. if (flags & WC_HASH_FLAG_ISCOPY) {
  10103. flag = 0;
  10104. }
  10105. }
  10106. wc_Sha512Free(&sha512);
  10107. res = TEST_RES_CHECK(flag == 0);
  10108. #endif
  10109. return res;
  10110. } /* END test_wc_Sha512GetFlags */
  10111. /*
  10112. * Unit test function for wc_Sha512FinalRaw()
  10113. */
  10114. static int test_wc_Sha512FinalRaw(void)
  10115. {
  10116. int res = TEST_SKIPPED;
  10117. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10118. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10119. !defined(WOLFSSL_NO_HASH_RAW)
  10120. wc_Sha512 sha512;
  10121. byte* hash_test[3];
  10122. byte hash1[WC_SHA512_DIGEST_SIZE];
  10123. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10124. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10125. int times, i, ret;
  10126. int flag = 0;
  10127. /* Initialize */
  10128. ret = wc_InitSha512(&sha512);
  10129. if (ret != 0) {
  10130. flag = ret;
  10131. }
  10132. if (!flag) {
  10133. hash_test[0] = hash1;
  10134. hash_test[1] = hash2;
  10135. hash_test[2] = hash3;
  10136. }
  10137. times = sizeof(hash_test) / sizeof(byte*);
  10138. /* Good test args. */
  10139. for (i = 0; i < times; i++) {
  10140. if (!flag) {
  10141. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  10142. if (ret != 0) {
  10143. flag = WOLFSSL_FATAL_ERROR;
  10144. }
  10145. }
  10146. }
  10147. /* Test bad args. */
  10148. if (!flag ) {
  10149. ret = wc_Sha512FinalRaw(NULL, NULL);
  10150. if (ret != BAD_FUNC_ARG) {
  10151. flag = WOLFSSL_FATAL_ERROR;
  10152. }
  10153. }
  10154. if (!flag) {
  10155. ret = wc_Sha512FinalRaw(NULL, hash1);
  10156. if (ret != BAD_FUNC_ARG) {
  10157. flag = WOLFSSL_FATAL_ERROR;
  10158. }
  10159. }
  10160. if (!flag) {
  10161. ret = wc_Sha512FinalRaw(&sha512, NULL);
  10162. if (ret != BAD_FUNC_ARG) {
  10163. flag = WOLFSSL_FATAL_ERROR;
  10164. }
  10165. }
  10166. wc_Sha512Free(&sha512);
  10167. res = TEST_RES_CHECK(flag == 0);
  10168. #endif
  10169. return res;
  10170. } /* END test_wc_Sha512FinalRaw */
  10171. /*
  10172. * Unit test function for wc_Sha512Free()
  10173. */
  10174. static int test_wc_Sha512Free(void)
  10175. {
  10176. int res = TEST_SKIPPED;
  10177. #ifdef WOLFSSL_SHA512
  10178. wc_Sha512Free(NULL);
  10179. res = TEST_RES_CHECK(1);
  10180. #endif
  10181. return res;
  10182. } /* END test_wc_Sha512Free */
  10183. #ifdef WOLFSSL_SHA512
  10184. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10185. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  10186. static int test_Sha512_Family_GetHash(int type )
  10187. {
  10188. int flag = 0;
  10189. int(*initFp)(wc_Sha512*);
  10190. int(*ghashFp)(wc_Sha512*, byte*);
  10191. wc_Sha512 sha512;
  10192. byte hash1[WC_SHA512_DIGEST_SIZE];
  10193. if (type == WC_HASH_TYPE_SHA512) {
  10194. initFp = wc_InitSha512;
  10195. ghashFp = wc_Sha512GetHash;
  10196. }
  10197. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10198. #if !defined(WOLFSSL_NOSHA512_224)
  10199. else if (type == WC_HASH_TYPE_SHA512_224) {
  10200. initFp = wc_InitSha512_224;
  10201. ghashFp = wc_Sha512_224GetHash;
  10202. }
  10203. #endif
  10204. #if !defined(WOLFSSL_NOSHA512_256)
  10205. else if (type == WC_HASH_TYPE_SHA512_256) {
  10206. initFp = wc_InitSha512_256;
  10207. ghashFp = wc_Sha512_256GetHash;
  10208. }
  10209. #endif
  10210. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10211. else {
  10212. initFp = NULL;
  10213. ghashFp = NULL;
  10214. }
  10215. if (initFp == NULL || ghashFp == NULL)
  10216. return TEST_FAIL;
  10217. if (!flag) {
  10218. flag = initFp(&sha512);
  10219. }
  10220. if (!flag) {
  10221. flag = ghashFp(&sha512, hash1);
  10222. }
  10223. /*test bad arguments*/
  10224. if (!flag) {
  10225. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  10226. flag = WOLFSSL_FATAL_ERROR;
  10227. }
  10228. if (!flag) {
  10229. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  10230. flag = WOLFSSL_FATAL_ERROR;
  10231. }
  10232. if (!flag) {
  10233. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  10234. flag = WOLFSSL_FATAL_ERROR;
  10235. }
  10236. wc_Sha512Free(&sha512);
  10237. return flag;
  10238. }
  10239. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10240. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10241. #endif /* WOLFSSL_SHA512 */
  10242. /*
  10243. * Unit test function for wc_Sha512GetHash()
  10244. */
  10245. static int test_wc_Sha512GetHash(void)
  10246. {
  10247. int res = TEST_SKIPPED;
  10248. #ifdef WOLFSSL_SHA512
  10249. wc_Sha512 sha512;
  10250. byte hash1[WC_SHA512_DIGEST_SIZE];
  10251. int flag = 0;
  10252. /* Initialize */
  10253. flag = wc_InitSha512(&sha512);
  10254. if (flag == 0) {
  10255. flag = wc_Sha512GetHash(&sha512, hash1);
  10256. }
  10257. /*test bad arguments*/
  10258. if (flag == 0) {
  10259. flag = wc_Sha512GetHash(NULL, NULL);
  10260. if (flag == BAD_FUNC_ARG) {
  10261. flag = 0;
  10262. }
  10263. }
  10264. if (flag == 0) {
  10265. flag = wc_Sha512GetHash(NULL, hash1);
  10266. if (flag == BAD_FUNC_ARG) {
  10267. flag = 0;
  10268. }
  10269. }
  10270. if (flag == 0) {
  10271. flag = wc_Sha512GetHash(&sha512, NULL);
  10272. if (flag == BAD_FUNC_ARG) {
  10273. flag = 0;
  10274. }
  10275. }
  10276. wc_Sha512Free(&sha512);
  10277. res = TEST_RES_CHECK(flag == 0);
  10278. #endif
  10279. return res;
  10280. } /* END test_wc_Sha512GetHash */
  10281. /*
  10282. * Unit test function for wc_Sha512Copy()
  10283. */
  10284. static int test_wc_Sha512Copy(void)
  10285. {
  10286. int res = TEST_SKIPPED;
  10287. #ifdef WOLFSSL_SHA512
  10288. wc_Sha512 sha512;
  10289. wc_Sha512 temp;
  10290. int flag;
  10291. /* Initialize */
  10292. flag = wc_InitSha512(&sha512);
  10293. if (flag == 0) {
  10294. flag = wc_InitSha512(&temp);
  10295. }
  10296. if (flag == 0) {
  10297. flag = wc_Sha512Copy(&sha512, &temp);
  10298. }
  10299. /*test bad arguments*/
  10300. if (flag == 0) {
  10301. flag = wc_Sha512Copy(NULL, NULL);
  10302. if (flag == BAD_FUNC_ARG) {
  10303. flag = 0;
  10304. }
  10305. }
  10306. if (flag == 0) {
  10307. flag = wc_Sha512Copy(NULL, &temp);
  10308. if (flag == BAD_FUNC_ARG) {
  10309. flag = 0;
  10310. }
  10311. }
  10312. if (flag == 0) {
  10313. flag = wc_Sha512Copy(&sha512, NULL);
  10314. if (flag == BAD_FUNC_ARG) {
  10315. flag = 0;
  10316. }
  10317. }
  10318. wc_Sha512Free(&sha512);
  10319. wc_Sha512Free(&temp);
  10320. res = TEST_RES_CHECK(flag == 0);
  10321. #endif
  10322. return res;
  10323. } /* END test_wc_Sha512Copy */
  10324. static int test_wc_InitSha512_224(void)
  10325. {
  10326. int res = TEST_SKIPPED;
  10327. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10328. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10329. wc_Sha512 sha512;
  10330. int ret;
  10331. int flag = 0;
  10332. /* Test good arg. */
  10333. ret = wc_InitSha512_224(&sha512);
  10334. if (ret != 0) {
  10335. flag = WOLFSSL_FATAL_ERROR;
  10336. }
  10337. /* Test bad arg. */
  10338. if (!flag) {
  10339. ret = wc_InitSha512_224(NULL);
  10340. if (ret != BAD_FUNC_ARG) {
  10341. flag = WOLFSSL_FATAL_ERROR;
  10342. }
  10343. }
  10344. wc_Sha512_224Free(&sha512);
  10345. res = TEST_RES_CHECK(flag == 0);
  10346. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10347. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10348. return res;
  10349. }
  10350. static int test_wc_Sha512_224Update(void)
  10351. {
  10352. int res = TEST_SKIPPED;
  10353. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10354. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10355. wc_Sha512 sha512;
  10356. byte hash[WC_SHA512_DIGEST_SIZE];
  10357. testVector a, c;
  10358. int ret;
  10359. int flag = 0;
  10360. ret = wc_InitSha512_224(&sha512);
  10361. if (ret != 0) {
  10362. flag = ret;
  10363. }
  10364. /* Input. */
  10365. if (!flag) {
  10366. a.input = "a";
  10367. a.inLen = XSTRLEN(a.input);
  10368. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  10369. if (ret != 0) {
  10370. flag = ret;
  10371. }
  10372. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  10373. if (ret != 0) {
  10374. flag = ret;
  10375. }
  10376. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10377. if (ret != 0) {
  10378. flag = ret;
  10379. }
  10380. ret = wc_Sha512_224Final(&sha512, hash);
  10381. if (ret != 0) {
  10382. flag = ret;
  10383. }
  10384. }
  10385. /* Update input. */
  10386. if (!flag) {
  10387. a.input = "abc";
  10388. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  10389. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  10390. a.inLen = XSTRLEN(a.input);
  10391. a.outLen = XSTRLEN(a.output);
  10392. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10393. if (ret != 0) {
  10394. flag = ret;
  10395. }
  10396. }
  10397. if (!flag) {
  10398. ret = wc_Sha512_224Final(&sha512, hash);
  10399. if (ret != 0) {
  10400. flag = ret;
  10401. }
  10402. }
  10403. if (!flag) {
  10404. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  10405. flag = WOLFSSL_FATAL_ERROR;
  10406. }
  10407. }
  10408. if (!flag) {
  10409. c.input = NULL;
  10410. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  10411. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10412. if (ret != BAD_FUNC_ARG) {
  10413. flag = WOLFSSL_FATAL_ERROR;
  10414. }
  10415. }
  10416. if (!flag) {
  10417. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  10418. if (ret != BAD_FUNC_ARG) {
  10419. flag = WOLFSSL_FATAL_ERROR;
  10420. }
  10421. }
  10422. wc_Sha512_224Free(&sha512);
  10423. res = TEST_RES_CHECK(flag == 0);
  10424. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10425. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10426. return res;
  10427. }
  10428. static int test_wc_Sha512_224Final(void)
  10429. {
  10430. int res = TEST_SKIPPED;
  10431. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10432. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10433. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  10434. res = TEST_RES_CHECK(ret == 0);
  10435. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10436. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10437. return res;
  10438. }
  10439. static int test_wc_Sha512_224GetFlags(void)
  10440. {
  10441. int res = TEST_SKIPPED;
  10442. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10443. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  10444. wc_Sha512 sha512, copy;
  10445. word32 flags = 0;
  10446. int flag = 0;
  10447. /* Initialize */
  10448. flag = wc_InitSha512_224(&sha512);
  10449. if (!flag) {
  10450. flag = wc_InitSha512_224(&copy);
  10451. }
  10452. if (!flag) {
  10453. flag = wc_Sha512_224Copy(&sha512, &copy);
  10454. }
  10455. if (!flag) {
  10456. flag = wc_Sha512_224GetFlags(&copy, &flags);
  10457. }
  10458. if (!flag) {
  10459. if (flags & WC_HASH_FLAG_ISCOPY)
  10460. flag = 0;
  10461. else
  10462. flag = WOLFSSL_FATAL_ERROR;
  10463. }
  10464. wc_Sha512_224Free(&copy);
  10465. wc_Sha512_224Free(&sha512);
  10466. res = TEST_RES_CHECK(flag == 0);
  10467. #endif
  10468. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10469. return res;
  10470. }
  10471. static int test_wc_Sha512_224FinalRaw(void)
  10472. {
  10473. int res = TEST_SKIPPED;
  10474. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10475. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  10476. !defined(WOLFSSL_NO_HASH_RAW)
  10477. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  10478. res = TEST_RES_CHECK(ret == 0);
  10479. #endif
  10480. return res;
  10481. }
  10482. static int test_wc_Sha512_224Free(void)
  10483. {
  10484. int res = TEST_SKIPPED;
  10485. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10486. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10487. wc_Sha512_224Free(NULL);
  10488. res = TEST_RES_CHECK(1);
  10489. #endif
  10490. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10491. return res;
  10492. }
  10493. static int test_wc_Sha512_224GetHash(void)
  10494. {
  10495. int res = TEST_SKIPPED;
  10496. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10497. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10498. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  10499. res = TEST_RES_CHECK(ret == 0);
  10500. #endif
  10501. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10502. return res;
  10503. }
  10504. static int test_wc_Sha512_224Copy(void)
  10505. {
  10506. int res = TEST_SKIPPED;
  10507. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10508. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10509. wc_Sha512 sha512;
  10510. wc_Sha512 temp;
  10511. int flag = 0;
  10512. /* Initialize */
  10513. flag = wc_InitSha512_224(&sha512);
  10514. if (flag == 0) {
  10515. flag = wc_InitSha512_224(&temp);
  10516. }
  10517. if (flag == 0) {
  10518. flag = wc_Sha512_224Copy(&sha512, &temp);
  10519. }
  10520. /*test bad arguments*/
  10521. if (flag == 0) {
  10522. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  10523. flag = WOLFSSL_FATAL_ERROR;
  10524. }
  10525. if (flag == 0) {
  10526. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  10527. flag = WOLFSSL_FATAL_ERROR;
  10528. }
  10529. if (flag == 0) {
  10530. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10531. flag = WOLFSSL_FATAL_ERROR;
  10532. }
  10533. wc_Sha512_224Free(&sha512);
  10534. wc_Sha512_224Free(&temp);
  10535. res = TEST_RES_CHECK(flag == 0);
  10536. #endif
  10537. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10538. return res;
  10539. }
  10540. static int test_wc_InitSha512_256(void)
  10541. {
  10542. int res = TEST_SKIPPED;
  10543. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10544. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10545. wc_Sha512 sha512;
  10546. int ret;
  10547. int flag = 0;
  10548. /* Test good arg. */
  10549. ret = wc_InitSha512_256(&sha512);
  10550. if (ret != 0) {
  10551. flag = WOLFSSL_FATAL_ERROR;
  10552. }
  10553. /* Test bad arg. */
  10554. if (!flag) {
  10555. ret = wc_InitSha512_256(NULL);
  10556. if (ret != BAD_FUNC_ARG) {
  10557. flag = WOLFSSL_FATAL_ERROR;
  10558. }
  10559. }
  10560. wc_Sha512_256Free(&sha512);
  10561. res = TEST_RES_CHECK(flag == 0);
  10562. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10563. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10564. return res;
  10565. }
  10566. static int test_wc_Sha512_256Update(void)
  10567. {
  10568. int res = TEST_SKIPPED;
  10569. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10570. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10571. wc_Sha512 sha512;
  10572. byte hash[WC_SHA512_DIGEST_SIZE];
  10573. testVector a, c;
  10574. int ret;
  10575. int flag = 0;
  10576. ret = wc_InitSha512_256(&sha512);
  10577. if (ret != 0) {
  10578. flag = ret;
  10579. }
  10580. /* Input. */
  10581. if (!flag) {
  10582. a.input = "a";
  10583. a.inLen = XSTRLEN(a.input);
  10584. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  10585. if (ret != 0) {
  10586. flag = ret;
  10587. }
  10588. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  10589. if (ret != 0) {
  10590. flag = ret;
  10591. }
  10592. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10593. if (ret != 0) {
  10594. flag = ret;
  10595. }
  10596. ret = wc_Sha512_256Final(&sha512, hash);
  10597. if (ret != 0) {
  10598. flag = ret;
  10599. }
  10600. }
  10601. /* Update input. */
  10602. if (!flag) {
  10603. a.input = "abc";
  10604. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  10605. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  10606. "\x07\xe7\xaf\x23";
  10607. a.inLen = XSTRLEN(a.input);
  10608. a.outLen = XSTRLEN(a.output);
  10609. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10610. if (ret != 0) {
  10611. flag = ret;
  10612. }
  10613. }
  10614. if (!flag) {
  10615. ret = wc_Sha512_256Final(&sha512, hash);
  10616. if (ret != 0) {
  10617. flag = ret;
  10618. }
  10619. }
  10620. if (!flag) {
  10621. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  10622. flag = WOLFSSL_FATAL_ERROR;
  10623. }
  10624. }
  10625. if (!flag) {
  10626. c.input = NULL;
  10627. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  10628. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10629. if (ret != BAD_FUNC_ARG) {
  10630. flag = WOLFSSL_FATAL_ERROR;
  10631. }
  10632. }
  10633. if (!flag) {
  10634. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  10635. if (ret != BAD_FUNC_ARG) {
  10636. flag = WOLFSSL_FATAL_ERROR;
  10637. }
  10638. }
  10639. wc_Sha512_256Free(&sha512);
  10640. res = TEST_RES_CHECK(flag == 0);
  10641. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10642. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10643. return res;
  10644. }
  10645. static int test_wc_Sha512_256Final(void)
  10646. {
  10647. int res = TEST_SKIPPED;
  10648. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10649. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10650. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  10651. res = TEST_RES_CHECK(ret == 0);
  10652. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10653. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10654. return res;
  10655. }
  10656. static int test_wc_Sha512_256GetFlags(void)
  10657. {
  10658. int res = TEST_SKIPPED;
  10659. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10660. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  10661. wc_Sha512 sha512, copy;
  10662. word32 flags = 0;
  10663. int flag = 0;
  10664. /* Initialize */
  10665. flag = wc_InitSha512_256(&sha512);
  10666. if (!flag ) {
  10667. flag = wc_InitSha512_256(&copy);
  10668. }
  10669. if (!flag ) {
  10670. flag = wc_Sha512_256Copy(&sha512, &copy);
  10671. }
  10672. if (!flag ) {
  10673. flag = wc_Sha512_256GetFlags(&copy, &flags);
  10674. }
  10675. if (!flag) {
  10676. if (flags & WC_HASH_FLAG_ISCOPY)
  10677. flag = 0;
  10678. else
  10679. flag = WOLFSSL_FATAL_ERROR;
  10680. }
  10681. wc_Sha512_256Free(&sha512);
  10682. res = TEST_RES_CHECK(flag == 0);
  10683. #endif
  10684. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10685. return res;
  10686. }
  10687. static int test_wc_Sha512_256FinalRaw(void)
  10688. {
  10689. int res = TEST_SKIPPED;
  10690. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10691. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  10692. !defined(WOLFSSL_NO_HASH_RAW)
  10693. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  10694. res = TEST_RES_CHECK(ret == 0);
  10695. #endif
  10696. return res;
  10697. }
  10698. static int test_wc_Sha512_256Free(void)
  10699. {
  10700. int res = TEST_SKIPPED;
  10701. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10702. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10703. wc_Sha512_256Free(NULL);
  10704. res = TEST_RES_CHECK(1);
  10705. #endif
  10706. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10707. return res;
  10708. }
  10709. static int test_wc_Sha512_256GetHash(void)
  10710. {
  10711. int res = TEST_SKIPPED;
  10712. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10713. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10714. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  10715. res = TEST_RES_CHECK(ret == 0);
  10716. #endif
  10717. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10718. return res;
  10719. }
  10720. static int test_wc_Sha512_256Copy(void)
  10721. {
  10722. int res = TEST_SKIPPED;
  10723. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10724. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10725. wc_Sha512 sha512;
  10726. wc_Sha512 temp;
  10727. int flag = 0;
  10728. /* Initialize */
  10729. flag = wc_InitSha512_256(&sha512);
  10730. if (flag == 0) {
  10731. flag = wc_InitSha512_256(&temp);
  10732. }
  10733. if (flag == 0) {
  10734. flag = wc_Sha512_256Copy(&sha512, &temp);
  10735. }
  10736. /*test bad arguments*/
  10737. if (flag == 0) {
  10738. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  10739. flag = WOLFSSL_FATAL_ERROR;
  10740. }
  10741. if (flag == 0) {
  10742. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  10743. flag = WOLFSSL_FATAL_ERROR;
  10744. }
  10745. if (flag == 0) {
  10746. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10747. flag = WOLFSSL_FATAL_ERROR;
  10748. }
  10749. wc_Sha512_256Free(&sha512);
  10750. wc_Sha512_256Free(&temp);
  10751. res = TEST_RES_CHECK(flag == 0);
  10752. #endif
  10753. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10754. return res;
  10755. }
  10756. /*
  10757. * Testing wc_InitSha384()
  10758. */
  10759. static int test_wc_InitSha384(void)
  10760. {
  10761. int res = TEST_SKIPPED;
  10762. #ifdef WOLFSSL_SHA384
  10763. wc_Sha384 sha384;
  10764. int ret;
  10765. int flag = 0;
  10766. /* Test good arg. */
  10767. ret = wc_InitSha384(&sha384);
  10768. if (ret != 0) {
  10769. flag = WOLFSSL_FATAL_ERROR;
  10770. }
  10771. /* Test bad arg. */
  10772. if (!flag) {
  10773. ret = wc_InitSha384(NULL);
  10774. if (ret != BAD_FUNC_ARG) {
  10775. flag = WOLFSSL_FATAL_ERROR;
  10776. }
  10777. }
  10778. wc_Sha384Free(&sha384);
  10779. res = TEST_RES_CHECK(flag == 0);
  10780. #endif
  10781. return res;
  10782. } /* END test_wc_InitSha384 */
  10783. /*
  10784. * test wc_Sha384Update()
  10785. */
  10786. static int test_wc_Sha384Update(void)
  10787. {
  10788. int res = TEST_SKIPPED;
  10789. #ifdef WOLFSSL_SHA384
  10790. wc_Sha384 sha384;
  10791. byte hash[WC_SHA384_DIGEST_SIZE];
  10792. testVector a, b, c;
  10793. int ret;
  10794. int flag = 0;
  10795. ret = wc_InitSha384(&sha384);
  10796. if (ret != 0) {
  10797. flag = ret;
  10798. }
  10799. /* Input */
  10800. if (!flag) {
  10801. a.input = "a";
  10802. a.inLen = XSTRLEN(a.input);
  10803. ret = wc_Sha384Update(&sha384, NULL, 0);
  10804. if (ret != 0) {
  10805. flag = ret;
  10806. }
  10807. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  10808. if (ret != 0) {
  10809. flag = ret;
  10810. }
  10811. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10812. if (ret != 0) {
  10813. flag = ret;
  10814. }
  10815. }
  10816. if (!flag) {
  10817. ret = wc_Sha384Final(&sha384, hash);
  10818. if (ret != 0) {
  10819. flag = ret;
  10820. }
  10821. }
  10822. /* Update input. */
  10823. if (!flag) {
  10824. a.input = "abc";
  10825. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  10826. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  10827. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  10828. "\xc8\x25\xa7";
  10829. a.inLen = XSTRLEN(a.input);
  10830. a.outLen = XSTRLEN(a.output);
  10831. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10832. if (ret != 0) {
  10833. flag = ret;
  10834. }
  10835. }
  10836. if (!flag) {
  10837. ret = wc_Sha384Final(&sha384, hash);
  10838. if (ret != 0) {
  10839. flag = ret;
  10840. }
  10841. }
  10842. if (!flag) {
  10843. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  10844. flag = WOLFSSL_FATAL_ERROR;
  10845. }
  10846. }
  10847. /* Pass in bad values. */
  10848. if (!flag) {
  10849. b.input = NULL;
  10850. b.inLen = 0;
  10851. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  10852. if (ret != 0) {
  10853. flag = ret;
  10854. }
  10855. }
  10856. if (!flag) {
  10857. c.input = NULL;
  10858. c.inLen = WC_SHA384_DIGEST_SIZE;
  10859. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  10860. if (ret != BAD_FUNC_ARG) {
  10861. flag = WOLFSSL_FATAL_ERROR;
  10862. }
  10863. }
  10864. if (!flag) {
  10865. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  10866. if (ret != BAD_FUNC_ARG) {
  10867. flag = WOLFSSL_FATAL_ERROR;
  10868. }
  10869. }
  10870. wc_Sha384Free(&sha384);
  10871. res = TEST_RES_CHECK(flag == 0);
  10872. #endif
  10873. return res;
  10874. } /* END test_wc_Sha384Update */
  10875. /*
  10876. * Unit test function for wc_Sha384Final();
  10877. */
  10878. static int test_wc_Sha384Final(void)
  10879. {
  10880. int res = TEST_SKIPPED;
  10881. #ifdef WOLFSSL_SHA384
  10882. wc_Sha384 sha384;
  10883. byte* hash_test[3];
  10884. byte hash1[WC_SHA384_DIGEST_SIZE];
  10885. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10886. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10887. int times, i, ret;
  10888. int flag = 0;
  10889. /* Initialize */
  10890. ret = wc_InitSha384(&sha384);
  10891. if (ret) {
  10892. flag = ret;
  10893. }
  10894. if (!flag) {
  10895. hash_test[0] = hash1;
  10896. hash_test[1] = hash2;
  10897. hash_test[2] = hash3;
  10898. }
  10899. times = sizeof(hash_test) / sizeof(byte*);
  10900. /* Good test args. */
  10901. for (i = 0; i < times; i++) {
  10902. if (!flag) {
  10903. ret = wc_Sha384Final(&sha384, hash_test[i]);
  10904. if (ret != 0) {
  10905. flag = WOLFSSL_FATAL_ERROR;
  10906. }
  10907. }
  10908. }
  10909. /* Test bad args. */
  10910. if (!flag) {
  10911. ret = wc_Sha384Final(NULL, NULL);
  10912. if (ret != BAD_FUNC_ARG) {
  10913. flag = WOLFSSL_FATAL_ERROR;
  10914. }
  10915. }
  10916. if (!flag) {
  10917. ret = wc_Sha384Final(NULL, hash1);
  10918. if (ret != BAD_FUNC_ARG) {
  10919. flag = WOLFSSL_FATAL_ERROR;
  10920. }
  10921. }
  10922. if (!flag) {
  10923. ret = wc_Sha384Final(&sha384, NULL);
  10924. if (ret != BAD_FUNC_ARG) {
  10925. flag = WOLFSSL_FATAL_ERROR;
  10926. }
  10927. }
  10928. wc_Sha384Free(&sha384);
  10929. res = TEST_RES_CHECK(flag == 0);
  10930. #endif
  10931. return res;
  10932. } /* END test_wc_Sha384Final */
  10933. /*
  10934. * Unit test function for wc_Sha384GetFlags()
  10935. */
  10936. static int test_wc_Sha384GetFlags(void)
  10937. {
  10938. int res = TEST_SKIPPED;
  10939. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  10940. wc_Sha384 sha384;
  10941. word32 flags = 0;
  10942. int flag = 0;
  10943. /* Initialize */
  10944. flag = wc_InitSha384(&sha384);
  10945. if (flag == 0) {
  10946. flag = wc_Sha384GetFlags(&sha384, &flags);
  10947. }
  10948. if (flag == 0) {
  10949. if (flags & WC_HASH_FLAG_ISCOPY) {
  10950. flag = 0;
  10951. }
  10952. }
  10953. wc_Sha384Free(&sha384);
  10954. res = TEST_RES_CHECK(flag == 0);
  10955. #endif
  10956. return res;
  10957. } /* END test_wc_Sha384GetFlags */
  10958. /*
  10959. * Unit test function for wc_Sha384FinalRaw()
  10960. */
  10961. static int test_wc_Sha384FinalRaw(void)
  10962. {
  10963. int res = TEST_SKIPPED;
  10964. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10965. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10966. !defined(WOLFSSL_NO_HASH_RAW)
  10967. wc_Sha384 sha384;
  10968. byte* hash_test[3];
  10969. byte hash1[WC_SHA384_DIGEST_SIZE];
  10970. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10971. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10972. int times, i, ret;
  10973. int flag = 0;
  10974. /* Initialize */
  10975. ret = wc_InitSha384(&sha384);
  10976. if (ret != 0) {
  10977. flag = ret;
  10978. }
  10979. if (!flag) {
  10980. hash_test[0] = hash1;
  10981. hash_test[1] = hash2;
  10982. hash_test[2] = hash3;
  10983. }
  10984. times = sizeof(hash_test) / sizeof(byte*);
  10985. /* Good test args. */
  10986. for (i = 0; i < times; i++) {
  10987. if (!flag) {
  10988. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  10989. if (ret != 0) {
  10990. flag = WOLFSSL_FATAL_ERROR;
  10991. }
  10992. }
  10993. }
  10994. /* Test bad args. */
  10995. if (!flag ) {
  10996. ret = wc_Sha384FinalRaw(NULL, NULL);
  10997. if (ret != BAD_FUNC_ARG) {
  10998. flag = WOLFSSL_FATAL_ERROR;
  10999. }
  11000. }
  11001. if (!flag) {
  11002. ret = wc_Sha384FinalRaw(NULL, hash1);
  11003. if (ret != BAD_FUNC_ARG) {
  11004. flag = WOLFSSL_FATAL_ERROR;
  11005. }
  11006. }
  11007. if (!flag) {
  11008. ret = wc_Sha384FinalRaw(&sha384, NULL);
  11009. if (ret != BAD_FUNC_ARG) {
  11010. flag = WOLFSSL_FATAL_ERROR;
  11011. }
  11012. }
  11013. wc_Sha384Free(&sha384);
  11014. res = TEST_RES_CHECK(flag == 0);
  11015. #endif
  11016. return res;
  11017. } /* END test_wc_Sha384FinalRaw */
  11018. /*
  11019. * Unit test function for wc_Sha384Free()
  11020. */
  11021. static int test_wc_Sha384Free(void)
  11022. {
  11023. int res = TEST_SKIPPED;
  11024. #ifdef WOLFSSL_SHA384
  11025. wc_Sha384Free(NULL);
  11026. res = TEST_RES_CHECK(1);
  11027. #endif
  11028. return res;
  11029. } /* END test_wc_Sha384Free */
  11030. /*
  11031. * Unit test function for wc_Sha384GetHash()
  11032. */
  11033. static int test_wc_Sha384GetHash(void)
  11034. {
  11035. int res = TEST_SKIPPED;
  11036. #ifdef WOLFSSL_SHA384
  11037. wc_Sha384 sha384;
  11038. byte hash1[WC_SHA384_DIGEST_SIZE];
  11039. int flag = 0;
  11040. /* Initialize */
  11041. flag = wc_InitSha384(&sha384);
  11042. if (flag == 0) {
  11043. flag = wc_Sha384GetHash(&sha384, hash1);
  11044. }
  11045. /*test bad arguments*/
  11046. if (flag == 0) {
  11047. flag = wc_Sha384GetHash(NULL, NULL);
  11048. if (flag == BAD_FUNC_ARG) {
  11049. flag = 0;
  11050. }
  11051. }
  11052. if (flag == 0) {
  11053. flag = wc_Sha384GetHash(NULL, hash1);
  11054. if (flag == BAD_FUNC_ARG) {
  11055. flag = 0;
  11056. }
  11057. }
  11058. if (flag == 0) {
  11059. flag = wc_Sha384GetHash(&sha384, NULL);
  11060. if (flag == BAD_FUNC_ARG) {
  11061. flag = 0;
  11062. }
  11063. }
  11064. wc_Sha384Free(&sha384);
  11065. res = TEST_RES_CHECK(flag == 0);
  11066. #endif
  11067. return res;
  11068. } /* END test_wc_Sha384GetHash */
  11069. /*
  11070. * Unit test function for wc_Sha384Copy()
  11071. */
  11072. static int test_wc_Sha384Copy(void)
  11073. {
  11074. int res = TEST_SKIPPED;
  11075. #ifdef WOLFSSL_SHA384
  11076. wc_Sha384 sha384;
  11077. wc_Sha384 temp;
  11078. int flag = 0;
  11079. /* Initialize */
  11080. flag = wc_InitSha384(&sha384);
  11081. if (flag == 0) {
  11082. flag = wc_InitSha384(&temp);
  11083. }
  11084. if (flag == 0) {
  11085. flag = wc_Sha384Copy(&sha384, &temp);
  11086. }
  11087. /*test bad arguments*/
  11088. if (flag == 0) {
  11089. flag = wc_Sha384Copy(NULL, NULL);
  11090. if (flag == BAD_FUNC_ARG) {
  11091. flag = 0;
  11092. }
  11093. }
  11094. if (flag == 0) {
  11095. flag = wc_Sha384Copy(NULL, &temp);
  11096. if (flag == BAD_FUNC_ARG) {
  11097. flag = 0;
  11098. }
  11099. }
  11100. if (flag == 0) {
  11101. flag = wc_Sha384Copy(&sha384, NULL);
  11102. if (flag == BAD_FUNC_ARG) {
  11103. flag = 0;
  11104. }
  11105. }
  11106. wc_Sha384Free(&sha384);
  11107. wc_Sha384Free(&temp);
  11108. res = TEST_RES_CHECK(flag == 0);
  11109. #endif
  11110. return res;
  11111. } /* END test_wc_Sha384Copy */
  11112. /*
  11113. * Testing wc_InitSha224();
  11114. */
  11115. static int test_wc_InitSha224(void)
  11116. {
  11117. int res = TEST_SKIPPED;
  11118. #ifdef WOLFSSL_SHA224
  11119. wc_Sha224 sha224;
  11120. int ret;
  11121. int flag = 0;
  11122. /* Test good arg. */
  11123. ret = wc_InitSha224(&sha224);
  11124. if (ret != 0) {
  11125. flag = WOLFSSL_FATAL_ERROR;
  11126. }
  11127. /* Test bad arg. */
  11128. if (!flag) {
  11129. ret = wc_InitSha224(NULL);
  11130. if (ret != BAD_FUNC_ARG) {
  11131. flag = WOLFSSL_FATAL_ERROR;
  11132. }
  11133. }
  11134. wc_Sha224Free(&sha224);
  11135. res = TEST_RES_CHECK(flag == 0);
  11136. #endif
  11137. return res;
  11138. } /* END test_wc_InitSha224 */
  11139. /*
  11140. * Unit test on wc_Sha224Update
  11141. */
  11142. static int test_wc_Sha224Update(void)
  11143. {
  11144. int res = TEST_SKIPPED;
  11145. #ifdef WOLFSSL_SHA224
  11146. wc_Sha224 sha224;
  11147. byte hash[WC_SHA224_DIGEST_SIZE];
  11148. testVector a, b, c;
  11149. int ret;
  11150. int flag = 0;
  11151. ret = wc_InitSha224(&sha224);
  11152. if (ret != 0) {
  11153. flag = ret;
  11154. }
  11155. /* Input. */
  11156. if (!flag) {
  11157. a.input = "a";
  11158. a.inLen = XSTRLEN(a.input);
  11159. ret = wc_Sha224Update(&sha224, NULL, 0);
  11160. if (ret != 0) {
  11161. flag = ret;
  11162. }
  11163. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  11164. if (ret != 0) {
  11165. flag = ret;
  11166. }
  11167. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11168. if (ret != 0) {
  11169. flag = ret;
  11170. }
  11171. }
  11172. if (!flag) {
  11173. ret = wc_Sha224Final(&sha224, hash);
  11174. if (ret != 0) {
  11175. flag = ret;
  11176. }
  11177. }
  11178. /* Update input. */
  11179. if (!flag) {
  11180. a.input = "abc";
  11181. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11182. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11183. a.inLen = XSTRLEN(a.input);
  11184. a.outLen = XSTRLEN(a.output);
  11185. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11186. if (ret != 0) {
  11187. flag = ret;
  11188. }
  11189. }
  11190. if (!flag) {
  11191. ret = wc_Sha224Final(&sha224, hash);
  11192. if (ret != 0) {
  11193. flag = ret;
  11194. }
  11195. }
  11196. if (!flag) {
  11197. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  11198. flag = WOLFSSL_FATAL_ERROR;
  11199. }
  11200. }
  11201. /* Pass in bad values. */
  11202. if (!flag) {
  11203. b.input = NULL;
  11204. b.inLen = 0;
  11205. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  11206. if (ret != 0) {
  11207. flag = ret;
  11208. }
  11209. }
  11210. if (!flag) {
  11211. c.input = NULL;
  11212. c.inLen = WC_SHA224_DIGEST_SIZE;
  11213. ret = wc_Sha224Update(&sha224, (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_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11220. if (ret != BAD_FUNC_ARG) {
  11221. flag = WOLFSSL_FATAL_ERROR;
  11222. }
  11223. }
  11224. wc_Sha224Free(&sha224);
  11225. res = TEST_RES_CHECK(flag == 0);
  11226. #endif
  11227. return res;
  11228. } /* END test_wc_Sha224Update */
  11229. /*
  11230. * Unit test for wc_Sha224Final();
  11231. */
  11232. static int test_wc_Sha224Final(void)
  11233. {
  11234. int res = TEST_SKIPPED;
  11235. #ifdef WOLFSSL_SHA224
  11236. wc_Sha224 sha224;
  11237. byte* hash_test[3];
  11238. byte hash1[WC_SHA224_DIGEST_SIZE];
  11239. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11240. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11241. int times, i, ret;
  11242. int flag = 0;
  11243. /* Initialize */
  11244. ret = wc_InitSha224(&sha224);
  11245. if (ret) {
  11246. flag = ret;
  11247. }
  11248. if (!flag) {
  11249. hash_test[0] = hash1;
  11250. hash_test[1] = hash2;
  11251. hash_test[2] = hash3;
  11252. }
  11253. times = sizeof(hash_test) / sizeof(byte*);
  11254. /* Good test args. */
  11255. /* Testing oversized buffers. */
  11256. for (i = 0; i < times; i++) {
  11257. if (!flag) {
  11258. ret = wc_Sha224Final(&sha224, hash_test[i]);
  11259. if (ret != 0) {
  11260. flag = WOLFSSL_FATAL_ERROR;
  11261. }
  11262. }
  11263. }
  11264. /* Test bad args. */
  11265. if (!flag) {
  11266. ret = wc_Sha224Final(NULL, NULL);
  11267. if (ret != BAD_FUNC_ARG) {
  11268. flag = WOLFSSL_FATAL_ERROR;
  11269. }
  11270. }
  11271. if (!flag) {
  11272. ret = wc_Sha224Final(NULL, hash1);
  11273. if (ret != BAD_FUNC_ARG) {
  11274. flag = WOLFSSL_FATAL_ERROR;
  11275. }
  11276. }
  11277. if (!flag) {
  11278. ret = wc_Sha224Final(&sha224, NULL);
  11279. if (ret != BAD_FUNC_ARG) {
  11280. flag = WOLFSSL_FATAL_ERROR;
  11281. }
  11282. }
  11283. wc_Sha224Free(&sha224);
  11284. res = TEST_RES_CHECK(flag == 0);
  11285. #endif
  11286. return res;
  11287. } /* END test_wc_Sha224Final */
  11288. /*
  11289. * Unit test function for wc_Sha224SetFlags()
  11290. */
  11291. static int test_wc_Sha224SetFlags(void)
  11292. {
  11293. int res = TEST_SKIPPED;
  11294. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11295. wc_Sha224 sha224;
  11296. word32 flags = 0;
  11297. int flag = 0;
  11298. /* Initialize */
  11299. flag = wc_InitSha224(&sha224);
  11300. if (flag == 0) {
  11301. flag = wc_Sha224SetFlags(&sha224, flags);
  11302. }
  11303. if (flag == 0) {
  11304. if (flags & WC_HASH_FLAG_ISCOPY) {
  11305. flag = 0;
  11306. }
  11307. }
  11308. wc_Sha224Free(&sha224);
  11309. res = TEST_RES_CHECK(flag == 0);
  11310. #endif
  11311. return res;
  11312. } /* END test_wc_Sha224SetFlags */
  11313. /*
  11314. * Unit test function for wc_Sha224GetFlags()
  11315. */
  11316. static int test_wc_Sha224GetFlags(void)
  11317. {
  11318. int res = TEST_SKIPPED;
  11319. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11320. wc_Sha224 sha224;
  11321. word32 flags = 0;
  11322. int flag = 0;
  11323. /* Initialize */
  11324. flag = wc_InitSha224(&sha224);
  11325. if (flag == 0) {
  11326. flag = wc_Sha224GetFlags(&sha224, &flags);
  11327. }
  11328. if (flag == 0) {
  11329. if (flags & WC_HASH_FLAG_ISCOPY) {
  11330. flag = 0;
  11331. }
  11332. }
  11333. wc_Sha224Free(&sha224);
  11334. res = TEST_RES_CHECK(flag == 0);
  11335. #endif
  11336. return res;
  11337. } /* END test_wc_Sha224GetFlags */
  11338. /*
  11339. * Unit test function for wc_Sha224Free()
  11340. */
  11341. static int test_wc_Sha224Free(void)
  11342. {
  11343. int res = TEST_SKIPPED;
  11344. #ifdef WOLFSSL_SHA224
  11345. wc_Sha224Free(NULL);
  11346. res = TEST_RES_CHECK(1);
  11347. #endif
  11348. return res;
  11349. } /* END test_wc_Sha224Free */
  11350. /*
  11351. * Unit test function for wc_Sha224GetHash()
  11352. */
  11353. static int test_wc_Sha224GetHash(void)
  11354. {
  11355. int res = TEST_SKIPPED;
  11356. #ifdef WOLFSSL_SHA224
  11357. wc_Sha224 sha224;
  11358. byte hash1[WC_SHA224_DIGEST_SIZE];
  11359. int flag = 0;
  11360. /* Initialize */
  11361. flag = wc_InitSha224(&sha224);
  11362. if (flag == 0) {
  11363. flag = wc_Sha224GetHash(&sha224, hash1);
  11364. }
  11365. /*test bad arguments*/
  11366. if (flag == 0) {
  11367. flag = wc_Sha224GetHash(NULL, NULL);
  11368. if (flag == BAD_FUNC_ARG) {
  11369. flag = 0;
  11370. }
  11371. }
  11372. if (flag == 0) {
  11373. flag = wc_Sha224GetHash(NULL, hash1);
  11374. if (flag == BAD_FUNC_ARG) {
  11375. flag = 0;
  11376. }
  11377. }
  11378. if (flag == 0) {
  11379. flag = wc_Sha224GetHash(&sha224, NULL);
  11380. if (flag == BAD_FUNC_ARG) {
  11381. flag = 0;
  11382. }
  11383. }
  11384. wc_Sha224Free(&sha224);
  11385. res = TEST_RES_CHECK(flag == 0);
  11386. #endif
  11387. return res;
  11388. } /* END test_wc_Sha224GetHash */
  11389. /*
  11390. * Unit test function for wc_Sha224Copy()
  11391. */
  11392. static int test_wc_Sha224Copy(void)
  11393. {
  11394. int res = TEST_SKIPPED;
  11395. #ifdef WOLFSSL_SHA224
  11396. wc_Sha224 sha224;
  11397. wc_Sha224 temp;
  11398. int flag = 0;
  11399. /* Initialize */
  11400. flag = wc_InitSha224(&sha224);
  11401. if (flag == 0) {
  11402. flag = wc_InitSha224(&temp);
  11403. }
  11404. if (flag == 0) {
  11405. flag = wc_Sha224Copy(&sha224, &temp);
  11406. }
  11407. /*test bad arguments*/
  11408. if (flag == 0) {
  11409. flag = wc_Sha224Copy(NULL, NULL);
  11410. if (flag == BAD_FUNC_ARG) {
  11411. flag = 0;
  11412. }
  11413. }
  11414. if (flag == 0) {
  11415. flag = wc_Sha224Copy(NULL, &temp);
  11416. if (flag == BAD_FUNC_ARG) {
  11417. flag = 0;
  11418. }
  11419. }
  11420. if (flag == 0) {
  11421. flag = wc_Sha224Copy(&sha224, NULL);
  11422. if (flag == BAD_FUNC_ARG) {
  11423. flag = 0;
  11424. }
  11425. }
  11426. wc_Sha224Free(&sha224);
  11427. wc_Sha224Free(&temp);
  11428. res = TEST_RES_CHECK(flag == 0);
  11429. #endif
  11430. return res;
  11431. } /* END test_wc_Sha224Copy */
  11432. /*
  11433. * Testing wc_InitRipeMd()
  11434. */
  11435. static int test_wc_InitRipeMd(void)
  11436. {
  11437. int res = TEST_SKIPPED;
  11438. #ifdef WOLFSSL_RIPEMD
  11439. RipeMd ripemd;
  11440. int ret;
  11441. int flag = 0;
  11442. /* Test good arg. */
  11443. ret = wc_InitRipeMd(&ripemd);
  11444. if (ret != 0) {
  11445. flag = WOLFSSL_FATAL_ERROR;
  11446. }
  11447. /* Test bad arg. */
  11448. if (!flag) {
  11449. ret = wc_InitRipeMd(NULL);
  11450. if (ret != BAD_FUNC_ARG) {
  11451. flag = WOLFSSL_FATAL_ERROR;
  11452. }
  11453. }
  11454. res = TEST_RES_CHECK(flag == 0);
  11455. #endif
  11456. return res;
  11457. } /* END test_wc_InitRipeMd */
  11458. /*
  11459. * Testing wc_RipeMdUpdate()
  11460. */
  11461. static int test_wc_RipeMdUpdate(void)
  11462. {
  11463. int res = TEST_SKIPPED;
  11464. #ifdef WOLFSSL_RIPEMD
  11465. RipeMd ripemd;
  11466. byte hash[RIPEMD_DIGEST_SIZE];
  11467. testVector a, b, c;
  11468. int ret;
  11469. int flag = 0;
  11470. ret = wc_InitRipeMd(&ripemd);
  11471. if (ret != 0) {
  11472. flag = ret;
  11473. }
  11474. /* Input */
  11475. if (!flag) {
  11476. a.input = "a";
  11477. a.inLen = XSTRLEN(a.input);
  11478. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  11479. if (ret != 0) {
  11480. flag = ret;
  11481. }
  11482. }
  11483. if (!flag) {
  11484. ret = wc_RipeMdFinal(&ripemd, hash);
  11485. if (ret != 0) {
  11486. flag = ret;
  11487. }
  11488. }
  11489. /* Update input. */
  11490. if (!flag) {
  11491. a.input = "abc";
  11492. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  11493. "\xb0\x87\xf1\x5a\x0b\xfc";
  11494. a.inLen = XSTRLEN(a.input);
  11495. a.outLen = XSTRLEN(a.output);
  11496. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  11497. if (ret != 0) {
  11498. flag = ret;
  11499. }
  11500. }
  11501. if (!flag) {
  11502. ret = wc_RipeMdFinal(&ripemd, hash);
  11503. if (ret != 0) {
  11504. flag = ret;
  11505. }
  11506. }
  11507. if (!flag) {
  11508. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  11509. flag = WOLFSSL_FATAL_ERROR;
  11510. }
  11511. }
  11512. /* Pass in bad values. */
  11513. if (!flag) {
  11514. b.input = NULL;
  11515. b.inLen = 0;
  11516. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  11517. if (ret != 0) {
  11518. flag = ret;
  11519. }
  11520. }
  11521. if (!flag) {
  11522. c.input = NULL;
  11523. c.inLen = RIPEMD_DIGEST_SIZE;
  11524. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  11525. if (ret != BAD_FUNC_ARG) {
  11526. flag = WOLFSSL_FATAL_ERROR;
  11527. }
  11528. }
  11529. if (!flag) {
  11530. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  11531. if (ret != BAD_FUNC_ARG) {
  11532. flag = WOLFSSL_FATAL_ERROR;
  11533. }
  11534. }
  11535. res = TEST_RES_CHECK(flag == 0);
  11536. #endif
  11537. return res;
  11538. } /* END test_wc_RipeMdUdpate */
  11539. /*
  11540. * Unit test function for wc_RipeMdFinal()
  11541. */
  11542. static int test_wc_RipeMdFinal(void)
  11543. {
  11544. int res = TEST_SKIPPED;
  11545. #ifdef WOLFSSL_RIPEMD
  11546. RipeMd ripemd;
  11547. byte* hash_test[3];
  11548. byte hash1[RIPEMD_DIGEST_SIZE];
  11549. byte hash2[2*RIPEMD_DIGEST_SIZE];
  11550. byte hash3[5*RIPEMD_DIGEST_SIZE];
  11551. int times, i, ret;
  11552. int flag = 0;
  11553. /* Initialize */
  11554. ret = wc_InitRipeMd(&ripemd);
  11555. if (ret != 0) {
  11556. flag = ret;
  11557. }
  11558. if (!flag) {
  11559. hash_test[0] = hash1;
  11560. hash_test[1] = hash2;
  11561. hash_test[2] = hash3;
  11562. }
  11563. times = sizeof(hash_test) / sizeof(byte*);
  11564. /* Testing oversized buffers. */
  11565. for (i = 0; i < times; i++) {
  11566. if (!flag) {
  11567. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  11568. if (ret != 0) {
  11569. flag = WOLFSSL_FATAL_ERROR;
  11570. }
  11571. }
  11572. }
  11573. /* Test bad args. */
  11574. if (!flag) {
  11575. ret = wc_RipeMdFinal(NULL, NULL);
  11576. if (ret != BAD_FUNC_ARG) {
  11577. flag = WOLFSSL_FATAL_ERROR;
  11578. }
  11579. }
  11580. if (!flag) {
  11581. ret = wc_RipeMdFinal(NULL, hash1);
  11582. if (ret != BAD_FUNC_ARG) {
  11583. flag = WOLFSSL_FATAL_ERROR;
  11584. }
  11585. }
  11586. if (!flag) {
  11587. ret = wc_RipeMdFinal(&ripemd, NULL);
  11588. if (ret != BAD_FUNC_ARG) {
  11589. flag = WOLFSSL_FATAL_ERROR;
  11590. }
  11591. }
  11592. res = TEST_RES_CHECK(flag == 0);
  11593. #endif
  11594. return res;
  11595. } /* END test_wc_RipeMdFinal */
  11596. /*
  11597. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  11598. * wc_InitSha3_512
  11599. */
  11600. static int test_wc_InitSha3(void)
  11601. {
  11602. int res = TEST_SKIPPED;
  11603. #if defined(WOLFSSL_SHA3)
  11604. wc_Sha3 sha3;
  11605. int ret = 0;
  11606. (void)sha3;
  11607. #if !defined(WOLFSSL_NOSHA3_224)
  11608. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11609. /* Test bad args. */
  11610. if (ret == 0) {
  11611. ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
  11612. if (ret == BAD_FUNC_ARG) {
  11613. ret = 0;
  11614. }
  11615. else if (ret == 0) {
  11616. ret = WOLFSSL_FATAL_ERROR;
  11617. }
  11618. }
  11619. wc_Sha3_224_Free(&sha3);
  11620. #endif /* NOSHA3_224 */
  11621. #if !defined(WOLFSSL_NOSHA3_256)
  11622. if (ret == 0) {
  11623. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11624. /* Test bad args. */
  11625. if (ret == 0) {
  11626. ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
  11627. if (ret == BAD_FUNC_ARG) {
  11628. ret = 0;
  11629. }
  11630. else if (ret == 0) {
  11631. ret = WOLFSSL_FATAL_ERROR;
  11632. }
  11633. }
  11634. wc_Sha3_256_Free(&sha3);
  11635. } /* END sha3_256 */
  11636. #endif /* NOSHA3_256 */
  11637. #if !defined(WOLFSSL_NOSHA3_384)
  11638. if (ret == 0) {
  11639. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11640. /* Test bad args. */
  11641. if (ret == 0) {
  11642. ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
  11643. if (ret == BAD_FUNC_ARG) {
  11644. ret = 0;
  11645. }
  11646. else if (ret == 0) {
  11647. ret = WOLFSSL_FATAL_ERROR;
  11648. }
  11649. }
  11650. wc_Sha3_384_Free(&sha3);
  11651. } /* END sha3_384 */
  11652. #endif /* NOSHA3_384 */
  11653. #if !defined(WOLFSSL_NOSHA3_512)
  11654. if (ret == 0) {
  11655. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11656. /* Test bad args. */
  11657. if (ret == 0) {
  11658. ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
  11659. if (ret == BAD_FUNC_ARG) {
  11660. ret = 0;
  11661. }
  11662. else if (ret == 0) {
  11663. ret = WOLFSSL_FATAL_ERROR;
  11664. }
  11665. }
  11666. wc_Sha3_512_Free(&sha3);
  11667. } /* END sha3_512 */
  11668. #endif /* NOSHA3_512 */
  11669. res = TEST_RES_CHECK(ret == 0);
  11670. #endif
  11671. return res;
  11672. } /* END test_wc_InitSha3 */
  11673. /*
  11674. * Testing wc_Sha3_Update()
  11675. */
  11676. static int testing_wc_Sha3_Update(void)
  11677. {
  11678. int res = TEST_SKIPPED;
  11679. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  11680. !defined(WOLFSSL_AFALG_XILINX)
  11681. wc_Sha3 sha3;
  11682. byte msg[] = "Everybody's working for the weekend.";
  11683. byte msg2[] = "Everybody gets Friday off.";
  11684. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11685. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11686. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11687. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11688. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11689. word32 msglen = sizeof(msg) - 1;
  11690. word32 msg2len = sizeof(msg2);
  11691. word32 msgCmplen = sizeof(msgCmp);
  11692. int ret = 0;
  11693. #if !defined(WOLFSSL_NOSHA3_224)
  11694. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11695. if (ret != 0) {
  11696. return TEST_FAIL;
  11697. }
  11698. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  11699. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11700. ret = WOLFSSL_FATAL_ERROR;
  11701. }
  11702. if (ret == 0) {
  11703. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11704. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11705. ret = WOLFSSL_FATAL_ERROR;
  11706. }
  11707. }
  11708. /* Pass bad args. */
  11709. if (ret == 0) {
  11710. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  11711. if (ret == BAD_FUNC_ARG) {
  11712. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  11713. }
  11714. if (ret == BAD_FUNC_ARG) {
  11715. wc_Sha3_224_Free(&sha3);
  11716. if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
  11717. return TEST_FAIL;
  11718. }
  11719. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  11720. if (ret == 0) {
  11721. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11722. }
  11723. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11724. ret = WOLFSSL_FATAL_ERROR;
  11725. }
  11726. }
  11727. }
  11728. wc_Sha3_224_Free(&sha3);
  11729. #endif /* SHA3_224 */
  11730. #if !defined(WOLFSSL_NOSHA3_256)
  11731. if (ret == 0) {
  11732. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11733. if (ret != 0) {
  11734. return TEST_FAIL;
  11735. }
  11736. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  11737. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11738. ret = WOLFSSL_FATAL_ERROR;
  11739. }
  11740. if (ret == 0) {
  11741. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11742. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11743. ret = WOLFSSL_FATAL_ERROR;
  11744. }
  11745. }
  11746. /* Pass bad args. */
  11747. if (ret == 0) {
  11748. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  11749. if (ret == BAD_FUNC_ARG) {
  11750. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  11751. }
  11752. if (ret == BAD_FUNC_ARG) {
  11753. wc_Sha3_256_Free(&sha3);
  11754. if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
  11755. return TEST_FAIL;
  11756. }
  11757. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  11758. if (ret == 0) {
  11759. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11760. }
  11761. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11762. ret = WOLFSSL_FATAL_ERROR;
  11763. }
  11764. }
  11765. }
  11766. wc_Sha3_256_Free(&sha3);
  11767. }
  11768. #endif /* SHA3_256 */
  11769. #if !defined(WOLFSSL_NOSHA3_384)
  11770. if (ret == 0) {
  11771. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11772. if (ret != 0) {
  11773. return TEST_FAIL;
  11774. }
  11775. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  11776. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11777. ret = WOLFSSL_FATAL_ERROR;
  11778. }
  11779. if (ret == 0) {
  11780. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11781. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11782. ret = WOLFSSL_FATAL_ERROR;
  11783. }
  11784. }
  11785. /* Pass bad args. */
  11786. if (ret == 0) {
  11787. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  11788. if (ret == BAD_FUNC_ARG) {
  11789. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  11790. }
  11791. if (ret == BAD_FUNC_ARG) {
  11792. wc_Sha3_384_Free(&sha3);
  11793. if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
  11794. return TEST_FAIL;
  11795. }
  11796. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  11797. if (ret == 0) {
  11798. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11799. }
  11800. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11801. ret = WOLFSSL_FATAL_ERROR;
  11802. }
  11803. }
  11804. }
  11805. wc_Sha3_384_Free(&sha3);
  11806. }
  11807. #endif /* SHA3_384 */
  11808. #if !defined(WOLFSSL_NOSHA3_512)
  11809. if (ret == 0) {
  11810. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11811. if (ret != 0) {
  11812. return TEST_FAIL;
  11813. }
  11814. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  11815. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11816. ret = WOLFSSL_FATAL_ERROR;
  11817. }
  11818. if (ret == 0) {
  11819. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11820. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11821. ret = WOLFSSL_FATAL_ERROR;
  11822. }
  11823. }
  11824. /* Pass bad args. */
  11825. if (ret == 0) {
  11826. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  11827. if (ret == BAD_FUNC_ARG) {
  11828. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  11829. }
  11830. if (ret == BAD_FUNC_ARG) {
  11831. wc_Sha3_512_Free(&sha3);
  11832. if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
  11833. return TEST_FAIL;
  11834. }
  11835. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  11836. if (ret == 0) {
  11837. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11838. }
  11839. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11840. ret = WOLFSSL_FATAL_ERROR;
  11841. }
  11842. }
  11843. }
  11844. wc_Sha3_512_Free(&sha3);
  11845. }
  11846. #endif /* SHA3_512 */
  11847. res = TEST_RES_CHECK(ret == 0);
  11848. #endif /* WOLFSSL_SHA3 */
  11849. return res;
  11850. } /* END testing_wc_Sha3_Update */
  11851. /*
  11852. * Testing wc_Sha3_224_Final()
  11853. */
  11854. static int test_wc_Sha3_224_Final(void)
  11855. {
  11856. int res = TEST_SKIPPED;
  11857. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11858. wc_Sha3 sha3;
  11859. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11860. "nopnopq";
  11861. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  11862. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  11863. "\x64\xea\xd0\xfc\xce\x33";
  11864. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11865. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  11866. int ret = 0;
  11867. /* Init stack variables. */
  11868. XMEMSET(hash, 0, sizeof(hash));
  11869. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11870. if (ret != 0) {
  11871. return TEST_FAIL;
  11872. }
  11873. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11874. if (ret == 0) {
  11875. ret = wc_Sha3_224_Final(&sha3, hash);
  11876. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11877. ret = WOLFSSL_FATAL_ERROR;
  11878. }
  11879. }
  11880. /* Test bad args. */
  11881. if (ret == 0) {
  11882. ret = wc_Sha3_224_Final(NULL, hash);
  11883. if (ret == 0) {
  11884. ret = wc_Sha3_224_Final(&sha3, NULL);
  11885. }
  11886. if (ret == BAD_FUNC_ARG) {
  11887. ret = 0;
  11888. }
  11889. else if (ret == 0) {
  11890. ret = WOLFSSL_FATAL_ERROR;
  11891. }
  11892. }
  11893. wc_Sha3_224_Free(&sha3);
  11894. if (ret == 0) {
  11895. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11896. if (ret != 0) {
  11897. return TEST_FAIL;
  11898. }
  11899. /* Init stack variables. */
  11900. XMEMSET(hash, 0, sizeof(hash));
  11901. XMEMSET(hashRet, 0, sizeof(hashRet));
  11902. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11903. if (ret == 0) {
  11904. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  11905. }
  11906. if (ret == 0) {
  11907. ret = wc_Sha3_224_Final(&sha3, hash);
  11908. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11909. ret = WOLFSSL_FATAL_ERROR;
  11910. }
  11911. }
  11912. if (ret == 0) {
  11913. /* Test bad args. */
  11914. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  11915. if (ret == BAD_FUNC_ARG) {
  11916. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  11917. }
  11918. if (ret == BAD_FUNC_ARG) {
  11919. ret = 0;
  11920. }
  11921. else if (ret == 0) {
  11922. ret = WOLFSSL_FATAL_ERROR;
  11923. }
  11924. }
  11925. }
  11926. wc_Sha3_224_Free(&sha3);
  11927. res = TEST_RES_CHECK(ret == 0);
  11928. #endif
  11929. return res;
  11930. } /* END test_wc_Sha3_224_Final */
  11931. /*
  11932. * Testing wc_Sha3_256_Final()
  11933. */
  11934. static int test_wc_Sha3_256_Final(void)
  11935. {
  11936. int res = TEST_SKIPPED;
  11937. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11938. wc_Sha3 sha3;
  11939. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11940. "nopnopq";
  11941. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  11942. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  11943. "\xdd\x97\x49\x6d\x33\x76";
  11944. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11945. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  11946. int ret = 0;
  11947. /* Init stack variables. */
  11948. XMEMSET(hash, 0, sizeof(hash));
  11949. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11950. if (ret != 0) {
  11951. return TEST_FAIL;
  11952. }
  11953. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11954. if (ret == 0) {
  11955. ret = wc_Sha3_256_Final(&sha3, hash);
  11956. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11957. ret = WOLFSSL_FATAL_ERROR;
  11958. }
  11959. }
  11960. /* Test bad args. */
  11961. if (ret == 0) {
  11962. ret = wc_Sha3_256_Final(NULL, hash);
  11963. if (ret == 0) {
  11964. ret = wc_Sha3_256_Final(&sha3, NULL);
  11965. }
  11966. if (ret == BAD_FUNC_ARG) {
  11967. ret = 0;
  11968. }
  11969. else if (ret == 0) {
  11970. ret = WOLFSSL_FATAL_ERROR;
  11971. }
  11972. }
  11973. wc_Sha3_256_Free(&sha3);
  11974. if (ret == 0) {
  11975. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11976. if (ret != 0) {
  11977. return TEST_FAIL;
  11978. }
  11979. /* Init stack variables. */
  11980. XMEMSET(hash, 0, sizeof(hash));
  11981. XMEMSET(hashRet, 0, sizeof(hashRet));
  11982. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11983. if (ret == 0) {
  11984. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  11985. }
  11986. if (ret == 0) {
  11987. ret = wc_Sha3_256_Final(&sha3, hash);
  11988. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11989. ret = WOLFSSL_FATAL_ERROR;
  11990. }
  11991. }
  11992. if (ret == 0) {
  11993. /* Test bad args. */
  11994. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  11995. if (ret == BAD_FUNC_ARG) {
  11996. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  11997. }
  11998. if (ret == BAD_FUNC_ARG) {
  11999. ret = 0;
  12000. }
  12001. else if (ret == 0) {
  12002. ret = WOLFSSL_FATAL_ERROR;
  12003. }
  12004. }
  12005. }
  12006. wc_Sha3_256_Free(&sha3);
  12007. res = TEST_RES_CHECK(ret == 0);
  12008. #endif
  12009. return res;
  12010. } /* END test_wc_Sha3_256_Final */
  12011. /*
  12012. * Testing wc_Sha3_384_Final()
  12013. */
  12014. static int test_wc_Sha3_384_Final(void)
  12015. {
  12016. int res = TEST_SKIPPED;
  12017. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12018. wc_Sha3 sha3;
  12019. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12020. "nopnopq";
  12021. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12022. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12023. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12024. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12025. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12026. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12027. int ret = 0;
  12028. /* Init stack variables. */
  12029. XMEMSET(hash, 0, sizeof(hash));
  12030. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12031. if (ret != 0) {
  12032. return TEST_FAIL;
  12033. }
  12034. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12035. if (ret == 0) {
  12036. ret = wc_Sha3_384_Final(&sha3, hash);
  12037. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12038. ret = WOLFSSL_FATAL_ERROR;
  12039. }
  12040. }
  12041. /* Test bad args. */
  12042. if (ret == 0) {
  12043. ret = wc_Sha3_384_Final(NULL, hash);
  12044. if (ret == 0) {
  12045. ret = wc_Sha3_384_Final(&sha3, NULL);
  12046. }
  12047. if (ret == BAD_FUNC_ARG) {
  12048. ret = 0;
  12049. }
  12050. else if (ret == 0) {
  12051. ret = WOLFSSL_FATAL_ERROR;
  12052. }
  12053. }
  12054. wc_Sha3_384_Free(&sha3);
  12055. if (ret == 0) {
  12056. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12057. if (ret != 0) {
  12058. return TEST_FAIL;
  12059. }
  12060. /* Init stack variables. */
  12061. XMEMSET(hash, 0, sizeof(hash));
  12062. XMEMSET(hashRet, 0, sizeof(hashRet));
  12063. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12064. if (ret == 0) {
  12065. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  12066. }
  12067. if (ret == 0) {
  12068. ret = wc_Sha3_384_Final(&sha3, hash);
  12069. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12070. ret = WOLFSSL_FATAL_ERROR;
  12071. }
  12072. }
  12073. if (ret == 0) {
  12074. /* Test bad args. */
  12075. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  12076. if (ret == BAD_FUNC_ARG) {
  12077. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  12078. }
  12079. if (ret == BAD_FUNC_ARG) {
  12080. ret = 0;
  12081. }
  12082. else if (ret == 0) {
  12083. ret = WOLFSSL_FATAL_ERROR;
  12084. }
  12085. }
  12086. }
  12087. wc_Sha3_384_Free(&sha3);
  12088. res = TEST_RES_CHECK(ret == 0);
  12089. #endif
  12090. return res;
  12091. } /* END test_wc_Sha3_384_Final */
  12092. /*
  12093. * Testing wc_Sha3_512_Final()
  12094. */
  12095. static int test_wc_Sha3_512_Final(void)
  12096. {
  12097. int res = TEST_SKIPPED;
  12098. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12099. !defined(WOLFSSL_NOSHA3_384)
  12100. wc_Sha3 sha3;
  12101. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12102. "nopnopq";
  12103. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12104. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12105. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12106. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12107. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12108. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12109. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12110. int ret = 0;
  12111. /* Init stack variables. */
  12112. XMEMSET(hash, 0, sizeof(hash));
  12113. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12114. if (ret != 0) {
  12115. return TEST_FAIL;
  12116. }
  12117. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12118. if (ret == 0) {
  12119. ret = wc_Sha3_512_Final(&sha3, hash);
  12120. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12121. ret = WOLFSSL_FATAL_ERROR;
  12122. }
  12123. }
  12124. /* Test bad args. */
  12125. if (ret == 0) {
  12126. ret = wc_Sha3_512_Final(NULL, hash);
  12127. if (ret == 0) {
  12128. ret = wc_Sha3_384_Final(&sha3, NULL);
  12129. }
  12130. if (ret == BAD_FUNC_ARG) {
  12131. ret = 0;
  12132. }
  12133. else if (ret == 0) {
  12134. ret = WOLFSSL_FATAL_ERROR;
  12135. }
  12136. }
  12137. wc_Sha3_512_Free(&sha3);
  12138. if (ret == 0) {
  12139. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12140. if (ret != 0) {
  12141. return TEST_FAIL;
  12142. }
  12143. /* Init stack variables. */
  12144. XMEMSET(hash, 0, sizeof(hash));
  12145. XMEMSET(hashRet, 0, sizeof(hashRet));
  12146. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12147. if (ret == 0) {
  12148. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  12149. }
  12150. if (ret == 0) {
  12151. ret = wc_Sha3_512_Final(&sha3, hash);
  12152. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12153. ret = WOLFSSL_FATAL_ERROR;
  12154. }
  12155. }
  12156. if (ret == 0) {
  12157. /* Test bad args. */
  12158. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  12159. if (ret == BAD_FUNC_ARG) {
  12160. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  12161. }
  12162. if (ret == BAD_FUNC_ARG) {
  12163. ret = 0;
  12164. }
  12165. else if (ret == 0) {
  12166. ret = WOLFSSL_FATAL_ERROR;
  12167. }
  12168. }
  12169. }
  12170. wc_Sha3_512_Free(&sha3);
  12171. res = TEST_RES_CHECK(ret == 0);
  12172. #endif
  12173. return res;
  12174. } /* END test_wc_Sha3_512_Final */
  12175. /*
  12176. * Testing wc_Sha3_224_Copy()
  12177. */
  12178. static int test_wc_Sha3_224_Copy(void)
  12179. {
  12180. int res = TEST_SKIPPED;
  12181. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12182. wc_Sha3 sha3, sha3Cpy;
  12183. const char* msg = TEST_STRING;
  12184. word32 msglen = (word32)TEST_STRING_SZ;
  12185. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12186. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12187. int ret = 0;
  12188. XMEMSET(hash, 0, sizeof(hash));
  12189. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12190. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12191. if (ret != 0) {
  12192. return TEST_FAIL;
  12193. }
  12194. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
  12195. if (ret != 0) {
  12196. wc_Sha3_224_Free(&sha3);
  12197. return TEST_FAIL;
  12198. }
  12199. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  12200. if (ret == 0) {
  12201. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  12202. if (ret == 0) {
  12203. ret = wc_Sha3_224_Final(&sha3, hash);
  12204. if (ret == 0) {
  12205. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  12206. }
  12207. }
  12208. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12209. ret = WOLFSSL_FATAL_ERROR;
  12210. }
  12211. }
  12212. /* Test bad args. */
  12213. if (ret == 0) {
  12214. ret = wc_Sha3_224_Copy(NULL, &sha3);
  12215. if (ret == BAD_FUNC_ARG) {
  12216. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  12217. }
  12218. if (ret == BAD_FUNC_ARG) {
  12219. ret = 0;
  12220. }
  12221. else if (ret == 0) {
  12222. ret = WOLFSSL_FATAL_ERROR;
  12223. }
  12224. }
  12225. res = TEST_RES_CHECK(ret == 0);
  12226. #endif
  12227. return res;
  12228. } /* END test_wc_Sha3_224_Copy */
  12229. /*
  12230. * Testing wc_Sha3_256_Copy()
  12231. */
  12232. static int test_wc_Sha3_256_Copy(void)
  12233. {
  12234. int res = TEST_SKIPPED;
  12235. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12236. wc_Sha3 sha3, sha3Cpy;
  12237. const char* msg = TEST_STRING;
  12238. word32 msglen = (word32)TEST_STRING_SZ;
  12239. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12240. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12241. int ret = 0;
  12242. XMEMSET(hash, 0, sizeof(hash));
  12243. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12244. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12245. if (ret != 0) {
  12246. return TEST_FAIL;
  12247. }
  12248. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
  12249. if (ret != 0) {
  12250. wc_Sha3_256_Free(&sha3);
  12251. return TEST_FAIL;
  12252. }
  12253. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  12254. if (ret == 0) {
  12255. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  12256. if (ret == 0) {
  12257. ret = wc_Sha3_256_Final(&sha3, hash);
  12258. if (ret == 0) {
  12259. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  12260. }
  12261. }
  12262. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12263. ret = WOLFSSL_FATAL_ERROR;
  12264. }
  12265. }
  12266. /* Test bad args. */
  12267. if (ret == 0) {
  12268. ret = wc_Sha3_256_Copy(NULL, &sha3);
  12269. if (ret == BAD_FUNC_ARG) {
  12270. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  12271. }
  12272. if (ret == BAD_FUNC_ARG) {
  12273. ret = 0;
  12274. }
  12275. else if (ret == 0) {
  12276. ret = WOLFSSL_FATAL_ERROR;
  12277. }
  12278. }
  12279. res = TEST_RES_CHECK(ret == 0);
  12280. #endif
  12281. return res;
  12282. } /* END test_wc_Sha3_256_Copy */
  12283. /*
  12284. * Testing wc_Sha3_384_Copy()
  12285. */
  12286. static int test_wc_Sha3_384_Copy(void)
  12287. {
  12288. int res = TEST_SKIPPED;
  12289. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12290. wc_Sha3 sha3, sha3Cpy;
  12291. const char* msg = TEST_STRING;
  12292. word32 msglen = (word32)TEST_STRING_SZ;
  12293. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12294. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12295. int ret = 0;
  12296. XMEMSET(hash, 0, sizeof(hash));
  12297. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12298. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12299. if (ret != 0) {
  12300. return TEST_FAIL;
  12301. }
  12302. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
  12303. if (ret != 0) {
  12304. wc_Sha3_384_Free(&sha3);
  12305. return TEST_FAIL;
  12306. }
  12307. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  12308. if (ret == 0) {
  12309. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  12310. if (ret == 0) {
  12311. ret = wc_Sha3_384_Final(&sha3, hash);
  12312. if (ret == 0) {
  12313. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  12314. }
  12315. }
  12316. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12317. ret = WOLFSSL_FATAL_ERROR;
  12318. }
  12319. }
  12320. /* Test bad args. */
  12321. if (ret == 0) {
  12322. ret = wc_Sha3_384_Copy(NULL, &sha3);
  12323. if (ret == BAD_FUNC_ARG) {
  12324. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  12325. }
  12326. if (ret == BAD_FUNC_ARG) {
  12327. ret = 0;
  12328. }
  12329. else if (ret == 0) {
  12330. ret = WOLFSSL_FATAL_ERROR;
  12331. }
  12332. }
  12333. res = TEST_RES_CHECK(ret == 0);
  12334. #endif
  12335. return res;
  12336. } /* END test_wc_Sha3_384_Copy */
  12337. /*
  12338. * Testing wc_Sha3_512_Copy()
  12339. */
  12340. static int test_wc_Sha3_512_Copy(void)
  12341. {
  12342. int res = TEST_SKIPPED;
  12343. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12344. wc_Sha3 sha3, sha3Cpy;
  12345. const char* msg = TEST_STRING;
  12346. word32 msglen = (word32)TEST_STRING_SZ;
  12347. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12348. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12349. int ret = 0;
  12350. XMEMSET(hash, 0, sizeof(hash));
  12351. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12352. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12353. if (ret != 0) {
  12354. return TEST_FAIL;
  12355. }
  12356. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
  12357. if (ret != 0) {
  12358. wc_Sha3_512_Free(&sha3);
  12359. return TEST_FAIL;
  12360. }
  12361. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  12362. if (ret == 0) {
  12363. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  12364. if (ret == 0) {
  12365. ret = wc_Sha3_512_Final(&sha3, hash);
  12366. if (ret == 0) {
  12367. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  12368. }
  12369. }
  12370. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12371. ret = WOLFSSL_FATAL_ERROR;
  12372. }
  12373. }
  12374. /* Test bad args. */
  12375. if (ret == 0) {
  12376. ret = wc_Sha3_512_Copy(NULL, &sha3);
  12377. if (ret == BAD_FUNC_ARG) {
  12378. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  12379. }
  12380. if (ret == BAD_FUNC_ARG) {
  12381. ret = 0;
  12382. }
  12383. else if (ret == 0) {
  12384. ret = WOLFSSL_FATAL_ERROR;
  12385. }
  12386. }
  12387. res = TEST_RES_CHECK(ret == 0);
  12388. #endif
  12389. return res;
  12390. } /* END test_wc_Sha3_512_Copy */
  12391. /*
  12392. * Unit test function for wc_Sha3_GetFlags()
  12393. */
  12394. static int test_wc_Sha3_GetFlags(void)
  12395. {
  12396. int res = TEST_SKIPPED;
  12397. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12398. wc_Sha3 sha3;
  12399. word32 flags = 0;
  12400. int ret = 0;
  12401. /* Initialize */
  12402. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12403. if (ret != 0) {
  12404. return TEST_FAIL;
  12405. }
  12406. if (ret == 0) {
  12407. ret = wc_Sha3_GetFlags(&sha3, &flags);
  12408. }
  12409. if (ret == 0) {
  12410. if (flags & WC_HASH_FLAG_ISCOPY) {
  12411. ret = 0;
  12412. }
  12413. }
  12414. wc_Sha3_224_Free(&sha3);
  12415. res = TEST_RES_CHECK(ret == 0);
  12416. #endif
  12417. return res;
  12418. } /* END test_wc_Sha3_GetFlags */
  12419. static int test_wc_InitShake256(void)
  12420. {
  12421. int res = TEST_SKIPPED;
  12422. #ifdef WOLFSSL_SHAKE256
  12423. wc_Shake shake;
  12424. int ret = 0;
  12425. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12426. /* Test bad args. */
  12427. if (ret == 0) {
  12428. ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
  12429. if (ret == BAD_FUNC_ARG) {
  12430. ret = 0;
  12431. }
  12432. else if (ret == 0) {
  12433. ret = WOLFSSL_FATAL_ERROR;
  12434. }
  12435. }
  12436. wc_Shake256_Free(&shake);
  12437. res = TEST_RES_CHECK(ret == 0);
  12438. #endif
  12439. return res;
  12440. } /* END test_wc_InitSha3 */
  12441. static int testing_wc_Shake256_Update(void)
  12442. {
  12443. int res = TEST_SKIPPED;
  12444. #ifdef WOLFSSL_SHAKE256
  12445. wc_Shake shake;
  12446. byte msg[] = "Everybody's working for the weekend.";
  12447. byte msg2[] = "Everybody gets Friday off.";
  12448. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12449. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12450. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12451. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12452. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12453. word32 msglen = sizeof(msg) - 1;
  12454. word32 msg2len = sizeof(msg2);
  12455. word32 msgCmplen = sizeof(msgCmp);
  12456. int ret = 0;
  12457. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12458. if (ret != 0) {
  12459. return TEST_FAIL;
  12460. }
  12461. ret = wc_Shake256_Update(&shake, msg, msglen);
  12462. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  12463. ret = WOLFSSL_FATAL_ERROR;
  12464. }
  12465. if (ret == 0) {
  12466. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  12467. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  12468. ret = WOLFSSL_FATAL_ERROR;
  12469. }
  12470. }
  12471. /* Pass bad args. */
  12472. if (ret == 0) {
  12473. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  12474. if (ret == BAD_FUNC_ARG) {
  12475. ret = wc_Shake256_Update(&shake, NULL, 5);
  12476. }
  12477. if (ret == BAD_FUNC_ARG) {
  12478. wc_Shake256_Free(&shake);
  12479. if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
  12480. return TEST_FAIL;
  12481. }
  12482. ret = wc_Shake256_Update(&shake, NULL, 0);
  12483. if (ret == 0) {
  12484. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  12485. }
  12486. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  12487. ret = WOLFSSL_FATAL_ERROR;
  12488. }
  12489. }
  12490. }
  12491. wc_Shake256_Free(&shake);
  12492. res = TEST_RES_CHECK(ret == 0);
  12493. #endif /* WOLFSSL_SHAKE256 */
  12494. return res;
  12495. }
  12496. static int test_wc_Shake256_Final(void)
  12497. {
  12498. int res = TEST_SKIPPED;
  12499. #ifdef WOLFSSL_SHAKE256
  12500. wc_Shake shake;
  12501. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12502. "nopnopq";
  12503. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12504. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12505. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12506. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12507. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12508. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12509. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12510. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12511. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12512. byte hash[114];
  12513. int ret = 0;
  12514. /* Init stack variables. */
  12515. XMEMSET(hash, 0, sizeof(hash));
  12516. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12517. if (ret != 0) {
  12518. return TEST_FAIL;
  12519. }
  12520. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  12521. if (ret == 0) {
  12522. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  12523. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  12524. ret = WOLFSSL_FATAL_ERROR;
  12525. }
  12526. }
  12527. /* Test bad args. */
  12528. if (ret == 0) {
  12529. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  12530. if (ret == 0) {
  12531. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  12532. }
  12533. if (ret == BAD_FUNC_ARG) {
  12534. ret = 0;
  12535. }
  12536. else if (ret == 0) {
  12537. ret = WOLFSSL_FATAL_ERROR;
  12538. }
  12539. }
  12540. wc_Shake256_Free(&shake);
  12541. res = TEST_RES_CHECK(ret == 0);
  12542. #endif
  12543. return res;
  12544. }
  12545. /*
  12546. * Testing wc_Shake256_Copy()
  12547. */
  12548. static int test_wc_Shake256_Copy(void)
  12549. {
  12550. int res = TEST_SKIPPED;
  12551. #ifdef WOLFSSL_SHAKE256
  12552. wc_Shake shake, shakeCpy;
  12553. const char* msg = TEST_STRING;
  12554. word32 msglen = (word32)TEST_STRING_SZ;
  12555. byte hash[144];
  12556. byte hashCpy[144];
  12557. word32 hashLen = sizeof(hash);
  12558. word32 hashLenCpy = sizeof(hashCpy);
  12559. int ret;
  12560. XMEMSET(hash, 0, sizeof(hash));
  12561. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12562. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12563. if (ret != 0) {
  12564. return TEST_FAIL;
  12565. }
  12566. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
  12567. if (ret != 0) {
  12568. wc_Shake256_Free(&shake);
  12569. return TEST_FAIL;
  12570. }
  12571. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  12572. if (ret == 0) {
  12573. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  12574. if (ret == 0) {
  12575. ret = wc_Shake256_Final(&shake, hash, hashLen);
  12576. if (ret == 0) {
  12577. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  12578. }
  12579. }
  12580. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12581. ret = WOLFSSL_FATAL_ERROR;
  12582. }
  12583. }
  12584. /* Test bad args. */
  12585. if (ret == 0) {
  12586. ret = wc_Shake256_Copy(NULL, &shake);
  12587. if (ret == BAD_FUNC_ARG) {
  12588. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  12589. }
  12590. if (ret == BAD_FUNC_ARG) {
  12591. ret = 0;
  12592. }
  12593. else if (ret == 0) {
  12594. ret = WOLFSSL_FATAL_ERROR;
  12595. }
  12596. }
  12597. wc_Shake256_Free(&shake);
  12598. res = TEST_RES_CHECK(ret == 0);
  12599. #endif
  12600. return res;
  12601. } /* END test_wc_Shake256_Copy */
  12602. /*
  12603. * Unit test function for wc_Shake256Hash()
  12604. */
  12605. static int test_wc_Shake256Hash(void)
  12606. {
  12607. int res = TEST_SKIPPED;
  12608. #ifdef WOLFSSL_SHAKE256
  12609. const byte data[] = { /* Hello World */
  12610. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12611. 0x72,0x6c,0x64
  12612. };
  12613. word32 len = sizeof(data);
  12614. byte hash[144];
  12615. word32 hashLen = sizeof(hash);
  12616. int ret;
  12617. ret = wc_Shake256Hash(data, len, hash, hashLen);
  12618. res = TEST_RES_CHECK(ret == 0);
  12619. #endif
  12620. return res;
  12621. } /* END test_wc_Shake256Hash */
  12622. /*
  12623. * Test function for wc_HmacSetKey
  12624. */
  12625. static int test_wc_Md5HmacSetKey(void)
  12626. {
  12627. int res = TEST_SKIPPED;
  12628. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12629. Hmac hmac;
  12630. int ret, times, itr;
  12631. int flag = 0;
  12632. const char* keys[]=
  12633. {
  12634. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12635. #ifndef HAVE_FIPS
  12636. "Jefe", /* smaller than minimum FIPS key size */
  12637. #endif
  12638. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12639. };
  12640. times = sizeof(keys) / sizeof(char*);
  12641. flag = 0;
  12642. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12643. if (ret != 0)
  12644. return TEST_FAIL;
  12645. for (itr = 0; itr < times; itr++) {
  12646. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12647. (word32)XSTRLEN(keys[itr]));
  12648. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12649. wc_HmacFree(&hmac);
  12650. if (ret == BAD_FUNC_ARG) {
  12651. return TEST_SUCCESS;
  12652. }
  12653. else {
  12654. return TEST_FAIL;
  12655. }
  12656. #else
  12657. if (ret != 0) {
  12658. flag = ret;
  12659. }
  12660. #endif
  12661. }
  12662. /* Bad args. */
  12663. if (!flag) {
  12664. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12665. (word32)XSTRLEN(keys[0]));
  12666. if (ret != BAD_FUNC_ARG) {
  12667. flag = WOLFSSL_FATAL_ERROR;
  12668. }
  12669. }
  12670. if (!flag) {
  12671. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  12672. if (ret != BAD_FUNC_ARG) {
  12673. flag = WOLFSSL_FATAL_ERROR;
  12674. }
  12675. }
  12676. if (!flag) {
  12677. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12678. (word32)XSTRLEN(keys[0]));
  12679. if (ret != BAD_FUNC_ARG) {
  12680. flag = WOLFSSL_FATAL_ERROR;
  12681. }
  12682. }
  12683. if (!flag) {
  12684. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12685. #ifdef HAVE_FIPS
  12686. if (ret != HMAC_MIN_KEYLEN_E) {
  12687. flag = WOLFSSL_FATAL_ERROR;
  12688. }
  12689. #else
  12690. if (ret != 0) {
  12691. flag = WOLFSSL_FATAL_ERROR;
  12692. }
  12693. #endif
  12694. }
  12695. wc_HmacFree(&hmac);
  12696. res = TEST_RES_CHECK(flag == 0);
  12697. #endif
  12698. return res;
  12699. } /* END test_wc_Md5HmacSetKey */
  12700. /*
  12701. * testing wc_HmacSetKey() on wc_Sha hash.
  12702. */
  12703. static int test_wc_ShaHmacSetKey(void)
  12704. {
  12705. int res = TEST_SKIPPED;
  12706. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12707. Hmac hmac;
  12708. int ret, times, itr;
  12709. int flag = 0;
  12710. const char* keys[]=
  12711. {
  12712. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12713. "\x0b\x0b\x0b",
  12714. #ifndef HAVE_FIPS
  12715. "Jefe", /* smaller than minimum FIPS key size */
  12716. #endif
  12717. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12718. "\xAA\xAA\xAA"
  12719. };
  12720. times = sizeof(keys) / sizeof(char*);
  12721. flag = 0;
  12722. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12723. if (ret != 0)
  12724. return ret;
  12725. for (itr = 0; itr < times; itr++) {
  12726. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12727. (word32)XSTRLEN(keys[itr]));
  12728. if (ret != 0) {
  12729. flag = ret;
  12730. }
  12731. }
  12732. /* Bad args. */
  12733. if (!flag) {
  12734. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12735. (word32)XSTRLEN(keys[0]));
  12736. if (ret != BAD_FUNC_ARG) {
  12737. flag = WOLFSSL_FATAL_ERROR;
  12738. }
  12739. }
  12740. if (!flag) {
  12741. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  12742. if (ret != BAD_FUNC_ARG) {
  12743. flag = WOLFSSL_FATAL_ERROR;
  12744. }
  12745. }
  12746. if (!flag) {
  12747. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12748. (word32)XSTRLEN(keys[0]));
  12749. if (ret != BAD_FUNC_ARG) {
  12750. flag = WOLFSSL_FATAL_ERROR;
  12751. }
  12752. }
  12753. if (!flag) {
  12754. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12755. #ifdef HAVE_FIPS
  12756. if (ret != HMAC_MIN_KEYLEN_E) {
  12757. flag = WOLFSSL_FATAL_ERROR;
  12758. }
  12759. #else
  12760. if (ret != 0) {
  12761. flag = WOLFSSL_FATAL_ERROR;
  12762. }
  12763. #endif
  12764. }
  12765. wc_HmacFree(&hmac);
  12766. res = TEST_RES_CHECK(flag == 0);
  12767. #endif
  12768. return res;
  12769. } /* END test_wc_ShaHmacSetKey() */
  12770. /*
  12771. * testing wc_HmacSetKey() on Sha224 hash.
  12772. */
  12773. static int test_wc_Sha224HmacSetKey(void)
  12774. {
  12775. int res = TEST_SKIPPED;
  12776. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12777. Hmac hmac;
  12778. int ret, times, itr;
  12779. int flag = 0;
  12780. const char* keys[]=
  12781. {
  12782. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12783. "\x0b\x0b\x0b",
  12784. #ifndef HAVE_FIPS
  12785. "Jefe", /* smaller than minimum FIPS key size */
  12786. #endif
  12787. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12788. "\xAA\xAA\xAA"
  12789. };
  12790. times = sizeof(keys) / sizeof(char*);
  12791. flag = 0;
  12792. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12793. if (ret != 0)
  12794. return ret;
  12795. for (itr = 0; itr < times; itr++) {
  12796. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  12797. (word32)XSTRLEN(keys[itr]));
  12798. if (ret != 0) {
  12799. flag = ret;
  12800. }
  12801. }
  12802. /* Bad args. */
  12803. if (!flag) {
  12804. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  12805. (word32)XSTRLEN(keys[0]));
  12806. if (ret != BAD_FUNC_ARG) {
  12807. flag = WOLFSSL_FATAL_ERROR;
  12808. }
  12809. }
  12810. if (!flag) {
  12811. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  12812. if (ret != BAD_FUNC_ARG) {
  12813. flag = WOLFSSL_FATAL_ERROR;
  12814. }
  12815. }
  12816. if (!flag) {
  12817. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12818. (word32)XSTRLEN(keys[0]));
  12819. if (ret != BAD_FUNC_ARG) {
  12820. flag = WOLFSSL_FATAL_ERROR;
  12821. }
  12822. }
  12823. if (!flag) {
  12824. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  12825. #ifdef HAVE_FIPS
  12826. if (ret != HMAC_MIN_KEYLEN_E) {
  12827. flag = WOLFSSL_FATAL_ERROR;
  12828. }
  12829. #else
  12830. if (ret != 0) {
  12831. flag = WOLFSSL_FATAL_ERROR;
  12832. }
  12833. #endif
  12834. }
  12835. wc_HmacFree(&hmac);
  12836. res = TEST_RES_CHECK(flag == 0);
  12837. #endif
  12838. return res;
  12839. } /* END test_wc_Sha224HmacSetKey() */
  12840. /*
  12841. * testing wc_HmacSetKey() on Sha256 hash
  12842. */
  12843. static int test_wc_Sha256HmacSetKey(void)
  12844. {
  12845. int res = TEST_SKIPPED;
  12846. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12847. Hmac hmac;
  12848. int ret, times, itr;
  12849. int flag = 0;
  12850. const char* keys[]=
  12851. {
  12852. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12853. "\x0b\x0b\x0b",
  12854. #ifndef HAVE_FIPS
  12855. "Jefe", /* smaller than minimum FIPS key size */
  12856. #endif
  12857. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12858. "\xAA\xAA\xAA"
  12859. };
  12860. times = sizeof(keys) / sizeof(char*);
  12861. flag = 0;
  12862. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12863. if (ret != 0)
  12864. return ret;
  12865. for (itr = 0; itr < times; itr++) {
  12866. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  12867. (word32)XSTRLEN(keys[itr]));
  12868. if (ret != 0) {
  12869. flag = ret;
  12870. }
  12871. }
  12872. /* Bad args. */
  12873. if (!flag) {
  12874. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  12875. (word32)XSTRLEN(keys[0]));
  12876. if (ret != BAD_FUNC_ARG) {
  12877. flag = WOLFSSL_FATAL_ERROR;
  12878. }
  12879. }
  12880. if (!flag) {
  12881. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  12882. if (ret != BAD_FUNC_ARG) {
  12883. flag = WOLFSSL_FATAL_ERROR;
  12884. }
  12885. }
  12886. if (!flag) {
  12887. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12888. (word32)XSTRLEN(keys[0]));
  12889. if (ret != BAD_FUNC_ARG) {
  12890. flag = WOLFSSL_FATAL_ERROR;
  12891. }
  12892. }
  12893. if (!flag) {
  12894. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  12895. #ifdef HAVE_FIPS
  12896. if (ret != HMAC_MIN_KEYLEN_E) {
  12897. flag = WOLFSSL_FATAL_ERROR;
  12898. }
  12899. #else
  12900. if (ret != 0) {
  12901. flag = WOLFSSL_FATAL_ERROR;
  12902. }
  12903. #endif
  12904. }
  12905. wc_HmacFree(&hmac);
  12906. res = TEST_RES_CHECK(flag == 0);
  12907. #endif
  12908. return res;
  12909. } /* END test_wc_Sha256HmacSetKey() */
  12910. /*
  12911. * testing wc_HmacSetKey on Sha384 hash.
  12912. */
  12913. static int test_wc_Sha384HmacSetKey(void)
  12914. {
  12915. int res = TEST_SKIPPED;
  12916. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12917. Hmac hmac;
  12918. int ret, times, itr;
  12919. int flag = 0;
  12920. const char* keys[]=
  12921. {
  12922. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12923. "\x0b\x0b\x0b",
  12924. #ifndef HAVE_FIPS
  12925. "Jefe", /* smaller than minimum FIPS key size */
  12926. #endif
  12927. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12928. "\xAA\xAA\xAA"
  12929. };
  12930. times = sizeof(keys) / sizeof(char*);
  12931. flag = 0;
  12932. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12933. if (ret != 0)
  12934. return ret;
  12935. for (itr = 0; itr < times; itr++) {
  12936. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  12937. (word32)XSTRLEN(keys[itr]));
  12938. if (ret != 0) {
  12939. flag = ret;
  12940. }
  12941. }
  12942. /* Bad args. */
  12943. if (!flag) {
  12944. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  12945. (word32)XSTRLEN(keys[0]));
  12946. if (ret != BAD_FUNC_ARG) {
  12947. flag = WOLFSSL_FATAL_ERROR;
  12948. }
  12949. }
  12950. if (!flag) {
  12951. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  12952. if (ret != BAD_FUNC_ARG) {
  12953. flag = WOLFSSL_FATAL_ERROR;
  12954. }
  12955. }
  12956. if (!flag) {
  12957. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12958. (word32)XSTRLEN(keys[0]));
  12959. if (ret != BAD_FUNC_ARG) {
  12960. flag = WOLFSSL_FATAL_ERROR;
  12961. }
  12962. }
  12963. if (!flag) {
  12964. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  12965. #ifdef HAVE_FIPS
  12966. if (ret != HMAC_MIN_KEYLEN_E) {
  12967. flag = WOLFSSL_FATAL_ERROR;
  12968. }
  12969. #else
  12970. if (ret != 0) {
  12971. flag = WOLFSSL_FATAL_ERROR;
  12972. }
  12973. #endif
  12974. }
  12975. wc_HmacFree(&hmac);
  12976. res = TEST_RES_CHECK(flag == 0);
  12977. #endif
  12978. return res;
  12979. } /* END test_wc_Sha384HmacSetKey() */
  12980. /*
  12981. * testing wc_HmacUpdate on wc_Md5 hash.
  12982. */
  12983. static int test_wc_Md5HmacUpdate(void)
  12984. {
  12985. int res = TEST_SKIPPED;
  12986. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  12987. Hmac hmac;
  12988. testVector a, b;
  12989. int ret;
  12990. int flag = 0;
  12991. #ifdef HAVE_FIPS
  12992. const char* keys =
  12993. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12994. #else
  12995. const char* keys = "Jefe";
  12996. #endif
  12997. a.input = "what do ya want for nothing?";
  12998. a.inLen = XSTRLEN(a.input);
  12999. b.input = "Hi There";
  13000. b.inLen = XSTRLEN(b.input);
  13001. flag = 0;
  13002. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13003. if (ret != 0)
  13004. return ret;
  13005. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  13006. if (ret != 0) {
  13007. flag = ret;
  13008. }
  13009. if (!flag) {
  13010. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13011. if (ret != 0) {
  13012. flag = ret;
  13013. }
  13014. }
  13015. /* Update Hmac. */
  13016. if (!flag) {
  13017. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13018. if (ret != 0) {
  13019. flag = ret;
  13020. }
  13021. }
  13022. /* Test bad args. */
  13023. if (!flag) {
  13024. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13025. if (ret != BAD_FUNC_ARG) {
  13026. flag = WOLFSSL_FATAL_ERROR;
  13027. }
  13028. }
  13029. if (!flag) {
  13030. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13031. if (ret != BAD_FUNC_ARG) {
  13032. flag = WOLFSSL_FATAL_ERROR;
  13033. }
  13034. }
  13035. if (!flag) {
  13036. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13037. if (ret != 0) {
  13038. flag = ret;
  13039. }
  13040. }
  13041. wc_HmacFree(&hmac);
  13042. res = TEST_RES_CHECK(flag == 0);
  13043. #endif
  13044. return res;
  13045. } /* END test_wc_Md5HmacUpdate */
  13046. /*
  13047. * testing wc_HmacUpdate on SHA hash.
  13048. */
  13049. static int test_wc_ShaHmacUpdate(void)
  13050. {
  13051. int res = TEST_SKIPPED;
  13052. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13053. Hmac hmac;
  13054. testVector a, b;
  13055. int ret;
  13056. int flag = 0;
  13057. #ifdef HAVE_FIPS
  13058. const char* keys =
  13059. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13060. #else
  13061. const char* keys = "Jefe";
  13062. #endif
  13063. a.input = "what do ya want for nothing?";
  13064. a.inLen = XSTRLEN(a.input);
  13065. b.input = "Hi There";
  13066. b.inLen = XSTRLEN(b.input);
  13067. flag = 0;
  13068. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13069. if (ret != 0)
  13070. return ret;
  13071. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  13072. if (ret != 0) {
  13073. flag = ret;
  13074. }
  13075. if (!flag) {
  13076. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13077. if (ret != 0) {
  13078. flag = ret;
  13079. }
  13080. }
  13081. /* Update Hmac. */
  13082. if (!flag) {
  13083. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13084. if (ret != 0) {
  13085. flag = ret;
  13086. }
  13087. }
  13088. /* Test bad args. */
  13089. if (!flag) {
  13090. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13091. if (ret != BAD_FUNC_ARG) {
  13092. flag = WOLFSSL_FATAL_ERROR;
  13093. }
  13094. }
  13095. if (!flag) {
  13096. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13097. if (ret != BAD_FUNC_ARG) {
  13098. flag = WOLFSSL_FATAL_ERROR;
  13099. }
  13100. }
  13101. if (!flag) {
  13102. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13103. if (ret != 0) {
  13104. flag = ret;
  13105. }
  13106. }
  13107. wc_HmacFree(&hmac);
  13108. res = TEST_RES_CHECK(flag == 0);
  13109. #endif
  13110. return res;
  13111. } /* END test_wc_ShaHmacUpdate */
  13112. /*
  13113. * testing wc_HmacUpdate on SHA224 hash.
  13114. */
  13115. static int test_wc_Sha224HmacUpdate(void)
  13116. {
  13117. int res = TEST_SKIPPED;
  13118. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13119. Hmac hmac;
  13120. testVector a, b;
  13121. int ret;
  13122. int flag = 0;
  13123. #ifdef HAVE_FIPS
  13124. const char* keys =
  13125. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13126. #else
  13127. const char* keys = "Jefe";
  13128. #endif
  13129. a.input = "what do ya want for nothing?";
  13130. a.inLen = XSTRLEN(a.input);
  13131. b.input = "Hi There";
  13132. b.inLen = XSTRLEN(b.input);
  13133. flag = 0;
  13134. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13135. if (ret != 0)
  13136. return ret;
  13137. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  13138. if (ret != 0) {
  13139. flag = ret;
  13140. }
  13141. if (!flag) {
  13142. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13143. if (ret != 0) {
  13144. flag = ret;
  13145. }
  13146. }
  13147. /* Update Hmac. */
  13148. if (!flag) {
  13149. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13150. if (ret != 0) {
  13151. flag = ret;
  13152. }
  13153. }
  13154. /* Test bad args. */
  13155. if (!flag) {
  13156. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13157. if (ret != BAD_FUNC_ARG) {
  13158. flag = WOLFSSL_FATAL_ERROR;
  13159. }
  13160. }
  13161. if (!flag) {
  13162. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13163. if (ret != BAD_FUNC_ARG) {
  13164. flag = WOLFSSL_FATAL_ERROR;
  13165. }
  13166. }
  13167. if (!flag) {
  13168. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13169. if (ret != 0) {
  13170. flag = ret;
  13171. }
  13172. }
  13173. wc_HmacFree(&hmac);
  13174. res = TEST_RES_CHECK(flag == 0);
  13175. #endif
  13176. return res;
  13177. } /* END test_wc_Sha224HmacUpdate */
  13178. /*
  13179. * testing wc_HmacUpdate on SHA256 hash.
  13180. */
  13181. static int test_wc_Sha256HmacUpdate(void)
  13182. {
  13183. int res = TEST_SKIPPED;
  13184. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13185. Hmac hmac;
  13186. testVector a, b;
  13187. int ret;
  13188. int flag = 0;
  13189. #ifdef HAVE_FIPS
  13190. const char* keys =
  13191. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13192. #else
  13193. const char* keys = "Jefe";
  13194. #endif
  13195. a.input = "what do ya want for nothing?";
  13196. a.inLen = XSTRLEN(a.input);
  13197. b.input = "Hi There";
  13198. b.inLen = XSTRLEN(b.input);
  13199. flag = 0;
  13200. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13201. if (ret != 0)
  13202. return ret;
  13203. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  13204. if (ret != 0) {
  13205. flag = ret;
  13206. }
  13207. if (!flag) {
  13208. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13209. if (ret != 0) {
  13210. flag = ret;
  13211. }
  13212. }
  13213. /* Update Hmac. */
  13214. if (!flag) {
  13215. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13216. if (ret != 0) {
  13217. flag = ret;
  13218. }
  13219. }
  13220. /* Test bad args. */
  13221. if (!flag) {
  13222. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13223. if (ret != BAD_FUNC_ARG) {
  13224. flag = WOLFSSL_FATAL_ERROR;
  13225. }
  13226. }
  13227. if (!flag) {
  13228. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13229. if (ret != BAD_FUNC_ARG) {
  13230. flag = WOLFSSL_FATAL_ERROR;
  13231. }
  13232. }
  13233. if (!flag) {
  13234. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13235. if (ret != 0) {
  13236. flag = ret;
  13237. }
  13238. }
  13239. wc_HmacFree(&hmac);
  13240. res = TEST_RES_CHECK(flag == 0);
  13241. #endif
  13242. return res;
  13243. } /* END test_wc_Sha256HmacUpdate */
  13244. /*
  13245. * testing wc_HmacUpdate on SHA384 hash.
  13246. */
  13247. static int test_wc_Sha384HmacUpdate(void)
  13248. {
  13249. int res = TEST_SKIPPED;
  13250. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13251. Hmac hmac;
  13252. testVector a, b;
  13253. int ret;
  13254. int flag = 0;
  13255. #ifdef HAVE_FIPS
  13256. const char* keys =
  13257. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13258. #else
  13259. const char* keys = "Jefe";
  13260. #endif
  13261. a.input = "what do ya want for nothing?";
  13262. a.inLen = XSTRLEN(a.input);
  13263. b.input = "Hi There";
  13264. b.inLen = XSTRLEN(b.input);
  13265. flag = 0;
  13266. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13267. if (ret != 0)
  13268. return ret;
  13269. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  13270. if (ret != 0) {
  13271. flag = ret;
  13272. }
  13273. if (!flag) {
  13274. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13275. if (ret != 0) {
  13276. flag = ret;
  13277. }
  13278. }
  13279. /* Update Hmac. */
  13280. if (!flag) {
  13281. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13282. if (ret != 0) {
  13283. flag = ret;
  13284. }
  13285. }
  13286. /* Test bad args. */
  13287. if (!flag) {
  13288. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13289. if (ret != BAD_FUNC_ARG) {
  13290. flag = WOLFSSL_FATAL_ERROR;
  13291. }
  13292. }
  13293. if (!flag) {
  13294. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13295. if (ret != BAD_FUNC_ARG) {
  13296. flag = WOLFSSL_FATAL_ERROR;
  13297. }
  13298. }
  13299. if (!flag) {
  13300. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13301. if (ret != 0) {
  13302. flag = ret;
  13303. }
  13304. }
  13305. wc_HmacFree(&hmac);
  13306. res = TEST_RES_CHECK(flag == 0);
  13307. #endif
  13308. return res;
  13309. } /* END test_wc_Sha384HmacUpdate */
  13310. /*
  13311. * Testing wc_HmacFinal() with MD5
  13312. */
  13313. static int test_wc_Md5HmacFinal(void)
  13314. {
  13315. int res = TEST_SKIPPED;
  13316. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13317. Hmac hmac;
  13318. byte hash[WC_MD5_DIGEST_SIZE];
  13319. testVector a;
  13320. int ret;
  13321. const char* key;
  13322. int flag = 0;
  13323. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13324. a.input = "Hi There";
  13325. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13326. "\x9d";
  13327. a.inLen = XSTRLEN(a.input);
  13328. a.outLen = XSTRLEN(a.output);
  13329. flag = 0;
  13330. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13331. if (ret != 0)
  13332. return ret;
  13333. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  13334. if (ret != 0) {
  13335. flag = ret;
  13336. }
  13337. if (!flag) {
  13338. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13339. if (ret != 0) {
  13340. flag = ret;
  13341. }
  13342. }
  13343. if (!flag) {
  13344. ret = wc_HmacFinal(&hmac, hash);
  13345. if (ret != 0) {
  13346. flag = ret;
  13347. }
  13348. }
  13349. if (!flag) {
  13350. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  13351. flag = WOLFSSL_FATAL_ERROR;
  13352. }
  13353. }
  13354. /* Try bad parameters. */
  13355. if (!flag) {
  13356. ret = wc_HmacFinal(NULL, hash);
  13357. if (ret != BAD_FUNC_ARG) {
  13358. flag = WOLFSSL_FATAL_ERROR;
  13359. }
  13360. }
  13361. #ifndef HAVE_FIPS
  13362. if (!flag) {
  13363. ret = wc_HmacFinal(&hmac, NULL);
  13364. if (ret != BAD_FUNC_ARG) {
  13365. flag = WOLFSSL_FATAL_ERROR;
  13366. }
  13367. }
  13368. #endif
  13369. wc_HmacFree(&hmac);
  13370. res = TEST_RES_CHECK(flag == 0);
  13371. #endif
  13372. return res;
  13373. } /* END test_wc_Md5HmacFinal */
  13374. /*
  13375. * Testing wc_HmacFinal() with SHA
  13376. */
  13377. static int test_wc_ShaHmacFinal(void)
  13378. {
  13379. int res = TEST_SKIPPED;
  13380. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13381. Hmac hmac;
  13382. byte hash[WC_SHA_DIGEST_SIZE];
  13383. testVector a;
  13384. int ret;
  13385. int flag = 0;
  13386. const char* key;
  13387. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13388. "\x0b\x0b\x0b";
  13389. a.input = "Hi There";
  13390. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13391. "\x8e\xf1\x46\xbe\x00";
  13392. a.inLen = XSTRLEN(a.input);
  13393. a.outLen = XSTRLEN(a.output);
  13394. flag = 0;
  13395. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13396. if (ret != 0)
  13397. return ret;
  13398. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  13399. if (ret != 0) {
  13400. flag = ret;
  13401. }
  13402. if (!flag) {
  13403. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13404. if (ret != 0) {
  13405. flag = ret;
  13406. }
  13407. }
  13408. if (!flag) {
  13409. ret = wc_HmacFinal(&hmac, hash);
  13410. if (ret != 0) {
  13411. flag = ret;
  13412. }
  13413. }
  13414. if (!flag) {
  13415. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  13416. flag = WOLFSSL_FATAL_ERROR;
  13417. }
  13418. }
  13419. /* Try bad parameters. */
  13420. if (!flag) {
  13421. ret = wc_HmacFinal(NULL, hash);
  13422. if (ret != BAD_FUNC_ARG) {
  13423. flag = WOLFSSL_FATAL_ERROR;
  13424. }
  13425. }
  13426. #ifndef HAVE_FIPS
  13427. if (!flag) {
  13428. ret = wc_HmacFinal(&hmac, NULL);
  13429. if (ret != BAD_FUNC_ARG) {
  13430. flag = WOLFSSL_FATAL_ERROR;
  13431. }
  13432. }
  13433. #endif
  13434. wc_HmacFree(&hmac);
  13435. res = TEST_RES_CHECK(flag == 0);
  13436. #endif
  13437. return res;
  13438. } /* END test_wc_ShaHmacFinal */
  13439. /*
  13440. * Testing wc_HmacFinal() with SHA224
  13441. */
  13442. static int test_wc_Sha224HmacFinal(void)
  13443. {
  13444. int res = TEST_SKIPPED;
  13445. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13446. Hmac hmac;
  13447. byte hash[WC_SHA224_DIGEST_SIZE];
  13448. testVector a;
  13449. int ret;
  13450. int flag = 0;
  13451. const char* key;
  13452. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13453. "\x0b\x0b\x0b";
  13454. a.input = "Hi There";
  13455. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13456. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13457. a.inLen = XSTRLEN(a.input);
  13458. a.outLen = XSTRLEN(a.output);
  13459. flag = 0;
  13460. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13461. if (ret != 0)
  13462. return ret;
  13463. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  13464. if (ret != 0) {
  13465. flag = ret;
  13466. }
  13467. if (!flag) {
  13468. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13469. if (ret != 0) {
  13470. flag = ret;
  13471. }
  13472. }
  13473. if (!flag) {
  13474. ret = wc_HmacFinal(&hmac, hash);
  13475. if (ret != 0) {
  13476. flag = ret;
  13477. }
  13478. }
  13479. if (!flag) {
  13480. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  13481. flag = WOLFSSL_FATAL_ERROR;
  13482. }
  13483. }
  13484. /* Try bad parameters. */
  13485. if (!flag) {
  13486. ret = wc_HmacFinal(NULL, hash);
  13487. if (ret != BAD_FUNC_ARG) {
  13488. flag = WOLFSSL_FATAL_ERROR;
  13489. }
  13490. }
  13491. #ifndef HAVE_FIPS
  13492. if (!flag) {
  13493. ret = wc_HmacFinal(&hmac, NULL);
  13494. if (ret != BAD_FUNC_ARG) {
  13495. flag = WOLFSSL_FATAL_ERROR;
  13496. }
  13497. }
  13498. #endif
  13499. wc_HmacFree(&hmac);
  13500. res = TEST_RES_CHECK(flag == 0);
  13501. #endif
  13502. return res;
  13503. } /* END test_wc_Sha224HmacFinal */
  13504. /*
  13505. * Testing wc_HmacFinal() with SHA256
  13506. */
  13507. static int test_wc_Sha256HmacFinal(void)
  13508. {
  13509. int res = TEST_SKIPPED;
  13510. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13511. Hmac hmac;
  13512. byte hash[WC_SHA256_DIGEST_SIZE];
  13513. testVector a;
  13514. int ret;
  13515. int flag = 0;
  13516. const char* key;
  13517. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13518. "\x0b\x0b\x0b";
  13519. a.input = "Hi There";
  13520. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13521. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13522. "\xcf\xf7";
  13523. a.inLen = XSTRLEN(a.input);
  13524. a.outLen = XSTRLEN(a.output);
  13525. flag = 0;
  13526. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13527. if (ret != 0)
  13528. return TEST_FAIL;
  13529. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  13530. if (ret != 0) {
  13531. flag = ret;
  13532. }
  13533. if (!flag) {
  13534. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13535. if (ret != 0) {
  13536. flag = ret;
  13537. }
  13538. }
  13539. if (!flag) {
  13540. ret = wc_HmacFinal(&hmac, hash);
  13541. if (ret != 0) {
  13542. flag = ret;
  13543. }
  13544. }
  13545. if (!flag) {
  13546. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  13547. flag = WOLFSSL_FATAL_ERROR;
  13548. }
  13549. }
  13550. /* Try bad parameters. */
  13551. if (!flag) {
  13552. ret = wc_HmacFinal(NULL, hash);
  13553. if (ret != BAD_FUNC_ARG) {
  13554. flag = WOLFSSL_FATAL_ERROR;
  13555. }
  13556. }
  13557. #ifndef HAVE_FIPS
  13558. if (!flag) {
  13559. ret = wc_HmacFinal(&hmac, NULL);
  13560. if (ret != BAD_FUNC_ARG) {
  13561. flag = WOLFSSL_FATAL_ERROR;
  13562. }
  13563. }
  13564. #endif
  13565. wc_HmacFree(&hmac);
  13566. res = TEST_RES_CHECK(flag == 0);
  13567. #endif
  13568. return res;
  13569. } /* END test_wc_Sha256HmacFinal */
  13570. /*
  13571. * Testing wc_HmacFinal() with SHA384
  13572. */
  13573. static int test_wc_Sha384HmacFinal(void)
  13574. {
  13575. int res = TEST_SKIPPED;
  13576. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13577. Hmac hmac;
  13578. byte hash[WC_SHA384_DIGEST_SIZE];
  13579. testVector a;
  13580. int ret;
  13581. int flag = 0;
  13582. const char* key;
  13583. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13584. "\x0b\x0b\x0b";
  13585. a.input = "Hi There";
  13586. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13587. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13588. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13589. "\xfa\x9c\xb6";
  13590. a.inLen = XSTRLEN(a.input);
  13591. a.outLen = XSTRLEN(a.output);
  13592. flag = 0;
  13593. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13594. if (ret != 0)
  13595. return ret;
  13596. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  13597. if (ret != 0) {
  13598. flag = ret;
  13599. }
  13600. if (!flag) {
  13601. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13602. if (ret != 0) {
  13603. flag = ret;
  13604. }
  13605. }
  13606. if (!flag) {
  13607. ret = wc_HmacFinal(&hmac, hash);
  13608. if (ret != 0) {
  13609. flag = ret;
  13610. }
  13611. }
  13612. if (!flag) {
  13613. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  13614. flag = WOLFSSL_FATAL_ERROR;
  13615. }
  13616. }
  13617. /* Try bad parameters. */
  13618. if (!flag) {
  13619. ret = wc_HmacFinal(NULL, hash);
  13620. if (ret != BAD_FUNC_ARG) {
  13621. flag = WOLFSSL_FATAL_ERROR;
  13622. }
  13623. }
  13624. #ifndef HAVE_FIPS
  13625. if (!flag) {
  13626. ret = wc_HmacFinal(&hmac, NULL);
  13627. if (ret != BAD_FUNC_ARG) {
  13628. flag = WOLFSSL_FATAL_ERROR;
  13629. }
  13630. }
  13631. #endif
  13632. wc_HmacFree(&hmac);
  13633. res = TEST_RES_CHECK(flag == 0);
  13634. #endif
  13635. return res;
  13636. } /* END test_wc_Sha384HmacFinal */
  13637. /*
  13638. * Testing wc_InitCmac()
  13639. */
  13640. static int test_wc_InitCmac(void)
  13641. {
  13642. int res = TEST_SKIPPED;
  13643. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13644. Cmac cmac1, cmac2, cmac3;
  13645. /* AES 128 key. */
  13646. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13647. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13648. /* AES 192 key. */
  13649. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13650. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13651. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13652. /* AES 256 key. */
  13653. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13654. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13655. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13656. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13657. word32 key1Sz = (word32)sizeof(key1) - 1;
  13658. word32 key2Sz = (word32)sizeof(key2) - 1;
  13659. word32 key3Sz = (word32)sizeof(key3) - 1;
  13660. int type = WC_CMAC_AES;
  13661. int ret = 0;
  13662. #ifdef WOLFSSL_AES_128
  13663. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  13664. #endif
  13665. #ifdef WOLFSSL_AES_192
  13666. if (ret == 0) {
  13667. wc_AesFree(&cmac1.aes);
  13668. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  13669. }
  13670. #endif
  13671. #ifdef WOLFSSL_AES_256
  13672. if (ret == 0) {
  13673. wc_AesFree(&cmac2.aes);
  13674. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  13675. }
  13676. #endif
  13677. /* Test bad args. */
  13678. if (ret == 0) {
  13679. wc_AesFree(&cmac3.aes);
  13680. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  13681. if (ret == BAD_FUNC_ARG) {
  13682. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  13683. }
  13684. if (ret == BAD_FUNC_ARG) {
  13685. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  13686. }
  13687. if (ret == BAD_FUNC_ARG) {
  13688. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  13689. }
  13690. if (ret == BAD_FUNC_ARG) {
  13691. ret = 0;
  13692. }
  13693. else {
  13694. ret = WOLFSSL_FATAL_ERROR;
  13695. }
  13696. }
  13697. (void)key1;
  13698. (void)key1Sz;
  13699. (void)key2;
  13700. (void)key2Sz;
  13701. (void)cmac1;
  13702. (void)cmac2;
  13703. res = TEST_RES_CHECK(ret == 0);
  13704. #endif
  13705. return res;
  13706. } /* END test_wc_InitCmac */
  13707. /*
  13708. * Testing wc_CmacUpdate()
  13709. */
  13710. static int test_wc_CmacUpdate(void)
  13711. {
  13712. int res = TEST_SKIPPED;
  13713. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13714. Cmac cmac;
  13715. byte key[] =
  13716. {
  13717. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13718. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13719. };
  13720. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13721. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13722. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13723. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13724. "\xf4";
  13725. word32 inSz = (word32)sizeof(in) - 1;
  13726. word32 keySz = (word32)sizeof(key);
  13727. int type = WC_CMAC_AES;
  13728. int ret = 0;
  13729. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13730. if (ret != 0) {
  13731. return ret;
  13732. }
  13733. ret = wc_CmacUpdate(&cmac, in, inSz);
  13734. /* Test bad args. */
  13735. if (ret == 0) {
  13736. ret = wc_CmacUpdate(NULL, in, inSz);
  13737. if (ret == BAD_FUNC_ARG) {
  13738. ret = wc_CmacUpdate(&cmac, NULL, 30);
  13739. }
  13740. if (ret == BAD_FUNC_ARG) {
  13741. ret = 0;
  13742. }
  13743. else if (ret == 0) {
  13744. ret = WOLFSSL_FATAL_ERROR;
  13745. }
  13746. wc_AesFree(&cmac.aes);
  13747. }
  13748. res = TEST_RES_CHECK(ret == 0);
  13749. #endif
  13750. return res;
  13751. } /* END test_wc_CmacUpdate */
  13752. /*
  13753. * Testing wc_CmacFinal()
  13754. */
  13755. static int test_wc_CmacFinal(void)
  13756. {
  13757. int res = TEST_SKIPPED;
  13758. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13759. Cmac cmac;
  13760. byte key[] =
  13761. {
  13762. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13763. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13764. };
  13765. byte msg[] =
  13766. {
  13767. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13768. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13769. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13770. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13771. 0xf4
  13772. };
  13773. /* Test vectors from CMACGenAES128.rsp from
  13774. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13775. * Per RFC4493 truncation of lsb is possible.
  13776. */
  13777. byte expMac[] =
  13778. {
  13779. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13780. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13781. };
  13782. byte mac[AES_BLOCK_SIZE];
  13783. word32 msgSz = (word32)sizeof(msg);
  13784. word32 keySz = (word32)sizeof(key);
  13785. word32 macSz = sizeof(mac);
  13786. word32 badMacSz = 17;
  13787. int expMacSz = sizeof(expMac);
  13788. int type = WC_CMAC_AES;
  13789. int ret = 0;
  13790. XMEMSET(mac, 0, macSz);
  13791. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13792. if (ret != 0) {
  13793. return ret;
  13794. }
  13795. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13796. if (ret == 0) {
  13797. ret = wc_CmacFinal(&cmac, mac, &macSz);
  13798. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13799. ret = WOLFSSL_FATAL_ERROR;
  13800. }
  13801. /* Pass in bad args. */
  13802. if (ret == 0) {
  13803. ret = wc_CmacFinal(NULL, mac, &macSz);
  13804. if (ret == BAD_FUNC_ARG) {
  13805. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  13806. }
  13807. if (ret == BAD_FUNC_ARG) {
  13808. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  13809. if (ret == BUFFER_E) {
  13810. ret = 0;
  13811. }
  13812. }
  13813. else if (ret == 0) {
  13814. ret = WOLFSSL_FATAL_ERROR;
  13815. }
  13816. }
  13817. }
  13818. res = TEST_RES_CHECK(ret == 0);
  13819. #endif
  13820. return res;
  13821. } /* END test_wc_CmacFinal */
  13822. /*
  13823. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13824. */
  13825. static int test_wc_AesCmacGenerate(void)
  13826. {
  13827. int res = TEST_SKIPPED;
  13828. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13829. Cmac cmac;
  13830. byte key[] =
  13831. {
  13832. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13833. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13834. };
  13835. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13836. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13837. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13838. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13839. byte mac[AES_BLOCK_SIZE];
  13840. word32 keySz = sizeof(key);
  13841. word32 macSz = sizeof(mac);
  13842. word32 msgSz = sizeof(msg) - 1;
  13843. word32 expMacSz = sizeof(expMac) - 1;
  13844. int type = WC_CMAC_AES;
  13845. int ret = 0;
  13846. XMEMSET(mac, 0, macSz);
  13847. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13848. if (ret != 0) {
  13849. return ret;
  13850. }
  13851. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13852. if (ret != 0) {
  13853. return ret;
  13854. }
  13855. else {
  13856. wc_AesFree(&cmac.aes);
  13857. }
  13858. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  13859. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13860. ret = WOLFSSL_FATAL_ERROR;
  13861. }
  13862. /* Pass in bad args. */
  13863. if (ret == 0) {
  13864. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  13865. if (ret == BAD_FUNC_ARG) {
  13866. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  13867. }
  13868. if (ret == BAD_FUNC_ARG) {
  13869. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  13870. }
  13871. if (ret == BAD_FUNC_ARG) {
  13872. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  13873. }
  13874. if (ret == BAD_FUNC_ARG) {
  13875. ret = 0;
  13876. }
  13877. else if (ret == 0) {
  13878. ret = WOLFSSL_FATAL_ERROR;
  13879. }
  13880. }
  13881. if (ret == 0) {
  13882. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  13883. /* Test bad args. */
  13884. if (ret == 0) {
  13885. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  13886. if (ret == BAD_FUNC_ARG) {
  13887. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  13888. }
  13889. if (ret == BAD_FUNC_ARG) {
  13890. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  13891. }
  13892. if (ret == BAD_FUNC_ARG) {
  13893. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  13894. }
  13895. if (ret == BAD_FUNC_ARG) {
  13896. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  13897. }
  13898. if (ret == BAD_FUNC_ARG) {
  13899. ret = 0;
  13900. }
  13901. else if (ret == 0) {
  13902. ret = WOLFSSL_FATAL_ERROR;
  13903. }
  13904. }
  13905. }
  13906. res = TEST_RES_CHECK(ret == 0);
  13907. #endif
  13908. return res;
  13909. } /* END test_wc_AesCmacGenerate */
  13910. /*
  13911. * Testing streaming AES-GCM API.
  13912. */
  13913. static int test_wc_AesGcmStream(void)
  13914. {
  13915. int res = TEST_SKIPPED;
  13916. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13917. defined(WOLFSSL_AESGCM_STREAM)
  13918. int ret = 0;
  13919. int i;
  13920. WC_RNG rng[1];
  13921. Aes aesEnc[1];
  13922. Aes aesDec[1];
  13923. byte tag[AES_BLOCK_SIZE];
  13924. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13925. byte out[AES_BLOCK_SIZE * 3 + 2];
  13926. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13927. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13928. byte key[AES_128_KEY_SIZE] = { 0, };
  13929. byte iv[AES_IV_SIZE] = { 1, };
  13930. byte ivOut[AES_IV_SIZE];
  13931. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13932. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13933. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13934. };
  13935. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13936. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13937. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13938. };
  13939. static const byte expTag[AES_BLOCK_SIZE] = {
  13940. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13941. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13942. };
  13943. /* Create a random for generating IV/nonce. */
  13944. AssertIntEQ(wc_InitRng(rng), 0);
  13945. /* Initialize data structures. */
  13946. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13947. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13948. /* BadParameters to streaming init. */
  13949. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13950. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13951. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13952. BAD_FUNC_ARG);
  13953. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13954. BAD_FUNC_ARG);
  13955. /* Bad parameters to encrypt update. */
  13956. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13957. BAD_FUNC_ARG);
  13958. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13959. BAD_FUNC_ARG);
  13960. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13961. BAD_FUNC_ARG);
  13962. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13963. BAD_FUNC_ARG);
  13964. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13965. BAD_FUNC_ARG);
  13966. /* Bad parameters to decrypt update. */
  13967. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13968. BAD_FUNC_ARG);
  13969. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13970. BAD_FUNC_ARG);
  13971. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13972. BAD_FUNC_ARG);
  13973. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13974. BAD_FUNC_ARG);
  13975. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13976. BAD_FUNC_ARG);
  13977. /* Bad parameters to encrypt final. */
  13978. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13979. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13980. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13981. BAD_FUNC_ARG);
  13982. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13983. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13984. BAD_FUNC_ARG);
  13985. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13986. BAD_FUNC_ARG);
  13987. /* Bad parameters to decrypt final. */
  13988. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13989. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13990. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13991. BAD_FUNC_ARG);
  13992. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13993. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13994. BAD_FUNC_ARG);
  13995. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13996. BAD_FUNC_ARG);
  13997. /* Check calling final before setting key fails. */
  13998. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13999. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14000. /* Check calling update before setting key else fails. */
  14001. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14002. MISSING_KEY);
  14003. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14004. MISSING_KEY);
  14005. /* Set key but not IV. */
  14006. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14007. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14008. /* Check calling final before setting IV fails. */
  14009. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14010. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14011. /* Check calling update before setting IV else fails. */
  14012. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14013. MISSING_IV);
  14014. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14015. MISSING_IV);
  14016. /* Set IV using fixed part IV and external IV APIs. */
  14017. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14018. rng), 0);
  14019. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14020. GCM_NONCE_MID_SZ), 0);
  14021. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14022. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14023. /* Encrypt and decrypt data. */
  14024. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14025. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14026. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14027. /* Finalize and check tag matches. */
  14028. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14029. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14030. /* Set key and IV through streaming init API. */
  14031. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14032. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14033. /* Encrypt/decrypt one block and AAD of one block. */
  14034. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14035. AES_BLOCK_SIZE), 0);
  14036. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14037. AES_BLOCK_SIZE), 0);
  14038. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14039. /* Finalize and check tag matches. */
  14040. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14041. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14042. /* Set key and IV through streaming init API. */
  14043. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14044. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14045. /* No data to encrypt/decrypt one byte of AAD. */
  14046. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14047. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14048. /* Finalize and check tag matches. */
  14049. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14050. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14051. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14052. /* Set key and IV through streaming init API. */
  14053. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14054. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14055. /* Encrypt/decrypt one byte and no AAD. */
  14056. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14057. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14058. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14059. /* Finalize and check tag matches. */
  14060. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14061. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14062. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14063. /* Set key and IV through streaming init API. */
  14064. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14065. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14066. /* Encryption AES is one byte at a time */
  14067. for (i = 0; i < (int)sizeof(aad); i++) {
  14068. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14069. 0);
  14070. }
  14071. for (i = 0; i < (int)sizeof(in); i++) {
  14072. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14073. 0);
  14074. }
  14075. /* Decryption AES is two bytes at a time */
  14076. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14077. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14078. 0);
  14079. }
  14080. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14081. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14082. 0), 0);
  14083. }
  14084. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14085. /* Finalize and check tag matches. */
  14086. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14087. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14088. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14089. /* Check streaming encryption can be decrypted with one shot. */
  14090. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14091. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14092. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14093. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14094. wc_AesFree(aesEnc);
  14095. wc_AesFree(aesDec);
  14096. wc_FreeRng(rng);
  14097. res = TEST_RES_CHECK(ret == 0);
  14098. #endif
  14099. return res;
  14100. } /* END test_wc_AesGcmStream */
  14101. /*
  14102. * unit test for wc_Des3_SetIV()
  14103. */
  14104. static int test_wc_Des3_SetIV(void)
  14105. {
  14106. int res = TEST_SKIPPED;
  14107. #ifndef NO_DES3
  14108. Des3 des;
  14109. int ret = 0;
  14110. const byte key[] =
  14111. {
  14112. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14113. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14114. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14115. };
  14116. const byte iv[] =
  14117. {
  14118. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14119. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14120. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14121. };
  14122. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14123. if (ret != 0)
  14124. return ret;
  14125. /* DES_ENCRYPTION or DES_DECRYPTION */
  14126. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14127. if (ret == 0) {
  14128. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14129. ret = WOLFSSL_FATAL_ERROR;
  14130. }
  14131. }
  14132. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14133. /* Test explicitly wc_Des3_SetIV() */
  14134. if (ret == 0) {
  14135. ret = wc_Des3_SetIV(NULL, iv);
  14136. if (ret == BAD_FUNC_ARG) {
  14137. ret = wc_Des3_SetIV(&des, NULL);
  14138. }
  14139. else if (ret == 0) {
  14140. ret = WOLFSSL_FATAL_ERROR;
  14141. }
  14142. }
  14143. #endif
  14144. wc_Des3Free(&des);
  14145. res = TEST_RES_CHECK(ret == 0);
  14146. #endif
  14147. return res;
  14148. } /* END test_wc_Des3_SetIV */
  14149. /*
  14150. * unit test for wc_Des3_SetKey()
  14151. */
  14152. static int test_wc_Des3_SetKey(void)
  14153. {
  14154. int res = TEST_SKIPPED;
  14155. #ifndef NO_DES3
  14156. Des3 des;
  14157. int ret = 0;
  14158. const byte key[] =
  14159. {
  14160. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14161. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14162. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14163. };
  14164. const byte iv[] =
  14165. {
  14166. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14167. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14168. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14169. };
  14170. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14171. if (ret != 0)
  14172. return ret;
  14173. /* DES_ENCRYPTION or DES_DECRYPTION */
  14174. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14175. if (ret == 0) {
  14176. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14177. ret = WOLFSSL_FATAL_ERROR;
  14178. }
  14179. }
  14180. /* Test bad args. */
  14181. if (ret == 0) {
  14182. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  14183. if (ret == BAD_FUNC_ARG) {
  14184. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  14185. }
  14186. if (ret == BAD_FUNC_ARG) {
  14187. ret = wc_Des3_SetKey(&des, key, iv, -1);
  14188. }
  14189. if (ret == BAD_FUNC_ARG) {
  14190. /* Default case. Should return 0. */
  14191. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  14192. }
  14193. } /* END if ret != 0 */
  14194. wc_Des3Free(&des);
  14195. res = TEST_RES_CHECK(ret == 0);
  14196. #endif
  14197. return res;
  14198. } /* END test_wc_Des3_SetKey */
  14199. /*
  14200. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14201. */
  14202. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14203. {
  14204. int res = TEST_SKIPPED;
  14205. #ifndef NO_DES3
  14206. Des3 des;
  14207. int ret = 0;
  14208. byte cipher[24];
  14209. byte plain[24];
  14210. const byte key[] =
  14211. {
  14212. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14213. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14214. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14215. };
  14216. const byte iv[] =
  14217. {
  14218. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14219. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14220. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14221. };
  14222. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14223. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14224. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14225. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14226. };
  14227. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14228. if (ret != 0)
  14229. return ret;
  14230. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14231. if (ret == 0) {
  14232. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  14233. if (ret == 0) {
  14234. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  14235. }
  14236. if (ret == 0) {
  14237. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  14238. }
  14239. }
  14240. if (ret == 0) {
  14241. if (XMEMCMP(plain, vector, 24) != 0) {
  14242. ret = WOLFSSL_FATAL_ERROR;
  14243. }
  14244. }
  14245. /* Pass in bad args. */
  14246. if (ret == 0) {
  14247. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  14248. if (ret == BAD_FUNC_ARG) {
  14249. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  14250. }
  14251. if (ret == BAD_FUNC_ARG) {
  14252. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  14253. }
  14254. if (ret != BAD_FUNC_ARG) {
  14255. ret = WOLFSSL_FATAL_ERROR;
  14256. }
  14257. else {
  14258. ret = 0;
  14259. }
  14260. }
  14261. if (ret == 0) {
  14262. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  14263. if (ret == BAD_FUNC_ARG) {
  14264. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  14265. }
  14266. if (ret == BAD_FUNC_ARG) {
  14267. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  14268. }
  14269. if (ret != BAD_FUNC_ARG) {
  14270. ret = WOLFSSL_FATAL_ERROR;
  14271. }
  14272. else {
  14273. ret = 0;
  14274. }
  14275. }
  14276. wc_Des3Free(&des);
  14277. res = TEST_RES_CHECK(ret == 0);
  14278. #endif
  14279. return res;
  14280. } /* END wc_Des3_CbcEncrypt */
  14281. /*
  14282. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14283. */
  14284. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14285. {
  14286. int res = TEST_SKIPPED;
  14287. #ifndef NO_DES3
  14288. int ret = 0;
  14289. word32 vectorSz, cipherSz;
  14290. byte cipher[24];
  14291. byte plain[24];
  14292. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14293. {
  14294. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14295. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14296. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14297. };
  14298. byte key[] =
  14299. {
  14300. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14301. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14302. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14303. };
  14304. byte iv[] =
  14305. {
  14306. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14307. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14308. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14309. };
  14310. vectorSz = sizeof(byte) * 24;
  14311. cipherSz = sizeof(byte) * 24;
  14312. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  14313. if (ret == 0) {
  14314. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  14315. if (ret == 0) {
  14316. if (XMEMCMP(plain, vector, 24) != 0) {
  14317. ret = WOLFSSL_FATAL_ERROR;
  14318. }
  14319. }
  14320. }
  14321. /* pass in bad args. */
  14322. if (ret == 0) {
  14323. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  14324. if (ret == BAD_FUNC_ARG) {
  14325. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  14326. }
  14327. if (ret == BAD_FUNC_ARG) {
  14328. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  14329. }
  14330. if (ret == BAD_FUNC_ARG) {
  14331. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  14332. key, NULL);
  14333. }
  14334. else {
  14335. /* Return code catch. */
  14336. ret = WOLFSSL_FAILURE;
  14337. }
  14338. }
  14339. if (ret == 0) {
  14340. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  14341. if (ret == BAD_FUNC_ARG) {
  14342. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  14343. }
  14344. if (ret == BAD_FUNC_ARG) {
  14345. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  14346. }
  14347. if (ret == BAD_FUNC_ARG) {
  14348. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  14349. }
  14350. else {
  14351. ret = WOLFSSL_FAILURE;
  14352. }
  14353. }
  14354. res = TEST_RES_CHECK(ret == 0);
  14355. #endif
  14356. return res;
  14357. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14358. /*
  14359. * Unit test for wc_Des3_EcbEncrypt
  14360. */
  14361. static int test_wc_Des3_EcbEncrypt(void)
  14362. {
  14363. int res = TEST_SKIPPED;
  14364. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14365. Des3 des;
  14366. int ret = 0;
  14367. byte cipher[24];
  14368. word32 cipherSz = sizeof(cipher);
  14369. const byte key[] =
  14370. {
  14371. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14372. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14373. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14374. };
  14375. const byte iv[] =
  14376. {
  14377. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14378. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14379. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14380. };
  14381. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14382. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14383. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14384. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14385. };
  14386. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14387. if (ret != 0) {
  14388. return ret;
  14389. }
  14390. if (ret == 0 ) {
  14391. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14392. }
  14393. /* Bad Cases */
  14394. if (ret == 0) {
  14395. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  14396. if (ret == BAD_FUNC_ARG) {
  14397. ret = 0;
  14398. }
  14399. }
  14400. if (ret == 0) {
  14401. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  14402. if (ret == BAD_FUNC_ARG) {
  14403. ret = 0;
  14404. }
  14405. }
  14406. if (ret == 0) {
  14407. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  14408. if (ret == BAD_FUNC_ARG) {
  14409. ret = 0;
  14410. }
  14411. }
  14412. if (ret == 0) {
  14413. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  14414. if (ret == BAD_FUNC_ARG) {
  14415. ret = 0;
  14416. }
  14417. }
  14418. if (ret == 0) {
  14419. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  14420. if (ret == BAD_FUNC_ARG) {
  14421. ret = 0;
  14422. }
  14423. }
  14424. /* Good Cases */
  14425. if (ret == 0) {
  14426. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  14427. }
  14428. wc_Des3Free(&des);
  14429. res = TEST_RES_CHECK(ret == 0);
  14430. #endif
  14431. return res;
  14432. } /* END test_wc_Des3_EcbEncrypt */
  14433. /*
  14434. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14435. */
  14436. static int test_wc_Chacha_SetKey(void)
  14437. {
  14438. int res = TEST_SKIPPED;
  14439. #ifdef HAVE_CHACHA
  14440. ChaCha ctx;
  14441. const byte key[] =
  14442. {
  14443. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14444. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14445. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14446. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14447. };
  14448. byte cipher[128];
  14449. int ret = 0;
  14450. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  14451. /* Test bad args. */
  14452. if (ret == 0) {
  14453. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  14454. if (ret == BAD_FUNC_ARG) {
  14455. ret = wc_Chacha_SetKey(&ctx, key, 18);
  14456. }
  14457. if (ret == BAD_FUNC_ARG) {
  14458. ret = 0;
  14459. }
  14460. else {
  14461. ret = WOLFSSL_FATAL_ERROR;
  14462. }
  14463. }
  14464. if (ret == 0) {
  14465. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  14466. }
  14467. if (ret == 0) {
  14468. /* Test bad args. */
  14469. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  14470. if (ret == BAD_FUNC_ARG) {
  14471. ret = 0;
  14472. }
  14473. else {
  14474. ret = WOLFSSL_FAILURE;
  14475. }
  14476. }
  14477. res = TEST_RES_CHECK(ret == 0);
  14478. #endif
  14479. return res;
  14480. } /* END test_wc_Chacha_SetKey */
  14481. /*
  14482. * unit test for wc_Poly1305SetKey()
  14483. */
  14484. static int test_wc_Poly1305SetKey(void)
  14485. {
  14486. int res = TEST_SKIPPED;
  14487. #ifdef HAVE_POLY1305
  14488. Poly1305 ctx;
  14489. const byte key[] =
  14490. {
  14491. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14492. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14493. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14494. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14495. };
  14496. int ret = 0;
  14497. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  14498. /* Test bad args. */
  14499. if (ret == 0) {
  14500. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  14501. if (ret == BAD_FUNC_ARG) {
  14502. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  14503. }
  14504. if (ret == BAD_FUNC_ARG) {
  14505. ret = wc_Poly1305SetKey(&ctx, key, 18);
  14506. }
  14507. if (ret == BAD_FUNC_ARG) {
  14508. ret = 0;
  14509. }
  14510. else {
  14511. ret = WOLFSSL_FATAL_ERROR;
  14512. }
  14513. }
  14514. res = TEST_RES_CHECK(ret == 0);
  14515. #endif
  14516. return res;
  14517. } /* END test_wc_Poly1305_SetKey() */
  14518. /*
  14519. * Testing wc_Chacha_Process()
  14520. */
  14521. static int test_wc_Chacha_Process(void)
  14522. {
  14523. int res = TEST_SKIPPED;
  14524. #ifdef HAVE_CHACHA
  14525. ChaCha enc, dec;
  14526. byte cipher[128];
  14527. byte plain[128];
  14528. const byte key[] =
  14529. {
  14530. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14531. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14532. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14533. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14534. };
  14535. const char* input = "Everybody gets Friday off.";
  14536. word32 keySz = sizeof(key)/sizeof(byte);
  14537. unsigned long int inlen = XSTRLEN(input);
  14538. int ret = 0;
  14539. /*Initialize stack varialbes.*/
  14540. XMEMSET(cipher, 0, 128);
  14541. XMEMSET(plain, 0, 128);
  14542. ret = wc_Chacha_SetKey(&enc, key, keySz);
  14543. AssertIntEQ(ret, 0);
  14544. ret = wc_Chacha_SetKey(&dec, key, keySz);
  14545. AssertIntEQ(ret, 0);
  14546. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  14547. AssertIntEQ(ret, 0);
  14548. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  14549. AssertIntEQ(ret, 0);
  14550. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  14551. AssertIntEQ(ret, 0);
  14552. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  14553. AssertIntEQ(ret, 0);
  14554. ret = XMEMCMP(input, plain, (int)inlen);
  14555. AssertIntEQ(ret, 0);
  14556. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14557. /* test checking and using leftovers, currently just in C code */
  14558. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  14559. AssertIntEQ(ret, 0);
  14560. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  14561. AssertIntEQ(ret, 0);
  14562. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  14563. AssertIntEQ(ret, 0);
  14564. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14565. (byte*)input + (inlen - 2), 2);
  14566. AssertIntEQ(ret, 0);
  14567. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  14568. AssertIntEQ(ret, 0);
  14569. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14570. (byte*)input + (inlen - 2), 2);
  14571. AssertIntEQ(ret, 0);
  14572. ret = XMEMCMP(input, plain, (int)inlen);
  14573. AssertIntEQ(ret, 0);
  14574. /* check edge cases with counter increment */
  14575. {
  14576. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14577. const byte expected[] = {
  14578. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14579. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14580. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14581. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14582. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14583. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14584. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14585. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14586. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14587. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14588. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14589. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14590. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14591. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14592. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14593. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14594. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14595. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14596. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14597. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14598. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14599. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14600. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14601. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14602. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14603. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14604. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14605. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14606. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14607. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14608. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14609. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14610. };
  14611. const byte iv2[] = {
  14612. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14613. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14614. };
  14615. byte input2[256];
  14616. int i;
  14617. for (i = 0; i < 256; i++)
  14618. input2[i] = i;
  14619. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  14620. AssertIntEQ(ret, 0);
  14621. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  14622. AssertIntEQ(ret, 0);
  14623. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14624. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  14625. AssertIntEQ(ret, 0);
  14626. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14627. /* partial */
  14628. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  14629. AssertIntEQ(ret, 0);
  14630. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14631. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  14632. AssertIntEQ(ret, 0);
  14633. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14634. }
  14635. #endif
  14636. /* Test bad args. */
  14637. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  14638. AssertIntEQ(ret, BAD_FUNC_ARG);
  14639. if (ret == BAD_FUNC_ARG) {
  14640. ret = 0;
  14641. }
  14642. res = TEST_RES_CHECK(ret == 0);
  14643. #endif
  14644. return res;
  14645. } /* END test_wc_Chacha_Process */
  14646. /*
  14647. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14648. */
  14649. static int test_wc_ChaCha20Poly1305_aead(void)
  14650. {
  14651. int res = TEST_SKIPPED;
  14652. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14653. const byte key[] = {
  14654. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14655. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14656. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14657. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14658. };
  14659. const byte plaintext[] = {
  14660. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14661. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14662. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14663. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14664. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14665. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14666. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14667. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14668. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14669. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14670. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14671. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14672. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14673. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14674. 0x74, 0x2e
  14675. };
  14676. const byte iv[] = {
  14677. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14678. 0x44, 0x45, 0x46, 0x47
  14679. };
  14680. const byte aad[] = { /* additional data */
  14681. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14682. 0xc4, 0xc5, 0xc6, 0xc7
  14683. };
  14684. const byte cipher[] = { /* expected output from operation */
  14685. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14686. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14687. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14688. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14689. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14690. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14691. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14692. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14693. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14694. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14695. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14696. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14697. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14698. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14699. 0x61, 0x16
  14700. };
  14701. const byte authTag[] = { /* expected output from operation */
  14702. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14703. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14704. };
  14705. byte generatedCiphertext[272];
  14706. byte generatedPlaintext[272];
  14707. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14708. int ret = 0;
  14709. /* Initialize stack variables. */
  14710. XMEMSET(generatedCiphertext, 0, 272);
  14711. XMEMSET(generatedPlaintext, 0, 272);
  14712. /* Test Encrypt */
  14713. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  14714. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14715. AssertIntEQ(ret, 0);
  14716. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  14717. AssertIntEQ(ret, 0);
  14718. /* Test bad args. */
  14719. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  14720. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14721. AssertIntEQ(ret, BAD_FUNC_ARG);
  14722. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14723. plaintext, sizeof(plaintext),
  14724. generatedCiphertext, generatedAuthTag);
  14725. AssertIntEQ(ret, BAD_FUNC_ARG);
  14726. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14727. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14728. AssertIntEQ(ret, BAD_FUNC_ARG);
  14729. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14730. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14731. AssertIntEQ(ret, BAD_FUNC_ARG);
  14732. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14733. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  14734. AssertIntEQ(ret, BAD_FUNC_ARG);
  14735. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14736. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  14737. if (ret == BAD_FUNC_ARG) {
  14738. ret = 0;
  14739. (void)ret; /* suppress never read */
  14740. }
  14741. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14742. sizeof(cipher), authTag, generatedPlaintext);
  14743. AssertIntEQ(ret, 0);
  14744. ret = XMEMCMP(generatedPlaintext, plaintext,
  14745. sizeof(plaintext)/sizeof(byte));
  14746. AssertIntEQ(ret, 0);
  14747. /* Test bad args. */
  14748. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14749. sizeof(cipher), authTag, generatedPlaintext);
  14750. AssertIntEQ(ret, BAD_FUNC_ARG);
  14751. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14752. cipher, sizeof(cipher), authTag, generatedPlaintext);
  14753. AssertIntEQ(ret, BAD_FUNC_ARG);
  14754. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14755. sizeof(cipher), authTag, generatedPlaintext);
  14756. AssertIntEQ(ret, BAD_FUNC_ARG);
  14757. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14758. sizeof(cipher), NULL, generatedPlaintext);
  14759. AssertIntEQ(ret, BAD_FUNC_ARG);
  14760. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14761. sizeof(cipher), authTag, NULL);
  14762. AssertIntEQ(ret, BAD_FUNC_ARG);
  14763. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14764. sizeof(cipher), authTag, generatedPlaintext);
  14765. AssertIntEQ(ret, BAD_FUNC_ARG);
  14766. if (ret == BAD_FUNC_ARG) {
  14767. ret = 0;
  14768. }
  14769. res = TEST_RES_CHECK(ret == 0);
  14770. #endif
  14771. return res;
  14772. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  14773. /*
  14774. * Testing function for wc_Rc2SetKey().
  14775. */
  14776. static int test_wc_Rc2SetKey(void)
  14777. {
  14778. int res = TEST_SKIPPED;
  14779. #ifdef WC_RC2
  14780. Rc2 rc2;
  14781. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14782. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14783. int ret = 0;
  14784. /* valid key and IV */
  14785. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14786. iv, 40);
  14787. if (ret == 0) {
  14788. /* valid key, no IV */
  14789. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14790. NULL, 40);
  14791. }
  14792. /* bad arguments */
  14793. if (ret == 0) {
  14794. /* null Rc2 struct */
  14795. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14796. iv, 40);
  14797. if (ret == BAD_FUNC_ARG) {
  14798. ret = 0;
  14799. }
  14800. }
  14801. if (ret == 0) {
  14802. /* null key */
  14803. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14804. iv, 40);
  14805. if (ret == BAD_FUNC_ARG) {
  14806. ret = 0;
  14807. }
  14808. }
  14809. if (ret == 0) {
  14810. /* key size == 0 */
  14811. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  14812. if (ret == WC_KEY_SIZE_E) {
  14813. ret = 0;
  14814. }
  14815. }
  14816. if (ret == 0) {
  14817. /* key size > 128 */
  14818. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  14819. if (ret == WC_KEY_SIZE_E) {
  14820. ret = 0;
  14821. }
  14822. }
  14823. if (ret == 0) {
  14824. /* effective bits == 0 */
  14825. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14826. iv, 0);
  14827. if (ret == WC_KEY_SIZE_E) {
  14828. ret = 0;
  14829. }
  14830. }
  14831. if (ret == 0) {
  14832. /* effective bits > 1024 */
  14833. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14834. iv, 1025);
  14835. if (ret == WC_KEY_SIZE_E) {
  14836. ret = 0;
  14837. }
  14838. }
  14839. res = TEST_RES_CHECK(ret == 0);
  14840. #endif
  14841. return res;
  14842. } /* END test_wc_Rc2SetKey */
  14843. /*
  14844. * Testing function for wc_Rc2SetIV().
  14845. */
  14846. static int test_wc_Rc2SetIV(void)
  14847. {
  14848. int res = TEST_SKIPPED;
  14849. #ifdef WC_RC2
  14850. Rc2 rc2;
  14851. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14852. int ret = 0;
  14853. /* valid IV */
  14854. ret = wc_Rc2SetIV(&rc2, iv);
  14855. if (ret == 0) {
  14856. /* valid NULL IV */
  14857. ret = wc_Rc2SetIV(&rc2, NULL);
  14858. }
  14859. /* bad arguments */
  14860. if (ret == 0) {
  14861. ret = wc_Rc2SetIV(NULL, iv);
  14862. if (ret == BAD_FUNC_ARG) {
  14863. ret = 0;
  14864. }
  14865. }
  14866. res = TEST_RES_CHECK(ret == 0);
  14867. #endif
  14868. return res;
  14869. } /* END test_wc_Rc2SetKey */
  14870. /*
  14871. * Testing function for wc_Rc2EcbEncrypt().
  14872. */
  14873. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14874. {
  14875. int res = TEST_SKIPPED;
  14876. #ifdef WC_RC2
  14877. Rc2 rc2;
  14878. int ret = 0;
  14879. int effectiveKeyBits = 63;
  14880. byte cipher[RC2_BLOCK_SIZE];
  14881. byte plain[RC2_BLOCK_SIZE];
  14882. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14883. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14884. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14885. XMEMSET(cipher, 0, sizeof(cipher));
  14886. XMEMSET(plain, 0, sizeof(plain));
  14887. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14888. NULL, effectiveKeyBits);
  14889. if (ret == 0) {
  14890. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  14891. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  14892. ret = WOLFSSL_FATAL_ERROR;
  14893. }
  14894. if (ret == 0) {
  14895. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  14896. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  14897. ret = WOLFSSL_FATAL_ERROR;
  14898. }
  14899. }
  14900. }
  14901. /* Rc2EcbEncrypt bad arguments */
  14902. if (ret == 0) {
  14903. /* null Rc2 struct */
  14904. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  14905. if (ret == BAD_FUNC_ARG) {
  14906. ret = 0;
  14907. }
  14908. }
  14909. if (ret == 0) {
  14910. /* null out buffer */
  14911. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  14912. if (ret == BAD_FUNC_ARG) {
  14913. ret = 0;
  14914. }
  14915. }
  14916. if (ret == 0) {
  14917. /* null input buffer */
  14918. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  14919. if (ret == BAD_FUNC_ARG) {
  14920. ret = 0;
  14921. }
  14922. }
  14923. if (ret == 0) {
  14924. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14925. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  14926. if (ret == BUFFER_E) {
  14927. ret = 0;
  14928. }
  14929. }
  14930. /* Rc2EcbDecrypt bad arguments */
  14931. if (ret == 0) {
  14932. /* null Rc2 struct */
  14933. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  14934. if (ret == BAD_FUNC_ARG) {
  14935. ret = 0;
  14936. }
  14937. }
  14938. if (ret == 0) {
  14939. /* null out buffer */
  14940. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  14941. if (ret == BAD_FUNC_ARG) {
  14942. ret = 0;
  14943. }
  14944. }
  14945. if (ret == 0) {
  14946. /* null input buffer */
  14947. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  14948. if (ret == BAD_FUNC_ARG) {
  14949. ret = 0;
  14950. }
  14951. }
  14952. if (ret == 0) {
  14953. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14954. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  14955. if (ret == BUFFER_E) {
  14956. ret = 0;
  14957. }
  14958. }
  14959. res = TEST_RES_CHECK(ret == 0);
  14960. #endif
  14961. return res;
  14962. } /* END test_wc_Rc2SetKey */
  14963. /*
  14964. * Testing function for wc_Rc2CbcEncrypt().
  14965. */
  14966. static int test_wc_Rc2CbcEncryptDecrypt(void)
  14967. {
  14968. int res = TEST_SKIPPED;
  14969. #ifdef WC_RC2
  14970. Rc2 rc2;
  14971. int ret = 0;
  14972. int effectiveKeyBits = 63;
  14973. byte cipher[RC2_BLOCK_SIZE*2];
  14974. byte plain[RC2_BLOCK_SIZE*2];
  14975. /* vector taken from test.c */
  14976. byte key[] = {
  14977. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14978. };
  14979. byte iv[] = {
  14980. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14981. };
  14982. byte input[] = {
  14983. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14984. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14985. };
  14986. byte output[] = {
  14987. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  14988. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  14989. };
  14990. XMEMSET(cipher, 0, sizeof(cipher));
  14991. XMEMSET(plain, 0, sizeof(plain));
  14992. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14993. iv, effectiveKeyBits);
  14994. if (ret == 0) {
  14995. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  14996. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  14997. ret = WOLFSSL_FATAL_ERROR;
  14998. }
  14999. else {
  15000. /* reset IV for decrypt */
  15001. ret = wc_Rc2SetIV(&rc2, iv);
  15002. }
  15003. if (ret == 0) {
  15004. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  15005. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  15006. ret = WOLFSSL_FATAL_ERROR;
  15007. }
  15008. }
  15009. }
  15010. /* Rc2CbcEncrypt bad arguments */
  15011. if (ret == 0) {
  15012. /* null Rc2 struct */
  15013. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  15014. if (ret == BAD_FUNC_ARG) {
  15015. ret = 0;
  15016. }
  15017. }
  15018. if (ret == 0) {
  15019. /* null out buffer */
  15020. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  15021. if (ret == BAD_FUNC_ARG) {
  15022. ret = 0;
  15023. }
  15024. }
  15025. if (ret == 0) {
  15026. /* null input buffer */
  15027. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  15028. if (ret == BAD_FUNC_ARG) {
  15029. ret = 0;
  15030. }
  15031. }
  15032. /* Rc2CbcDecrypt bad arguments */
  15033. if (ret == 0) {
  15034. /* in size is 0 */
  15035. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  15036. if (ret != 0) {
  15037. ret = WOLFSSL_FATAL_ERROR;
  15038. }
  15039. }
  15040. if (ret == 0) {
  15041. /* null Rc2 struct */
  15042. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  15043. if (ret == BAD_FUNC_ARG) {
  15044. ret = 0;
  15045. }
  15046. }
  15047. if (ret == 0) {
  15048. /* null out buffer */
  15049. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  15050. if (ret == BAD_FUNC_ARG) {
  15051. ret = 0;
  15052. }
  15053. }
  15054. if (ret == 0) {
  15055. /* null input buffer */
  15056. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  15057. if (ret == BAD_FUNC_ARG) {
  15058. ret = 0;
  15059. }
  15060. }
  15061. res = TEST_RES_CHECK(ret == 0);
  15062. #endif
  15063. return res;
  15064. } /* END test_wc_Rc2SetKey */
  15065. /*
  15066. * Testing function for wc_AesSetIV
  15067. */
  15068. static int test_wc_AesSetIV(void)
  15069. {
  15070. int res = TEST_SKIPPED;
  15071. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15072. Aes aes;
  15073. int ret = 0;
  15074. byte key16[] =
  15075. {
  15076. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15077. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15078. };
  15079. byte iv1[] = "1234567890abcdef";
  15080. byte iv2[] = "0987654321fedcba";
  15081. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15082. if (ret != 0)
  15083. return ret;
  15084. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15085. iv1, AES_ENCRYPTION);
  15086. if (ret == 0) {
  15087. ret = wc_AesSetIV(&aes, iv2);
  15088. }
  15089. /* Test bad args. */
  15090. if (ret == 0) {
  15091. ret = wc_AesSetIV(NULL, iv1);
  15092. if (ret == BAD_FUNC_ARG) {
  15093. /* NULL iv should return 0. */
  15094. ret = wc_AesSetIV(&aes, NULL);
  15095. }
  15096. else {
  15097. ret = WOLFSSL_FATAL_ERROR;
  15098. }
  15099. }
  15100. wc_AesFree(&aes);
  15101. res = TEST_RES_CHECK(ret == 0);
  15102. #endif
  15103. return res;
  15104. } /* test_wc_AesSetIV */
  15105. /*
  15106. * Testing function for wc_AesSetKey().
  15107. */
  15108. static int test_wc_AesSetKey(void)
  15109. {
  15110. int res = TEST_SKIPPED;
  15111. #ifndef NO_AES
  15112. Aes aes;
  15113. int ret = 0;
  15114. byte key16[] =
  15115. {
  15116. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15117. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15118. };
  15119. #ifdef WOLFSSL_AES_192
  15120. byte key24[] =
  15121. {
  15122. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15123. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15124. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15125. };
  15126. #endif
  15127. #ifdef WOLFSSL_AES_256
  15128. byte key32[] =
  15129. {
  15130. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15131. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15132. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15133. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15134. };
  15135. #endif
  15136. byte badKey16[] =
  15137. {
  15138. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15139. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15140. };
  15141. byte iv[] = "1234567890abcdef";
  15142. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15143. if (ret != 0)
  15144. return ret;
  15145. #ifdef WOLFSSL_AES_128
  15146. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15147. iv, AES_ENCRYPTION);
  15148. #endif
  15149. #ifdef WOLFSSL_AES_192
  15150. if (ret == 0) {
  15151. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  15152. iv, AES_ENCRYPTION);
  15153. }
  15154. #endif
  15155. #ifdef WOLFSSL_AES_256
  15156. if (ret == 0) {
  15157. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  15158. iv, AES_ENCRYPTION);
  15159. }
  15160. #endif
  15161. /* Pass in bad args. */
  15162. if (ret == 0) {
  15163. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  15164. iv, AES_ENCRYPTION);
  15165. if (ret == BAD_FUNC_ARG) {
  15166. ret = wc_AesSetKey(&aes, badKey16,
  15167. (word32) sizeof(badKey16) / sizeof(byte),
  15168. iv, AES_ENCRYPTION);
  15169. }
  15170. if (ret == BAD_FUNC_ARG) {
  15171. ret = 0;
  15172. }
  15173. else {
  15174. ret = WOLFSSL_FATAL_ERROR;
  15175. }
  15176. }
  15177. wc_AesFree(&aes);
  15178. res = TEST_RES_CHECK(ret == 0);
  15179. #endif
  15180. return res;
  15181. } /* END test_wc_AesSetKey */
  15182. /*
  15183. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15184. * and wc_AesCbcDecryptWithKey()
  15185. */
  15186. static int test_wc_AesCbcEncryptDecrypt(void)
  15187. {
  15188. int res = TEST_SKIPPED;
  15189. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15190. defined(WOLFSSL_AES_256)
  15191. Aes aes;
  15192. int ret = 0;
  15193. byte key32[] =
  15194. {
  15195. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15196. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15197. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15198. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15199. };
  15200. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  15201. {
  15202. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15203. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15204. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  15205. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  15206. };
  15207. byte iv[] = "1234567890abcdef";
  15208. byte enc[sizeof(vector)];
  15209. byte dec[sizeof(vector)];
  15210. int cbcE = WOLFSSL_FATAL_ERROR;
  15211. int cbcD = WOLFSSL_FATAL_ERROR;
  15212. int cbcDWK = WOLFSSL_FATAL_ERROR;
  15213. byte dec2[sizeof(vector)];
  15214. /* Init stack variables. */
  15215. XMEMSET(enc, 0, sizeof(enc));
  15216. XMEMSET(dec, 0, sizeof(vector));
  15217. XMEMSET(dec2, 0, sizeof(vector));
  15218. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15219. if (ret != 0)
  15220. return ret;
  15221. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  15222. if (ret == 0) {
  15223. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  15224. if (ret == 0) {
  15225. /* Re init for decrypt and set flag. */
  15226. cbcE = 0;
  15227. wc_AesFree(&aes);
  15228. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  15229. iv, AES_DECRYPTION);
  15230. }
  15231. if (ret == 0) {
  15232. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  15233. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  15234. ret = WOLFSSL_FATAL_ERROR;
  15235. }
  15236. else {
  15237. /* Set flag. */
  15238. cbcD = 0;
  15239. }
  15240. }
  15241. }
  15242. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  15243. if (ret == 0 || cbcE == 0) {
  15244. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15245. key32, sizeof(key32)/sizeof(byte), iv);
  15246. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  15247. cbcDWK = 0;
  15248. }
  15249. }
  15250. /* Pass in bad args */
  15251. if (cbcE == 0) {
  15252. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  15253. if (cbcE == BAD_FUNC_ARG) {
  15254. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  15255. }
  15256. if (cbcE == BAD_FUNC_ARG) {
  15257. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  15258. }
  15259. if (cbcE == BAD_FUNC_ARG) {
  15260. cbcE = 0;
  15261. }
  15262. else {
  15263. cbcE = WOLFSSL_FATAL_ERROR;
  15264. }
  15265. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15266. if (cbcE == 0) {
  15267. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  15268. }
  15269. if (cbcE == BAD_LENGTH_E) {
  15270. cbcE = 0;
  15271. }
  15272. else {
  15273. cbcE = WOLFSSL_FATAL_ERROR;
  15274. }
  15275. #endif
  15276. }
  15277. if (cbcE == 0) {
  15278. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15279. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15280. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15281. "mode (v2), skip test");
  15282. #else
  15283. /* Test passing in size of 0 */
  15284. XMEMSET(enc, 0, sizeof(enc));
  15285. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  15286. if (cbcE == 0) {
  15287. /* Check enc was not modified */
  15288. int i;
  15289. for (i = 0; i < (int)sizeof(enc); i++)
  15290. cbcE |= enc[i];
  15291. }
  15292. #endif
  15293. }
  15294. if (cbcE != 0) {
  15295. wc_AesFree(&aes);
  15296. return TEST_FAIL;
  15297. }
  15298. if (cbcD == 0) {
  15299. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  15300. if (cbcD == BAD_FUNC_ARG) {
  15301. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  15302. }
  15303. if (cbcD == BAD_FUNC_ARG) {
  15304. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  15305. }
  15306. if (cbcD == BAD_FUNC_ARG) {
  15307. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  15308. }
  15309. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15310. if (cbcD == BAD_LENGTH_E) {
  15311. cbcD = 0;
  15312. }
  15313. else {
  15314. cbcD = WOLFSSL_FATAL_ERROR;
  15315. }
  15316. #else
  15317. if (cbcD == BAD_FUNC_ARG) {
  15318. cbcD = 0;
  15319. }
  15320. else {
  15321. cbcD = WOLFSSL_FATAL_ERROR;
  15322. }
  15323. #endif
  15324. }
  15325. if (cbcD == 0) {
  15326. /* Test passing in size of 0 */
  15327. XMEMSET(dec, 0, sizeof(dec));
  15328. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  15329. if (cbcD == 0) {
  15330. /* Check dec was not modified */
  15331. int i;
  15332. for (i = 0; i < (int)sizeof(dec); i++)
  15333. cbcD |= dec[i];
  15334. }
  15335. }
  15336. if (cbcD != 0) {
  15337. wc_AesFree(&aes);
  15338. return TEST_FAIL;
  15339. }
  15340. if (cbcDWK == 0) {
  15341. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15342. key32, sizeof(key32)/sizeof(byte), iv);
  15343. if (cbcDWK == BAD_FUNC_ARG) {
  15344. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15345. key32, sizeof(key32)/sizeof(byte), iv);
  15346. }
  15347. if (cbcDWK == BAD_FUNC_ARG) {
  15348. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15349. NULL, sizeof(key32)/sizeof(byte), iv);
  15350. }
  15351. if (cbcDWK == BAD_FUNC_ARG) {
  15352. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15353. key32, sizeof(key32)/sizeof(byte), NULL);
  15354. }
  15355. if (cbcDWK == BAD_FUNC_ARG) {
  15356. cbcDWK = 0;
  15357. }
  15358. else {
  15359. cbcDWK = WOLFSSL_FATAL_ERROR;
  15360. }
  15361. }
  15362. wc_AesFree(&aes);
  15363. res = TEST_RES_CHECK(cbcDWK == 0);
  15364. #endif
  15365. return res;
  15366. } /* END test_wc_AesCbcEncryptDecrypt */
  15367. /*
  15368. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15369. */
  15370. static int test_wc_AesCtrEncryptDecrypt(void)
  15371. {
  15372. int res = TEST_SKIPPED;
  15373. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15374. Aes aesEnc, aesDec;
  15375. int ret = 0;
  15376. byte key32[] =
  15377. {
  15378. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15379. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15380. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15381. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15382. };
  15383. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15384. {
  15385. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15386. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15387. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15388. };
  15389. byte iv[] = "1234567890abcdef";
  15390. byte enc[AES_BLOCK_SIZE * 2];
  15391. byte dec[AES_BLOCK_SIZE * 2];
  15392. /* Init stack variables. */
  15393. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15394. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15395. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  15396. if (ret != 0)
  15397. return ret;
  15398. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  15399. if (ret != 0) {
  15400. wc_AesFree(&aesEnc);
  15401. return ret;
  15402. }
  15403. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  15404. iv, AES_ENCRYPTION);
  15405. if (ret == 0) {
  15406. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15407. sizeof(vector)/sizeof(byte));
  15408. if (ret == 0) {
  15409. /* Decrypt with wc_AesCtrEncrypt() */
  15410. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  15411. iv, AES_ENCRYPTION);
  15412. }
  15413. if (ret == 0) {
  15414. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  15415. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  15416. ret = WOLFSSL_FATAL_ERROR;
  15417. }
  15418. }
  15419. }
  15420. /* Test bad args. */
  15421. if (ret == 0) {
  15422. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  15423. if (ret == BAD_FUNC_ARG) {
  15424. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  15425. }
  15426. if (ret == BAD_FUNC_ARG) {
  15427. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  15428. }
  15429. if (ret == BAD_FUNC_ARG) {
  15430. ret = 0;
  15431. }
  15432. else {
  15433. ret = WOLFSSL_FATAL_ERROR;
  15434. }
  15435. }
  15436. wc_AesFree(&aesEnc);
  15437. wc_AesFree(&aesDec);
  15438. res = TEST_RES_CHECK(ret == 0);
  15439. #endif
  15440. return res;
  15441. } /* END test_wc_AesCtrEncryptDecrypt */
  15442. /*
  15443. * test function for wc_AesGcmSetKey()
  15444. */
  15445. static int test_wc_AesGcmSetKey(void)
  15446. {
  15447. int res = TEST_SKIPPED;
  15448. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15449. Aes aes;
  15450. int ret = 0;
  15451. #ifdef WOLFSSL_AES_128
  15452. byte key16[] =
  15453. {
  15454. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15455. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15456. };
  15457. #endif
  15458. #ifdef WOLFSSL_AES_192
  15459. byte key24[] =
  15460. {
  15461. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15462. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15463. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15464. };
  15465. #endif
  15466. #ifdef WOLFSSL_AES_256
  15467. byte key32[] =
  15468. {
  15469. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15470. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15471. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15472. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15473. };
  15474. #endif
  15475. byte badKey16[] =
  15476. {
  15477. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15478. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15479. };
  15480. byte badKey24[] =
  15481. {
  15482. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15483. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15484. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15485. };
  15486. byte badKey32[] =
  15487. {
  15488. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15489. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15490. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15491. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15492. };
  15493. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15494. if (ret != 0)
  15495. return ret;
  15496. #ifdef WOLFSSL_AES_128
  15497. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  15498. #endif
  15499. #ifdef WOLFSSL_AES_192
  15500. if (ret == 0) {
  15501. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  15502. }
  15503. #endif
  15504. #ifdef WOLFSSL_AES_256
  15505. if (ret == 0) {
  15506. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  15507. }
  15508. #endif
  15509. /* Pass in bad args. */
  15510. if (ret == 0) {
  15511. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  15512. if (ret == BAD_FUNC_ARG) {
  15513. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  15514. }
  15515. if (ret == BAD_FUNC_ARG) {
  15516. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  15517. }
  15518. if (ret == BAD_FUNC_ARG) {
  15519. ret = 0;
  15520. }
  15521. else {
  15522. ret = WOLFSSL_FATAL_ERROR;
  15523. }
  15524. }
  15525. wc_AesFree(&aes);
  15526. res = TEST_RES_CHECK(ret == 0);
  15527. #endif
  15528. return res;
  15529. } /* END test_wc_AesGcmSetKey */
  15530. /*
  15531. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15532. */
  15533. static int test_wc_AesGcmEncryptDecrypt(void)
  15534. {
  15535. int res = TEST_SKIPPED;
  15536. /* WOLFSSL_AFALG requires 12 byte IV */
  15537. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15538. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15539. Aes aes;
  15540. byte key32[] =
  15541. {
  15542. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15543. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15544. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15545. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15546. };
  15547. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15548. {
  15549. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15550. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15551. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15552. };
  15553. const byte a[] =
  15554. {
  15555. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15556. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15557. 0xab, 0xad, 0xda, 0xd2
  15558. };
  15559. byte iv[] = "1234567890a";
  15560. byte longIV[] = "1234567890abcdefghij";
  15561. byte enc[sizeof(vector)];
  15562. byte resultT[AES_BLOCK_SIZE];
  15563. byte dec[sizeof(vector)];
  15564. int gcmD = WOLFSSL_FATAL_ERROR;
  15565. int gcmE = WOLFSSL_FATAL_ERROR;
  15566. int ret = 0;
  15567. /* Init stack variables. */
  15568. XMEMSET(enc, 0, sizeof(vector));
  15569. XMEMSET(dec, 0, sizeof(vector));
  15570. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15571. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15572. if (ret != 0)
  15573. return ret;
  15574. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  15575. if (ret == 0) {
  15576. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  15577. iv, sizeof(iv)/sizeof(byte), resultT,
  15578. sizeof(resultT), a, sizeof(a));
  15579. }
  15580. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  15581. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  15582. iv, sizeof(iv)/sizeof(byte), resultT,
  15583. sizeof(resultT), a, sizeof(a));
  15584. if (gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  15585. gcmD = WOLFSSL_FATAL_ERROR;
  15586. }
  15587. }
  15588. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15589. if (gcmE == 0) {
  15590. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  15591. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  15592. a, sizeof(a));
  15593. if (gcmE == BAD_FUNC_ARG) {
  15594. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15595. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  15596. resultT, sizeof(resultT) + 1, a, sizeof(a));
  15597. }
  15598. if (gcmE == BAD_FUNC_ARG) {
  15599. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15600. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  15601. resultT, sizeof(resultT) - 5, a, sizeof(a));
  15602. }
  15603. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15604. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15605. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15606. /* FIPS does not check the lower bound of ivSz */
  15607. #else
  15608. if (gcmE == BAD_FUNC_ARG) {
  15609. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15610. sizeof(vector), iv, 0,
  15611. resultT, sizeof(resultT), a, sizeof(a));
  15612. }
  15613. #endif
  15614. if (gcmE == BAD_FUNC_ARG) {
  15615. gcmE = 0;
  15616. }
  15617. else {
  15618. gcmE = WOLFSSL_FATAL_ERROR;
  15619. }
  15620. }
  15621. /* This case is now considered good. Long IVs are now allowed.
  15622. * Except for the original FIPS release, it still has an upper
  15623. * bound on the IV length. */
  15624. #if (!defined(HAVE_FIPS) || \
  15625. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15626. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15627. if (gcmE == 0) {
  15628. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15629. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  15630. a, sizeof(a));
  15631. }
  15632. #else
  15633. (void)longIV;
  15634. #endif /* Old FIPS */
  15635. /* END wc_AesGcmEncrypt */
  15636. if (gcmE != 0) {
  15637. wc_AesFree(&aes);
  15638. return TEST_FAIL;
  15639. }
  15640. #ifdef HAVE_AES_DECRYPT
  15641. if (gcmD == 0) {
  15642. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  15643. iv, sizeof(iv)/sizeof(byte), resultT,
  15644. sizeof(resultT), a, sizeof(a));
  15645. if (gcmD == BAD_FUNC_ARG) {
  15646. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  15647. iv, sizeof(iv)/sizeof(byte), resultT,
  15648. sizeof(resultT), a, sizeof(a));
  15649. }
  15650. if (gcmD == BAD_FUNC_ARG) {
  15651. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  15652. iv, sizeof(iv)/sizeof(byte), resultT,
  15653. sizeof(resultT), a, sizeof(a));
  15654. }
  15655. if (gcmD == BAD_FUNC_ARG) {
  15656. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15657. NULL, sizeof(iv)/sizeof(byte), resultT,
  15658. sizeof(resultT), a, sizeof(a));
  15659. }
  15660. if (gcmD == BAD_FUNC_ARG) {
  15661. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15662. iv, sizeof(iv)/sizeof(byte), NULL,
  15663. sizeof(resultT), a, sizeof(a));
  15664. }
  15665. if (gcmD == BAD_FUNC_ARG) {
  15666. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15667. iv, sizeof(iv)/sizeof(byte), resultT,
  15668. sizeof(resultT) + 1, a, sizeof(a));
  15669. }
  15670. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15671. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15672. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15673. /* FIPS does not check the lower bound of ivSz */
  15674. #else
  15675. if (gcmD == BAD_FUNC_ARG) {
  15676. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15677. iv, 0, resultT,
  15678. sizeof(resultT), a, sizeof(a));
  15679. }
  15680. #endif
  15681. if (gcmD == BAD_FUNC_ARG) {
  15682. gcmD = 0;
  15683. }
  15684. else {
  15685. gcmD = WOLFSSL_FATAL_ERROR;
  15686. }
  15687. res = TEST_RES_CHECK(gcmD == 0);
  15688. } /* END wc_AesGcmDecrypt */
  15689. #endif /* HAVE_AES_DECRYPT */
  15690. wc_AesFree(&aes);
  15691. #endif
  15692. return res;
  15693. } /* END test_wc_AesGcmEncryptDecrypt */
  15694. /*
  15695. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  15696. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  15697. */
  15698. static int test_wc_AesGcmMixedEncDecLongIV(void)
  15699. {
  15700. int ret = TEST_SKIPPED;
  15701. #if (!defined(HAVE_FIPS) || \
  15702. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15703. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  15704. const byte key[] = {
  15705. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15706. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15707. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15708. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15709. };
  15710. const byte in[] = {
  15711. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15712. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15713. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15714. };
  15715. const byte aad[] = {
  15716. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15717. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15718. 0xab, 0xad, 0xda, 0xd2
  15719. };
  15720. Aes aesEnc, aesDec;
  15721. byte iv[] = "1234567890abcdefghij";
  15722. byte out[sizeof(in)];
  15723. byte plain[sizeof(in)];
  15724. byte tag[AES_BLOCK_SIZE];
  15725. XMEMSET(out, 0, sizeof(out));
  15726. XMEMSET(plain, 0, sizeof(plain));
  15727. XMEMSET(tag, 0, sizeof(tag));
  15728. /* Perform one-shot encryption using long IV */
  15729. AssertIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15730. AssertIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  15731. AssertIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  15732. tag, sizeof(tag), aad, sizeof(aad)), 0);
  15733. /* Perform streaming decryption using long IV */
  15734. AssertIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15735. AssertIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  15736. AssertIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  15737. sizeof(aad)), 0);
  15738. AssertIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  15739. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15740. /* Free resources */
  15741. wc_AesFree(&aesEnc);
  15742. wc_AesFree(&aesDec);
  15743. ret = TEST_SUCCESS;
  15744. #endif
  15745. return ret;
  15746. } /* END wc_AesGcmMixedEncDecLongIV */
  15747. /*
  15748. * unit test for wc_GmacSetKey()
  15749. */
  15750. static int test_wc_GmacSetKey(void)
  15751. {
  15752. int res = TEST_SKIPPED;
  15753. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15754. Gmac gmac;
  15755. byte key16[] =
  15756. {
  15757. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15758. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15759. };
  15760. #ifdef WOLFSSL_AES_192
  15761. byte key24[] =
  15762. {
  15763. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15764. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15765. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15766. };
  15767. #endif
  15768. #ifdef WOLFSSL_AES_256
  15769. byte key32[] =
  15770. {
  15771. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15772. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15773. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15774. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15775. };
  15776. #endif
  15777. byte badKey16[] =
  15778. {
  15779. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15780. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15781. };
  15782. byte badKey24[] =
  15783. {
  15784. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15785. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15786. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15787. };
  15788. byte badKey32[] =
  15789. {
  15790. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15791. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15792. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15793. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15794. };
  15795. int ret = 0;
  15796. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15797. if (ret != 0)
  15798. return ret;
  15799. #ifdef WOLFSSL_AES_128
  15800. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  15801. #endif
  15802. #ifdef WOLFSSL_AES_192
  15803. if (ret == 0) {
  15804. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15805. }
  15806. #endif
  15807. #ifdef WOLFSSL_AES_256
  15808. if (ret == 0) {
  15809. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15810. }
  15811. #endif
  15812. /* Pass in bad args. */
  15813. if (ret == 0) {
  15814. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  15815. if (ret == BAD_FUNC_ARG) {
  15816. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  15817. }
  15818. if (ret == BAD_FUNC_ARG) {
  15819. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  15820. }
  15821. if (ret == BAD_FUNC_ARG) {
  15822. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  15823. }
  15824. if (ret == BAD_FUNC_ARG) {
  15825. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  15826. }
  15827. if (ret == BAD_FUNC_ARG) {
  15828. ret = 0;
  15829. }
  15830. else {
  15831. ret = WOLFSSL_FATAL_ERROR;
  15832. }
  15833. }
  15834. wc_AesFree(&gmac.aes);
  15835. res = TEST_RES_CHECK(ret == 0);
  15836. #endif
  15837. return res;
  15838. } /* END test_wc_GmacSetKey */
  15839. /*
  15840. * unit test for wc_GmacUpdate
  15841. */
  15842. static int test_wc_GmacUpdate(void)
  15843. {
  15844. int res = TEST_SKIPPED;
  15845. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15846. Gmac gmac;
  15847. #ifdef WOLFSSL_AES_128
  15848. const byte key16[] =
  15849. {
  15850. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15851. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15852. };
  15853. #endif
  15854. #ifdef WOLFSSL_AES_192
  15855. byte key24[] =
  15856. {
  15857. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15858. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15859. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15860. };
  15861. #endif
  15862. #ifdef WOLFSSL_AES_256
  15863. byte key32[] =
  15864. {
  15865. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15866. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15867. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15868. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15869. };
  15870. #endif
  15871. #ifdef WOLFSSL_AES_128
  15872. const byte authIn[] =
  15873. {
  15874. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15875. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15876. };
  15877. #endif
  15878. #ifdef WOLFSSL_AES_192
  15879. const byte authIn2[] =
  15880. {
  15881. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15882. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15883. };
  15884. #endif
  15885. const byte authIn3[] =
  15886. {
  15887. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15888. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15889. };
  15890. #ifdef WOLFSSL_AES_128
  15891. const byte tag1[] = /* Known. */
  15892. {
  15893. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15894. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15895. };
  15896. #endif
  15897. #ifdef WOLFSSL_AES_192
  15898. const byte tag2[] = /* Known */
  15899. {
  15900. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15901. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15902. };
  15903. #endif
  15904. const byte tag3[] = /* Known */
  15905. {
  15906. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15907. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15908. };
  15909. #ifdef WOLFSSL_AES_128
  15910. const byte iv[] =
  15911. {
  15912. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15913. 0xe2, 0x8c, 0x8f, 0x16
  15914. };
  15915. #endif
  15916. #ifdef WOLFSSL_AES_192
  15917. const byte iv2[] =
  15918. {
  15919. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15920. 0x7e, 0x1a, 0x6f, 0xbc
  15921. };
  15922. #endif
  15923. const byte iv3[] =
  15924. {
  15925. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15926. 0xc3, 0xfb, 0x6c, 0x8a
  15927. };
  15928. byte tagOut[16];
  15929. byte tagOut2[24];
  15930. byte tagOut3[32];
  15931. int ret = 0;
  15932. /* Init stack variables. */
  15933. XMEMSET(tagOut, 0, sizeof(tagOut));
  15934. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15935. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15936. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15937. if (ret != 0)
  15938. return ret;
  15939. #ifdef WOLFSSL_AES_128
  15940. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  15941. if (ret == 0) {
  15942. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15943. tagOut, sizeof(tag1));
  15944. if (ret == 0) {
  15945. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  15946. }
  15947. wc_AesFree(&gmac.aes);
  15948. }
  15949. #endif
  15950. #ifdef WOLFSSL_AES_192
  15951. if (ret == 0) {
  15952. XMEMSET(&gmac, 0, sizeof(Gmac));
  15953. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15954. }
  15955. if (ret == 0) {
  15956. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  15957. sizeof(authIn2), tagOut2, sizeof(tag2));
  15958. }
  15959. if (ret == 0) {
  15960. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  15961. wc_AesFree(&gmac.aes);
  15962. }
  15963. #endif
  15964. #ifdef WOLFSSL_AES_256
  15965. if (ret == 0) {
  15966. XMEMSET(&gmac, 0, sizeof(Gmac));
  15967. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15968. }
  15969. if (ret == 0) {
  15970. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15971. sizeof(authIn3), tagOut3, sizeof(tag3));
  15972. }
  15973. if (ret == 0) {
  15974. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  15975. }
  15976. #endif
  15977. /*Pass bad args. */
  15978. if (ret == 0) {
  15979. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  15980. sizeof(authIn3), tagOut3, sizeof(tag3));
  15981. if (ret == BAD_FUNC_ARG) {
  15982. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15983. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  15984. }
  15985. if (ret == BAD_FUNC_ARG) {
  15986. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15987. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  15988. }
  15989. if (ret == BAD_FUNC_ARG) {
  15990. ret = 0;
  15991. }
  15992. else {
  15993. ret = WOLFSSL_FATAL_ERROR;
  15994. }
  15995. }
  15996. wc_AesFree(&gmac.aes);
  15997. res = TEST_RES_CHECK(ret == 0);
  15998. #endif
  15999. return res;
  16000. } /* END test_wc_GmacUpdate */
  16001. /*
  16002. * testing wc_CamelliaSetKey
  16003. */
  16004. static int test_wc_CamelliaSetKey(void)
  16005. {
  16006. int res = TEST_SKIPPED;
  16007. #ifdef HAVE_CAMELLIA
  16008. Camellia camellia;
  16009. /*128-bit key*/
  16010. static const byte key16[] =
  16011. {
  16012. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16013. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16014. };
  16015. /* 192-bit key */
  16016. static const byte key24[] =
  16017. {
  16018. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16019. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16020. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16021. };
  16022. /* 256-bit key */
  16023. static const byte key32[] =
  16024. {
  16025. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16026. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16027. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16028. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16029. };
  16030. static const byte iv[] =
  16031. {
  16032. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16033. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16034. };
  16035. int ret = 0;
  16036. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  16037. if (ret == 0) {
  16038. ret = wc_CamelliaSetKey(&camellia, key16,
  16039. (word32)sizeof(key16), NULL);
  16040. if (ret == 0) {
  16041. ret = wc_CamelliaSetKey(&camellia, key24,
  16042. (word32)sizeof(key24), iv);
  16043. }
  16044. if (ret == 0) {
  16045. ret = wc_CamelliaSetKey(&camellia, key24,
  16046. (word32)sizeof(key24), NULL);
  16047. }
  16048. if (ret == 0) {
  16049. ret = wc_CamelliaSetKey(&camellia, key32,
  16050. (word32)sizeof(key32), iv);
  16051. }
  16052. if (ret == 0) {
  16053. ret = wc_CamelliaSetKey(&camellia, key32,
  16054. (word32)sizeof(key32), NULL);
  16055. }
  16056. }
  16057. /* Bad args. */
  16058. if (ret == 0) {
  16059. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  16060. if (ret != BAD_FUNC_ARG) {
  16061. ret = WOLFSSL_FATAL_ERROR;
  16062. }
  16063. else {
  16064. ret = 0;
  16065. }
  16066. } /* END bad args. */
  16067. res = TEST_RES_CHECK(ret == 0);
  16068. #endif
  16069. return res;
  16070. } /* END test_wc_CammeliaSetKey */
  16071. /*
  16072. * Testing wc_CamelliaSetIV()
  16073. */
  16074. static int test_wc_CamelliaSetIV(void)
  16075. {
  16076. int res = TEST_SKIPPED;
  16077. #ifdef HAVE_CAMELLIA
  16078. Camellia camellia;
  16079. static const byte iv[] =
  16080. {
  16081. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16082. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16083. };
  16084. int ret = 0;
  16085. ret = wc_CamelliaSetIV(&camellia, iv);
  16086. if (ret == 0) {
  16087. ret = wc_CamelliaSetIV(&camellia, NULL);
  16088. }
  16089. /* Bad args. */
  16090. if (ret == 0) {
  16091. ret = wc_CamelliaSetIV(NULL, NULL);
  16092. if (ret != BAD_FUNC_ARG) {
  16093. ret = WOLFSSL_FATAL_ERROR;
  16094. }
  16095. else {
  16096. ret = 0;
  16097. }
  16098. }
  16099. res = TEST_RES_CHECK(ret == 0);
  16100. #endif
  16101. return res;
  16102. } /*END test_wc_CamelliaSetIV*/
  16103. /*
  16104. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16105. */
  16106. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16107. {
  16108. int res = TEST_SKIPPED;
  16109. #ifdef HAVE_CAMELLIA
  16110. Camellia camellia;
  16111. static const byte key24[] =
  16112. {
  16113. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16114. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16115. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16116. };
  16117. static const byte iv[] =
  16118. {
  16119. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16120. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16121. };
  16122. static const byte plainT[] =
  16123. {
  16124. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16125. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16126. };
  16127. byte enc[sizeof(plainT)];
  16128. byte dec[sizeof(enc)];
  16129. int camE = WOLFSSL_FATAL_ERROR;
  16130. int camD = WOLFSSL_FATAL_ERROR;
  16131. int ret = 0;
  16132. /*Init stack variables.*/
  16133. XMEMSET(enc, 0, 16);
  16134. XMEMSET(enc, 0, 16);
  16135. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  16136. if (ret == 0) {
  16137. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  16138. if (ret == 0) {
  16139. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  16140. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16141. ret = WOLFSSL_FATAL_ERROR;
  16142. }
  16143. }
  16144. }
  16145. /* Pass bad args. */
  16146. if (ret == 0) {
  16147. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  16148. if (camE == BAD_FUNC_ARG) {
  16149. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  16150. }
  16151. if (camE == BAD_FUNC_ARG) {
  16152. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  16153. }
  16154. if (camE == BAD_FUNC_ARG) {
  16155. camE = 0;
  16156. }
  16157. else {
  16158. camE = WOLFSSL_FATAL_ERROR;
  16159. }
  16160. }
  16161. if (camE != 0) {
  16162. return TEST_FAIL;
  16163. }
  16164. if (ret == 0) {
  16165. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  16166. if (camD == BAD_FUNC_ARG) {
  16167. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  16168. }
  16169. if (camD == BAD_FUNC_ARG) {
  16170. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  16171. }
  16172. if (camD == BAD_FUNC_ARG) {
  16173. camD = 0;
  16174. }
  16175. else {
  16176. camD = WOLFSSL_FATAL_ERROR;
  16177. }
  16178. }
  16179. res = TEST_RES_CHECK(camD == 0);
  16180. #endif
  16181. return res;
  16182. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16183. /*
  16184. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16185. */
  16186. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16187. {
  16188. int res = TEST_SKIPPED;
  16189. #ifdef HAVE_CAMELLIA
  16190. Camellia camellia;
  16191. static const byte key24[] =
  16192. {
  16193. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16194. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16195. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16196. };
  16197. static const byte plainT[] =
  16198. {
  16199. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16200. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16201. };
  16202. byte enc[CAMELLIA_BLOCK_SIZE];
  16203. byte dec[CAMELLIA_BLOCK_SIZE];
  16204. int camCbcE = WOLFSSL_FATAL_ERROR;
  16205. int camCbcD = WOLFSSL_FATAL_ERROR;
  16206. int ret = 0;
  16207. /* Init stack variables. */
  16208. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16209. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16210. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16211. if (ret == 0) {
  16212. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16213. if (ret != 0) {
  16214. ret = WOLFSSL_FATAL_ERROR;
  16215. }
  16216. }
  16217. if (ret == 0) {
  16218. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16219. if (ret == 0) {
  16220. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  16221. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16222. ret = WOLFSSL_FATAL_ERROR;
  16223. }
  16224. }
  16225. }
  16226. /* Pass in bad args. */
  16227. if (ret == 0) {
  16228. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16229. if (camCbcE == BAD_FUNC_ARG) {
  16230. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16231. CAMELLIA_BLOCK_SIZE);
  16232. }
  16233. if (camCbcE == BAD_FUNC_ARG) {
  16234. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16235. CAMELLIA_BLOCK_SIZE);
  16236. }
  16237. if (camCbcE == BAD_FUNC_ARG) {
  16238. camCbcE = 0;
  16239. }
  16240. else {
  16241. camCbcE = WOLFSSL_FATAL_ERROR;
  16242. }
  16243. }
  16244. if (camCbcE != 0) {
  16245. return TEST_FAIL;
  16246. }
  16247. if (ret == 0) {
  16248. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  16249. if (camCbcD == BAD_FUNC_ARG) {
  16250. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16251. CAMELLIA_BLOCK_SIZE);
  16252. }
  16253. if (camCbcD == BAD_FUNC_ARG) {
  16254. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16255. CAMELLIA_BLOCK_SIZE);
  16256. }
  16257. if (camCbcD == BAD_FUNC_ARG) {
  16258. camCbcD = 0;
  16259. }
  16260. else {
  16261. camCbcD = WOLFSSL_FATAL_ERROR;
  16262. }
  16263. } /* END bad args. */
  16264. res = TEST_RES_CHECK(camCbcD == 0);
  16265. #endif
  16266. return res;
  16267. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16268. /*
  16269. * Testing wc_Arc4SetKey()
  16270. */
  16271. static int test_wc_Arc4SetKey(void)
  16272. {
  16273. int res = TEST_SKIPPED;
  16274. #ifndef NO_RC4
  16275. Arc4 arc;
  16276. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16277. int keyLen = 8;
  16278. int ret = 0;
  16279. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  16280. /* Test bad args. */
  16281. if (ret == 0) {
  16282. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  16283. if (ret == BAD_FUNC_ARG)
  16284. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  16285. if (ret == BAD_FUNC_ARG)
  16286. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  16287. if (ret == BAD_FUNC_ARG)
  16288. ret = WOLFSSL_ERROR_NONE;
  16289. else
  16290. ret = WOLFSSL_FATAL_ERROR;
  16291. } /* END test bad args. */
  16292. res = TEST_RES_CHECK(ret == 0);
  16293. #endif
  16294. return res;
  16295. } /* END test_wc_Arc4SetKey */
  16296. /*
  16297. * Testing wc_Arc4Process for ENC/DEC.
  16298. */
  16299. static int test_wc_Arc4Process(void)
  16300. {
  16301. int res = TEST_SKIPPED;
  16302. #ifndef NO_RC4
  16303. Arc4 enc, dec;
  16304. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16305. int keyLen = 8;
  16306. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16307. byte cipher[8];
  16308. byte plain[8];
  16309. int ret;
  16310. /* Init stack variables */
  16311. XMEMSET(cipher, 0, sizeof(cipher));
  16312. XMEMSET(plain, 0, sizeof(plain));
  16313. /* Use for async. */
  16314. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  16315. if (ret == 0) {
  16316. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  16317. }
  16318. if (ret == 0) {
  16319. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  16320. }
  16321. if (ret == 0) {
  16322. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  16323. }
  16324. if (ret == 0) {
  16325. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  16326. }
  16327. if (ret == 0) {
  16328. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  16329. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  16330. ret = WOLFSSL_FATAL_ERROR;
  16331. }
  16332. else {
  16333. ret = 0;
  16334. }
  16335. }
  16336. /* Bad args. */
  16337. if (ret == 0) {
  16338. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  16339. if (ret == BAD_FUNC_ARG) {
  16340. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  16341. }
  16342. if (ret == BAD_FUNC_ARG) {
  16343. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  16344. }
  16345. if (ret == BAD_FUNC_ARG) {
  16346. ret = 0;
  16347. }
  16348. else {
  16349. ret = WOLFSSL_FATAL_ERROR;
  16350. }
  16351. }
  16352. wc_Arc4Free(&enc);
  16353. wc_Arc4Free(&dec);
  16354. res = TEST_RES_CHECK(ret == 0);
  16355. #endif
  16356. return res;
  16357. }/* END test_wc_Arc4Process */
  16358. /*
  16359. * Testing wc_Init RsaKey()
  16360. */
  16361. static int test_wc_InitRsaKey(void)
  16362. {
  16363. int res = TEST_SKIPPED;
  16364. #ifndef NO_RSA
  16365. RsaKey key;
  16366. int ret = 0;
  16367. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16368. /* Test bad args. */
  16369. if (ret == 0) {
  16370. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  16371. #ifndef HAVE_USER_RSA
  16372. if (ret == BAD_FUNC_ARG) {
  16373. ret = 0;
  16374. }
  16375. else {
  16376. #else
  16377. if (ret == USER_CRYPTO_ERROR) {
  16378. ret = 0;
  16379. }
  16380. else {
  16381. #endif
  16382. ret = WOLFSSL_FATAL_ERROR;
  16383. }
  16384. } /* end if */
  16385. if (wc_FreeRsaKey(&key) || ret != 0) {
  16386. ret = WOLFSSL_FATAL_ERROR;
  16387. }
  16388. res = TEST_RES_CHECK(ret == 0);
  16389. #endif
  16390. return res;
  16391. } /* END test_wc_InitRsaKey */
  16392. /*
  16393. * Testing wc_RsaPrivateKeyDecode()
  16394. */
  16395. static int test_wc_RsaPrivateKeyDecode(void)
  16396. {
  16397. int res = TEST_SKIPPED;
  16398. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16399. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16400. RsaKey key;
  16401. byte* tmp;
  16402. word32 idx = 0;
  16403. int bytes = 0;
  16404. int ret = 0;
  16405. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16406. if (tmp == NULL) {
  16407. ret = WOLFSSL_FATAL_ERROR;
  16408. }
  16409. if (ret == 0) {
  16410. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16411. }
  16412. if (ret == 0) {
  16413. #ifdef USE_CERT_BUFFERS_1024
  16414. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16415. bytes = sizeof_client_key_der_1024;
  16416. #else
  16417. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16418. bytes = sizeof_client_key_der_2048;
  16419. #endif /* Use cert buffers. */
  16420. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  16421. }
  16422. #ifndef HAVE_USER_RSA
  16423. /* Test bad args. */
  16424. if (ret == 0) {
  16425. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  16426. if (ret == BAD_FUNC_ARG) {
  16427. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  16428. }
  16429. if (ret == BAD_FUNC_ARG) {
  16430. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16431. }
  16432. if (ret == BAD_FUNC_ARG) {
  16433. ret = 0;
  16434. }
  16435. else {
  16436. ret = WOLFSSL_FATAL_ERROR;
  16437. }
  16438. }
  16439. #else
  16440. /* Test bad args. User RSA. */
  16441. if (ret == 0) {
  16442. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  16443. if (ret == USER_CRYPTO_ERROR) {
  16444. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  16445. }
  16446. if (ret == USER_CRYPTO_ERROR) {
  16447. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16448. }
  16449. if (ret == USER_CRYPTO_ERROR) {
  16450. ret = 0;
  16451. }
  16452. else {
  16453. ret = WOLFSSL_FATAL_ERROR;
  16454. }
  16455. }
  16456. #endif
  16457. if (tmp != NULL) {
  16458. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16459. }
  16460. if (wc_FreeRsaKey(&key) || ret != 0) {
  16461. ret = WOLFSSL_FATAL_ERROR;
  16462. }
  16463. res = TEST_RES_CHECK(ret == 0);
  16464. #endif
  16465. return res;
  16466. } /* END test_wc_RsaPrivateKeyDecode */
  16467. /*
  16468. * Testing wc_RsaPublicKeyDecode()
  16469. */
  16470. static int test_wc_RsaPublicKeyDecode(void)
  16471. {
  16472. int res = TEST_SKIPPED;
  16473. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16474. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16475. RsaKey keyPub;
  16476. byte* tmp;
  16477. word32 idx = 0;
  16478. int bytes = 0;
  16479. word32 keySz = 0;
  16480. word32 tstKeySz = 0;
  16481. int ret = 0;
  16482. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16483. XFILE f;
  16484. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16485. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16486. byte buf[4096];
  16487. #endif
  16488. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16489. if (tmp == NULL) {
  16490. ret = WOLFSSL_FATAL_ERROR;
  16491. }
  16492. if (ret == 0) {
  16493. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  16494. }
  16495. if (ret == 0) {
  16496. #ifdef USE_CERT_BUFFERS_1024
  16497. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16498. bytes = sizeof_client_keypub_der_1024;
  16499. keySz = 1024;
  16500. #else
  16501. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16502. bytes = sizeof_client_keypub_der_2048;
  16503. keySz = 2048;
  16504. #endif
  16505. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  16506. }
  16507. #ifndef HAVE_USER_RSA
  16508. /* Pass in bad args. */
  16509. if (ret == 0) {
  16510. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  16511. if (ret == BAD_FUNC_ARG) {
  16512. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  16513. }
  16514. if (ret == BAD_FUNC_ARG) {
  16515. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16516. }
  16517. if (ret == BAD_FUNC_ARG) {
  16518. ret = 0;
  16519. }
  16520. else {
  16521. ret = WOLFSSL_FATAL_ERROR;
  16522. }
  16523. }
  16524. #else
  16525. /* Pass in bad args. */
  16526. if (ret == 0) {
  16527. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  16528. if (ret == USER_CRYPTO_ERROR) {
  16529. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  16530. }
  16531. if (ret == USER_CRYPTO_ERROR) {
  16532. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16533. }
  16534. if (ret == USER_CRYPTO_ERROR) {
  16535. ret = 0;
  16536. }
  16537. else {
  16538. ret = WOLFSSL_FATAL_ERROR;
  16539. }
  16540. }
  16541. #endif
  16542. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  16543. ret = WOLFSSL_FATAL_ERROR;
  16544. }
  16545. if (ret == 0) {
  16546. /* Test for getting modulus key size */
  16547. idx = 0;
  16548. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16549. &tstKeySz, NULL, NULL);
  16550. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  16551. }
  16552. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16553. f = XFOPEN(rsaPssPubKey, "rb");
  16554. AssertTrue((f != XBADFILE));
  16555. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  16556. XFCLOSE(f);
  16557. idx = 0;
  16558. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16559. NULL), 0);
  16560. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  16561. AssertTrue((f != XBADFILE));
  16562. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  16563. XFCLOSE(f);
  16564. idx = 0;
  16565. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16566. NULL), 0);
  16567. #endif
  16568. if (tmp != NULL) {
  16569. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16570. }
  16571. res = TEST_RES_CHECK(ret == 0);
  16572. #endif
  16573. return res;
  16574. } /* END test_wc_RsaPublicKeyDecode */
  16575. /*
  16576. * Testing wc_RsaPublicKeyDecodeRaw()
  16577. */
  16578. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16579. {
  16580. int res = TEST_SKIPPED;
  16581. #if !defined(NO_RSA)
  16582. RsaKey key;
  16583. const byte n = 0x23;
  16584. const byte e = 0x03;
  16585. int nSz = sizeof(n);
  16586. int eSz = sizeof(e);
  16587. int ret;
  16588. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16589. if (ret == 0) {
  16590. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  16591. }
  16592. #ifndef HAVE_USER_RSA
  16593. /* Pass in bad args. */
  16594. if (ret == 0) {
  16595. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  16596. if (ret == BAD_FUNC_ARG) {
  16597. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  16598. }
  16599. if (ret == BAD_FUNC_ARG) {
  16600. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  16601. }
  16602. if (ret == BAD_FUNC_ARG) {
  16603. ret = 0;
  16604. }
  16605. else {
  16606. ret = WOLFSSL_FATAL_ERROR;
  16607. }
  16608. }
  16609. #else
  16610. /* Pass in bad args. User RSA. */
  16611. if (ret == 0) {
  16612. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  16613. if (ret == USER_CRYPTO_ERROR) {
  16614. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  16615. }
  16616. if (ret == USER_CRYPTO_ERROR) {
  16617. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  16618. }
  16619. if (ret == USER_CRYPTO_ERROR) {
  16620. ret = 0;
  16621. }
  16622. else {
  16623. ret = WOLFSSL_FATAL_ERROR;
  16624. }
  16625. }
  16626. #endif
  16627. if (wc_FreeRsaKey(&key) || ret != 0) {
  16628. ret = WOLFSSL_FATAL_ERROR;
  16629. }
  16630. res = TEST_RES_CHECK(ret == 0);
  16631. #endif
  16632. return res;
  16633. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16634. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16635. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16636. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16637. * trying until it gets a probable prime. */
  16638. #ifdef HAVE_FIPS
  16639. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16640. {
  16641. int ret;
  16642. for (;;) {
  16643. ret = wc_MakeRsaKey(key, size, e, rng);
  16644. if (ret != PRIME_GEN_E) break;
  16645. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16646. "trying again.\n");
  16647. }
  16648. return ret;
  16649. }
  16650. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16651. #else
  16652. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16653. #endif
  16654. #endif
  16655. /*
  16656. * Testing wc_MakeRsaKey()
  16657. */
  16658. static int test_wc_MakeRsaKey(void)
  16659. {
  16660. int res = TEST_SKIPPED;
  16661. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16662. RsaKey genKey;
  16663. WC_RNG rng;
  16664. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16665. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16666. int bits = 1024;
  16667. #else
  16668. int bits = 2048;
  16669. #endif
  16670. int ret = 0;
  16671. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  16672. if (ret == 0) {
  16673. ret = wc_InitRng(&rng);
  16674. if (ret == 0) {
  16675. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  16676. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  16677. ret = WOLFSSL_FATAL_ERROR;
  16678. }
  16679. }
  16680. }
  16681. #ifndef HAVE_USER_RSA
  16682. /* Test bad args. */
  16683. if (ret == 0) {
  16684. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  16685. if (ret == BAD_FUNC_ARG) {
  16686. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  16687. }
  16688. if (ret == BAD_FUNC_ARG) {
  16689. /* e < 3 */
  16690. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16691. }
  16692. if (ret == BAD_FUNC_ARG) {
  16693. /* e & 1 == 0 */
  16694. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16695. }
  16696. if (ret == BAD_FUNC_ARG) {
  16697. ret = 0;
  16698. }
  16699. else {
  16700. ret = WOLFSSL_FATAL_ERROR;
  16701. }
  16702. }
  16703. #else
  16704. /* Test bad args. */
  16705. if (ret == 0) {
  16706. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  16707. if (ret == USER_CRYPTO_ERROR) {
  16708. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  16709. }
  16710. if (ret == USER_CRYPTO_ERROR) {
  16711. /* e < 3 */
  16712. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16713. }
  16714. if (ret == USER_CRYPTO_ERROR) {
  16715. /* e & 1 == 0 */
  16716. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16717. }
  16718. if (ret == USER_CRYPTO_ERROR) {
  16719. ret = 0;
  16720. }
  16721. else {
  16722. ret = WOLFSSL_FATAL_ERROR;
  16723. }
  16724. }
  16725. #endif
  16726. if (wc_FreeRng(&rng) || ret != 0) {
  16727. ret = WOLFSSL_FATAL_ERROR;
  16728. }
  16729. res = TEST_RES_CHECK(ret == 0);
  16730. #endif
  16731. return res;
  16732. } /* END test_wc_MakeRsaKey */
  16733. /*
  16734. * Test the bounds checking on the cipher text versus the key modulus.
  16735. * 1. Make a new RSA key.
  16736. * 2. Set c to 1.
  16737. * 3. Decrypt c into k. (error)
  16738. * 4. Copy the key modulus to c and sub 1 from the copy.
  16739. * 5. Decrypt c into k. (error)
  16740. * Valid bounds test cases are covered by all the other RSA tests.
  16741. */
  16742. static int test_RsaDecryptBoundsCheck(void)
  16743. {
  16744. int res = TEST_SKIPPED;
  16745. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16746. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16747. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16748. WC_RNG rng;
  16749. RsaKey key;
  16750. byte flatC[256];
  16751. word32 flatCSz;
  16752. byte out[256];
  16753. word32 outSz = sizeof(out);
  16754. int ret;
  16755. XMEMSET(&rng, 0, sizeof(rng));
  16756. ret = wc_InitRng(&rng);
  16757. if (ret == 0)
  16758. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16759. if (ret == 0) {
  16760. const byte* derKey;
  16761. word32 derKeySz;
  16762. word32 idx = 0;
  16763. #ifdef USE_CERT_BUFFERS_1024
  16764. derKey = server_key_der_1024;
  16765. derKeySz = (word32)sizeof_server_key_der_1024;
  16766. flatCSz = 128;
  16767. #else
  16768. derKey = server_key_der_2048;
  16769. derKeySz = (word32)sizeof_server_key_der_2048;
  16770. flatCSz = 256;
  16771. #endif
  16772. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  16773. }
  16774. if (ret == 0) {
  16775. XMEMSET(flatC, 0, flatCSz);
  16776. flatC[flatCSz-1] = 1;
  16777. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16778. RSA_PRIVATE_DECRYPT, &rng);
  16779. if (ret == RSA_OUT_OF_RANGE_E) {
  16780. mp_int c;
  16781. mp_init_copy(&c, &key.n);
  16782. mp_sub_d(&c, 1, &c);
  16783. mp_to_unsigned_bin(&c, flatC);
  16784. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16785. RSA_PRIVATE_DECRYPT, NULL);
  16786. mp_clear(&c);
  16787. }
  16788. if (ret == RSA_OUT_OF_RANGE_E)
  16789. ret = 0;
  16790. else
  16791. ret = WOLFSSL_FATAL_ERROR;
  16792. }
  16793. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  16794. ret = WOLFSSL_FATAL_ERROR;
  16795. res = TEST_RES_CHECK(ret == 0);
  16796. #endif
  16797. return res;
  16798. } /* END test_wc_RsaDecryptBoundsCheck */
  16799. /*
  16800. * Testing wc_SetKeyUsage()
  16801. */
  16802. static int test_wc_SetKeyUsage(void)
  16803. {
  16804. int res = TEST_SKIPPED;
  16805. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16806. Cert myCert;
  16807. int ret = 0;
  16808. ret = wc_InitCert(&myCert);
  16809. if (ret == 0) {
  16810. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  16811. if (ret == 0) {
  16812. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  16813. }
  16814. if (ret == 0) {
  16815. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  16816. }
  16817. if (ret == 0) {
  16818. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  16819. }
  16820. if (ret == 0) {
  16821. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  16822. }
  16823. }
  16824. /* Test bad args. */
  16825. if (ret == 0) {
  16826. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  16827. if (ret == BAD_FUNC_ARG) {
  16828. ret = wc_SetKeyUsage(&myCert, NULL);
  16829. }
  16830. if (ret == BAD_FUNC_ARG) {
  16831. ret = wc_SetKeyUsage(&myCert, "");
  16832. }
  16833. if (ret == KEYUSAGE_E) {
  16834. ret = wc_SetKeyUsage(&myCert, ",");
  16835. }
  16836. if (ret == KEYUSAGE_E) {
  16837. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  16838. }
  16839. if (ret == KEYUSAGE_E) {
  16840. ret = 0;
  16841. }
  16842. else {
  16843. ret = WOLFSSL_FATAL_ERROR;
  16844. }
  16845. }
  16846. res = TEST_RES_CHECK(ret == 0);
  16847. #endif
  16848. return res;
  16849. } /* END test_wc_SetKeyUsage */
  16850. /*
  16851. * Testing wc_CheckProbablePrime()
  16852. */
  16853. static int test_wc_CheckProbablePrime(void)
  16854. {
  16855. int res = TEST_SKIPPED;
  16856. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16857. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16858. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16859. RsaKey key;
  16860. WC_RNG rng;
  16861. byte e[3];
  16862. word32 eSz = (word32)sizeof(e);
  16863. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16864. word32 nSz = (word32)sizeof(n);
  16865. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16866. word32 dSz = (word32)sizeof(d);
  16867. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16868. word32 pSz = (word32)sizeof(p);
  16869. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16870. word32 qSz = (word32)sizeof(q);
  16871. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16872. int ret = 0;
  16873. int* isPrime;
  16874. int test[5];
  16875. isPrime = test;
  16876. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16877. if (ret == 0) {
  16878. ret = wc_InitRng(&rng);
  16879. }
  16880. if (ret == 0) {
  16881. ret = wc_RsaSetRNG(&key, &rng);
  16882. }
  16883. if (ret == 0) {
  16884. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  16885. }
  16886. if (ret == 0) {
  16887. PRIVATE_KEY_UNLOCK();
  16888. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  16889. p, &pSz, q, &qSz);
  16890. PRIVATE_KEY_LOCK();
  16891. }
  16892. /* Bad cases */
  16893. if (ret == 0) {
  16894. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  16895. nlen, isPrime);
  16896. if (ret == BAD_FUNC_ARG) {
  16897. ret = 0;
  16898. }
  16899. }
  16900. if (ret == 0) {
  16901. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  16902. nlen, isPrime);
  16903. if (ret == BAD_FUNC_ARG) {
  16904. ret = 0;
  16905. }
  16906. }
  16907. if (ret == 0) {
  16908. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  16909. nlen, isPrime);
  16910. if (ret == BAD_FUNC_ARG) {
  16911. ret = 0;
  16912. }
  16913. }
  16914. if (ret == 0) {
  16915. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  16916. nlen, isPrime);
  16917. if (ret == BAD_FUNC_ARG) {
  16918. ret = 0;
  16919. }
  16920. }
  16921. if (ret == 0) {
  16922. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  16923. nlen, isPrime);
  16924. if (ret == BAD_FUNC_ARG) {
  16925. ret = 0;
  16926. }
  16927. }
  16928. if (ret == 0) {
  16929. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  16930. nlen, isPrime);
  16931. if (ret == BAD_FUNC_ARG) {
  16932. ret = 0;
  16933. }
  16934. }
  16935. if (ret == 0) {
  16936. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  16937. nlen, isPrime);
  16938. if (ret == BAD_FUNC_ARG) {
  16939. ret = 0;
  16940. }
  16941. }
  16942. /* Good case */
  16943. if (ret == 0) {
  16944. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  16945. nlen, isPrime);
  16946. }
  16947. wc_FreeRsaKey(&key);
  16948. wc_FreeRng(&rng);
  16949. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16950. res = TEST_RES_CHECK(ret == 0);
  16951. #endif
  16952. return res;
  16953. } /* END test_wc_CheckProbablePrime */
  16954. /*
  16955. * Testing wc_RsaPSS_Verify()
  16956. */
  16957. static int test_wc_RsaPSS_Verify(void)
  16958. {
  16959. int res = TEST_SKIPPED;
  16960. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16961. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16962. RsaKey key;
  16963. WC_RNG rng;
  16964. int sz = 256;
  16965. byte* pt;
  16966. const char* szMessage = "This is the string to be signed";
  16967. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16968. unsigned char pDecrypted[2048/8];
  16969. word32 outLen = sizeof(pDecrypted);
  16970. int ret = 0;
  16971. pt = pDecrypted;
  16972. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16973. if (ret == 0) {
  16974. ret = wc_InitRng(&rng);
  16975. }
  16976. if (ret == 0) {
  16977. ret = wc_RsaSetRNG(&key, &rng);
  16978. }
  16979. if (ret == 0) {
  16980. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16981. }
  16982. if (ret == 0) {
  16983. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  16984. pSignature, sizeof(pSignature),
  16985. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16986. if (ret > 0) {
  16987. sz = ret;
  16988. ret = 0;
  16989. }
  16990. }
  16991. /* Bad cases */
  16992. if (ret == 0) {
  16993. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16994. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16995. if (ret == BAD_FUNC_ARG) {
  16996. ret = 0;
  16997. }
  16998. }
  16999. if (ret == 0) {
  17000. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17001. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17002. if (ret == BAD_FUNC_ARG) {
  17003. ret = 0;
  17004. }
  17005. }
  17006. if (ret == 0) {
  17007. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  17008. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17009. if (ret == BAD_FUNC_ARG) {
  17010. ret = 0;
  17011. }
  17012. }
  17013. if (ret == 0) {
  17014. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17015. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17016. if (ret == BAD_FUNC_ARG) {
  17017. ret = 0;
  17018. }
  17019. }
  17020. /* Good case */
  17021. if (ret == 0) {
  17022. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17023. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17024. if (ret > 0) {
  17025. ret = 0;
  17026. }
  17027. }
  17028. wc_FreeRsaKey(&key);
  17029. wc_FreeRng(&rng);
  17030. res = TEST_RES_CHECK(ret == 0);
  17031. #endif
  17032. return res;
  17033. } /* END test_wc_RsaPSS_Verify */
  17034. /*
  17035. * Testing wc_RsaPSS_VerifyCheck()
  17036. */
  17037. static int test_wc_RsaPSS_VerifyCheck(void)
  17038. {
  17039. int res = TEST_SKIPPED;
  17040. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17041. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17042. RsaKey key;
  17043. WC_RNG rng;
  17044. int sz = 256; /* 2048/8 */
  17045. byte* pt;
  17046. byte digest[32];
  17047. word32 digestSz = sizeof(digest);
  17048. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17049. word32 pSignatureSz = sizeof(pSignature);
  17050. unsigned char pDecrypted[2048/8];
  17051. word32 outLen = sizeof(pDecrypted);
  17052. int ret = 0;
  17053. pt = pDecrypted;
  17054. XMEMSET(digest, 0, sizeof(digest));
  17055. XMEMSET(pSignature, 0, sizeof(pSignature));
  17056. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17057. if (ret == 0) {
  17058. ret = wc_InitRng(&rng);
  17059. }
  17060. if (ret == 0) {
  17061. ret = wc_RsaSetRNG(&key, &rng);
  17062. }
  17063. if (ret == 0) {
  17064. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17065. }
  17066. if (ret == 0) {
  17067. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17068. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17069. }
  17070. if (ret == 0) {
  17071. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17072. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17073. if (ret > 0) {
  17074. sz = ret;
  17075. ret = 0;
  17076. }
  17077. }
  17078. /* Bad cases */
  17079. if (ret == 0) {
  17080. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  17081. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17082. if (ret == BAD_FUNC_ARG) {
  17083. ret = 0;
  17084. }
  17085. }
  17086. if (ret == 0) {
  17087. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  17088. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17089. if (ret == BAD_FUNC_ARG) {
  17090. ret = 0;
  17091. }
  17092. }
  17093. if (ret == 0) {
  17094. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  17095. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17096. if (ret == BAD_FUNC_ARG) {
  17097. ret = 0;
  17098. }
  17099. }
  17100. if (ret == 0) {
  17101. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  17102. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17103. if (ret == BAD_FUNC_ARG) {
  17104. ret = 0;
  17105. }
  17106. }
  17107. /* Good case */
  17108. if (ret == 0) {
  17109. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  17110. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17111. if (ret > 0) {
  17112. ret = 0;
  17113. }
  17114. }
  17115. wc_FreeRsaKey(&key);
  17116. wc_FreeRng(&rng);
  17117. res = TEST_RES_CHECK(ret == 0);
  17118. #endif
  17119. return res;
  17120. } /* END test_wc_RsaPSS_VerifyCheck */
  17121. /*
  17122. * Testing wc_RsaPSS_VerifyCheckInline()
  17123. */
  17124. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17125. {
  17126. int res = TEST_SKIPPED;
  17127. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17128. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17129. RsaKey key;
  17130. WC_RNG rng;
  17131. int sz = 256;
  17132. byte* pt;
  17133. byte digest[32];
  17134. word32 digestSz = sizeof(digest);
  17135. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17136. unsigned char pDecrypted[2048/8];
  17137. int ret;
  17138. pt = pDecrypted;
  17139. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17140. XMEMSET(digest, 0, sizeof(digest));
  17141. XMEMSET(pSignature, 0, sizeof(pSignature));
  17142. if (ret == 0) {
  17143. ret = wc_InitRng(&rng);
  17144. }
  17145. if (ret == 0) {
  17146. ret = wc_RsaSetRNG(&key, &rng);
  17147. }
  17148. if (ret == 0) {
  17149. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17150. }
  17151. if (ret == 0) {
  17152. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17153. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17154. }
  17155. if (ret == 0) {
  17156. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  17157. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17158. if (ret > 0) {
  17159. sz = ret;
  17160. ret = 0;
  17161. }
  17162. }
  17163. /* Bad Cases */
  17164. if (ret == 0) {
  17165. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  17166. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17167. if (ret == BAD_FUNC_ARG) {
  17168. ret = 0;
  17169. }
  17170. }
  17171. if (ret == 0) {
  17172. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  17173. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17174. if (ret == BAD_FUNC_ARG) {
  17175. ret = 0;
  17176. }
  17177. }
  17178. if (ret == 0) {
  17179. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  17180. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17181. if (ret == BAD_FUNC_ARG) {
  17182. ret = 0;
  17183. }
  17184. }
  17185. if (ret == 0) {
  17186. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17187. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  17188. if (ret == BAD_FUNC_ARG) {
  17189. ret = 0;
  17190. }
  17191. }
  17192. /* Good case */
  17193. if (ret == 0) {
  17194. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17195. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17196. if (ret > 0) {
  17197. ret = 0;
  17198. }
  17199. }
  17200. wc_FreeRsaKey(&key);
  17201. wc_FreeRng(&rng);
  17202. res = TEST_RES_CHECK(ret == 0);
  17203. #endif
  17204. return res;
  17205. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17206. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17207. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17208. {
  17209. (void)flag;
  17210. (void)type;
  17211. (void)file;
  17212. (void)line;
  17213. }
  17214. #endif
  17215. /*
  17216. * Testing wc_LockMutex_ex
  17217. */
  17218. static int test_wc_LockMutex_ex(void)
  17219. {
  17220. int res = TEST_SKIPPED;
  17221. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17222. int ret = 0;
  17223. int flag = CRYPTO_LOCK;
  17224. int type = 0;
  17225. const char* file = "./test-LockMutex_ex.txt";
  17226. int line = 0;
  17227. /* without SetMutexCb */
  17228. ret = wc_LockMutex_ex(flag, type, file, line);
  17229. if (ret == BAD_STATE_E) {
  17230. ret = 0;
  17231. }
  17232. /* with SetMutexCb */
  17233. if (ret == 0) {
  17234. ret = wc_SetMutexCb(sample_mutex_cb);
  17235. if (ret == 0) {
  17236. ret = wc_LockMutex_ex(flag, type, file, line);
  17237. }
  17238. }
  17239. res = TEST_RES_CHECK(ret == 0);
  17240. #endif
  17241. return res;
  17242. }/*End test_wc_LockMutex_ex*/
  17243. /*
  17244. * Testing wc_SetMutexCb
  17245. */
  17246. static int test_wc_SetMutexCb(void)
  17247. {
  17248. int res = TEST_SKIPPED;
  17249. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17250. int ret = wc_SetMutexCb(sample_mutex_cb);
  17251. res = TEST_RES_CHECK(ret == 0);
  17252. #endif
  17253. return res;
  17254. }/*End test_wc_SetMutexCb*/
  17255. /*
  17256. * Testing wc_RsaKeyToDer()
  17257. */
  17258. static int test_wc_RsaKeyToDer(void)
  17259. {
  17260. int res = TEST_SKIPPED;
  17261. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17262. RsaKey genKey;
  17263. WC_RNG rng;
  17264. byte* der;
  17265. int ret = 0;
  17266. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17267. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17268. int bits = 1024;
  17269. word32 derSz = 611;
  17270. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17271. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17272. #else
  17273. int bits = 2048;
  17274. word32 derSz = 1196;
  17275. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17276. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17277. #endif
  17278. XMEMSET(&rng, 0, sizeof(rng));
  17279. XMEMSET(&genKey, 0, sizeof(genKey));
  17280. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17281. if (der == NULL) {
  17282. ret = WOLFSSL_FATAL_ERROR;
  17283. }
  17284. /* Init structures. */
  17285. if (ret == 0) {
  17286. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17287. }
  17288. if (ret == 0) {
  17289. ret = wc_InitRng(&rng);
  17290. }
  17291. /* Make key. */
  17292. if (ret == 0) {
  17293. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17294. if (ret != 0) {
  17295. ret = WOLFSSL_FATAL_ERROR;
  17296. }
  17297. }
  17298. if (ret == 0) {
  17299. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  17300. if (ret > 0) {
  17301. ret = 0;
  17302. }
  17303. else {
  17304. ret = WOLFSSL_FATAL_ERROR;
  17305. }
  17306. }
  17307. #ifndef HAVE_USER_RSA
  17308. /* Pass good/bad args. */
  17309. if (ret == 0) {
  17310. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17311. if (ret == BAD_FUNC_ARG) {
  17312. /* Get just the output length */
  17313. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17314. }
  17315. if (ret > 0) {
  17316. /* Try Public Key. */
  17317. genKey.type = 0;
  17318. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17319. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17320. /* Put back to Private Key */
  17321. genKey.type = 1;
  17322. #endif
  17323. }
  17324. if (ret == BAD_FUNC_ARG) {
  17325. ret = 0;
  17326. }
  17327. else {
  17328. ret = WOLFSSL_FATAL_ERROR;
  17329. }
  17330. }
  17331. #else
  17332. /* Pass good/bad args. */
  17333. if (ret == 0) {
  17334. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17335. if (ret == USER_CRYPTO_ERROR) {
  17336. /* Get just the output length */
  17337. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17338. }
  17339. if (ret > 0) {
  17340. /* Try Public Key. */
  17341. genKey.type = 0;
  17342. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17343. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17344. /* Put back to Private Key */
  17345. genKey.type = 1;
  17346. #endif
  17347. }
  17348. if (ret == USER_CRYPTO_ERROR) {
  17349. ret = 0;
  17350. }
  17351. else {
  17352. ret = WOLFSSL_FATAL_ERROR;
  17353. }
  17354. }
  17355. #endif
  17356. if (der != NULL) {
  17357. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17358. }
  17359. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  17360. ret = WOLFSSL_FATAL_ERROR;
  17361. }
  17362. if (wc_FreeRng(&rng) || ret != 0) {
  17363. ret = WOLFSSL_FATAL_ERROR;
  17364. }
  17365. res = TEST_RES_CHECK(ret == 0);
  17366. #endif
  17367. return res;
  17368. } /* END test_wc_RsaKeyToDer */
  17369. /*
  17370. * Testing wc_RsaKeyToPublicDer()
  17371. */
  17372. static int test_wc_RsaKeyToPublicDer(void)
  17373. {
  17374. int res = TEST_SKIPPED;
  17375. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17376. RsaKey key;
  17377. WC_RNG rng;
  17378. byte* der;
  17379. int ret = 0;
  17380. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17381. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17382. int bits = 1024;
  17383. word32 derLen = 162;
  17384. #else
  17385. int bits = 2048;
  17386. word32 derLen = 294;
  17387. #endif
  17388. XMEMSET(&rng, 0, sizeof(rng));
  17389. XMEMSET(&key, 0, sizeof(key));
  17390. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17391. if (der == NULL) {
  17392. ret = WOLFSSL_FATAL_ERROR;
  17393. }
  17394. if (ret == 0) {
  17395. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17396. }
  17397. if (ret == 0) {
  17398. ret = wc_InitRng(&rng);
  17399. }
  17400. if (ret == 0) {
  17401. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17402. }
  17403. if (ret == 0) {
  17404. /* test getting size only */
  17405. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  17406. if (ret >= 0)
  17407. ret = 0;
  17408. }
  17409. if (ret == 0) {
  17410. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  17411. if (ret >= 0) {
  17412. ret = 0;
  17413. }
  17414. else {
  17415. ret = WOLFSSL_FATAL_ERROR;
  17416. }
  17417. }
  17418. if (ret == 0) {
  17419. /* test getting size only */
  17420. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  17421. if (ret >= 0)
  17422. ret = 0;
  17423. }
  17424. if (ret == 0) {
  17425. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  17426. if (ret >= 0) {
  17427. ret = 0;
  17428. }
  17429. else {
  17430. ret = WOLFSSL_FATAL_ERROR;
  17431. }
  17432. }
  17433. #ifndef HAVE_USER_RSA
  17434. /* Pass in bad args. */
  17435. if (ret == 0) {
  17436. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  17437. if (ret == BAD_FUNC_ARG) {
  17438. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  17439. }
  17440. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  17441. ret = 0;
  17442. }
  17443. else {
  17444. ret = WOLFSSL_FATAL_ERROR;
  17445. }
  17446. }
  17447. #else
  17448. /* Pass in bad args. */
  17449. if (ret == 0) {
  17450. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  17451. if (ret == USER_CRYPTO_ERROR) {
  17452. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  17453. }
  17454. if (ret == USER_CRYPTO_ERROR) {
  17455. ret = 0;
  17456. }
  17457. else {
  17458. ret = WOLFSSL_FATAL_ERROR;
  17459. }
  17460. }
  17461. #endif
  17462. if (der != NULL) {
  17463. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17464. }
  17465. if (wc_FreeRsaKey(&key) || ret != 0) {
  17466. ret = WOLFSSL_FATAL_ERROR;
  17467. }
  17468. if (wc_FreeRng(&rng) || ret != 0) {
  17469. ret = WOLFSSL_FATAL_ERROR;
  17470. }
  17471. res = TEST_RES_CHECK(ret == 0);
  17472. #endif
  17473. return res;
  17474. } /* END test_wc_RsaKeyToPublicDer */
  17475. /*
  17476. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17477. */
  17478. static int test_wc_RsaPublicEncryptDecrypt(void)
  17479. {
  17480. int res = TEST_SKIPPED;
  17481. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17482. RsaKey key;
  17483. WC_RNG rng;
  17484. int ret = 0;
  17485. const char inStr[] = TEST_STRING;
  17486. const word32 plainLen = (word32)TEST_STRING_SZ;
  17487. const word32 inLen = (word32)TEST_STRING_SZ;
  17488. int bits = TEST_RSA_BITS;
  17489. const word32 cipherLen = TEST_RSA_BYTES;
  17490. word32 cipherLenResult = cipherLen;
  17491. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17492. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17493. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17494. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17495. if (in == NULL || plain == NULL || cipher == NULL) {
  17496. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  17497. return MEMORY_E;
  17498. }
  17499. #endif
  17500. XMEMCPY(in, inStr, inLen);
  17501. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17502. if (ret == 0) {
  17503. ret = wc_InitRng(&rng);
  17504. }
  17505. if (ret == 0) {
  17506. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17507. }
  17508. /* Encrypt. */
  17509. if (ret == 0) {
  17510. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  17511. if (ret >= 0) {
  17512. cipherLenResult = ret;
  17513. ret = 0;
  17514. }
  17515. else {
  17516. ret = WOLFSSL_FATAL_ERROR;
  17517. }
  17518. }
  17519. /* Pass bad args. */
  17520. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  17521. if (ret != 0) {
  17522. return TEST_FAIL;
  17523. }
  17524. /* Decrypt */
  17525. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17526. /* Bind rng */
  17527. if (ret == 0) {
  17528. ret = wc_RsaSetRNG(&key, &rng);
  17529. }
  17530. #endif
  17531. if (ret == 0) {
  17532. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  17533. }
  17534. if (ret >= 0) {
  17535. ret = XMEMCMP(plain, inStr, plainLen);
  17536. }
  17537. /* Pass in bad args. */
  17538. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  17539. WC_FREE_VAR(in, NULL);
  17540. WC_FREE_VAR(plain, NULL);
  17541. WC_FREE_VAR(cipher, NULL);
  17542. if (wc_FreeRsaKey(&key) || ret != 0) {
  17543. ret = WOLFSSL_FATAL_ERROR;
  17544. }
  17545. if (wc_FreeRng(&rng) || ret != 0) {
  17546. ret = WOLFSSL_FATAL_ERROR;
  17547. }
  17548. res = TEST_RES_CHECK(ret == 0);
  17549. #endif
  17550. return res;
  17551. } /* END test_wc_RsaPublicEncryptDecrypt */
  17552. /*
  17553. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17554. */
  17555. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17556. {
  17557. int result = TEST_SKIPPED;
  17558. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17559. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  17560. && !defined(NO_SHA256)
  17561. RsaKey key;
  17562. WC_RNG rng;
  17563. int ret;
  17564. const char inStr[] = TEST_STRING;
  17565. const word32 inLen = (word32)TEST_STRING_SZ;
  17566. const word32 plainSz = (word32)TEST_STRING_SZ;
  17567. byte* res = NULL;
  17568. int idx = 0;
  17569. int bits = TEST_RSA_BITS;
  17570. const word32 cipherSz = TEST_RSA_BYTES;
  17571. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17572. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17573. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17574. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17575. if (in == NULL || plain == NULL || cipher == NULL) {
  17576. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  17577. return TEST_FAIL;
  17578. }
  17579. #endif
  17580. XMEMCPY(in, inStr, inLen);
  17581. /* Initialize stack structures. */
  17582. XMEMSET(&rng, 0, sizeof(rng));
  17583. XMEMSET(&key, 0, sizeof(key));
  17584. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  17585. if (ret == 0) {
  17586. ret = wc_InitRng(&rng);
  17587. }
  17588. if (ret == 0) {
  17589. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17590. }
  17591. /* Encrypt */
  17592. if (ret == 0) {
  17593. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  17594. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  17595. if (ret >= 0) {
  17596. idx = ret;
  17597. ret = 0;
  17598. }
  17599. else {
  17600. ret = WOLFSSL_FATAL_ERROR;
  17601. }
  17602. }
  17603. /* Pass bad args. */
  17604. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  17605. if (ret != 0) {
  17606. return TEST_FAIL;
  17607. }
  17608. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17609. /* Decrypt */
  17610. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17611. if (ret == 0) {
  17612. ret = wc_RsaSetRNG(&key, &rng);
  17613. }
  17614. #endif
  17615. if (ret == 0) {
  17616. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  17617. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  17618. WC_MGF1SHA256, NULL, 0);
  17619. }
  17620. if (ret >= 0) {
  17621. if (!XMEMCMP(plain, inStr, plainSz)) {
  17622. ret = 0;
  17623. }
  17624. else {
  17625. ret = WOLFSSL_FATAL_ERROR;
  17626. }
  17627. }
  17628. /*Pass bad args.*/
  17629. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  17630. if (ret != 0) {
  17631. return TEST_FAIL;
  17632. }
  17633. if (ret == 0) {
  17634. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  17635. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  17636. WC_MGF1SHA256, NULL, 0);
  17637. if (ret >= 0) {
  17638. if (!XMEMCMP(inStr, res, plainSz)) {
  17639. ret = 0;
  17640. }
  17641. else {
  17642. ret = WOLFSSL_FATAL_ERROR;
  17643. }
  17644. }
  17645. }
  17646. #endif
  17647. WC_FREE_VAR(in, NULL);
  17648. WC_FREE_VAR(plain, NULL);
  17649. WC_FREE_VAR(cipher, NULL);
  17650. if (wc_FreeRsaKey(&key) || ret != 0) {
  17651. ret = WOLFSSL_FATAL_ERROR;
  17652. }
  17653. if (wc_FreeRng(&rng) || ret != 0) {
  17654. ret = WOLFSSL_FATAL_ERROR;
  17655. }
  17656. result = TEST_RES_CHECK(ret == 0);
  17657. #endif
  17658. return result;
  17659. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17660. /*
  17661. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17662. */
  17663. static int test_wc_RsaSSL_SignVerify(void)
  17664. {
  17665. int res = TEST_SKIPPED;
  17666. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17667. RsaKey key;
  17668. WC_RNG rng;
  17669. int ret = 0;
  17670. const char inStr[] = TEST_STRING;
  17671. const word32 plainSz = (word32)TEST_STRING_SZ;
  17672. const word32 inLen = (word32)TEST_STRING_SZ;
  17673. word32 idx = 0;
  17674. int bits = TEST_RSA_BITS;
  17675. const word32 outSz = TEST_RSA_BYTES;
  17676. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17677. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17678. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17679. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17680. if (in == NULL || out == NULL || plain == NULL) {
  17681. fprintf(stderr, "test_wc_RsaSSL_SignVerify failed\n");
  17682. return TEST_FAIL;
  17683. }
  17684. #endif
  17685. XMEMCPY(in, inStr, inLen);
  17686. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17687. if (ret == 0) {
  17688. ret = wc_InitRng(&rng);
  17689. }
  17690. if (ret == 0) {
  17691. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17692. }
  17693. /* Sign. */
  17694. if (ret == 0) {
  17695. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  17696. if (ret == (int)outSz) {
  17697. idx = ret;
  17698. ret = 0;
  17699. }
  17700. else {
  17701. ret = WOLFSSL_FATAL_ERROR;
  17702. }
  17703. }
  17704. #ifndef HAVE_USER_RSA
  17705. /* Test bad args. */
  17706. if (ret == 0) {
  17707. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17708. if (ret == BAD_FUNC_ARG) {
  17709. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17710. }
  17711. if (ret == BAD_FUNC_ARG) {
  17712. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17713. }
  17714. if (ret == BAD_FUNC_ARG) {
  17715. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17716. }
  17717. if (ret == BAD_FUNC_ARG) {
  17718. ret = 0;
  17719. }
  17720. else {
  17721. ret = WOLFSSL_FATAL_ERROR;
  17722. }
  17723. }
  17724. #else
  17725. /* Test bad args. */
  17726. if (ret == 0) {
  17727. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17728. if (ret == USER_CRYPTO_ERROR) {
  17729. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17730. }
  17731. if (ret == USER_CRYPTO_ERROR) {
  17732. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17733. }
  17734. if (ret == USER_CRYPTO_ERROR) {
  17735. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17736. }
  17737. if (ret == USER_CRYPTO_ERROR) {
  17738. ret = 0;
  17739. }
  17740. else {
  17741. ret = WOLFSSL_FATAL_ERROR;
  17742. }
  17743. }
  17744. #endif
  17745. if (ret != 0) {
  17746. return TEST_FAIL;
  17747. }
  17748. /* Verify. */
  17749. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  17750. if (ret == (int)inLen) {
  17751. ret = 0;
  17752. }
  17753. else {
  17754. ret = WOLFSSL_FATAL_ERROR;
  17755. }
  17756. #ifndef HAVE_USER_RSA
  17757. /* Pass bad args. */
  17758. if (ret == 0) {
  17759. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17760. if (ret == BAD_FUNC_ARG) {
  17761. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17762. }
  17763. if (ret == BAD_FUNC_ARG) {
  17764. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17765. }
  17766. if (ret == BAD_FUNC_ARG) {
  17767. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17768. }
  17769. if (ret == BAD_FUNC_ARG) {
  17770. ret = 0;
  17771. }
  17772. else {
  17773. ret = WOLFSSL_FATAL_ERROR;
  17774. }
  17775. }
  17776. #else
  17777. /* Pass bad args. */
  17778. if (ret == 0) {
  17779. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17780. if (ret == USER_CRYPTO_ERROR) {
  17781. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17782. }
  17783. if (ret == USER_CRYPTO_ERROR) {
  17784. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17785. }
  17786. if (ret == USER_CRYPTO_ERROR) {
  17787. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17788. }
  17789. if (ret == USER_CRYPTO_ERROR) {
  17790. ret = 0;
  17791. }
  17792. else {
  17793. ret = WOLFSSL_FATAL_ERROR;
  17794. }
  17795. }
  17796. #endif
  17797. WC_FREE_VAR(in, NULL);
  17798. WC_FREE_VAR(out, NULL);
  17799. WC_FREE_VAR(plain, NULL);
  17800. if (wc_FreeRsaKey(&key) || ret != 0) {
  17801. ret = WOLFSSL_FATAL_ERROR;
  17802. }
  17803. if (wc_FreeRng(&rng) || ret != 0) {
  17804. ret = WOLFSSL_FATAL_ERROR;
  17805. }
  17806. res = TEST_RES_CHECK(ret == 0);
  17807. #endif
  17808. return res;
  17809. } /* END test_wc_RsaSSL_SignVerify */
  17810. /*
  17811. * Testing wc_RsaEncryptSize()
  17812. */
  17813. static int test_wc_RsaEncryptSize(void)
  17814. {
  17815. int res = TEST_SKIPPED;
  17816. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17817. RsaKey key;
  17818. WC_RNG rng;
  17819. int ret;
  17820. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17821. if (ret == 0) {
  17822. ret = wc_InitRng(&rng);
  17823. }
  17824. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17825. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17826. if (ret == 0) {
  17827. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  17828. if (ret == 0) {
  17829. ret = wc_RsaEncryptSize(&key);
  17830. }
  17831. if (ret == 128) {
  17832. ret = 0;
  17833. }
  17834. else {
  17835. ret = WOLFSSL_FATAL_ERROR;
  17836. }
  17837. }
  17838. if (wc_FreeRsaKey(&key) || ret != 0) {
  17839. ret = WOLFSSL_FATAL_ERROR;
  17840. }
  17841. else {
  17842. ret = 0;
  17843. }
  17844. #endif
  17845. if (ret == 0) {
  17846. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  17847. if (ret == 0) {
  17848. ret = wc_RsaEncryptSize(&key);
  17849. }
  17850. if (ret == 256) {
  17851. ret = 0;
  17852. }
  17853. else {
  17854. ret = WOLFSSL_FATAL_ERROR;
  17855. }
  17856. }
  17857. /* Pass in bad arg. */
  17858. if (ret == 0) {
  17859. ret = wc_RsaEncryptSize(NULL);
  17860. #ifndef HAVE_USER_RSA
  17861. if (ret == BAD_FUNC_ARG) {
  17862. ret = 0;
  17863. }
  17864. else {
  17865. ret = WOLFSSL_FATAL_ERROR;
  17866. }
  17867. #endif
  17868. }
  17869. if (wc_FreeRsaKey(&key) || ret != 0) {
  17870. ret = WOLFSSL_FATAL_ERROR;
  17871. }
  17872. if (wc_FreeRng(&rng) || ret != 0) {
  17873. ret = WOLFSSL_FATAL_ERROR;
  17874. }
  17875. res = TEST_RES_CHECK(ret == 0);
  17876. #endif
  17877. return res;
  17878. } /* END test_wc_RsaEncryptSize*/
  17879. /*
  17880. * Testing wc_RsaFlattenPublicKey()
  17881. */
  17882. static int test_wc_RsaFlattenPublicKey(void)
  17883. {
  17884. int res = TEST_SKIPPED;
  17885. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17886. RsaKey key;
  17887. WC_RNG rng;
  17888. int ret = 0;
  17889. byte e[256];
  17890. byte n[256];
  17891. word32 eSz = sizeof(e);
  17892. word32 nSz = sizeof(n);
  17893. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17894. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17895. int bits = 1024;
  17896. #else
  17897. int bits = 2048;
  17898. #endif
  17899. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17900. if (ret == 0) {
  17901. ret = wc_InitRng(&rng);
  17902. }
  17903. if (ret == 0) {
  17904. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17905. if (ret >= 0) {
  17906. ret = 0;
  17907. }
  17908. else {
  17909. ret = WOLFSSL_FATAL_ERROR;
  17910. }
  17911. }
  17912. if (ret == 0) {
  17913. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  17914. }
  17915. #ifndef HAVE_USER_RSA
  17916. /* Pass bad args. */
  17917. if (ret == 0) {
  17918. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17919. if (ret == BAD_FUNC_ARG) {
  17920. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17921. }
  17922. if (ret == BAD_FUNC_ARG) {
  17923. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17924. }
  17925. if (ret == BAD_FUNC_ARG) {
  17926. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17927. }
  17928. if (ret == BAD_FUNC_ARG) {
  17929. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17930. }
  17931. if (ret == BAD_FUNC_ARG) {
  17932. ret = 0;
  17933. }
  17934. else {
  17935. ret = WOLFSSL_FATAL_ERROR;
  17936. }
  17937. }
  17938. #else
  17939. /* Pass bad args. */
  17940. if (ret == 0) {
  17941. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17942. if (ret == USER_CRYPTO_ERROR) {
  17943. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17944. }
  17945. if (ret == USER_CRYPTO_ERROR) {
  17946. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17947. }
  17948. if (ret == USER_CRYPTO_ERROR) {
  17949. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17950. }
  17951. if (ret == USER_CRYPTO_ERROR) {
  17952. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17953. }
  17954. if (ret == USER_CRYPTO_ERROR) {
  17955. ret = 0;
  17956. }
  17957. else {
  17958. ret = WOLFSSL_FATAL_ERROR;
  17959. }
  17960. }
  17961. #endif
  17962. if (wc_FreeRsaKey(&key) || ret != 0) {
  17963. ret = WOLFSSL_FATAL_ERROR;
  17964. }
  17965. if (wc_FreeRng(&rng) || ret != 0) {
  17966. ret = WOLFSSL_FATAL_ERROR;
  17967. }
  17968. res = TEST_RES_CHECK(ret == 0);
  17969. #endif
  17970. return res;
  17971. } /* END test_wc_RsaFlattenPublicKey */
  17972. /*
  17973. * unit test for wc_AesCcmSetKey
  17974. */
  17975. static int test_wc_AesCcmSetKey(void)
  17976. {
  17977. int res = TEST_SKIPPED;
  17978. #ifdef HAVE_AESCCM
  17979. Aes aes;
  17980. int ret = 0;
  17981. const byte key16[] =
  17982. {
  17983. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17984. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17985. };
  17986. const byte key24[] =
  17987. {
  17988. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17989. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17990. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17991. };
  17992. const byte key32[] =
  17993. {
  17994. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17995. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17996. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17997. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17998. };
  17999. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  18000. if (ret != 0)
  18001. return ret;
  18002. #ifdef WOLFSSL_AES_128
  18003. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  18004. #endif
  18005. #ifdef WOLFSSL_AES_192
  18006. if (ret == 0) {
  18007. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  18008. }
  18009. #endif
  18010. #ifdef WOLFSSL_AES_256
  18011. if (ret == 0) {
  18012. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  18013. }
  18014. #endif
  18015. /* Test bad args. */
  18016. if (ret == 0) {
  18017. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  18018. if (ret == BAD_FUNC_ARG) {
  18019. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  18020. }
  18021. if (ret == BAD_FUNC_ARG) {
  18022. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  18023. }
  18024. if (ret != BAD_FUNC_ARG) {
  18025. ret = WOLFSSL_FATAL_ERROR;
  18026. }
  18027. else {
  18028. ret = 0;
  18029. }
  18030. }
  18031. wc_AesFree(&aes);
  18032. res = TEST_RES_CHECK(ret == 0);
  18033. #endif
  18034. return res;
  18035. } /* END test_wc_AesCcmSetKey */
  18036. /*
  18037. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  18038. */
  18039. static int test_wc_AesCcmEncryptDecrypt(void)
  18040. {
  18041. int res = TEST_SKIPPED;
  18042. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  18043. Aes aes;
  18044. int ret = 0;
  18045. const byte key16[] =
  18046. {
  18047. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18048. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18049. };
  18050. /* plaintext */
  18051. const byte plainT[] =
  18052. {
  18053. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18054. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18055. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18056. };
  18057. /* nonce */
  18058. const byte iv[] =
  18059. {
  18060. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18061. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18062. };
  18063. const byte c[] = /* cipher text. */
  18064. {
  18065. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18066. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18067. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18068. };
  18069. const byte t[] = /* Auth tag */
  18070. {
  18071. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18072. };
  18073. const byte authIn[] =
  18074. {
  18075. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18076. };
  18077. byte cipherOut[sizeof(plainT)];
  18078. byte authTag[sizeof(t)];
  18079. int ccmE = WOLFSSL_FATAL_ERROR;
  18080. #ifdef HAVE_AES_DECRYPT
  18081. int ccmD = WOLFSSL_FATAL_ERROR;
  18082. byte plainOut[sizeof(cipherOut)];
  18083. #endif
  18084. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  18085. if (ret != 0)
  18086. return ret;
  18087. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  18088. if (ret == 0) {
  18089. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18090. iv, sizeof(iv), authTag, sizeof(authTag),
  18091. authIn , sizeof(authIn));
  18092. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  18093. XMEMCMP(t, authTag, sizeof(t))) {
  18094. ccmE = WOLFSSL_FATAL_ERROR;
  18095. ret = WOLFSSL_FATAL_ERROR;
  18096. }
  18097. #ifdef HAVE_AES_DECRYPT
  18098. if (ret == 0) {
  18099. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18100. sizeof(plainOut), iv, sizeof(iv),
  18101. authTag, sizeof(authTag),
  18102. authIn, sizeof(authIn));
  18103. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  18104. ccmD = WOLFSSL_FATAL_ERROR;
  18105. }
  18106. }
  18107. #endif
  18108. }
  18109. /* Pass in bad args. Encrypt*/
  18110. if (ret == 0 && ccmE == 0) {
  18111. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18112. iv, sizeof(iv), authTag, sizeof(authTag),
  18113. authIn , sizeof(authIn));
  18114. if (ccmE == BAD_FUNC_ARG) {
  18115. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18116. iv, sizeof(iv), authTag, sizeof(authTag),
  18117. authIn , sizeof(authIn));
  18118. }
  18119. if (ccmE == BAD_FUNC_ARG) {
  18120. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18121. iv, sizeof(iv), authTag, sizeof(authTag),
  18122. authIn , sizeof(authIn));
  18123. }
  18124. if (ccmE == BAD_FUNC_ARG) {
  18125. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18126. NULL, sizeof(iv), authTag, sizeof(authTag),
  18127. authIn , sizeof(authIn));
  18128. }
  18129. if (ccmE == BAD_FUNC_ARG) {
  18130. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18131. iv, sizeof(iv), NULL, sizeof(authTag),
  18132. authIn , sizeof(authIn));
  18133. }
  18134. if (ccmE == BAD_FUNC_ARG) {
  18135. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18136. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  18137. authIn , sizeof(authIn));
  18138. }
  18139. if (ccmE == BAD_FUNC_ARG) {
  18140. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18141. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  18142. authIn , sizeof(authIn));
  18143. }
  18144. if (ccmE != BAD_FUNC_ARG) {
  18145. ccmE = WOLFSSL_FATAL_ERROR;
  18146. }
  18147. else {
  18148. ccmE = 0;
  18149. }
  18150. } /* End Encrypt */
  18151. if (ccmE != 0) {
  18152. wc_AesFree(&aes);
  18153. return TEST_FAIL;
  18154. }
  18155. #ifdef HAVE_AES_DECRYPT
  18156. /* Pass in bad args. Decrypt*/
  18157. if (ret == 0 && ccmD == 0) {
  18158. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18159. iv, sizeof(iv), authTag, sizeof(authTag),
  18160. authIn, sizeof(authIn));
  18161. if (ccmD == BAD_FUNC_ARG) {
  18162. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18163. iv, sizeof(iv), authTag, sizeof(authTag),
  18164. authIn, sizeof(authIn));
  18165. }
  18166. if (ccmD == BAD_FUNC_ARG) {
  18167. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18168. iv, sizeof(iv), authTag, sizeof(authTag),
  18169. authIn, sizeof(authIn));
  18170. }
  18171. if (ccmD == BAD_FUNC_ARG) {
  18172. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18173. sizeof(plainOut), NULL, sizeof(iv),
  18174. authTag, sizeof(authTag),
  18175. authIn, sizeof(authIn));
  18176. }
  18177. if (ccmD == BAD_FUNC_ARG) {
  18178. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18179. sizeof(plainOut), iv, sizeof(iv), NULL,
  18180. sizeof(authTag), authIn, sizeof(authIn));
  18181. }
  18182. if (ccmD == BAD_FUNC_ARG) {
  18183. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18184. sizeof(plainOut), iv, sizeof(iv) + 1,
  18185. authTag, sizeof(authTag),
  18186. authIn, sizeof(authIn));
  18187. }
  18188. if (ccmD == BAD_FUNC_ARG) {
  18189. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18190. sizeof(plainOut), iv, sizeof(iv) - 7,
  18191. authTag, sizeof(authTag),
  18192. authIn, sizeof(authIn));
  18193. }
  18194. if (ccmD != BAD_FUNC_ARG) {
  18195. ccmD = WOLFSSL_FATAL_ERROR;
  18196. }
  18197. else {
  18198. ccmD = 0;
  18199. }
  18200. } /* END Decrypt */
  18201. res = TEST_RES_CHECK(ccmD == 0);
  18202. #endif
  18203. wc_AesFree(&aes);
  18204. #endif /* HAVE_AESCCM */
  18205. return res;
  18206. } /* END test_wc_AesCcmEncryptDecrypt */
  18207. /*
  18208. * Testing wc_InitDsaKey()
  18209. */
  18210. static int test_wc_InitDsaKey(void)
  18211. {
  18212. int res = TEST_SKIPPED;
  18213. #ifndef NO_DSA
  18214. DsaKey key;
  18215. int ret = 0;
  18216. ret = wc_InitDsaKey(&key);
  18217. /* Pass in bad args. */
  18218. if (ret == 0) {
  18219. ret = wc_InitDsaKey(NULL);
  18220. if (ret == BAD_FUNC_ARG) {
  18221. ret = 0;
  18222. }
  18223. else {
  18224. ret = WOLFSSL_FATAL_ERROR;
  18225. }
  18226. }
  18227. wc_FreeDsaKey(&key);
  18228. res = TEST_RES_CHECK(ret == 0);
  18229. #endif
  18230. return res;
  18231. } /* END test_wc_InitDsaKey */
  18232. /*
  18233. * Testing wc_DsaSign() and wc_DsaVerify()
  18234. */
  18235. static int test_wc_DsaSignVerify(void)
  18236. {
  18237. int res = TEST_SKIPPED;
  18238. #if !defined(NO_DSA)
  18239. DsaKey key;
  18240. WC_RNG rng;
  18241. wc_Sha sha;
  18242. int ret = 0;
  18243. byte signature[DSA_SIG_SIZE];
  18244. byte hash[WC_SHA_DIGEST_SIZE];
  18245. word32 idx = 0;
  18246. word32 bytes;
  18247. int answer;
  18248. #ifdef USE_CERT_BUFFERS_1024
  18249. byte tmp[ONEK_BUF];
  18250. XMEMSET(tmp, 0, sizeof(tmp));
  18251. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18252. bytes = sizeof_dsa_key_der_1024;
  18253. #elif defined(USE_CERT_BUFFERS_2048)
  18254. byte tmp[TWOK_BUF];
  18255. XMEMSET(tmp, 0, sizeof(tmp));
  18256. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18257. bytes = sizeof_dsa_key_der_2048;
  18258. #else
  18259. byte tmp[TWOK_BUF];
  18260. XMEMSET(tmp, 0, sizeof(tmp));
  18261. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18262. if (fp == XBADFILE) {
  18263. return WOLFSSL_BAD_FILE;
  18264. }
  18265. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18266. XFCLOSE(fp);
  18267. #endif /* END USE_CERT_BUFFERS_1024 */
  18268. ret = wc_InitSha(&sha);
  18269. if (ret == 0) {
  18270. ret = wc_ShaUpdate(&sha, tmp, bytes);
  18271. if (ret == 0) {
  18272. ret = wc_ShaFinal(&sha, hash);
  18273. }
  18274. if (ret == 0) {
  18275. ret = wc_InitDsaKey(&key);
  18276. }
  18277. if (ret == 0) {
  18278. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18279. }
  18280. if (ret == 0) {
  18281. ret = wc_InitRng(&rng);
  18282. }
  18283. }
  18284. /* Sign. */
  18285. if (ret == 0) {
  18286. ret = wc_DsaSign(hash, signature, &key, &rng);
  18287. }
  18288. /* Test bad args. */
  18289. if (ret == 0) {
  18290. ret = wc_DsaSign(NULL, signature, &key, &rng);
  18291. if (ret == BAD_FUNC_ARG) {
  18292. ret = wc_DsaSign(hash, NULL, &key, &rng);
  18293. }
  18294. if (ret == BAD_FUNC_ARG) {
  18295. ret = wc_DsaSign(hash, signature, NULL, &rng);
  18296. }
  18297. if (ret == BAD_FUNC_ARG) {
  18298. ret = wc_DsaSign(hash, signature, &key, NULL);
  18299. }
  18300. if (ret == BAD_FUNC_ARG) {
  18301. ret = 0;
  18302. }
  18303. else {
  18304. ret = WOLFSSL_FATAL_ERROR;
  18305. }
  18306. }
  18307. if (ret == 0) {
  18308. /* Verify. */
  18309. ret = wc_DsaVerify(hash, signature, &key, &answer);
  18310. if (ret != 0 || answer != 1) {
  18311. ret = WOLFSSL_FATAL_ERROR;
  18312. }
  18313. else {
  18314. ret = 0;
  18315. }
  18316. }
  18317. /* Pass in bad args. */
  18318. if (ret == 0) {
  18319. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  18320. if (ret == BAD_FUNC_ARG) {
  18321. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  18322. }
  18323. if (ret == BAD_FUNC_ARG) {
  18324. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  18325. }
  18326. if (ret == BAD_FUNC_ARG) {
  18327. ret = wc_DsaVerify(hash, signature, &key, NULL);
  18328. }
  18329. if (ret == BAD_FUNC_ARG) {
  18330. ret = 0;
  18331. }
  18332. else {
  18333. ret = WOLFSSL_FATAL_ERROR;
  18334. }
  18335. }
  18336. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18337. /* hard set q to 0 and test fail case */
  18338. mp_free(&key.q);
  18339. mp_init(&key.q);
  18340. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18341. mp_set(&key.q, 1);
  18342. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18343. #endif
  18344. if (wc_FreeRng(&rng) && ret == 0) {
  18345. ret = WOLFSSL_FATAL_ERROR;
  18346. }
  18347. wc_FreeDsaKey(&key);
  18348. wc_ShaFree(&sha);
  18349. res = TEST_RES_CHECK(ret == 0);
  18350. #endif
  18351. return res;
  18352. } /* END test_wc_DsaSign */
  18353. /*
  18354. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18355. */
  18356. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18357. {
  18358. int res = TEST_SKIPPED;
  18359. #if !defined(NO_DSA)
  18360. DsaKey key;
  18361. word32 bytes;
  18362. word32 idx = 0;
  18363. int priv = 0;
  18364. int pub = 0;
  18365. int ret = 0;
  18366. #ifdef USE_CERT_BUFFERS_1024
  18367. byte tmp[ONEK_BUF];
  18368. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18369. bytes = sizeof_dsa_key_der_1024;
  18370. #elif defined(USE_CERT_BUFFERS_2048)
  18371. byte tmp[TWOK_BUF];
  18372. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18373. bytes = sizeof_dsa_key_der_2048;
  18374. #else
  18375. byte tmp[TWOK_BUF];
  18376. XMEMSET(tmp, 0, sizeof(tmp));
  18377. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18378. if (fp == XBADFILE)
  18379. {
  18380. return WOLFSSL_BAD_FILE;
  18381. }
  18382. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18383. XFCLOSE(fp);
  18384. #endif /* END USE_CERT_BUFFERS_1024 */
  18385. ret = wc_InitDsaKey(&key);
  18386. if (ret == 0) {
  18387. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18388. /* Test bad args. */
  18389. if (priv == 0) {
  18390. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  18391. if (priv == BAD_FUNC_ARG) {
  18392. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  18393. }
  18394. if (priv == BAD_FUNC_ARG) {
  18395. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  18396. }
  18397. if (priv == BAD_FUNC_ARG) {
  18398. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18399. }
  18400. if (priv == ASN_PARSE_E || priv == BUFFER_E) {
  18401. priv = 0;
  18402. }
  18403. else {
  18404. priv = WOLFSSL_FATAL_ERROR;
  18405. }
  18406. }
  18407. wc_FreeDsaKey(&key);
  18408. ret = wc_InitDsaKey(&key);
  18409. }
  18410. if (ret == 0) {
  18411. idx = 0; /* Reset */
  18412. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18413. /* Test bad args. */
  18414. if (pub == 0) {
  18415. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  18416. if (pub == BAD_FUNC_ARG) {
  18417. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  18418. }
  18419. if (pub == BAD_FUNC_ARG) {
  18420. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  18421. }
  18422. if (pub == BAD_FUNC_ARG) {
  18423. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18424. }
  18425. if (pub == ASN_PARSE_E || pub == BUFFER_E) {
  18426. pub = 0;
  18427. }
  18428. else {
  18429. pub = WOLFSSL_FATAL_ERROR;
  18430. }
  18431. }
  18432. } /* END Public Key */
  18433. wc_FreeDsaKey(&key);
  18434. res = TEST_RES_CHECK(ret == 0 && pub == 0 && priv == 0);
  18435. #endif /* !NO_DSA */
  18436. return res;
  18437. } /* END test_wc_DsaPublicPrivateKeyDecode */
  18438. /*
  18439. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  18440. */
  18441. static int test_wc_MakeDsaKey(void)
  18442. {
  18443. int res = TEST_SKIPPED;
  18444. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18445. DsaKey genKey;
  18446. WC_RNG rng;
  18447. int ret = 0;
  18448. XMEMSET(&rng, 0, sizeof(rng));
  18449. XMEMSET(&genKey, 0, sizeof(genKey));
  18450. ret = wc_InitRng(&rng);
  18451. if (ret == 0) {
  18452. ret = wc_InitDsaKey(&genKey);
  18453. }
  18454. if (ret == 0) {
  18455. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  18456. }
  18457. /* Test bad args. */
  18458. if (ret == 0) {
  18459. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  18460. if (ret == BAD_FUNC_ARG) {
  18461. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  18462. }
  18463. if (ret == BAD_FUNC_ARG) {
  18464. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  18465. }
  18466. if (ret == BAD_FUNC_ARG) {
  18467. ret = 0;
  18468. }
  18469. else {
  18470. ret = WOLFSSL_FATAL_ERROR;
  18471. }
  18472. }
  18473. if (ret == 0) {
  18474. ret = wc_MakeDsaKey(&rng, &genKey);
  18475. }
  18476. /* Test bad args. */
  18477. if (ret == 0) {
  18478. ret = wc_MakeDsaKey(NULL, &genKey);
  18479. if (ret == BAD_FUNC_ARG) {
  18480. ret = wc_MakeDsaKey(&rng, NULL);
  18481. }
  18482. if (ret == BAD_FUNC_ARG) {
  18483. ret = 0;
  18484. }
  18485. else {
  18486. ret = WOLFSSL_FATAL_ERROR;
  18487. }
  18488. }
  18489. if (wc_FreeRng(&rng) && ret == 0) {
  18490. ret = WOLFSSL_FAILURE;
  18491. }
  18492. wc_FreeDsaKey(&genKey);
  18493. res = TEST_RES_CHECK(ret == 0);
  18494. #endif
  18495. return res;
  18496. } /* END test_wc_MakeDsaKey */
  18497. /*
  18498. * Testing wc_DsaKeyToDer()
  18499. */
  18500. static int test_wc_DsaKeyToDer(void)
  18501. {
  18502. int res = TEST_SKIPPED;
  18503. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18504. DsaKey genKey;
  18505. WC_RNG rng;
  18506. word32 bytes;
  18507. word32 idx = 0;
  18508. int ret = 0;
  18509. #ifdef USE_CERT_BUFFERS_1024
  18510. byte tmp[ONEK_BUF];
  18511. byte der[ONEK_BUF];
  18512. XMEMSET(tmp, 0, sizeof(tmp));
  18513. XMEMSET(der, 0, sizeof(der));
  18514. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18515. bytes = sizeof_dsa_key_der_1024;
  18516. #elif defined(USE_CERT_BUFFERS_2048)
  18517. byte tmp[TWOK_BUF];
  18518. byte der[TWOK_BUF];
  18519. XMEMSET(tmp, 0, sizeof(tmp));
  18520. XMEMSET(der, 0, sizeof(der));
  18521. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18522. bytes = sizeof_dsa_key_der_2048;
  18523. #else
  18524. byte tmp[TWOK_BUF];
  18525. byte der[TWOK_BUF];
  18526. XMEMSET(tmp, 0, sizeof(tmp));
  18527. XMEMSET(der, 0, sizeof(der));
  18528. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18529. if (fp == XBADFILE) {
  18530. return WOLFSSL_BAD_FILE;
  18531. }
  18532. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18533. XFCLOSE(fp);
  18534. #endif /* END USE_CERT_BUFFERS_1024 */
  18535. XMEMSET(&rng, 0, sizeof(rng));
  18536. XMEMSET(&genKey, 0, sizeof(genKey));
  18537. ret = wc_InitRng(&rng);
  18538. if (ret == 0) {
  18539. ret = wc_InitDsaKey(&genKey);
  18540. }
  18541. if (ret == 0) {
  18542. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  18543. if (ret == 0) {
  18544. wc_FreeDsaKey(&genKey);
  18545. ret = wc_InitDsaKey(&genKey);
  18546. }
  18547. }
  18548. if (ret == 0) {
  18549. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  18550. }
  18551. if (ret == 0) {
  18552. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  18553. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  18554. ret = 0;
  18555. }
  18556. }
  18557. /* Test bad args. */
  18558. if (ret == 0) {
  18559. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  18560. if (ret == BAD_FUNC_ARG) {
  18561. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  18562. }
  18563. if (ret == BAD_FUNC_ARG) {
  18564. ret = 0;
  18565. }
  18566. else {
  18567. ret = WOLFSSL_FATAL_ERROR;
  18568. }
  18569. }
  18570. if (wc_FreeRng(&rng) && ret == 0) {
  18571. ret = WOLFSSL_FATAL_ERROR;
  18572. }
  18573. wc_FreeDsaKey(&genKey);
  18574. res = TEST_RES_CHECK(ret == 0);
  18575. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18576. return res;
  18577. } /* END test_wc_DsaKeyToDer */
  18578. /*
  18579. * Testing wc_DsaKeyToPublicDer()
  18580. * (indirectly testing setDsaPublicKey())
  18581. */
  18582. static int test_wc_DsaKeyToPublicDer(void)
  18583. {
  18584. int res = TEST_SKIPPED;
  18585. #ifndef HAVE_SELFTEST
  18586. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18587. DsaKey genKey;
  18588. WC_RNG rng;
  18589. byte* der;
  18590. word32 sz;
  18591. int ret = 0;
  18592. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18593. if (der == NULL) {
  18594. ret = WOLFSSL_FATAL_ERROR;
  18595. }
  18596. if (ret == 0) {
  18597. ret = wc_InitDsaKey(&genKey);
  18598. }
  18599. if (ret == 0) {
  18600. ret = wc_InitRng(&rng);
  18601. }
  18602. if (ret == 0) {
  18603. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  18604. }
  18605. if (ret == 0) {
  18606. ret = wc_MakeDsaKey(&rng, &genKey);
  18607. }
  18608. if (ret == 0) {
  18609. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  18610. if (ret >= 0) {
  18611. sz = ret;
  18612. ret = 0;
  18613. }
  18614. else {
  18615. ret = WOLFSSL_FATAL_ERROR;
  18616. }
  18617. }
  18618. if (ret == 0) {
  18619. word32 idx = 0;
  18620. wc_FreeDsaKey(&genKey);
  18621. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  18622. }
  18623. /* Test without the SubjectPublicKeyInfo header */
  18624. if (ret == 0) {
  18625. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  18626. if (ret >= 0) {
  18627. sz = ret;
  18628. ret = 0;
  18629. }
  18630. else {
  18631. ret = WOLFSSL_FATAL_ERROR;
  18632. }
  18633. }
  18634. if (ret == 0) {
  18635. word32 idx = 0;
  18636. wc_FreeDsaKey(&genKey);
  18637. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  18638. }
  18639. /* Test bad args. */
  18640. if (ret == 0) {
  18641. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  18642. if (ret == BAD_FUNC_ARG) {
  18643. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  18644. }
  18645. if (ret == BAD_FUNC_ARG) {
  18646. ret = 0;
  18647. }
  18648. else {
  18649. ret = WOLFSSL_FATAL_ERROR;
  18650. }
  18651. }
  18652. if (wc_FreeRng(&rng) && ret == 0) {
  18653. ret = WOLFSSL_FATAL_ERROR;
  18654. }
  18655. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18656. wc_FreeDsaKey(&genKey);
  18657. res = TEST_RES_CHECK(ret == 0);
  18658. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18659. #endif /* !HAVE_SELFTEST */
  18660. return res;
  18661. } /* END test_wc_DsaKeyToPublicDer */
  18662. /*
  18663. * Testing wc_DsaImportParamsRaw()
  18664. */
  18665. static int test_wc_DsaImportParamsRaw(void)
  18666. {
  18667. int res = TEST_SKIPPED;
  18668. #if !defined(NO_DSA)
  18669. DsaKey key;
  18670. int ret = 0;
  18671. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18672. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18673. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18674. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18675. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18676. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18677. "80a0093113a8bd73";
  18678. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18679. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18680. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18681. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18682. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18683. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18684. "76341a7e7d9";
  18685. /* invalid p and q parameters */
  18686. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18687. const char* invalidQ = "96c5390a";
  18688. ret = wc_InitDsaKey(&key);
  18689. if (ret == 0) {
  18690. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18691. }
  18692. /* test bad args */
  18693. if (ret == 0) {
  18694. /* null key struct */
  18695. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  18696. if (ret == BAD_FUNC_ARG) {
  18697. /* null param pointers */
  18698. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  18699. }
  18700. if (ret == BAD_FUNC_ARG) {
  18701. /* illegal p length */
  18702. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  18703. }
  18704. if (ret == BAD_FUNC_ARG) {
  18705. /* illegal q length */
  18706. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  18707. if (ret == BAD_FUNC_ARG)
  18708. ret = 0;
  18709. }
  18710. }
  18711. wc_FreeDsaKey(&key);
  18712. res = TEST_RES_CHECK(ret == 0);
  18713. #endif
  18714. return res;
  18715. } /* END test_wc_DsaImportParamsRaw */
  18716. /*
  18717. * Testing wc_DsaImportParamsRawCheck()
  18718. */
  18719. static int test_wc_DsaImportParamsRawCheck(void)
  18720. {
  18721. int res = TEST_SKIPPED;
  18722. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18723. DsaKey key;
  18724. int ret = 0;
  18725. int trusted = 0;
  18726. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18727. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18728. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18729. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18730. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18731. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18732. "80a0093113a8bd73";
  18733. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18734. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18735. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18736. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18737. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18738. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18739. "76341a7e7d9";
  18740. /* invalid p and q parameters */
  18741. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18742. const char* invalidQ = "96c5390a";
  18743. ret = wc_InitDsaKey(&key);
  18744. if (ret == 0) {
  18745. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  18746. }
  18747. /* test bad args */
  18748. if (ret == 0) {
  18749. /* null key struct */
  18750. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  18751. if (ret == BAD_FUNC_ARG) {
  18752. /* null param pointers */
  18753. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  18754. }
  18755. if (ret == BAD_FUNC_ARG) {
  18756. /* illegal p length */
  18757. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  18758. }
  18759. if (ret == BAD_FUNC_ARG) {
  18760. /* illegal q length */
  18761. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  18762. if (ret == BAD_FUNC_ARG)
  18763. ret = 0;
  18764. }
  18765. }
  18766. wc_FreeDsaKey(&key);
  18767. res = TEST_RES_CHECK(ret == 0);
  18768. #endif
  18769. return res;
  18770. } /* END test_wc_DsaImportParamsRawCheck */
  18771. /*
  18772. * Testing wc_DsaExportParamsRaw()
  18773. */
  18774. static int test_wc_DsaExportParamsRaw(void)
  18775. {
  18776. int res = TEST_SKIPPED;
  18777. #if !defined(NO_DSA)
  18778. DsaKey key;
  18779. int ret = 0;
  18780. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18781. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18782. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18783. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18784. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18785. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18786. "80a0093113a8bd73";
  18787. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18788. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18789. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18790. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18791. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18792. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18793. "76341a7e7d9";
  18794. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18795. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18796. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18797. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18798. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18799. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18800. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18801. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18802. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18803. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18804. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18805. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18806. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18807. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18808. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18809. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18810. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18811. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18812. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18813. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18814. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18815. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18816. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18817. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18818. byte pOut[MAX_DSA_PARAM_SIZE];
  18819. byte qOut[MAX_DSA_PARAM_SIZE];
  18820. byte gOut[MAX_DSA_PARAM_SIZE];
  18821. word32 pOutSz, qOutSz, gOutSz;
  18822. ret = wc_InitDsaKey(&key);
  18823. if (ret == 0) {
  18824. /* first test using imported raw parameters, for expected */
  18825. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18826. }
  18827. if (ret == 0) {
  18828. pOutSz = sizeof(pOut);
  18829. qOutSz = sizeof(qOut);
  18830. gOutSz = sizeof(gOut);
  18831. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18832. gOut, &gOutSz);
  18833. }
  18834. if (ret == 0) {
  18835. /* validate exported parameters are correct */
  18836. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  18837. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  18838. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  18839. ret = -1;
  18840. }
  18841. }
  18842. /* test bad args */
  18843. if (ret == 0) {
  18844. /* null key struct */
  18845. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  18846. gOut, &gOutSz);
  18847. if (ret == BAD_FUNC_ARG) {
  18848. /* null output pointers */
  18849. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  18850. NULL, &gOutSz);
  18851. }
  18852. if (ret == LENGTH_ONLY_E) {
  18853. /* null output size pointers */
  18854. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  18855. gOut, NULL);
  18856. }
  18857. if (ret == BAD_FUNC_ARG) {
  18858. /* p output buffer size too small */
  18859. pOutSz = 1;
  18860. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18861. gOut, &gOutSz);
  18862. pOutSz = sizeof(pOut);
  18863. }
  18864. if (ret == BUFFER_E) {
  18865. /* q output buffer size too small */
  18866. qOutSz = 1;
  18867. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18868. gOut, &gOutSz);
  18869. qOutSz = sizeof(qOut);
  18870. }
  18871. if (ret == BUFFER_E) {
  18872. /* g output buffer size too small */
  18873. gOutSz = 1;
  18874. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18875. gOut, &gOutSz);
  18876. if (ret == BUFFER_E)
  18877. ret = 0;
  18878. }
  18879. }
  18880. wc_FreeDsaKey(&key);
  18881. res = TEST_RES_CHECK(ret == 0);
  18882. #endif
  18883. return res;
  18884. } /* END test_wc_DsaExportParamsRaw */
  18885. /*
  18886. * Testing wc_DsaExportKeyRaw()
  18887. */
  18888. static int test_wc_DsaExportKeyRaw(void)
  18889. {
  18890. int res = TEST_SKIPPED;
  18891. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18892. DsaKey key;
  18893. WC_RNG rng;
  18894. int ret = 0;
  18895. byte xOut[MAX_DSA_PARAM_SIZE];
  18896. byte yOut[MAX_DSA_PARAM_SIZE];
  18897. word32 xOutSz, yOutSz;
  18898. XMEMSET(&rng, 0, sizeof(rng));
  18899. XMEMSET(&key, 0, sizeof(key));
  18900. ret = wc_InitRng(&rng);
  18901. if (ret == 0) {
  18902. ret = wc_InitDsaKey(&key);
  18903. }
  18904. if (ret == 0) {
  18905. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  18906. if (ret == 0) {
  18907. ret = wc_MakeDsaKey(&rng, &key);
  18908. }
  18909. }
  18910. /* try successful export */
  18911. if (ret == 0) {
  18912. xOutSz = sizeof(xOut);
  18913. yOutSz = sizeof(yOut);
  18914. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18915. }
  18916. /* test bad args */
  18917. if (ret == 0) {
  18918. /* null key struct */
  18919. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  18920. if (ret == BAD_FUNC_ARG) {
  18921. /* null output pointers */
  18922. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  18923. }
  18924. if (ret == LENGTH_ONLY_E) {
  18925. /* null output size pointers */
  18926. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  18927. }
  18928. if (ret == BAD_FUNC_ARG) {
  18929. /* x output buffer size too small */
  18930. xOutSz = 1;
  18931. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18932. xOutSz = sizeof(xOut);
  18933. }
  18934. if (ret == BUFFER_E) {
  18935. /* y output buffer size too small */
  18936. yOutSz = 1;
  18937. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18938. if (ret == BUFFER_E)
  18939. ret = 0;
  18940. }
  18941. }
  18942. wc_FreeDsaKey(&key);
  18943. wc_FreeRng(&rng);
  18944. res = TEST_RES_CHECK(ret == 0);
  18945. #endif
  18946. return res;
  18947. } /* END test_wc_DsaExportParamsRaw */
  18948. /*
  18949. * Testing wc_ed25519_make_key().
  18950. */
  18951. static int test_wc_ed25519_make_key(void)
  18952. {
  18953. int res = TEST_SKIPPED;
  18954. #if defined(HAVE_ED25519)
  18955. ed25519_key key;
  18956. WC_RNG rng;
  18957. unsigned char pubkey[ED25519_PUB_KEY_SIZE];
  18958. int ret = 0;
  18959. ret = wc_InitRng(&rng);
  18960. if (ret == 0) {
  18961. ret = wc_ed25519_init(&key);
  18962. }
  18963. if (ret == 0) {
  18964. ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
  18965. if (ret == ECC_PRIV_KEY_E) {
  18966. ret = 0;
  18967. }
  18968. else if (ret == 0) {
  18969. ret = -1;
  18970. }
  18971. }
  18972. if (ret == 0) {
  18973. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18974. }
  18975. /* Test bad args. */
  18976. if (ret == 0) {
  18977. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  18978. if (ret == BAD_FUNC_ARG) {
  18979. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  18980. }
  18981. if (ret == BAD_FUNC_ARG) {
  18982. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  18983. }
  18984. if (ret == BAD_FUNC_ARG) {
  18985. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  18986. }
  18987. if (ret == BAD_FUNC_ARG) {
  18988. ret = 0;
  18989. }
  18990. else if (ret == 0) {
  18991. ret = WOLFSSL_FATAL_ERROR;
  18992. }
  18993. }
  18994. if (wc_FreeRng(&rng) && ret == 0) {
  18995. ret = WOLFSSL_FATAL_ERROR;
  18996. }
  18997. wc_ed25519_free(&key);
  18998. res = TEST_RES_CHECK(ret == 0);
  18999. #endif
  19000. return res;
  19001. } /* END test_wc_ed25519_make_key */
  19002. /*
  19003. * Testing wc_ed25519_init()
  19004. */
  19005. static int test_wc_ed25519_init(void)
  19006. {
  19007. int res = TEST_SKIPPED;
  19008. #if defined(HAVE_ED25519)
  19009. ed25519_key key;
  19010. int ret = 0;
  19011. ret = wc_ed25519_init(&key);
  19012. /* Test bad args. */
  19013. if (ret == 0) {
  19014. ret = wc_ed25519_init(NULL);
  19015. if (ret == BAD_FUNC_ARG) {
  19016. ret = 0;
  19017. }
  19018. else if (ret == 0) {
  19019. ret = WOLFSSL_FATAL_ERROR;
  19020. }
  19021. }
  19022. wc_ed25519_free(&key);
  19023. res = TEST_RES_CHECK(ret == 0);
  19024. #endif
  19025. return res;
  19026. } /* END test_wc_ed25519_init */
  19027. /*
  19028. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19029. */
  19030. static int test_wc_ed25519_sign_msg(void)
  19031. {
  19032. int res = TEST_SKIPPED;
  19033. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19034. WC_RNG rng;
  19035. ed25519_key key;
  19036. int ret = 0;
  19037. byte msg[] = "Everybody gets Friday off.\n";
  19038. byte sig[ED25519_SIG_SIZE];
  19039. word32 msglen = sizeof(msg);
  19040. word32 siglen = sizeof(sig);
  19041. word32 badSigLen = sizeof(sig) - 1;
  19042. #ifdef HAVE_ED25519_VERIFY
  19043. int verify_ok = 0; /*1 = Verify success.*/
  19044. #endif
  19045. /* Initialize stack variables. */
  19046. XMEMSET(sig, 0, siglen);
  19047. /* Initialize key. */
  19048. ret = wc_InitRng(&rng);
  19049. if (ret == 0) {
  19050. ret = wc_ed25519_init(&key);
  19051. if (ret == 0) {
  19052. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19053. }
  19054. }
  19055. if (ret == 0) {
  19056. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  19057. }
  19058. /* Test bad args. */
  19059. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  19060. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  19061. if (ret == BAD_FUNC_ARG) {
  19062. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  19063. }
  19064. if (ret == BAD_FUNC_ARG) {
  19065. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  19066. }
  19067. if (ret == BAD_FUNC_ARG) {
  19068. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  19069. }
  19070. if (ret == BAD_FUNC_ARG) {
  19071. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  19072. }
  19073. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  19074. badSigLen -= 1;
  19075. ret = 0;
  19076. }
  19077. else if (ret == 0) {
  19078. ret = WOLFSSL_FATAL_ERROR;
  19079. }
  19080. } /* END sign */
  19081. #ifdef HAVE_ED25519_VERIFY
  19082. if (ret == 0) {
  19083. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  19084. if (ret == 0 && verify_ok == 1) {
  19085. ret = 0;
  19086. }
  19087. else if (ret == 0) {
  19088. ret = WOLFSSL_FATAL_ERROR;
  19089. }
  19090. /* Test bad args. */
  19091. if (ret == 0) {
  19092. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  19093. msglen, &verify_ok, &key),
  19094. BAD_FUNC_ARG);
  19095. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  19096. msglen, &verify_ok, &key),
  19097. BAD_FUNC_ARG);
  19098. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19099. &key);
  19100. if (ret == BAD_FUNC_ARG) {
  19101. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  19102. &verify_ok, &key);
  19103. }
  19104. if (ret == BAD_FUNC_ARG) {
  19105. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19106. NULL, &key);
  19107. }
  19108. if (ret == BAD_FUNC_ARG) {
  19109. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19110. &verify_ok, NULL);
  19111. }
  19112. if (ret == BAD_FUNC_ARG) {
  19113. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  19114. &verify_ok, &key);
  19115. }
  19116. if (ret == BAD_FUNC_ARG) {
  19117. ret = 0;
  19118. }
  19119. else if (ret == 0) {
  19120. ret = WOLFSSL_FATAL_ERROR;
  19121. }
  19122. }
  19123. } /* END verify. */
  19124. #endif /* Verify. */
  19125. if (wc_FreeRng(&rng) && ret == 0) {
  19126. ret = WOLFSSL_FATAL_ERROR;
  19127. }
  19128. wc_ed25519_free(&key);
  19129. res = TEST_RES_CHECK(ret == 0);
  19130. #endif
  19131. return res;
  19132. } /* END test_wc_ed25519_sign_msg */
  19133. /*
  19134. * Testing wc_ed25519_import_public()
  19135. */
  19136. static int test_wc_ed25519_import_public(void)
  19137. {
  19138. int res = TEST_SKIPPED;
  19139. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19140. WC_RNG rng;
  19141. ed25519_key pubKey;
  19142. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19143. word32 inlen = sizeof(in);
  19144. int ret = 0;
  19145. ret = wc_InitRng(&rng);
  19146. if (ret == 0) {
  19147. ret = wc_ed25519_init(&pubKey);
  19148. if (ret == 0) {
  19149. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  19150. }
  19151. }
  19152. if (ret == 0) {
  19153. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  19154. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19155. ret = 0;
  19156. }
  19157. else {
  19158. ret = WOLFSSL_FATAL_ERROR;
  19159. }
  19160. /* Test bad args. */
  19161. if (ret == 0) {
  19162. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  19163. if (ret == BAD_FUNC_ARG) {
  19164. ret = wc_ed25519_import_public(in, inlen, NULL);
  19165. }
  19166. if (ret == BAD_FUNC_ARG) {
  19167. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  19168. }
  19169. if (ret == BAD_FUNC_ARG) {
  19170. ret = 0;
  19171. }
  19172. else if (ret == 0) {
  19173. ret = WOLFSSL_FATAL_ERROR;
  19174. }
  19175. }
  19176. }
  19177. if (wc_FreeRng(&rng) && ret == 0) {
  19178. ret = WOLFSSL_FATAL_ERROR;
  19179. }
  19180. wc_ed25519_free(&pubKey);
  19181. res = TEST_RES_CHECK(ret == 0);
  19182. #endif
  19183. return res;
  19184. } /* END wc_ed25519_import_public */
  19185. /*
  19186. * Testing wc_ed25519_import_private_key()
  19187. */
  19188. static int test_wc_ed25519_import_private_key(void)
  19189. {
  19190. int res = TEST_SKIPPED;
  19191. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19192. WC_RNG rng;
  19193. ed25519_key key;
  19194. int ret;
  19195. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19196. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19197. word32 privKeySz = sizeof(privKey);
  19198. word32 pubKeySz = sizeof(pubKey);
  19199. #ifdef HAVE_ED25519_KEY_EXPORT
  19200. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19201. word32 bothKeysSz = sizeof(bothKeys);
  19202. #endif
  19203. ret = wc_InitRng(&rng);
  19204. if (ret != 0) {
  19205. return ret;
  19206. }
  19207. ret = wc_ed25519_init(&key);
  19208. if (ret != 0) {
  19209. wc_FreeRng(&rng);
  19210. return ret;
  19211. }
  19212. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19213. if (ret == 0) {
  19214. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19215. pubKeySz, &key, 1);
  19216. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19217. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19218. ret = WOLFSSL_FATAL_ERROR;
  19219. }
  19220. }
  19221. #ifdef HAVE_ED25519_KEY_EXPORT
  19222. if (ret == 0)
  19223. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  19224. if (ret == 0) {
  19225. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19226. &key, 1);
  19227. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19228. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19229. ret = WOLFSSL_FATAL_ERROR;
  19230. }
  19231. }
  19232. #endif
  19233. /* Test bad args. */
  19234. if (ret == 0) {
  19235. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19236. &key);
  19237. if (ret == BAD_FUNC_ARG) {
  19238. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19239. pubKeySz, &key);
  19240. }
  19241. if (ret == BAD_FUNC_ARG) {
  19242. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19243. pubKeySz, NULL);
  19244. }
  19245. if (ret == BAD_FUNC_ARG) {
  19246. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19247. pubKeySz, &key);
  19248. }
  19249. if (ret == BAD_FUNC_ARG) {
  19250. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19251. pubKeySz - 1, &key);
  19252. }
  19253. if (ret == BAD_FUNC_ARG) {
  19254. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19255. 0, &key);
  19256. }
  19257. if (ret == BAD_FUNC_ARG) {
  19258. ret = 0;
  19259. }
  19260. else if (ret == 0) {
  19261. ret = WOLFSSL_FATAL_ERROR;
  19262. }
  19263. }
  19264. if (wc_FreeRng(&rng) && ret == 0) {
  19265. ret = WOLFSSL_FATAL_ERROR;
  19266. }
  19267. wc_ed25519_free(&key);
  19268. res = TEST_RES_CHECK(ret == 0);
  19269. #endif
  19270. return res;
  19271. } /* END test_wc_ed25519_import_private_key */
  19272. /*
  19273. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19274. */
  19275. static int test_wc_ed25519_export(void)
  19276. {
  19277. int res = TEST_SKIPPED;
  19278. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19279. WC_RNG rng;
  19280. ed25519_key key;
  19281. int ret = 0;
  19282. byte priv[ED25519_PRV_KEY_SIZE];
  19283. byte pub[ED25519_PUB_KEY_SIZE];
  19284. word32 privSz = sizeof(priv);
  19285. word32 pubSz = sizeof(pub);
  19286. ret = wc_InitRng(&rng);
  19287. if (ret != 0) {
  19288. return ret;
  19289. }
  19290. ret = wc_ed25519_init(&key);
  19291. if (ret != 0) {
  19292. wc_FreeRng(&rng);
  19293. return ret;
  19294. }
  19295. if (ret == 0) {
  19296. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19297. }
  19298. if (ret == 0) {
  19299. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  19300. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  19301. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  19302. ret = WOLFSSL_FATAL_ERROR;
  19303. }
  19304. if (ret == 0) {
  19305. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  19306. if (ret == BAD_FUNC_ARG) {
  19307. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  19308. }
  19309. if (ret == BAD_FUNC_ARG) {
  19310. ret = wc_ed25519_export_public(&key, pub, NULL);
  19311. }
  19312. if (ret == BAD_FUNC_ARG) {
  19313. ret = 0;
  19314. }
  19315. else if (ret == 0) {
  19316. ret = WOLFSSL_FATAL_ERROR;
  19317. }
  19318. }
  19319. }
  19320. if (ret == 0) {
  19321. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  19322. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  19323. || XMEMCMP(key.k, priv, privSz) != 0)) {
  19324. ret = WOLFSSL_FATAL_ERROR;
  19325. }
  19326. if (ret == 0) {
  19327. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  19328. if (ret == BAD_FUNC_ARG) {
  19329. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  19330. }
  19331. if (ret == BAD_FUNC_ARG) {
  19332. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  19333. }
  19334. if (ret == BAD_FUNC_ARG) {
  19335. ret = 0;
  19336. }
  19337. else if (ret == 0) {
  19338. ret = WOLFSSL_FATAL_ERROR;
  19339. }
  19340. }
  19341. }
  19342. if (wc_FreeRng(&rng) && ret == 0) {
  19343. ret = WOLFSSL_FATAL_ERROR;
  19344. }
  19345. wc_ed25519_free(&key);
  19346. res = TEST_RES_CHECK(ret == 0);
  19347. #endif
  19348. return res;
  19349. } /* END test_wc_ed25519_export */
  19350. /*
  19351. * Testing wc_ed25519_size()
  19352. */
  19353. static int test_wc_ed25519_size(void)
  19354. {
  19355. int res = TEST_SKIPPED;
  19356. #if defined(HAVE_ED25519)
  19357. WC_RNG rng;
  19358. ed25519_key key;
  19359. int ret;
  19360. ret = wc_InitRng(&rng);
  19361. if (ret != 0) {
  19362. return ret;
  19363. }
  19364. ret = wc_ed25519_init(&key);
  19365. if (ret != 0) {
  19366. wc_FreeRng(&rng);
  19367. return ret;
  19368. }
  19369. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19370. if (ret != 0) {
  19371. wc_FreeRng(&rng);
  19372. wc_ed25519_free(&key);
  19373. return ret;
  19374. }
  19375. ret = wc_ed25519_size(&key);
  19376. /* Test bad args. */
  19377. if (ret == ED25519_KEY_SIZE) {
  19378. ret = wc_ed25519_size(NULL);
  19379. if (ret == BAD_FUNC_ARG) {
  19380. ret = 0;
  19381. }
  19382. }
  19383. if (ret == 0) {
  19384. ret = wc_ed25519_sig_size(&key);
  19385. if (ret == ED25519_SIG_SIZE) {
  19386. ret = 0;
  19387. }
  19388. /* Test bad args. */
  19389. if (ret == 0) {
  19390. ret = wc_ed25519_sig_size(NULL);
  19391. if (ret == BAD_FUNC_ARG) {
  19392. ret = 0;
  19393. }
  19394. }
  19395. } /* END wc_ed25519_sig_size() */
  19396. if (ret == 0) {
  19397. ret = wc_ed25519_pub_size(&key);
  19398. if (ret == ED25519_PUB_KEY_SIZE) {
  19399. ret = 0;
  19400. }
  19401. if (ret == 0) {
  19402. ret = wc_ed25519_pub_size(NULL);
  19403. if (ret == BAD_FUNC_ARG) {
  19404. ret = 0;
  19405. }
  19406. }
  19407. } /* END wc_ed25519_pub_size */
  19408. if (ret == 0) {
  19409. ret = wc_ed25519_priv_size(&key);
  19410. if (ret == ED25519_PRV_KEY_SIZE) {
  19411. ret = 0;
  19412. }
  19413. if (ret == 0) {
  19414. ret = wc_ed25519_priv_size(NULL);
  19415. if (ret == BAD_FUNC_ARG) {
  19416. ret = 0;
  19417. }
  19418. }
  19419. } /* END wc_ed25519_pub_size */
  19420. if (wc_FreeRng(&rng) && ret == 0) {
  19421. ret = WOLFSSL_FATAL_ERROR;
  19422. }
  19423. wc_ed25519_free(&key);
  19424. res = TEST_RES_CHECK(ret == 0);
  19425. #endif
  19426. return res;
  19427. } /* END test_wc_ed25519_size */
  19428. /*
  19429. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19430. */
  19431. static int test_wc_ed25519_exportKey(void)
  19432. {
  19433. int res = TEST_SKIPPED;
  19434. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19435. WC_RNG rng;
  19436. ed25519_key key;
  19437. int ret = 0;
  19438. byte priv[ED25519_PRV_KEY_SIZE];
  19439. byte pub[ED25519_PUB_KEY_SIZE];
  19440. byte privOnly[ED25519_PRV_KEY_SIZE];
  19441. word32 privSz = sizeof(priv);
  19442. word32 pubSz = sizeof(pub);
  19443. word32 privOnlySz = sizeof(privOnly);
  19444. ret = wc_InitRng(&rng);
  19445. if (ret != 0) {
  19446. return TEST_FAIL;
  19447. }
  19448. ret = wc_ed25519_init(&key);
  19449. if (ret != 0) {
  19450. wc_FreeRng(&rng);
  19451. return TEST_FAIL;
  19452. }
  19453. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19454. if (ret != 0) {
  19455. wc_FreeRng(&rng);
  19456. wc_ed25519_free(&key);
  19457. return TEST_FAIL;
  19458. }
  19459. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  19460. if (ret == 0) {
  19461. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  19462. if (ret == BAD_FUNC_ARG) {
  19463. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  19464. }
  19465. if (ret == BAD_FUNC_ARG) {
  19466. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  19467. }
  19468. if (ret == BAD_FUNC_ARG) {
  19469. ret = 0;
  19470. }
  19471. else if (ret == 0) {
  19472. ret = WOLFSSL_FATAL_ERROR;
  19473. }
  19474. }
  19475. if (ret == 0) {
  19476. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  19477. if (ret == 0) {
  19478. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  19479. if (ret == BAD_FUNC_ARG) {
  19480. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  19481. }
  19482. if (ret == BAD_FUNC_ARG) {
  19483. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  19484. }
  19485. if (ret == BAD_FUNC_ARG) {
  19486. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  19487. }
  19488. if (ret == BAD_FUNC_ARG) {
  19489. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  19490. }
  19491. if (ret == BAD_FUNC_ARG) {
  19492. ret = 0;
  19493. }
  19494. else if (ret == 0) {
  19495. ret = WOLFSSL_FATAL_ERROR;
  19496. }
  19497. }
  19498. } /* END wc_ed25519_export_key() */
  19499. /* Cross check output. */
  19500. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  19501. ret = WOLFSSL_FATAL_ERROR;
  19502. }
  19503. if (wc_FreeRng(&rng) && ret == 0) {
  19504. ret = WOLFSSL_FATAL_ERROR;
  19505. }
  19506. wc_ed25519_free(&key);
  19507. res = TEST_RES_CHECK(ret == 0);
  19508. #endif
  19509. return res;
  19510. } /* END test_wc_ed25519_exportKey */
  19511. /*
  19512. * Testing wc_Ed25519PublicKeyToDer
  19513. */
  19514. static int test_wc_Ed25519PublicKeyToDer(void)
  19515. {
  19516. int res = TEST_SKIPPED;
  19517. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19518. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19519. int tmp;
  19520. ed25519_key key;
  19521. byte derBuf[1024];
  19522. int ret = 0;
  19523. /* Test bad args */
  19524. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  19525. if (tmp != BAD_FUNC_ARG) {
  19526. ret = WOLFSSL_FATAL_ERROR;
  19527. }
  19528. if (ret == 0) {
  19529. wc_ed25519_init(&key);
  19530. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  19531. if (tmp != BUFFER_E) {
  19532. ret = WOLFSSL_FATAL_ERROR;
  19533. }
  19534. wc_ed25519_free(&key);
  19535. }
  19536. /* Test good args */
  19537. if (ret == 0) {
  19538. WC_RNG rng;
  19539. ret = wc_InitRng(&rng);
  19540. if (ret != 0) {
  19541. return TEST_FAIL;
  19542. }
  19543. ret = wc_ed25519_init(&key);
  19544. if (ret != 0) {
  19545. wc_FreeRng(&rng);
  19546. return TEST_FAIL;
  19547. }
  19548. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19549. if (ret != 0) {
  19550. wc_FreeRng(&rng);
  19551. wc_ed25519_free(&key);
  19552. return TEST_FAIL;
  19553. }
  19554. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  19555. if (tmp <= 0) {
  19556. ret = WOLFSSL_FATAL_ERROR;
  19557. }
  19558. wc_FreeRng(&rng);
  19559. wc_ed25519_free(&key);
  19560. }
  19561. res = TEST_RES_CHECK(ret == 0);
  19562. #endif
  19563. return res;
  19564. } /* END testing wc_Ed25519PublicKeyToDer */
  19565. /*
  19566. * Testing wc_curve25519_init and wc_curve25519_free.
  19567. */
  19568. static int test_wc_curve25519_init(void)
  19569. {
  19570. int res = TEST_SKIPPED;
  19571. #if defined(HAVE_CURVE25519)
  19572. curve25519_key key;
  19573. int ret = 0;
  19574. ret = wc_curve25519_init(&key);
  19575. /* Test bad args for wc_curve25519_init */
  19576. if (ret == 0) {
  19577. ret = wc_curve25519_init(NULL);
  19578. if (ret == BAD_FUNC_ARG) {
  19579. ret = 0;
  19580. }
  19581. else if (ret == 0) {
  19582. ret = WOLFSSL_FATAL_ERROR;
  19583. }
  19584. }
  19585. /* Test good args for wc_curve_25519_free */
  19586. wc_curve25519_free(&key);
  19587. wc_curve25519_free(NULL);
  19588. res = TEST_RES_CHECK(ret == 0);
  19589. #endif
  19590. return res;
  19591. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19592. /*
  19593. * Testing test_wc_curve25519_size.
  19594. */
  19595. static int test_wc_curve25519_size(void)
  19596. {
  19597. int res = TEST_SKIPPED;
  19598. #if defined(HAVE_CURVE25519)
  19599. curve25519_key key;
  19600. int ret = 0;
  19601. ret = wc_curve25519_init(&key);
  19602. /* Test good args for wc_curve25519_size */
  19603. if (ret == 0) {
  19604. ret = wc_curve25519_size(&key);
  19605. }
  19606. /* Test bad args for wc_curve25519_size */
  19607. if (ret != 0) {
  19608. ret = wc_curve25519_size(NULL);
  19609. }
  19610. wc_curve25519_free(&key);
  19611. res = TEST_RES_CHECK(ret == 0);
  19612. #endif
  19613. return res;
  19614. } /* END test_wc_curve25519_size*/
  19615. /*
  19616. * Testing test_wc_curve25519_export_key_raw().
  19617. */
  19618. static int test_wc_curve25519_export_key_raw(void)
  19619. {
  19620. int res = TEST_SKIPPED;
  19621. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19622. curve25519_key key;
  19623. WC_RNG rng;
  19624. int ret = 0;
  19625. byte privateKey[CURVE25519_KEYSIZE];
  19626. byte publicKey[CURVE25519_KEYSIZE];
  19627. word32 prvkSz;
  19628. word32 pubkSz;
  19629. byte prik[CURVE25519_KEYSIZE];
  19630. byte pubk[CURVE25519_KEYSIZE];
  19631. word32 prksz;
  19632. word32 pbksz;
  19633. if (0 != wc_InitRng(&rng)) {
  19634. return TEST_FAIL;
  19635. }
  19636. if (0 != wc_curve25519_init(&key)) {
  19637. wc_FreeRng(&rng);
  19638. return TEST_FAIL;
  19639. }
  19640. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19641. /*
  19642. bad-argument-test cases
  19643. target function sould return BAD_FUNC_ARG
  19644. */
  19645. if (ret == 0) {
  19646. prvkSz = CURVE25519_KEYSIZE;
  19647. pubkSz = CURVE25519_KEYSIZE;
  19648. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19649. NULL, privateKey, &prvkSz, publicKey, &pubkSz)) {
  19650. ret = -1;
  19651. }
  19652. }
  19653. if (ret == 0) {
  19654. prvkSz = CURVE25519_KEYSIZE;
  19655. pubkSz = CURVE25519_KEYSIZE;
  19656. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19657. &key, NULL, &prvkSz, publicKey, &pubkSz)) {
  19658. ret = -1;
  19659. }
  19660. }
  19661. if (ret == 0) {
  19662. prvkSz = CURVE25519_KEYSIZE;
  19663. pubkSz = CURVE25519_KEYSIZE;
  19664. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19665. &key, privateKey, NULL, publicKey, &pubkSz)) {
  19666. ret = -1;
  19667. }
  19668. }
  19669. if (ret == 0) {
  19670. /* prvkSz = CURVE25519_KEYSIZE; */
  19671. pubkSz = CURVE25519_KEYSIZE;
  19672. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19673. &key, privateKey, &prvkSz, NULL, &pubkSz)) {
  19674. ret = -1;
  19675. }
  19676. }
  19677. if (ret == 0) {
  19678. prvkSz = CURVE25519_KEYSIZE;
  19679. pubkSz = CURVE25519_KEYSIZE;
  19680. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19681. &key, privateKey, &prvkSz, publicKey, NULL )) {
  19682. ret = -1;
  19683. }
  19684. }
  19685. /*
  19686. cross-testing
  19687. */
  19688. if (ret == 0) {
  19689. prksz = CURVE25519_KEYSIZE;
  19690. ret = wc_curve25519_export_private_raw(&key, prik, &prksz);
  19691. }
  19692. if (ret == 0) {
  19693. pbksz = CURVE25519_KEYSIZE;
  19694. ret = wc_curve25519_export_public(&key, pubk, &pbksz);
  19695. }
  19696. if (ret == 0) {
  19697. prvkSz = CURVE25519_KEYSIZE;
  19698. /* pubkSz = CURVE25519_KEYSIZE; */
  19699. ret = wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19700. publicKey, &pubkSz);
  19701. }
  19702. if (ret == 0) {
  19703. if ((prksz == CURVE25519_KEYSIZE) &&
  19704. (pbksz == CURVE25519_KEYSIZE) &&
  19705. (prvkSz == CURVE25519_KEYSIZE) &&
  19706. (pubkSz == CURVE25519_KEYSIZE)) {
  19707. if (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  19708. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)) {
  19709. ret = -1;
  19710. }
  19711. }
  19712. }
  19713. wc_curve25519_free(&key);
  19714. wc_FreeRng(&rng);
  19715. res = TEST_RES_CHECK(ret == 0);
  19716. #endif
  19717. return res;
  19718. } /* end of test_wc_curve25519_export_key_raw */
  19719. /*
  19720. * Testing test_wc_curve25519_export_key_raw_ex().
  19721. */
  19722. static int test_wc_curve25519_export_key_raw_ex(void)
  19723. {
  19724. int res = TEST_SKIPPED;
  19725. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19726. curve25519_key key;
  19727. WC_RNG rng;
  19728. int ret;
  19729. byte privateKey[CURVE25519_KEYSIZE];
  19730. byte publicKey[CURVE25519_KEYSIZE];
  19731. word32 prvkSz;
  19732. word32 pubkSz;
  19733. byte prik[CURVE25519_KEYSIZE];
  19734. byte pubk[CURVE25519_KEYSIZE];
  19735. word32 prksz;
  19736. word32 pbksz;
  19737. if (0 != wc_InitRng(&rng)) {
  19738. return TEST_FAIL;
  19739. }
  19740. if (0 != wc_curve25519_init(&key)) {
  19741. wc_FreeRng(&rng);
  19742. return TEST_FAIL;
  19743. }
  19744. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19745. /*
  19746. bad-argument-test cases
  19747. target function sould return BAD_FUNC_ARG
  19748. */
  19749. if (ret == 0) {
  19750. prvkSz = CURVE25519_KEYSIZE;
  19751. pubkSz = CURVE25519_KEYSIZE;
  19752. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  19753. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19754. ret = -1;
  19755. }
  19756. }
  19757. if (ret == 0) {
  19758. prvkSz = CURVE25519_KEYSIZE;
  19759. pubkSz = CURVE25519_KEYSIZE;
  19760. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  19761. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19762. ret = -1;
  19763. }
  19764. }
  19765. if (ret == 0) {
  19766. prvkSz = CURVE25519_KEYSIZE;
  19767. pubkSz = CURVE25519_KEYSIZE;
  19768. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  19769. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19770. ret = -1;
  19771. }
  19772. }
  19773. if (ret == 0) {
  19774. /* prvkSz = CURVE25519_KEYSIZE; */
  19775. pubkSz = CURVE25519_KEYSIZE;
  19776. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19777. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19778. ret = -1;
  19779. }
  19780. }
  19781. if (ret == 0) {
  19782. prvkSz = CURVE25519_KEYSIZE;
  19783. pubkSz = CURVE25519_KEYSIZE;
  19784. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19785. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)) {
  19786. ret = -1;
  19787. }
  19788. }
  19789. if (ret == 0) {
  19790. prvkSz = CURVE25519_KEYSIZE;
  19791. /* pubkSz = CURVE25519_KEYSIZE; */
  19792. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  19793. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19794. ret = -1;
  19795. }
  19796. }
  19797. if (ret == 0) {
  19798. prvkSz = CURVE25519_KEYSIZE;
  19799. pubkSz = CURVE25519_KEYSIZE;
  19800. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL,
  19801. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19802. ret = -1;
  19803. }
  19804. }
  19805. if (ret == 0) {
  19806. prvkSz = CURVE25519_KEYSIZE;
  19807. pubkSz = CURVE25519_KEYSIZE;
  19808. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19809. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19810. ret = -1;
  19811. }
  19812. }
  19813. if (ret == 0) {
  19814. /* prvkSz = CURVE25519_KEYSIZE; */
  19815. pubkSz = CURVE25519_KEYSIZE;
  19816. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19817. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)) {
  19818. ret = -1;
  19819. }
  19820. }
  19821. if (ret == 0) {
  19822. prvkSz = CURVE25519_KEYSIZE;
  19823. pubkSz = CURVE25519_KEYSIZE;
  19824. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19825. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)) {
  19826. ret = -1;
  19827. }
  19828. }
  19829. /* illegal value for endien */
  19830. if (ret == 0) {
  19831. prvkSz = CURVE25519_KEYSIZE;
  19832. /* pubkSz = CURVE25519_KEYSIZE; */
  19833. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex(&key, privateKey,
  19834. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10)) {
  19835. ret = -1;
  19836. }
  19837. }
  19838. /*
  19839. cross-testing
  19840. */
  19841. if (ret == 0) {
  19842. prksz = CURVE25519_KEYSIZE;
  19843. ret = wc_curve25519_export_private_raw( &key, prik, &prksz);
  19844. }
  19845. if (ret == 0) {
  19846. pbksz = CURVE25519_KEYSIZE;
  19847. ret = wc_curve25519_export_public( &key, pubk, &pbksz);
  19848. }
  19849. if (ret == 0) {
  19850. prvkSz = CURVE25519_KEYSIZE;
  19851. /* pubkSz = CURVE25519_KEYSIZE; */
  19852. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19853. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  19854. }
  19855. if (ret == 0 && (prksz != CURVE25519_KEYSIZE ||
  19856. pbksz != CURVE25519_KEYSIZE ||
  19857. prvkSz != CURVE25519_KEYSIZE ||
  19858. pubkSz != CURVE25519_KEYSIZE)) {
  19859. ret = -1;
  19860. }
  19861. if (ret == 0 && (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  19862. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE))) {
  19863. ret = -1;
  19864. }
  19865. if (ret == 0) {
  19866. ret = wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19867. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN);
  19868. }
  19869. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  19870. pubkSz != CURVE25519_KEYSIZE)) {
  19871. ret = -1;
  19872. }
  19873. /*
  19874. try once with another endian
  19875. */
  19876. if (ret == 0) {
  19877. prvkSz = CURVE25519_KEYSIZE;
  19878. pubkSz = CURVE25519_KEYSIZE;
  19879. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19880. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  19881. }
  19882. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  19883. pubkSz != CURVE25519_KEYSIZE)) {
  19884. ret = -1;
  19885. }
  19886. wc_curve25519_free(&key);
  19887. wc_FreeRng(&rng);
  19888. res = TEST_RES_CHECK(ret == 0);
  19889. #endif
  19890. return res;
  19891. } /* end of test_wc_curve25519_export_key_raw_ex */
  19892. /*
  19893. * Testing wc_curve25519_make_key
  19894. */
  19895. static int test_wc_curve25519_make_key(void)
  19896. {
  19897. int res = TEST_SKIPPED;
  19898. #if defined(HAVE_CURVE25519)
  19899. WC_RNG rng;
  19900. curve25519_key key;
  19901. int keysize;
  19902. int ret;
  19903. ret = wc_curve25519_init(&key);
  19904. if (ret == 0) {
  19905. ret = wc_InitRng(&rng);
  19906. }
  19907. if (ret == 0) {
  19908. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19909. if (ret == 0) {
  19910. keysize = wc_curve25519_size(&key);
  19911. if (keysize != CURVE25519_KEYSIZE) {
  19912. ret = WOLFSSL_FATAL_ERROR;
  19913. }
  19914. }
  19915. if (ret == 0) {
  19916. ret = wc_curve25519_make_key(&rng, keysize, &key);
  19917. }
  19918. }
  19919. /*test bad cases*/
  19920. if (ret == 0) {
  19921. ret = wc_curve25519_make_key(NULL, 0, NULL);
  19922. if (ret == BAD_FUNC_ARG) {
  19923. ret = 0;
  19924. }
  19925. }
  19926. if (ret == 0) {
  19927. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  19928. if (ret == BAD_FUNC_ARG) {
  19929. ret = 0;
  19930. }
  19931. }
  19932. if (ret == 0) {
  19933. ret = wc_curve25519_make_key(NULL, keysize, &key);
  19934. if (ret == BAD_FUNC_ARG) {
  19935. ret = 0;
  19936. }
  19937. }
  19938. if (ret == 0) {
  19939. ret = wc_curve25519_make_key(&rng, 0, &key);
  19940. if (ret == ECC_BAD_ARG_E) {
  19941. ret = 0;
  19942. }
  19943. }
  19944. wc_curve25519_free(&key);
  19945. wc_FreeRng(&rng);
  19946. res = TEST_RES_CHECK(ret == 0);
  19947. #endif
  19948. return res;
  19949. } /*END test_wc_curve25519_make_key*/
  19950. /*
  19951. * Testing wc_curve25519_shared_secret_ex
  19952. */
  19953. static int test_wc_curve25519_shared_secret_ex(void)
  19954. {
  19955. int res = TEST_SKIPPED;
  19956. #if defined(HAVE_CURVE25519)
  19957. WC_RNG rng;
  19958. curve25519_key private_key, public_key;
  19959. byte out[CURVE25519_KEYSIZE];
  19960. word32 outLen = sizeof(out);
  19961. int endian = EC25519_BIG_ENDIAN;
  19962. int ret;
  19963. ret = wc_curve25519_init(&private_key);
  19964. if (ret == 0) {
  19965. ret = wc_curve25519_init(&public_key);
  19966. }
  19967. if (ret == 0) {
  19968. ret = wc_InitRng(&rng);
  19969. }
  19970. if (ret == 0) {
  19971. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  19972. }
  19973. if (ret == 0) {
  19974. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  19975. }
  19976. if (ret == 0) {
  19977. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19978. &outLen, endian);
  19979. }
  19980. /*test bad cases*/
  19981. if (ret == 0) {
  19982. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  19983. 0, endian);
  19984. if (ret == 0) {
  19985. ret = -1;
  19986. }
  19987. if (ret == BAD_FUNC_ARG) {
  19988. ret = 0;
  19989. }
  19990. }
  19991. if (ret == 0) {
  19992. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  19993. &outLen, endian);
  19994. if (ret == 0) {
  19995. ret = -1;
  19996. }
  19997. else if (ret == BAD_FUNC_ARG) {
  19998. ret = 0;
  19999. }
  20000. }
  20001. if (ret == 0) {
  20002. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  20003. &outLen, endian);
  20004. if (ret == 0) {
  20005. ret = -1;
  20006. }
  20007. else if (ret == BAD_FUNC_ARG) {
  20008. ret = 0;
  20009. }
  20010. }
  20011. if (ret == 0) {
  20012. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20013. &outLen, endian);
  20014. if (ret == 0) {
  20015. ret = -1;
  20016. }
  20017. else if (ret == BAD_FUNC_ARG) {
  20018. ret = 0;
  20019. }
  20020. }
  20021. if (ret == 0) {
  20022. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20023. NULL, endian);
  20024. if (ret == 0) {
  20025. ret = -1;
  20026. }
  20027. else if (ret == BAD_FUNC_ARG) {
  20028. ret = 0;
  20029. }
  20030. }
  20031. if (ret == 0) {
  20032. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  20033. *increasing to 0x8f checks for error being returned*/
  20034. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20035. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20036. &outLen, endian);
  20037. if (ret == 0) {
  20038. ret = -1;
  20039. }
  20040. else if (ret == ECC_BAD_ARG_E) {
  20041. ret = 0;
  20042. }
  20043. }
  20044. outLen = outLen - 2;
  20045. if (ret == 0) {
  20046. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20047. &outLen, endian);
  20048. if (ret == 0) {
  20049. ret = -1;
  20050. }
  20051. else if (ret == BAD_FUNC_ARG) {
  20052. ret = 0;
  20053. }
  20054. }
  20055. wc_curve25519_free(&private_key);
  20056. wc_curve25519_free(&public_key);
  20057. wc_FreeRng(&rng);
  20058. res = TEST_RES_CHECK(ret == 0);
  20059. #endif
  20060. return res;
  20061. } /*END test_wc_curve25519_shared_secret_ex*/
  20062. /*
  20063. * Testing wc_curve25519_make_pub
  20064. */
  20065. static int test_wc_curve25519_make_pub(void)
  20066. {
  20067. int res = TEST_SKIPPED;
  20068. #ifdef HAVE_CURVE25519
  20069. WC_RNG rng;
  20070. curve25519_key key;
  20071. byte out[CURVE25519_KEYSIZE];
  20072. int ret;
  20073. ret = wc_curve25519_init(&key);
  20074. if (ret == 0) {
  20075. ret = wc_InitRng(&rng);
  20076. if (ret == 0) {
  20077. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20078. }
  20079. }
  20080. if (ret == 0) {
  20081. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  20082. }
  20083. /*test bad cases*/
  20084. if (ret == 0) {
  20085. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  20086. if (ret == ECC_BAD_ARG_E) {
  20087. ret = 0;
  20088. }
  20089. }
  20090. if (ret == 0) {
  20091. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  20092. if (ret == ECC_BAD_ARG_E) {
  20093. ret = 0;
  20094. }
  20095. }
  20096. if (ret == 0) {
  20097. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  20098. if (ret == ECC_BAD_ARG_E) {
  20099. ret = 0;
  20100. }
  20101. }
  20102. if (ret == 0) {
  20103. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  20104. if (ret == ECC_BAD_ARG_E) {
  20105. ret = 0;
  20106. }
  20107. }
  20108. if (ret == 0) {
  20109. /* verify clamping test */
  20110. key.k[0] |= ~248;
  20111. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20112. if (ret == ECC_BAD_ARG_E) {
  20113. ret = 0;
  20114. }
  20115. key.k[0] &= 248;
  20116. }
  20117. /* repeat the expected-to-succeed test. */
  20118. if (ret == 0) {
  20119. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20120. }
  20121. wc_curve25519_free(&key);
  20122. wc_FreeRng(&rng);
  20123. res = TEST_RES_CHECK(ret == 0);
  20124. #endif
  20125. return res;
  20126. } /*END test_wc_curve25519_make_pub */
  20127. /*
  20128. * Testing test_wc_curve25519_export_public_ex
  20129. */
  20130. static int test_wc_curve25519_export_public_ex(void)
  20131. {
  20132. int res = TEST_SKIPPED;
  20133. #if defined(HAVE_CURVE25519)
  20134. WC_RNG rng;
  20135. curve25519_key key;
  20136. byte out[CURVE25519_KEYSIZE];
  20137. word32 outLen = sizeof(out);
  20138. int endian = EC25519_BIG_ENDIAN;
  20139. int ret;
  20140. ret = wc_curve25519_init(&key);
  20141. if (ret == 0) {
  20142. ret = wc_InitRng(&rng);
  20143. }
  20144. if (ret == 0) {
  20145. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20146. if (ret == 0) {
  20147. ret = wc_curve25519_export_public(&key, out, &outLen);
  20148. }
  20149. if (ret == 0) {
  20150. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20151. }
  20152. }
  20153. /*test bad cases*/
  20154. if (ret == 0) {
  20155. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  20156. if (ret == BAD_FUNC_ARG) {
  20157. ret = 0;
  20158. }
  20159. }
  20160. if (ret == 0) {
  20161. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  20162. if (ret == BAD_FUNC_ARG) {
  20163. ret = 0;
  20164. }
  20165. }
  20166. if (ret == 0) {
  20167. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  20168. if (ret == BAD_FUNC_ARG) {
  20169. ret = 0;
  20170. }
  20171. }
  20172. if (ret == 0) {
  20173. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  20174. if (ret == BAD_FUNC_ARG) {
  20175. ret = 0;
  20176. }
  20177. }
  20178. outLen = outLen - 2;
  20179. if (ret == 0) {
  20180. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20181. if (ret == ECC_BAD_ARG_E) {
  20182. ret = 0;
  20183. }
  20184. }
  20185. wc_curve25519_free(&key);
  20186. wc_FreeRng(&rng);
  20187. res = TEST_RES_CHECK(ret == 0);
  20188. #endif
  20189. return res;
  20190. } /*END test_wc_curve25519_export_public_ex*/
  20191. /*
  20192. * Testing test_wc_curve25519_import_private_raw_ex
  20193. */
  20194. static int test_wc_curve25519_import_private_raw_ex(void)
  20195. {
  20196. int res = TEST_SKIPPED;
  20197. #if defined(HAVE_CURVE25519)
  20198. WC_RNG rng;
  20199. curve25519_key key;
  20200. byte priv[CURVE25519_KEYSIZE];
  20201. byte pub[CURVE25519_KEYSIZE];
  20202. word32 privSz = sizeof(priv);
  20203. word32 pubSz = sizeof(pub);
  20204. int endian = EC25519_BIG_ENDIAN;
  20205. int ret;
  20206. ret = wc_curve25519_init(&key);
  20207. if (ret == 0) {
  20208. ret = wc_InitRng(&rng);
  20209. }
  20210. if (ret == 0) {
  20211. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20212. if (ret == 0) {
  20213. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  20214. }
  20215. if (ret == 0) {
  20216. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  20217. }
  20218. if (ret == 0) {
  20219. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20220. &key, endian);
  20221. }
  20222. }
  20223. /*test bad cases*/
  20224. if (ret == 0) {
  20225. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20226. endian);
  20227. if (ret == BAD_FUNC_ARG) {
  20228. ret = 0;
  20229. }
  20230. }
  20231. if (ret == 0) {
  20232. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20233. &key, endian);
  20234. if (ret == BAD_FUNC_ARG) {
  20235. ret = 0;
  20236. }
  20237. }
  20238. if (ret == 0) {
  20239. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20240. &key, endian);
  20241. if (ret == BAD_FUNC_ARG) {
  20242. ret = 0;
  20243. }
  20244. }
  20245. if (ret == 0) {
  20246. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20247. NULL, endian);
  20248. if (ret == BAD_FUNC_ARG) {
  20249. ret = 0;
  20250. }
  20251. }
  20252. if (ret == 0) {
  20253. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20254. &key, endian);
  20255. if (ret == ECC_BAD_ARG_E) {
  20256. ret = 0;
  20257. }
  20258. }
  20259. if (ret == 0) {
  20260. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20261. &key, endian);
  20262. if (ret == ECC_BAD_ARG_E) {
  20263. ret = 0;
  20264. }
  20265. }
  20266. if (ret == 0) {
  20267. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20268. &key, EC25519_LITTLE_ENDIAN);
  20269. }
  20270. wc_curve25519_free(&key);
  20271. wc_FreeRng(&rng);
  20272. res = TEST_RES_CHECK(ret == 0);
  20273. #endif
  20274. return res;
  20275. } /*END test_wc_curve25519_import_private_raw_ex*/
  20276. /*
  20277. * Testing test_wc_curve25519_import_private
  20278. */
  20279. static int test_wc_curve25519_import_private(void)
  20280. {
  20281. int res = TEST_SKIPPED;
  20282. #if defined(HAVE_CURVE25519)
  20283. curve25519_key key;
  20284. WC_RNG rng;
  20285. byte priv[CURVE25519_KEYSIZE];
  20286. word32 privSz = sizeof(priv);
  20287. int ret;
  20288. ret = wc_curve25519_init(&key);
  20289. if (ret == 0) {
  20290. ret = wc_InitRng(&rng);
  20291. }
  20292. if (ret == 0) {
  20293. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20294. if (ret == 0) {
  20295. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  20296. }
  20297. }
  20298. if (ret == 0) {
  20299. ret = wc_curve25519_import_private(priv, privSz, &key);
  20300. }
  20301. wc_curve25519_free(&key);
  20302. wc_FreeRng(&rng);
  20303. res = TEST_RES_CHECK(ret == 0);
  20304. #endif
  20305. return res;
  20306. } /*END test_wc_curve25519_import*/
  20307. /*
  20308. * Testing test_wc_curve25519_export_private_raw_ex
  20309. */
  20310. static int test_wc_curve25519_export_private_raw_ex(void)
  20311. {
  20312. int res = TEST_SKIPPED;
  20313. #if defined(HAVE_CURVE25519)
  20314. curve25519_key key;
  20315. byte out[CURVE25519_KEYSIZE];
  20316. word32 outLen = sizeof(out);
  20317. int endian = EC25519_BIG_ENDIAN;
  20318. int ret;
  20319. ret = wc_curve25519_init(&key);
  20320. if (ret == 0) {
  20321. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20322. }
  20323. /*test bad cases*/
  20324. if (ret == 0) {
  20325. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  20326. if (ret == BAD_FUNC_ARG) {
  20327. ret = 0;
  20328. }
  20329. }
  20330. if (ret == 0) {
  20331. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  20332. if (ret == BAD_FUNC_ARG) {
  20333. ret = 0;
  20334. }
  20335. }
  20336. if (ret == 0) {
  20337. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  20338. if (ret == BAD_FUNC_ARG) {
  20339. ret = 0;
  20340. }
  20341. }
  20342. if (ret == 0) {
  20343. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  20344. if (ret == BAD_FUNC_ARG) {
  20345. ret = 0;
  20346. }
  20347. }
  20348. if (ret == 0) {
  20349. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20350. EC25519_LITTLE_ENDIAN);
  20351. }
  20352. outLen = outLen - 2;
  20353. if (ret == 0) {
  20354. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20355. if (ret == ECC_BAD_ARG_E) {
  20356. ret = 0;
  20357. }
  20358. }
  20359. wc_curve25519_free(&key);
  20360. res = TEST_RES_CHECK(ret == 0);
  20361. #endif
  20362. return res;
  20363. }/*END test_wc_curve25519_export_private_raw_ex*/
  20364. /*
  20365. * Testing wc_ed448_make_key().
  20366. */
  20367. static int test_wc_ed448_make_key(void)
  20368. {
  20369. int res = TEST_SKIPPED;
  20370. #if defined(HAVE_ED448)
  20371. ed448_key key;
  20372. WC_RNG rng;
  20373. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20374. int ret;
  20375. ret = wc_InitRng(&rng);
  20376. if (ret == 0) {
  20377. ret = wc_ed448_init(&key);
  20378. }
  20379. if (ret == 0) {
  20380. ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
  20381. if (ret == ECC_PRIV_KEY_E) {
  20382. ret = 0;
  20383. }
  20384. else if (ret == 0) {
  20385. ret = -1;
  20386. }
  20387. }
  20388. if (ret == 0) {
  20389. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20390. }
  20391. /* Test bad args. */
  20392. if (ret == 0) {
  20393. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  20394. if (ret == BAD_FUNC_ARG) {
  20395. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  20396. }
  20397. if (ret == BAD_FUNC_ARG) {
  20398. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  20399. }
  20400. if (ret == BAD_FUNC_ARG) {
  20401. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  20402. }
  20403. if (ret == BAD_FUNC_ARG) {
  20404. ret = 0;
  20405. }
  20406. else if (ret == 0) {
  20407. ret = WOLFSSL_FATAL_ERROR;
  20408. }
  20409. }
  20410. if (wc_FreeRng(&rng) && ret == 0) {
  20411. ret = WOLFSSL_FATAL_ERROR;
  20412. }
  20413. wc_ed448_free(&key);
  20414. res = TEST_RES_CHECK(ret == 0);
  20415. #endif
  20416. return res;
  20417. } /* END test_wc_ed448_make_key */
  20418. /*
  20419. * Testing wc_ed448_init()
  20420. */
  20421. static int test_wc_ed448_init(void)
  20422. {
  20423. int res = TEST_SKIPPED;
  20424. #if defined(HAVE_ED448)
  20425. ed448_key key;
  20426. int ret;
  20427. ret = wc_ed448_init(&key);
  20428. /* Test bad args. */
  20429. if (ret == 0) {
  20430. ret = wc_ed448_init(NULL);
  20431. if (ret == BAD_FUNC_ARG) {
  20432. ret = 0;
  20433. }
  20434. else if (ret == 0) {
  20435. ret = WOLFSSL_FATAL_ERROR;
  20436. }
  20437. }
  20438. wc_ed448_free(&key);
  20439. res = TEST_RES_CHECK(ret == 0);
  20440. #endif
  20441. return res;
  20442. } /* END test_wc_ed448_init */
  20443. /*
  20444. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20445. */
  20446. static int test_wc_ed448_sign_msg(void)
  20447. {
  20448. int res = TEST_SKIPPED;
  20449. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20450. WC_RNG rng;
  20451. ed448_key key;
  20452. byte msg[] = "Everybody gets Friday off.\n";
  20453. byte sig[ED448_SIG_SIZE];
  20454. word32 msglen = sizeof(msg);
  20455. word32 siglen = sizeof(sig);
  20456. word32 badSigLen = sizeof(sig) - 1;
  20457. #ifdef HAVE_ED448_VERIFY
  20458. int verify_ok = 0; /*1 = Verify success.*/
  20459. #endif
  20460. int ret;
  20461. /* Initialize stack variables. */
  20462. XMEMSET(sig, 0, siglen);
  20463. /* Initialize key. */
  20464. ret = wc_InitRng(&rng);
  20465. if (ret == 0) {
  20466. ret = wc_ed448_init(&key);
  20467. if (ret == 0) {
  20468. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20469. }
  20470. }
  20471. if (ret == 0) {
  20472. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  20473. }
  20474. /* Test bad args. */
  20475. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  20476. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  20477. if (ret == BAD_FUNC_ARG) {
  20478. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  20479. }
  20480. if (ret == BAD_FUNC_ARG) {
  20481. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  20482. }
  20483. if (ret == BAD_FUNC_ARG) {
  20484. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  20485. }
  20486. if (ret == BAD_FUNC_ARG) {
  20487. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  20488. NULL, 0);
  20489. }
  20490. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  20491. badSigLen -= 1;
  20492. ret = 0;
  20493. }
  20494. else if (ret == 0) {
  20495. ret = WOLFSSL_FATAL_ERROR;
  20496. }
  20497. } /* END sign */
  20498. #ifdef HAVE_ED448_VERIFY
  20499. if (ret == 0) {
  20500. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20501. &key, NULL, 0);
  20502. if (ret == 0 && verify_ok == 1) {
  20503. ret = 0;
  20504. }
  20505. else if (ret == 0) {
  20506. ret = WOLFSSL_FATAL_ERROR;
  20507. }
  20508. /* Test bad args. */
  20509. if (ret == 0) {
  20510. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  20511. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  20512. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  20513. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  20514. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20515. &key, NULL, 0);
  20516. if (ret == BAD_FUNC_ARG) {
  20517. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  20518. &verify_ok, &key, NULL, 0);
  20519. }
  20520. if (ret == BAD_FUNC_ARG) {
  20521. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  20522. NULL, &key, NULL, 0);
  20523. }
  20524. if (ret == BAD_FUNC_ARG) {
  20525. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  20526. &verify_ok, NULL, NULL, 0);
  20527. }
  20528. if (ret == BAD_FUNC_ARG) {
  20529. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  20530. &verify_ok, &key, NULL, 0);
  20531. }
  20532. if (ret == BAD_FUNC_ARG) {
  20533. ret = 0;
  20534. }
  20535. else if (ret == 0) {
  20536. ret = WOLFSSL_FATAL_ERROR;
  20537. }
  20538. }
  20539. } /* END verify. */
  20540. #endif /* Verify. */
  20541. if (wc_FreeRng(&rng) && ret == 0) {
  20542. ret = WOLFSSL_FATAL_ERROR;
  20543. }
  20544. wc_ed448_free(&key);
  20545. res = TEST_RES_CHECK(ret == 0);
  20546. #endif
  20547. return res;
  20548. } /* END test_wc_ed448_sign_msg */
  20549. /*
  20550. * Testing wc_ed448_import_public()
  20551. */
  20552. static int test_wc_ed448_import_public(void)
  20553. {
  20554. int res = TEST_SKIPPED;
  20555. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20556. WC_RNG rng;
  20557. ed448_key pubKey;
  20558. const byte in[] =
  20559. "Ed448PublicKeyUnitTest.................................\n";
  20560. word32 inlen = sizeof(in);
  20561. int ret = 0;
  20562. ret = wc_InitRng(&rng);
  20563. if (ret == 0) {
  20564. ret = wc_ed448_init(&pubKey);
  20565. if (ret == 0) {
  20566. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  20567. }
  20568. }
  20569. if (ret == 0) {
  20570. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  20571. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  20572. ret = 0;
  20573. }
  20574. else {
  20575. ret = WOLFSSL_FATAL_ERROR;
  20576. }
  20577. /* Test bad args. */
  20578. if (ret == 0) {
  20579. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  20580. if (ret == BAD_FUNC_ARG) {
  20581. ret = wc_ed448_import_public(in, inlen, NULL);
  20582. }
  20583. if (ret == BAD_FUNC_ARG) {
  20584. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  20585. }
  20586. if (ret == BAD_FUNC_ARG) {
  20587. ret = 0;
  20588. }
  20589. else if (ret == 0) {
  20590. ret = WOLFSSL_FATAL_ERROR;
  20591. }
  20592. }
  20593. }
  20594. if (wc_FreeRng(&rng) && ret == 0) {
  20595. ret = WOLFSSL_FATAL_ERROR;
  20596. }
  20597. wc_ed448_free(&pubKey);
  20598. res = TEST_RES_CHECK(ret == 0);
  20599. #endif
  20600. return res;
  20601. } /* END wc_ed448_import_public */
  20602. /*
  20603. * Testing wc_ed448_import_private_key()
  20604. */
  20605. static int test_wc_ed448_import_private_key(void)
  20606. {
  20607. int res = TEST_SKIPPED;
  20608. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20609. WC_RNG rng;
  20610. ed448_key key;
  20611. const byte privKey[] =
  20612. "Ed448PrivateKeyUnitTest................................\n";
  20613. const byte pubKey[] =
  20614. "Ed448PublicKeyUnitTest.................................\n";
  20615. word32 privKeySz = sizeof(privKey);
  20616. word32 pubKeySz = sizeof(pubKey);
  20617. #ifdef HAVE_ED448_KEY_EXPORT
  20618. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20619. word32 bothKeysSz = sizeof(bothKeys);
  20620. #endif
  20621. int ret;
  20622. ret = wc_InitRng(&rng);
  20623. if (ret != 0) {
  20624. return TEST_FAIL;
  20625. }
  20626. ret = wc_ed448_init(&key);
  20627. if (ret != 0) {
  20628. wc_FreeRng(&rng);
  20629. return TEST_FAIL;
  20630. }
  20631. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20632. if (ret == 0) {
  20633. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20634. pubKeySz, &key, 1);
  20635. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20636. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20637. ret = WOLFSSL_FATAL_ERROR;
  20638. }
  20639. }
  20640. #ifdef HAVE_ED448_KEY_EXPORT
  20641. if (ret == 0)
  20642. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  20643. if (ret == 0) {
  20644. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20645. &key, 1);
  20646. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20647. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20648. ret = WOLFSSL_FATAL_ERROR;
  20649. }
  20650. }
  20651. #endif
  20652. /* Test bad args. */
  20653. if (ret == 0) {
  20654. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20655. &key);
  20656. if (ret == BAD_FUNC_ARG) {
  20657. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20658. pubKeySz, &key);
  20659. }
  20660. if (ret == BAD_FUNC_ARG) {
  20661. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20662. pubKeySz, NULL);
  20663. }
  20664. if (ret == BAD_FUNC_ARG) {
  20665. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20666. pubKeySz, &key);
  20667. }
  20668. if (ret == BAD_FUNC_ARG) {
  20669. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20670. pubKeySz - 1, &key);
  20671. }
  20672. if (ret == BAD_FUNC_ARG) {
  20673. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20674. 0, &key);
  20675. }
  20676. if (ret == BAD_FUNC_ARG) {
  20677. ret = 0;
  20678. }
  20679. else if (ret == 0) {
  20680. ret = WOLFSSL_FATAL_ERROR;
  20681. }
  20682. }
  20683. if (wc_FreeRng(&rng) && ret == 0) {
  20684. ret = WOLFSSL_FATAL_ERROR;
  20685. }
  20686. wc_ed448_free(&key);
  20687. res = TEST_RES_CHECK(ret == 0);
  20688. #endif
  20689. return res;
  20690. } /* END test_wc_ed448_import_private_key */
  20691. /*
  20692. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20693. */
  20694. static int test_wc_ed448_export(void)
  20695. {
  20696. int res = TEST_SKIPPED;
  20697. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20698. WC_RNG rng;
  20699. ed448_key key;
  20700. byte priv[ED448_PRV_KEY_SIZE];
  20701. byte pub[ED448_PUB_KEY_SIZE];
  20702. word32 privSz = sizeof(priv);
  20703. word32 pubSz = sizeof(pub);
  20704. int ret;
  20705. ret = wc_InitRng(&rng);
  20706. if (ret != 0) {
  20707. return TEST_FAIL;
  20708. }
  20709. ret = wc_ed448_init(&key);
  20710. if (ret != 0) {
  20711. wc_FreeRng(&rng);
  20712. return TEST_FAIL;
  20713. }
  20714. if (ret == 0) {
  20715. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20716. }
  20717. if (ret == 0) {
  20718. ret = wc_ed448_export_public(&key, pub, &pubSz);
  20719. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  20720. XMEMCMP(key.p, pub, pubSz) != 0)) {
  20721. ret = WOLFSSL_FATAL_ERROR;
  20722. }
  20723. if (ret == 0) {
  20724. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  20725. if (ret == BAD_FUNC_ARG) {
  20726. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  20727. }
  20728. if (ret == BAD_FUNC_ARG) {
  20729. ret = wc_ed448_export_public(&key, pub, NULL);
  20730. }
  20731. if (ret == BAD_FUNC_ARG) {
  20732. ret = 0;
  20733. }
  20734. else if (ret == 0) {
  20735. ret = WOLFSSL_FATAL_ERROR;
  20736. }
  20737. }
  20738. }
  20739. if (ret == 0) {
  20740. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  20741. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  20742. XMEMCMP(key.k, priv, privSz) != 0)) {
  20743. ret = WOLFSSL_FATAL_ERROR;
  20744. }
  20745. if (ret == 0) {
  20746. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  20747. if (ret == BAD_FUNC_ARG) {
  20748. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  20749. }
  20750. if (ret == BAD_FUNC_ARG) {
  20751. ret = wc_ed448_export_private_only(&key, priv, NULL);
  20752. }
  20753. if (ret == BAD_FUNC_ARG) {
  20754. ret = 0;
  20755. }
  20756. else if (ret == 0) {
  20757. ret = WOLFSSL_FATAL_ERROR;
  20758. }
  20759. }
  20760. }
  20761. if (wc_FreeRng(&rng) && ret == 0) {
  20762. ret = WOLFSSL_FATAL_ERROR;
  20763. }
  20764. wc_ed448_free(&key);
  20765. res = TEST_RES_CHECK(ret == 0);
  20766. #endif
  20767. return res;
  20768. } /* END test_wc_ed448_export */
  20769. /*
  20770. * Testing wc_ed448_size()
  20771. */
  20772. static int test_wc_ed448_size(void)
  20773. {
  20774. int res = TEST_SKIPPED;
  20775. #if defined(HAVE_ED448)
  20776. WC_RNG rng;
  20777. ed448_key key;
  20778. int ret = 0;
  20779. ret = wc_InitRng(&rng);
  20780. if (ret != 0) {
  20781. return TEST_FAIL;
  20782. }
  20783. ret = wc_ed448_init(&key);
  20784. if (ret != 0) {
  20785. wc_FreeRng(&rng);
  20786. return TEST_FAIL;
  20787. }
  20788. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20789. if (ret != 0) {
  20790. wc_FreeRng(&rng);
  20791. wc_ed448_free(&key);
  20792. return TEST_FAIL;
  20793. }
  20794. ret = wc_ed448_size(&key);
  20795. /* Test bad args. */
  20796. if (ret == ED448_KEY_SIZE) {
  20797. ret = wc_ed448_size(NULL);
  20798. if (ret == BAD_FUNC_ARG) {
  20799. ret = 0;
  20800. }
  20801. }
  20802. if (ret == 0) {
  20803. ret = wc_ed448_sig_size(&key);
  20804. if (ret == ED448_SIG_SIZE) {
  20805. ret = 0;
  20806. }
  20807. /* Test bad args. */
  20808. if (ret == 0) {
  20809. ret = wc_ed448_sig_size(NULL);
  20810. if (ret == BAD_FUNC_ARG) {
  20811. ret = 0;
  20812. }
  20813. }
  20814. } /* END wc_ed448_sig_size() */
  20815. if (ret == 0) {
  20816. ret = wc_ed448_pub_size(&key);
  20817. if (ret == ED448_PUB_KEY_SIZE) {
  20818. ret = 0;
  20819. }
  20820. if (ret == 0) {
  20821. ret = wc_ed448_pub_size(NULL);
  20822. if (ret == BAD_FUNC_ARG) {
  20823. ret = 0;
  20824. }
  20825. }
  20826. } /* END wc_ed448_pub_size */
  20827. if (ret == 0) {
  20828. ret = wc_ed448_priv_size(&key);
  20829. if (ret == ED448_PRV_KEY_SIZE) {
  20830. ret = 0;
  20831. }
  20832. if (ret == 0) {
  20833. ret = wc_ed448_priv_size(NULL);
  20834. if (ret == BAD_FUNC_ARG) {
  20835. ret = 0;
  20836. }
  20837. }
  20838. } /* END wc_ed448_pub_size */
  20839. if (wc_FreeRng(&rng) && ret == 0) {
  20840. ret = WOLFSSL_FATAL_ERROR;
  20841. }
  20842. wc_ed448_free(&key);
  20843. res = TEST_RES_CHECK(ret == 0);
  20844. #endif
  20845. return res;
  20846. } /* END test_wc_ed448_size */
  20847. /*
  20848. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20849. */
  20850. static int test_wc_ed448_exportKey(void)
  20851. {
  20852. int res = TEST_SKIPPED;
  20853. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20854. WC_RNG rng;
  20855. ed448_key key;
  20856. byte priv[ED448_PRV_KEY_SIZE];
  20857. byte pub[ED448_PUB_KEY_SIZE];
  20858. byte privOnly[ED448_PRV_KEY_SIZE];
  20859. word32 privSz = sizeof(priv);
  20860. word32 pubSz = sizeof(pub);
  20861. word32 privOnlySz = sizeof(privOnly);
  20862. int ret;
  20863. ret = wc_InitRng(&rng);
  20864. if (ret != 0) {
  20865. return TEST_FAIL;
  20866. }
  20867. ret = wc_ed448_init(&key);
  20868. if (ret != 0) {
  20869. wc_FreeRng(&rng);
  20870. return TEST_FAIL;
  20871. }
  20872. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20873. if (ret != 0) {
  20874. wc_FreeRng(&rng);
  20875. wc_ed448_free(&key);
  20876. return TEST_FAIL;
  20877. }
  20878. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  20879. if (ret == 0) {
  20880. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  20881. if (ret == BAD_FUNC_ARG) {
  20882. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  20883. }
  20884. if (ret == BAD_FUNC_ARG) {
  20885. ret = wc_ed448_export_private(&key, privOnly, NULL);
  20886. }
  20887. if (ret == BAD_FUNC_ARG) {
  20888. ret = 0;
  20889. }
  20890. else if (ret == 0) {
  20891. ret = WOLFSSL_FATAL_ERROR;
  20892. }
  20893. }
  20894. if (ret == 0) {
  20895. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  20896. if (ret == 0) {
  20897. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  20898. if (ret == BAD_FUNC_ARG) {
  20899. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  20900. }
  20901. if (ret == BAD_FUNC_ARG) {
  20902. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  20903. }
  20904. if (ret == BAD_FUNC_ARG) {
  20905. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  20906. }
  20907. if (ret == BAD_FUNC_ARG) {
  20908. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  20909. }
  20910. if (ret == BAD_FUNC_ARG) {
  20911. ret = 0;
  20912. }
  20913. else if (ret == 0) {
  20914. ret = WOLFSSL_FATAL_ERROR;
  20915. }
  20916. }
  20917. } /* END wc_ed448_export_key() */
  20918. /* Cross check output. */
  20919. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20920. ret = WOLFSSL_FATAL_ERROR;
  20921. }
  20922. if (wc_FreeRng(&rng) && ret == 0) {
  20923. ret = WOLFSSL_FATAL_ERROR;
  20924. }
  20925. wc_ed448_free(&key);
  20926. res = TEST_RES_CHECK(ret == 0);
  20927. #endif
  20928. return res;
  20929. } /* END test_wc_ed448_exportKey */
  20930. /*
  20931. * Testing wc_Ed448PublicKeyToDer
  20932. */
  20933. static int test_wc_Ed448PublicKeyToDer(void)
  20934. {
  20935. int res = TEST_SKIPPED;
  20936. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20937. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20938. int tmp;
  20939. ed448_key key;
  20940. byte derBuf[1024];
  20941. int ret = 0;
  20942. /* Test bad args */
  20943. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  20944. if (tmp != BAD_FUNC_ARG) {
  20945. ret = WOLFSSL_FATAL_ERROR;
  20946. }
  20947. if (ret == 0) {
  20948. wc_ed448_init(&key);
  20949. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  20950. if (tmp != BUFFER_E) {
  20951. ret = WOLFSSL_FATAL_ERROR;
  20952. }
  20953. wc_ed448_free(&key);
  20954. }
  20955. /* Test good args */
  20956. if (ret == 0) {
  20957. WC_RNG rng;
  20958. ret = wc_InitRng(&rng);
  20959. if (ret != 0) {
  20960. return TEST_FAIL;
  20961. }
  20962. ret = wc_ed448_init(&key);
  20963. if (ret != 0) {
  20964. wc_FreeRng(&rng);
  20965. return TEST_FAIL;
  20966. }
  20967. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20968. if (ret != 0) {
  20969. wc_FreeRng(&rng);
  20970. wc_ed448_free(&key);
  20971. return TEST_FAIL;
  20972. }
  20973. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  20974. if (tmp <= 0) {
  20975. ret = WOLFSSL_FATAL_ERROR;
  20976. }
  20977. wc_FreeRng(&rng);
  20978. wc_ed448_free(&key);
  20979. }
  20980. res = TEST_RES_CHECK(ret == 0);
  20981. #endif
  20982. return res;
  20983. } /* END testing wc_Ed448PublicKeyToDer */
  20984. /*
  20985. * Testing wc_curve448_init and wc_curve448_free.
  20986. */
  20987. static int test_wc_curve448_init(void)
  20988. {
  20989. int res = TEST_SKIPPED;
  20990. #if defined(HAVE_CURVE448)
  20991. curve448_key key;
  20992. int ret = 0;
  20993. ret = wc_curve448_init(&key);
  20994. /* Test bad args for wc_curve448_init */
  20995. if (ret == 0) {
  20996. ret = wc_curve448_init(NULL);
  20997. if (ret == BAD_FUNC_ARG) {
  20998. ret = 0;
  20999. }
  21000. else if (ret == 0) {
  21001. ret = WOLFSSL_FATAL_ERROR;
  21002. }
  21003. }
  21004. /* Test good args for wc_curve_448_free */
  21005. wc_curve448_free(&key);
  21006. wc_curve448_free(NULL);
  21007. res = TEST_RES_CHECK(ret == 0);
  21008. #endif
  21009. return res;
  21010. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21011. /*
  21012. * Testing wc_curve448_make_key
  21013. */
  21014. static int test_wc_curve448_make_key(void)
  21015. {
  21016. int res = TEST_SKIPPED;
  21017. #if defined(HAVE_CURVE448)
  21018. WC_RNG rng;
  21019. curve448_key key;
  21020. int keysize;
  21021. int ret;
  21022. ret = wc_curve448_init(&key);
  21023. if (ret == 0) {
  21024. ret = wc_InitRng(&rng);
  21025. }
  21026. if (ret == 0) {
  21027. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21028. if (ret == 0) {
  21029. keysize = wc_curve448_size(&key);
  21030. if (keysize != CURVE448_KEY_SIZE) {
  21031. ret = WOLFSSL_FATAL_ERROR;
  21032. }
  21033. }
  21034. if (ret == 0) {
  21035. ret = wc_curve448_make_key(&rng, keysize, &key);
  21036. }
  21037. }
  21038. /* test bad cases */
  21039. if (ret == 0) {
  21040. ret = wc_curve448_make_key(NULL, 0, NULL);
  21041. if (ret == BAD_FUNC_ARG) {
  21042. ret = 0;
  21043. }
  21044. }
  21045. if (ret == 0) {
  21046. ret = wc_curve448_make_key(&rng, keysize, NULL);
  21047. if (ret == BAD_FUNC_ARG) {
  21048. ret = 0;
  21049. }
  21050. }
  21051. if (ret == 0) {
  21052. ret = wc_curve448_make_key(NULL, keysize, &key);
  21053. if (ret == BAD_FUNC_ARG) {
  21054. ret = 0;
  21055. }
  21056. }
  21057. if (ret == 0) {
  21058. ret = wc_curve448_make_key(&rng, 0, &key);
  21059. if (ret == ECC_BAD_ARG_E) {
  21060. ret = 0;
  21061. }
  21062. }
  21063. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21064. ret = WOLFSSL_FATAL_ERROR;
  21065. }
  21066. wc_curve448_free(&key);
  21067. res = TEST_RES_CHECK(ret == 0);
  21068. #endif
  21069. return res;
  21070. } /*END test_wc_curve448_make_key*/
  21071. /*
  21072. * Testing test_wc_curve448_shared_secret_ex
  21073. */
  21074. static int test_wc_curve448_shared_secret_ex(void)
  21075. {
  21076. int res = TEST_SKIPPED;
  21077. #if defined(HAVE_CURVE448)
  21078. WC_RNG rng;
  21079. curve448_key private_key, public_key;
  21080. byte out[CURVE448_KEY_SIZE];
  21081. word32 outLen = sizeof(out);
  21082. int endian = EC448_BIG_ENDIAN;
  21083. int ret;
  21084. ret = wc_curve448_init(&private_key);
  21085. if (ret == 0) {
  21086. ret = wc_InitRng(&rng);
  21087. if (ret == 0) {
  21088. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  21089. }
  21090. }
  21091. if (ret == 0) {
  21092. ret = wc_curve448_init(&public_key);
  21093. }
  21094. if (ret == 0) {
  21095. if (ret == 0) {
  21096. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  21097. }
  21098. }
  21099. if (ret == 0) {
  21100. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21101. &outLen, endian);
  21102. }
  21103. /* test bad cases */
  21104. if (ret == 0) {
  21105. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian);
  21106. if (ret == BAD_FUNC_ARG) {
  21107. ret = 0;
  21108. }
  21109. }
  21110. if (ret == 0) {
  21111. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  21112. &outLen, endian);
  21113. if (ret == BAD_FUNC_ARG) {
  21114. ret = 0;
  21115. }
  21116. }
  21117. if (ret == 0) {
  21118. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  21119. &outLen, endian);
  21120. if (ret == BAD_FUNC_ARG) {
  21121. ret = 0;
  21122. }
  21123. }
  21124. if (ret == 0) {
  21125. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21126. &outLen, endian);
  21127. if (ret == BAD_FUNC_ARG) {
  21128. ret = 0;
  21129. }
  21130. }
  21131. if (ret == 0) {
  21132. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21133. NULL, endian);
  21134. if (ret == BAD_FUNC_ARG) {
  21135. ret = 0;
  21136. }
  21137. }
  21138. outLen = outLen - 2;
  21139. if (ret == 0) {
  21140. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21141. &outLen, endian);
  21142. if (ret == BAD_FUNC_ARG) {
  21143. ret = 0;
  21144. }
  21145. }
  21146. wc_curve448_free(&private_key);
  21147. wc_curve448_free(&public_key);
  21148. wc_FreeRng(&rng);
  21149. res = TEST_RES_CHECK(ret == 0);
  21150. #endif
  21151. return res;
  21152. } /*END test_wc_curve448_shared_secret_ex*/
  21153. /*
  21154. * Testing test_wc_curve448_export_public_ex
  21155. */
  21156. static int test_wc_curve448_export_public_ex(void)
  21157. {
  21158. int res = TEST_SKIPPED;
  21159. #if defined(HAVE_CURVE448)
  21160. WC_RNG rng;
  21161. curve448_key key;
  21162. byte out[CURVE448_KEY_SIZE];
  21163. word32 outLen = sizeof(out);
  21164. int endian = EC448_BIG_ENDIAN;
  21165. int ret;
  21166. ret = wc_curve448_init(&key);
  21167. if (ret == 0) {
  21168. ret = wc_InitRng(&rng);
  21169. }
  21170. if (ret == 0) {
  21171. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21172. if (ret == 0) {
  21173. ret = wc_curve448_export_public(&key, out, &outLen);
  21174. }
  21175. if (ret == 0) {
  21176. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21177. }
  21178. }
  21179. /*test bad cases*/
  21180. if (ret == 0) {
  21181. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  21182. if (ret == BAD_FUNC_ARG) {
  21183. ret = 0;
  21184. }
  21185. }
  21186. if (ret == 0) {
  21187. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  21188. if (ret == BAD_FUNC_ARG) {
  21189. ret = 0;
  21190. }
  21191. }
  21192. if (ret == 0) {
  21193. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  21194. if (ret == BAD_FUNC_ARG) {
  21195. ret = 0;
  21196. }
  21197. }
  21198. if (ret == 0) {
  21199. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  21200. if (ret == BAD_FUNC_ARG) {
  21201. ret = 0;
  21202. }
  21203. }
  21204. outLen = outLen - 2;
  21205. if (ret == 0) {
  21206. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21207. if (ret == ECC_BAD_ARG_E) {
  21208. ret = 0;
  21209. }
  21210. }
  21211. wc_curve448_free(&key);
  21212. wc_FreeRng(&rng);
  21213. res = TEST_RES_CHECK(ret == 0);
  21214. #endif
  21215. return res;
  21216. } /*END test_wc_curve448_export_public_ex*/
  21217. /*
  21218. * Testing test_wc_curve448_export_private_raw_ex
  21219. */
  21220. static int test_wc_curve448_export_private_raw_ex(void)
  21221. {
  21222. int res = TEST_SKIPPED;
  21223. #if defined(HAVE_CURVE448)
  21224. curve448_key key;
  21225. byte out[CURVE448_KEY_SIZE];
  21226. word32 outLen = sizeof(out);
  21227. int endian = EC448_BIG_ENDIAN;
  21228. int ret;
  21229. ret = wc_curve448_init(&key);
  21230. if (ret == 0) {
  21231. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21232. }
  21233. /*test bad cases*/
  21234. if (ret == 0) {
  21235. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  21236. if (ret == BAD_FUNC_ARG) {
  21237. ret = 0;
  21238. }
  21239. }
  21240. if (ret == 0) {
  21241. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  21242. if (ret == BAD_FUNC_ARG) {
  21243. ret = 0;
  21244. }
  21245. }
  21246. if (ret == 0) {
  21247. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  21248. if (ret == BAD_FUNC_ARG) {
  21249. ret = 0;
  21250. }
  21251. }
  21252. if (ret == 0) {
  21253. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  21254. if (ret == BAD_FUNC_ARG) {
  21255. ret = 0;
  21256. }
  21257. }
  21258. if (ret == 0) {
  21259. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21260. EC448_LITTLE_ENDIAN);
  21261. }
  21262. outLen = outLen - 2;
  21263. if (ret == 0) {
  21264. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21265. if (ret == ECC_BAD_ARG_E) {
  21266. ret = 0;
  21267. }
  21268. }
  21269. wc_curve448_free(&key);
  21270. res = TEST_RES_CHECK(ret == 0);
  21271. #endif
  21272. return res;
  21273. }/*END test_wc_curve448_export_private_raw_ex*/
  21274. /*
  21275. * Testing test_wc_curve448_import_private_raw_ex
  21276. */
  21277. static int test_wc_curve448_import_private_raw_ex(void)
  21278. {
  21279. int res = TEST_SKIPPED;
  21280. #if defined(HAVE_CURVE448)
  21281. WC_RNG rng;
  21282. curve448_key key;
  21283. byte priv[CURVE448_KEY_SIZE];
  21284. byte pub[CURVE448_KEY_SIZE];
  21285. word32 privSz = sizeof(priv);
  21286. word32 pubSz = sizeof(pub);
  21287. int endian = EC448_BIG_ENDIAN;
  21288. int ret;
  21289. ret = wc_curve448_init(&key);
  21290. if (ret == 0) {
  21291. ret = wc_InitRng(&rng);
  21292. }
  21293. if (ret == 0) {
  21294. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21295. if (ret == 0) {
  21296. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21297. }
  21298. if (ret == 0) {
  21299. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21300. }
  21301. if (ret == 0) {
  21302. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21303. &key, endian);
  21304. }
  21305. }
  21306. /* test bad cases */
  21307. if (ret == 0) {
  21308. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  21309. if (ret == BAD_FUNC_ARG) {
  21310. ret = 0;
  21311. }
  21312. }
  21313. if (ret == 0) {
  21314. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21315. &key, endian);
  21316. if (ret == BAD_FUNC_ARG) {
  21317. ret = 0;
  21318. }
  21319. }
  21320. if (ret == 0) {
  21321. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21322. &key, endian);
  21323. if (ret == BAD_FUNC_ARG) {
  21324. ret = 0;
  21325. }
  21326. }
  21327. if (ret == 0) {
  21328. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21329. NULL, endian);
  21330. if (ret == BAD_FUNC_ARG) {
  21331. ret = 0;
  21332. }
  21333. }
  21334. if (ret == 0) {
  21335. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21336. &key, endian);
  21337. if (ret == ECC_BAD_ARG_E) {
  21338. ret = 0;
  21339. }
  21340. }
  21341. if (ret == 0) {
  21342. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21343. &key, endian);
  21344. if (ret == ECC_BAD_ARG_E) {
  21345. ret = 0;
  21346. }
  21347. }
  21348. if (ret == 0) {
  21349. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21350. &key, EC448_LITTLE_ENDIAN);
  21351. }
  21352. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21353. ret = WOLFSSL_FATAL_ERROR;
  21354. }
  21355. wc_curve448_free(&key);
  21356. res = TEST_RES_CHECK(ret == 0);
  21357. #endif
  21358. return res;
  21359. } /*END test_wc_curve448_import_private_raw_ex*/
  21360. /*
  21361. * Testing test_curve448_export_key_raw
  21362. */
  21363. static int test_wc_curve448_export_key_raw(void)
  21364. {
  21365. int res = TEST_SKIPPED;
  21366. #if defined(HAVE_CURVE448)
  21367. WC_RNG rng;
  21368. curve448_key key;
  21369. byte priv[CURVE448_KEY_SIZE];
  21370. byte pub[CURVE448_KEY_SIZE];
  21371. word32 privSz = sizeof(priv);
  21372. word32 pubSz = sizeof(pub);
  21373. int ret;
  21374. ret = wc_curve448_init(&key);
  21375. if (ret == 0) {
  21376. ret = wc_InitRng(&rng);
  21377. }
  21378. if (ret == 0) {
  21379. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21380. if (ret == 0) {
  21381. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21382. }
  21383. if (ret == 0) {
  21384. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21385. }
  21386. if (ret == 0) {
  21387. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  21388. }
  21389. }
  21390. wc_curve448_free(&key);
  21391. wc_FreeRng(&rng);
  21392. res = TEST_RES_CHECK(ret == 0);
  21393. #endif
  21394. return res;
  21395. }/*END test_wc_curve448_import_private_raw_ex*/
  21396. /*
  21397. * Testing test_wc_curve448_import_private
  21398. */
  21399. static int test_wc_curve448_import_private(void)
  21400. {
  21401. int res = TEST_SKIPPED;
  21402. #if defined(HAVE_CURVE448)
  21403. curve448_key key;
  21404. WC_RNG rng;
  21405. byte priv[CURVE448_KEY_SIZE];
  21406. word32 privSz = sizeof(priv);
  21407. int ret;
  21408. ret = wc_curve448_init(&key);
  21409. if (ret == 0) {
  21410. ret = wc_InitRng(&rng);
  21411. }
  21412. if (ret == 0) {
  21413. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21414. if (ret == 0) {
  21415. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21416. }
  21417. }
  21418. if (ret == 0) {
  21419. ret = wc_curve448_import_private(priv, privSz, &key);
  21420. }
  21421. wc_curve448_free(&key);
  21422. wc_FreeRng(&rng);
  21423. res = TEST_RES_CHECK(ret == 0);
  21424. #endif
  21425. return res;
  21426. } /*END test_wc_curve448_import*/
  21427. /*
  21428. * Testing test_wc_curve448_size.
  21429. */
  21430. static int test_wc_curve448_size(void)
  21431. {
  21432. int res = TEST_SKIPPED;
  21433. #if defined(HAVE_CURVE448)
  21434. curve448_key key;
  21435. int ret = 0;
  21436. ret = wc_curve448_init(&key);
  21437. /* Test good args for wc_curve448_size */
  21438. if (ret == 0) {
  21439. ret = wc_curve448_size(&key);
  21440. }
  21441. /* Test bad args for wc_curve448_size */
  21442. if (ret != 0) {
  21443. ret = wc_curve448_size(NULL);
  21444. }
  21445. wc_curve448_free(&key);
  21446. res = TEST_RES_CHECK(ret == 0);
  21447. #endif
  21448. return res;
  21449. } /* END test_wc_curve448_size*/
  21450. /*
  21451. * Testing wc_ecc_make_key.
  21452. */
  21453. static int test_wc_ecc_make_key(void)
  21454. {
  21455. int res = TEST_SKIPPED;
  21456. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21457. WC_RNG rng;
  21458. ecc_key key;
  21459. int ret;
  21460. ret = wc_InitRng(&rng);
  21461. if (ret != 0)
  21462. return TEST_FAIL;
  21463. ret = wc_ecc_init(&key);
  21464. if (ret == 0) {
  21465. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21466. #if defined(WOLFSSL_ASYNC_CRYPT)
  21467. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21468. #endif
  21469. /* Pass in bad args. */
  21470. if (ret == 0) {
  21471. ret = wc_ecc_make_key(NULL, KEY14, &key);
  21472. if (ret == BAD_FUNC_ARG) {
  21473. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  21474. }
  21475. if (ret == BAD_FUNC_ARG) {
  21476. ret = 0;
  21477. }
  21478. else if (ret == 0) {
  21479. ret = WOLFSSL_FATAL_ERROR;
  21480. }
  21481. }
  21482. wc_ecc_free(&key);
  21483. }
  21484. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21485. ret = WOLFSSL_FATAL_ERROR;
  21486. }
  21487. #ifdef FP_ECC
  21488. wc_ecc_fp_free();
  21489. #endif
  21490. res = TEST_RES_CHECK(ret == 0);
  21491. #endif
  21492. return res;
  21493. } /* END test_wc_ecc_make_key */
  21494. /*
  21495. * Testing wc_ecc_init()
  21496. */
  21497. static int test_wc_ecc_init(void)
  21498. {
  21499. int res = TEST_SKIPPED;
  21500. #ifdef HAVE_ECC
  21501. ecc_key key;
  21502. int ret;
  21503. ret = wc_ecc_init(&key);
  21504. /* Pass in bad args. */
  21505. if (ret == 0) {
  21506. ret = wc_ecc_init(NULL);
  21507. if (ret == BAD_FUNC_ARG) {
  21508. ret = 0;
  21509. }
  21510. else if (ret == 0) {
  21511. ret = WOLFSSL_FATAL_ERROR;
  21512. }
  21513. }
  21514. wc_ecc_free(&key);
  21515. res = TEST_RES_CHECK(ret == 0);
  21516. #endif
  21517. return res;
  21518. } /* END test_wc_ecc_init */
  21519. /*
  21520. * Testing wc_ecc_check_key()
  21521. */
  21522. static int test_wc_ecc_check_key(void)
  21523. {
  21524. int res = TEST_SKIPPED;
  21525. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21526. WC_RNG rng;
  21527. ecc_key key;
  21528. int ret;
  21529. XMEMSET(&rng, 0, sizeof(rng));
  21530. XMEMSET(&key, 0, sizeof(key));
  21531. ret = wc_InitRng(&rng);
  21532. if (ret == 0) {
  21533. ret = wc_ecc_init(&key);
  21534. if (ret == 0) {
  21535. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21536. #if defined(WOLFSSL_ASYNC_CRYPT)
  21537. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21538. #endif
  21539. }
  21540. }
  21541. if (ret == 0) {
  21542. ret = wc_ecc_check_key(&key);
  21543. }
  21544. /* Pass in bad args. */
  21545. if (ret == 0) {
  21546. ret = wc_ecc_check_key(NULL);
  21547. if (ret == BAD_FUNC_ARG) {
  21548. ret = 0;
  21549. }
  21550. else if (ret == 0) {
  21551. ret = WOLFSSL_FATAL_ERROR;
  21552. }
  21553. }
  21554. if (wc_FreeRng(&rng) && ret == 0) {
  21555. ret = WOLFSSL_FATAL_ERROR;
  21556. }
  21557. wc_ecc_free(&key);
  21558. #ifdef FP_ECC
  21559. wc_ecc_fp_free();
  21560. #endif
  21561. res = TEST_RES_CHECK(ret == 0);
  21562. #endif
  21563. return res;
  21564. } /* END test_wc_ecc_check_key */
  21565. /*
  21566. * Testing wc_ecc_get_generator()
  21567. */
  21568. static int test_wc_ecc_get_generator(void)
  21569. {
  21570. int res = TEST_SKIPPED;
  21571. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21572. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21573. ecc_point* pt;
  21574. int ret = 0;
  21575. pt = wc_ecc_new_point();
  21576. if (!pt) {
  21577. ret = WOLFSSL_FATAL_ERROR;
  21578. }
  21579. if (ret == 0) {
  21580. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21581. }
  21582. /* Test bad args. */
  21583. if (ret == MP_OKAY) {
  21584. /* Returns Zero for bad arg. */
  21585. ret = wc_ecc_get_generator(pt, -1);
  21586. if (ret != MP_OKAY)
  21587. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21588. if (ret != MP_OKAY)
  21589. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  21590. * increase this number */
  21591. if (ret != MP_OKAY)
  21592. wc_ecc_get_generator(NULL, -1);
  21593. ret = (ret == MP_OKAY) ? WOLFSSL_FATAL_ERROR : 0;
  21594. }
  21595. wc_ecc_del_point(pt);
  21596. res = TEST_RES_CHECK(ret == 0);
  21597. #endif
  21598. return res;
  21599. } /* END test_wc_ecc_get_generator */
  21600. /*
  21601. * Testing wc_ecc_size()
  21602. */
  21603. static int test_wc_ecc_size(void)
  21604. {
  21605. int res = TEST_SKIPPED;
  21606. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21607. WC_RNG rng;
  21608. ecc_key key;
  21609. int ret;
  21610. XMEMSET(&rng, 0, sizeof(rng));
  21611. XMEMSET(&key, 0, sizeof(key));
  21612. ret = wc_InitRng(&rng);
  21613. if (ret == 0) {
  21614. ret = wc_ecc_init(&key);
  21615. if (ret == 0) {
  21616. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21617. #if defined(WOLFSSL_ASYNC_CRYPT)
  21618. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21619. #endif
  21620. }
  21621. }
  21622. if (ret == 0) {
  21623. ret = wc_ecc_size(&key);
  21624. if (ret == KEY14) {
  21625. ret = 0;
  21626. }
  21627. else if (ret == 0) {
  21628. ret = WOLFSSL_FATAL_ERROR;
  21629. }
  21630. }
  21631. /* Test bad args. */
  21632. if (ret == 0) {
  21633. /* Returns Zero for bad arg. */
  21634. ret = wc_ecc_size(NULL);
  21635. }
  21636. if (wc_FreeRng(&rng) && ret == 0) {
  21637. ret = WOLFSSL_FATAL_ERROR;
  21638. }
  21639. wc_ecc_free(&key);
  21640. res = TEST_RES_CHECK(ret == 0);
  21641. #endif
  21642. return res;
  21643. } /* END test_wc_ecc_size */
  21644. static int test_wc_ecc_params(void)
  21645. {
  21646. int res = TEST_SKIPPED;
  21647. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21648. It was added after certifications */
  21649. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21650. const ecc_set_type* ecc_set;
  21651. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21652. /* Test for SECP256R1 curve */
  21653. int curve_id = ECC_SECP256R1;
  21654. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  21655. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  21656. ecc_set = wc_ecc_get_curve_params(curve_idx);
  21657. AssertNotNull(ecc_set);
  21658. AssertIntEQ(ecc_set->id, curve_id);
  21659. #endif
  21660. /* Test case when SECP256R1 is not enabled */
  21661. /* Test that we get curve params for index 0 */
  21662. ecc_set = wc_ecc_get_curve_params(0);
  21663. AssertNotNull(ecc_set);
  21664. res = TEST_RES_CHECK(1);
  21665. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21666. return res;
  21667. }
  21668. /*
  21669. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21670. */
  21671. static int test_wc_ecc_signVerify_hash(void)
  21672. {
  21673. int res = TEST_SKIPPED;
  21674. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21675. WC_RNG rng;
  21676. ecc_key key;
  21677. int ret;
  21678. int signH = WOLFSSL_FATAL_ERROR;
  21679. #ifdef HAVE_ECC_VERIFY
  21680. int verifyH = WOLFSSL_FATAL_ERROR;
  21681. int verify = 0;
  21682. #endif
  21683. word32 siglen = ECC_BUFSIZE;
  21684. byte sig[ECC_BUFSIZE];
  21685. byte adjustedSig[ECC_BUFSIZE+1];
  21686. byte digest[] = TEST_STRING;
  21687. word32 digestlen = (word32)TEST_STRING_SZ;
  21688. /* Init stack var */
  21689. XMEMSET(sig, 0, siglen);
  21690. XMEMSET(&key, 0, sizeof(key));
  21691. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21692. /* Init structs. */
  21693. ret = wc_InitRng(&rng);
  21694. if (ret == 0) {
  21695. ret = wc_ecc_init(&key);
  21696. if (ret == 0) {
  21697. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21698. #if defined(WOLFSSL_ASYNC_CRYPT)
  21699. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21700. #endif
  21701. }
  21702. }
  21703. if (ret == 0) {
  21704. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  21705. }
  21706. /* Check bad args. */
  21707. if (ret == 0) {
  21708. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  21709. if (signH == ECC_BAD_ARG_E) {
  21710. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  21711. &rng, &key);
  21712. }
  21713. if (signH == ECC_BAD_ARG_E) {
  21714. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  21715. &rng, &key);
  21716. }
  21717. if (signH == ECC_BAD_ARG_E) {
  21718. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21719. NULL, &key);
  21720. }
  21721. if (signH == ECC_BAD_ARG_E) {
  21722. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21723. &rng, NULL);
  21724. }
  21725. if (signH == ECC_BAD_ARG_E) {
  21726. signH = 0;
  21727. }
  21728. else if (ret == 0) {
  21729. signH = WOLFSSL_FATAL_ERROR;
  21730. }
  21731. }
  21732. #ifdef HAVE_ECC_VERIFY
  21733. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  21734. if (verify != 1 && ret == 0) {
  21735. ret = WOLFSSL_FATAL_ERROR;
  21736. }
  21737. /* test check on length of signature passed in */
  21738. XMEMCPY(adjustedSig, sig, siglen);
  21739. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21740. #ifndef NO_STRICT_ECDSA_LEN
  21741. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21742. &verify, &key), 0);
  21743. #else
  21744. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21745. * is allowed */
  21746. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21747. &verify, &key), 0);
  21748. #endif
  21749. /* Test bad args. */
  21750. if (ret == 0) {
  21751. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  21752. &verify, &key);
  21753. if (verifyH == ECC_BAD_ARG_E) {
  21754. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  21755. &verify, &key);
  21756. }
  21757. if (verifyH == ECC_BAD_ARG_E) {
  21758. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21759. NULL, &key);
  21760. }
  21761. if (verifyH == ECC_BAD_ARG_E) {
  21762. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21763. &verify, NULL);
  21764. }
  21765. if (verifyH == ECC_BAD_ARG_E) {
  21766. verifyH = 0;
  21767. }
  21768. else if (ret == 0) {
  21769. verifyH = WOLFSSL_FATAL_ERROR;
  21770. }
  21771. }
  21772. #endif /* HAVE_ECC_VERIFY */
  21773. if (wc_FreeRng(&rng) && ret == 0) {
  21774. ret = WOLFSSL_FATAL_ERROR;
  21775. }
  21776. wc_ecc_free(&key);
  21777. #ifdef FP_ECC
  21778. wc_ecc_fp_free();
  21779. #endif
  21780. res = TEST_RES_CHECK(ret == 0 && signH == 0 && verifyH == 0);
  21781. #endif
  21782. return res;
  21783. } /* END test_wc_ecc_sign_hash */
  21784. /*
  21785. * Testing wc_ecc_shared_secret()
  21786. */
  21787. static int test_wc_ecc_shared_secret(void)
  21788. {
  21789. int res = TEST_SKIPPED;
  21790. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21791. ecc_key key, pubKey;
  21792. WC_RNG rng;
  21793. int ret;
  21794. byte out[KEY32];
  21795. int keySz = sizeof(out);
  21796. word32 outlen = (word32)sizeof(out);
  21797. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21798. const char* qx =
  21799. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21800. const char* qy =
  21801. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21802. const char* d =
  21803. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21804. const char* curveName = "SECP256R1";
  21805. const byte expected_shared_secret[] =
  21806. {
  21807. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21808. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21809. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21810. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21811. };
  21812. #endif
  21813. PRIVATE_KEY_UNLOCK();
  21814. /* Initialize variables. */
  21815. XMEMSET(out, 0, keySz);
  21816. XMEMSET(&rng, 0, sizeof(rng));
  21817. XMEMSET(&key, 0, sizeof(key));
  21818. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21819. ret = wc_InitRng(&rng);
  21820. if (ret == 0) {
  21821. ret = wc_ecc_init(&key);
  21822. if (ret == 0) {
  21823. ret = wc_ecc_init(&pubKey);
  21824. }
  21825. }
  21826. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21827. if (ret == 0) {
  21828. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21829. }
  21830. if (ret == 0) {
  21831. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  21832. }
  21833. #else
  21834. if (ret == 0) {
  21835. ret = wc_ecc_make_key(&rng, keySz, &key);
  21836. #if defined(WOLFSSL_ASYNC_CRYPT)
  21837. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21838. #endif
  21839. }
  21840. if (ret == 0) {
  21841. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  21842. #if defined(WOLFSSL_ASYNC_CRYPT)
  21843. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21844. #endif
  21845. }
  21846. #endif
  21847. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21848. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21849. !defined(HAVE_SELFTEST)
  21850. if (ret == 0) {
  21851. ret = wc_ecc_set_rng(&key, &rng);
  21852. }
  21853. #endif
  21854. if (ret == 0) {
  21855. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21856. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21857. if (ret == 0) {
  21858. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  21859. ret = WOLFSSL_FATAL_ERROR;
  21860. }
  21861. }
  21862. #endif
  21863. /* Test bad args. */
  21864. if (ret == 0) {
  21865. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  21866. if (ret == BAD_FUNC_ARG) {
  21867. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  21868. }
  21869. if (ret == BAD_FUNC_ARG) {
  21870. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  21871. }
  21872. if (ret == BAD_FUNC_ARG) {
  21873. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  21874. }
  21875. if (ret == BAD_FUNC_ARG) {
  21876. /* Invalid length */
  21877. outlen = 1;
  21878. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21879. }
  21880. if (ret == BUFFER_E) {
  21881. ret = 0;
  21882. }
  21883. else if (ret == 0) {
  21884. ret = WOLFSSL_FATAL_ERROR;
  21885. }
  21886. }
  21887. }
  21888. if (wc_FreeRng(&rng) && ret == 0) {
  21889. ret = WOLFSSL_FATAL_ERROR;
  21890. }
  21891. wc_ecc_free(&key);
  21892. wc_ecc_free(&pubKey);
  21893. #ifdef FP_ECC
  21894. wc_ecc_fp_free();
  21895. #endif
  21896. PRIVATE_KEY_LOCK();
  21897. res = TEST_RES_CHECK(ret == 0);
  21898. #endif
  21899. return res;
  21900. } /* END tests_wc_ecc_shared_secret */
  21901. /*
  21902. * testint wc_ecc_export_x963()
  21903. */
  21904. static int test_wc_ecc_export_x963(void)
  21905. {
  21906. int res = TEST_SKIPPED;
  21907. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21908. ecc_key key;
  21909. WC_RNG rng;
  21910. byte out[ECC_ASN963_MAX_BUF_SZ];
  21911. word32 outlen = sizeof(out);
  21912. int ret = 0;
  21913. PRIVATE_KEY_UNLOCK();
  21914. /* Initialize variables. */
  21915. XMEMSET(out, 0, outlen);
  21916. XMEMSET(&rng, 0, sizeof(rng));
  21917. XMEMSET(&key, 0, sizeof(key));
  21918. ret = wc_InitRng(&rng);
  21919. if (ret == 0) {
  21920. ret = wc_ecc_init(&key);
  21921. if (ret == 0) {
  21922. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21923. #if defined(WOLFSSL_ASYNC_CRYPT)
  21924. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21925. #endif
  21926. }
  21927. }
  21928. if (ret == 0) {
  21929. ret = wc_ecc_export_x963(&key, out, &outlen);
  21930. }
  21931. /* Test bad args. */
  21932. if (ret == 0) {
  21933. ret = wc_ecc_export_x963(NULL, out, &outlen);
  21934. if (ret == ECC_BAD_ARG_E) {
  21935. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  21936. }
  21937. if (ret == LENGTH_ONLY_E) {
  21938. ret = wc_ecc_export_x963(&key, out, NULL);
  21939. }
  21940. if (ret == ECC_BAD_ARG_E) {
  21941. key.idx = -4;
  21942. ret = wc_ecc_export_x963(&key, out, &outlen);
  21943. }
  21944. if (ret == ECC_BAD_ARG_E) {
  21945. ret = 0;
  21946. }
  21947. else {
  21948. ret = WOLFSSL_FATAL_ERROR;
  21949. }
  21950. }
  21951. if (wc_FreeRng(&rng) && ret == 0) {
  21952. ret = WOLFSSL_FATAL_ERROR;
  21953. }
  21954. wc_ecc_free(&key);
  21955. #ifdef FP_ECC
  21956. wc_ecc_fp_free();
  21957. #endif
  21958. PRIVATE_KEY_LOCK();
  21959. res = TEST_RES_CHECK(ret == 0);
  21960. #endif
  21961. return res;
  21962. } /* END test_wc_ecc_export_x963 */
  21963. /*
  21964. * Testing wc_ecc_export_x963_ex()
  21965. * compile with --enable-compkey will use compression.
  21966. */
  21967. static int test_wc_ecc_export_x963_ex(void)
  21968. {
  21969. int res = TEST_SKIPPED;
  21970. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21971. ecc_key key;
  21972. WC_RNG rng;
  21973. int ret = 0;
  21974. byte out[ECC_ASN963_MAX_BUF_SZ];
  21975. word32 outlen = sizeof(out);
  21976. #ifdef HAVE_COMP_KEY
  21977. word32 badOutLen = 5;
  21978. #endif
  21979. /* Init stack variables. */
  21980. XMEMSET(out, 0, outlen);
  21981. XMEMSET(&rng, 0, sizeof(rng));
  21982. XMEMSET(&key, 0, sizeof(key));
  21983. ret = wc_InitRng(&rng);
  21984. if (ret == 0) {
  21985. ret = wc_ecc_init(&key);
  21986. if (ret == 0) {
  21987. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21988. #if defined(WOLFSSL_ASYNC_CRYPT)
  21989. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21990. #endif
  21991. }
  21992. }
  21993. #ifdef HAVE_COMP_KEY
  21994. if (ret == 0) {
  21995. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21996. }
  21997. #else
  21998. if (ret == 0) {
  21999. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  22000. }
  22001. #endif
  22002. /* Test bad args. */
  22003. #ifdef HAVE_COMP_KEY
  22004. if (ret == 0) {
  22005. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  22006. if (ret == BAD_FUNC_ARG) {
  22007. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  22008. }
  22009. if (ret == BAD_FUNC_ARG) {
  22010. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  22011. }
  22012. if (ret == BAD_FUNC_ARG) {
  22013. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  22014. }
  22015. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  22016. if (ret == BUFFER_E)
  22017. #else
  22018. if (ret == LENGTH_ONLY_E)
  22019. #endif
  22020. {
  22021. key.idx = -4;
  22022. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  22023. }
  22024. if (ret == ECC_BAD_ARG_E) {
  22025. ret = 0;
  22026. }
  22027. else {
  22028. ret = WOLFSSL_FATAL_ERROR;
  22029. }
  22030. }
  22031. #else
  22032. if (ret == 0) {
  22033. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  22034. if (ret == BAD_FUNC_ARG) {
  22035. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  22036. }
  22037. if (ret == BAD_FUNC_ARG) {
  22038. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  22039. }
  22040. if (ret == NOT_COMPILED_IN) {
  22041. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  22042. }
  22043. if (ret == BAD_FUNC_ARG) {
  22044. key.idx = -4;
  22045. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  22046. }
  22047. if (ret == ECC_BAD_ARG_E) {
  22048. ret = 0;
  22049. }
  22050. else if (ret == 0) {
  22051. ret = WOLFSSL_FATAL_ERROR;
  22052. }
  22053. }
  22054. #endif
  22055. if (wc_FreeRng(&rng) && ret == 0) {
  22056. ret = WOLFSSL_FATAL_ERROR;
  22057. }
  22058. wc_ecc_free(&key);
  22059. #ifdef FP_ECC
  22060. wc_ecc_fp_free();
  22061. #endif
  22062. res = TEST_RES_CHECK(ret == 0);
  22063. #endif
  22064. return res;
  22065. } /* END test_wc_ecc_export_x963_ex */
  22066. /*
  22067. * testing wc_ecc_import_x963()
  22068. */
  22069. static int test_wc_ecc_import_x963(void)
  22070. {
  22071. int res = TEST_SKIPPED;
  22072. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22073. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22074. ecc_key pubKey, key;
  22075. WC_RNG rng;
  22076. byte x963[ECC_ASN963_MAX_BUF_SZ];
  22077. word32 x963Len = (word32)sizeof(x963);
  22078. int ret;
  22079. /* Init stack variables. */
  22080. XMEMSET(x963, 0, x963Len);
  22081. XMEMSET(&rng, 0, sizeof(rng));
  22082. XMEMSET(&key, 0, sizeof(key));
  22083. XMEMSET(&pubKey, 0, sizeof(pubKey));
  22084. ret = wc_InitRng(&rng);
  22085. if (ret == 0) {
  22086. ret = wc_ecc_init(&pubKey);
  22087. if (ret == 0) {
  22088. ret = wc_ecc_init(&key);
  22089. }
  22090. if (ret == 0) {
  22091. ret = wc_ecc_make_key(&rng, KEY24, &key);
  22092. #if defined(WOLFSSL_ASYNC_CRYPT)
  22093. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22094. #endif
  22095. }
  22096. if (ret == 0) {
  22097. PRIVATE_KEY_UNLOCK();
  22098. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  22099. PRIVATE_KEY_LOCK();
  22100. }
  22101. }
  22102. if (ret == 0) {
  22103. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  22104. }
  22105. /* Test bad args. */
  22106. if (ret == 0) {
  22107. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  22108. if (ret == BAD_FUNC_ARG) {
  22109. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  22110. }
  22111. if (ret == BAD_FUNC_ARG) {
  22112. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  22113. }
  22114. if (ret == ECC_BAD_ARG_E) {
  22115. ret = 0;
  22116. }
  22117. else if (ret == 0) {
  22118. ret = WOLFSSL_FATAL_ERROR;
  22119. }
  22120. }
  22121. if (wc_FreeRng(&rng) && ret == 0) {
  22122. ret = WOLFSSL_FATAL_ERROR;
  22123. }
  22124. wc_ecc_free(&key);
  22125. wc_ecc_free(&pubKey);
  22126. #ifdef FP_ECC
  22127. wc_ecc_fp_free();
  22128. #endif
  22129. res = TEST_RES_CHECK(ret == 0);
  22130. #endif
  22131. return res;
  22132. } /* END wc_ecc_import_x963 */
  22133. /*
  22134. * testing wc_ecc_import_private_key()
  22135. */
  22136. static int ecc_import_private_key(void)
  22137. {
  22138. int res = TEST_SKIPPED;
  22139. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22140. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22141. ecc_key key, keyImp;
  22142. WC_RNG rng;
  22143. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  22144. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  22145. word32 privKeySz = (word32)sizeof(privKey);
  22146. word32 x963KeySz = (word32)sizeof(x963Key);
  22147. int ret;
  22148. /* Init stack variables. */
  22149. XMEMSET(privKey, 0, privKeySz);
  22150. XMEMSET(x963Key, 0, x963KeySz);
  22151. XMEMSET(&rng, 0, sizeof(rng));
  22152. XMEMSET(&key, 0, sizeof(key));
  22153. XMEMSET(&keyImp, 0, sizeof(keyImp));
  22154. ret = wc_InitRng(&rng);
  22155. if (ret == 0) {
  22156. ret = wc_ecc_init(&key);
  22157. if (ret == 0) {
  22158. ret = wc_ecc_init(&keyImp);
  22159. }
  22160. if (ret == 0) {
  22161. ret = wc_ecc_make_key(&rng, KEY48, &key);
  22162. #if defined(WOLFSSL_ASYNC_CRYPT)
  22163. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22164. #endif
  22165. }
  22166. if (ret == 0) {
  22167. PRIVATE_KEY_UNLOCK();
  22168. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  22169. PRIVATE_KEY_LOCK();
  22170. }
  22171. if (ret == 0) {
  22172. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  22173. }
  22174. }
  22175. if (ret == 0) {
  22176. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22177. x963KeySz, &keyImp);
  22178. }
  22179. /* Pass in bad args. */
  22180. if (ret == 0) {
  22181. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22182. x963KeySz, NULL);
  22183. if (ret == BAD_FUNC_ARG) {
  22184. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  22185. x963KeySz, &keyImp);
  22186. }
  22187. if (ret == BAD_FUNC_ARG) {
  22188. ret = 0;
  22189. }
  22190. else if (ret == 0) {
  22191. ret = WOLFSSL_FATAL_ERROR;
  22192. }
  22193. }
  22194. if (wc_FreeRng(&rng) && ret == 0) {
  22195. ret = WOLFSSL_FATAL_ERROR;
  22196. }
  22197. wc_ecc_free(&key);
  22198. wc_ecc_free(&keyImp);
  22199. #ifdef FP_ECC
  22200. wc_ecc_fp_free();
  22201. #endif
  22202. res = TEST_RES_CHECK(ret == 0);
  22203. #endif
  22204. return res;
  22205. } /* END wc_ecc_import_private_key */
  22206. /*
  22207. * Testing wc_ecc_export_private_only()
  22208. */
  22209. static int test_wc_ecc_export_private_only(void)
  22210. {
  22211. int res = TEST_SKIPPED;
  22212. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22213. ecc_key key;
  22214. WC_RNG rng;
  22215. byte out[ECC_PRIV_KEY_BUF];
  22216. word32 outlen = sizeof(out);
  22217. int ret;
  22218. /* Init stack variables. */
  22219. XMEMSET(out, 0, outlen);
  22220. XMEMSET(&rng, 0, sizeof(rng));
  22221. XMEMSET(&key, 0, sizeof(key));
  22222. ret = wc_InitRng(&rng);
  22223. if (ret == 0) {
  22224. ret = wc_ecc_init(&key);
  22225. if (ret == 0) {
  22226. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22227. #if defined(WOLFSSL_ASYNC_CRYPT)
  22228. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22229. #endif
  22230. }
  22231. }
  22232. if (ret == 0) {
  22233. ret = wc_ecc_export_private_only(&key, out, &outlen);
  22234. }
  22235. /* Pass in bad args. */
  22236. if (ret == 0) {
  22237. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  22238. if (ret == BAD_FUNC_ARG) {
  22239. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  22240. }
  22241. if (ret == BAD_FUNC_ARG) {
  22242. ret = wc_ecc_export_private_only(&key, out, NULL);
  22243. }
  22244. if (ret == BAD_FUNC_ARG) {
  22245. ret = 0;
  22246. }
  22247. else if (ret == 0) {
  22248. ret = WOLFSSL_FATAL_ERROR;
  22249. }
  22250. }
  22251. if (wc_FreeRng(&rng) && ret == 0) {
  22252. ret = WOLFSSL_FATAL_ERROR;
  22253. }
  22254. wc_ecc_free(&key);
  22255. #ifdef FP_ECC
  22256. wc_ecc_fp_free();
  22257. #endif
  22258. res = TEST_RES_CHECK(ret == 0);
  22259. #endif
  22260. return res;
  22261. } /* END test_wc_ecc_export_private_only */
  22262. /*
  22263. * Testing wc_ecc_rs_to_sig()
  22264. */
  22265. static int test_wc_ecc_rs_to_sig(void)
  22266. {
  22267. int res = TEST_SKIPPED;
  22268. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22269. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22270. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22271. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22272. const char* zeroStr = "0";
  22273. byte sig[ECC_MAX_SIG_SIZE];
  22274. word32 siglen = (word32)sizeof(sig);
  22275. /*R and S max size is the order of curve. 2^192.*/
  22276. int keySz = KEY24;
  22277. byte r[KEY24];
  22278. byte s[KEY24];
  22279. word32 rlen = (word32)sizeof(r);
  22280. word32 slen = (word32)sizeof(s);
  22281. int ret;
  22282. /* Init stack variables. */
  22283. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22284. XMEMSET(r, 0, keySz);
  22285. XMEMSET(s, 0, keySz);
  22286. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  22287. if (ret == 0) {
  22288. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  22289. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) || \
  22290. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  22291. if (ret == ASN_PARSE_E) {
  22292. ret = 0;
  22293. }
  22294. #endif
  22295. }
  22296. /* Test bad args. */
  22297. if (ret == 0) {
  22298. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  22299. if (ret == ECC_BAD_ARG_E) {
  22300. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  22301. }
  22302. if (ret == ECC_BAD_ARG_E) {
  22303. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  22304. }
  22305. if (ret == ECC_BAD_ARG_E) {
  22306. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  22307. }
  22308. if (ret == ECC_BAD_ARG_E) {
  22309. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  22310. }
  22311. if (ret == MP_ZERO_E) {
  22312. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  22313. }
  22314. if (ret == MP_ZERO_E) {
  22315. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  22316. }
  22317. if (ret == ECC_BAD_ARG_E) {
  22318. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  22319. }
  22320. if (ret == ECC_BAD_ARG_E) {
  22321. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  22322. }
  22323. if (ret == ECC_BAD_ARG_E) {
  22324. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  22325. }
  22326. if (ret == ECC_BAD_ARG_E) {
  22327. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  22328. }
  22329. if (ret == ECC_BAD_ARG_E) {
  22330. ret = 0;
  22331. }
  22332. else if (ret == 0) {
  22333. ret = WOLFSSL_FATAL_ERROR;
  22334. }
  22335. }
  22336. res = TEST_RES_CHECK(ret == 0);
  22337. #endif
  22338. return res;
  22339. } /* END test_wc_ecc_rs_to_sig */
  22340. static int test_wc_ecc_import_raw(void)
  22341. {
  22342. int res = TEST_SKIPPED;
  22343. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22344. ecc_key key;
  22345. int ret = 0;
  22346. const char* qx =
  22347. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22348. const char* qy =
  22349. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22350. const char* d =
  22351. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22352. const char* curveName = "SECP256R1";
  22353. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22354. const char* kNullStr = "";
  22355. #endif
  22356. ret = wc_ecc_init(&key);
  22357. /* Test good import */
  22358. if (ret == 0) {
  22359. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22360. }
  22361. /* Test bad args. */
  22362. if (ret == 0) {
  22363. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  22364. if (ret == BAD_FUNC_ARG) {
  22365. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  22366. }
  22367. if (ret == BAD_FUNC_ARG) {
  22368. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  22369. }
  22370. if (ret == BAD_FUNC_ARG) {
  22371. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  22372. }
  22373. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22374. if (ret == BAD_FUNC_ARG) {
  22375. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22376. wc_ecc_free(&key);
  22377. #endif
  22378. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  22379. if (ret == ECC_INF_E)
  22380. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22381. }
  22382. #endif
  22383. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22384. if (ret == BAD_FUNC_ARG) {
  22385. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22386. wc_ecc_free(&key);
  22387. #endif
  22388. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  22389. /* Note: SP math "is point" failure returns MP_VAL */
  22390. if (ret == ECC_INF_E || ret == MP_VAL) {
  22391. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22392. }
  22393. }
  22394. if (ret == BAD_FUNC_ARG) {
  22395. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22396. wc_ecc_free(&key);
  22397. #endif
  22398. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  22399. /* Note: SP math "is point" failure returns MP_VAL */
  22400. if (ret == ECC_INF_E || ret == MP_VAL) {
  22401. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22402. }
  22403. }
  22404. #endif
  22405. if (ret == BAD_FUNC_ARG) {
  22406. ret = 0;
  22407. }
  22408. }
  22409. wc_ecc_free(&key);
  22410. res = TEST_RES_CHECK(ret == 0);
  22411. #endif
  22412. return res;
  22413. } /* END test_wc_ecc_import_raw */
  22414. static int test_wc_ecc_import_unsigned(void)
  22415. {
  22416. int res = TEST_SKIPPED;
  22417. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22418. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  22419. ecc_key key;
  22420. const byte qx[] = {
  22421. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22422. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22423. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22424. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22425. };
  22426. const byte qy[] = {
  22427. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22428. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22429. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22430. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22431. };
  22432. const byte d[] = {
  22433. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22434. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22435. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22436. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22437. };
  22438. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22439. const byte nullBytes[32] = {0};
  22440. #endif
  22441. int curveId = ECC_SECP256R1;
  22442. int ret;
  22443. ret = wc_ecc_init(&key);
  22444. if (ret == 0) {
  22445. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22446. curveId);
  22447. }
  22448. /* Test bad args. */
  22449. if (ret == 0) {
  22450. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22451. curveId);
  22452. if (ret == BAD_FUNC_ARG) {
  22453. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22454. curveId);
  22455. }
  22456. if (ret == BAD_FUNC_ARG) {
  22457. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22458. curveId);
  22459. }
  22460. if (ret == BAD_FUNC_ARG) {
  22461. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22462. ECC_CURVE_INVALID);
  22463. }
  22464. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22465. if (ret == BAD_FUNC_ARG) {
  22466. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22467. (byte*)nullBytes, (byte*)nullBytes, curveId);
  22468. }
  22469. #endif
  22470. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  22471. ret = 0;
  22472. }
  22473. }
  22474. wc_ecc_free(&key);
  22475. res = TEST_RES_CHECK(ret == 0);
  22476. #endif
  22477. return res;
  22478. } /* END test_wc_ecc_import_unsigned */
  22479. /*
  22480. * Testing wc_ecc_sig_size()
  22481. */
  22482. static int test_wc_ecc_sig_size(void)
  22483. {
  22484. int res = TEST_SKIPPED;
  22485. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22486. ecc_key key;
  22487. WC_RNG rng;
  22488. int keySz = KEY16;
  22489. int ret = 0;
  22490. XMEMSET(&rng, 0, sizeof(rng));
  22491. XMEMSET(&key, 0, sizeof(key));
  22492. ret = wc_InitRng(&rng);
  22493. if (ret == 0) {
  22494. ret = wc_ecc_init(&key);
  22495. if (ret == 0) {
  22496. ret = wc_ecc_make_key(&rng, keySz, &key);
  22497. #if defined(WOLFSSL_ASYNC_CRYPT)
  22498. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22499. #endif
  22500. }
  22501. }
  22502. if (ret == 0) {
  22503. ret = wc_ecc_sig_size(&key);
  22504. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  22505. ret = 0;
  22506. }
  22507. }
  22508. if (wc_FreeRng(&rng) && ret == 0) {
  22509. ret = WOLFSSL_FATAL_ERROR;
  22510. }
  22511. wc_ecc_free(&key);
  22512. res = TEST_RES_CHECK(ret == 0);
  22513. #endif
  22514. return res;
  22515. } /* END test_wc_ecc_sig_size */
  22516. /*
  22517. * Testing wc_ecc_ctx_new()
  22518. */
  22519. static int test_wc_ecc_ctx_new(void)
  22520. {
  22521. int res = TEST_SKIPPED;
  22522. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22523. WC_RNG rng;
  22524. int ret = 0;
  22525. ecEncCtx* cli = NULL;
  22526. ecEncCtx* srv = NULL;
  22527. ret = wc_InitRng(&rng);
  22528. if (ret == 0) {
  22529. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  22530. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  22531. }
  22532. if (ret == 0 && (cli == NULL || srv == NULL)) {
  22533. ret = WOLFSSL_FATAL_ERROR;
  22534. }
  22535. wc_ecc_ctx_free(cli);
  22536. wc_ecc_ctx_free(srv);
  22537. /* Test bad args. */
  22538. if (ret == 0) {
  22539. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22540. cli = wc_ecc_ctx_new(0, &rng);
  22541. if (cli != NULL) {
  22542. ret = WOLFSSL_FATAL_ERROR;
  22543. }
  22544. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  22545. if (cli != NULL) {
  22546. ret = WOLFSSL_FATAL_ERROR;
  22547. }
  22548. }
  22549. if (wc_FreeRng(&rng) && ret == 0) {
  22550. ret = WOLFSSL_FATAL_ERROR;
  22551. }
  22552. wc_ecc_ctx_free(cli);
  22553. res = TEST_RES_CHECK(ret == 0);
  22554. #endif
  22555. return res;
  22556. } /* END test_wc_ecc_ctx_new */
  22557. /*
  22558. * Tesing wc_ecc_reset()
  22559. */
  22560. static int test_wc_ecc_ctx_reset(void)
  22561. {
  22562. int res = TEST_SKIPPED;
  22563. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22564. ecEncCtx* ctx = NULL;
  22565. WC_RNG rng;
  22566. int ret = 0;
  22567. ret = wc_InitRng(&rng);
  22568. if (ret == 0) {
  22569. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  22570. ret = WOLFSSL_FATAL_ERROR;
  22571. }
  22572. }
  22573. if (ret == 0) {
  22574. ret = wc_ecc_ctx_reset(ctx, &rng);
  22575. }
  22576. /* Pass in bad args. */
  22577. if (ret == 0) {
  22578. ret = wc_ecc_ctx_reset(NULL, &rng);
  22579. if (ret == BAD_FUNC_ARG) {
  22580. ret = wc_ecc_ctx_reset(ctx, NULL);
  22581. }
  22582. if (ret == BAD_FUNC_ARG) {
  22583. ret = 0;
  22584. }
  22585. else if (ret == 0) {
  22586. ret = WOLFSSL_FATAL_ERROR;
  22587. }
  22588. }
  22589. if (wc_FreeRng(&rng) && ret == 0) {
  22590. ret = WOLFSSL_FATAL_ERROR;
  22591. }
  22592. wc_ecc_ctx_free(ctx);
  22593. res = TEST_RES_CHECK(ret == 0);
  22594. #endif
  22595. return res;
  22596. } /* END test_wc_ecc_ctx_reset */
  22597. /*
  22598. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22599. */
  22600. static int test_wc_ecc_ctx_set_peer_salt(void)
  22601. {
  22602. int res = TEST_SKIPPED;
  22603. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22604. WC_RNG rng;
  22605. ecEncCtx* cliCtx = NULL;
  22606. ecEncCtx* servCtx = NULL;
  22607. const byte* cliSalt = NULL;
  22608. const byte* servSalt = NULL;
  22609. int ret = 0;
  22610. ret = wc_InitRng(&rng);
  22611. if (ret == 0) {
  22612. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  22613. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  22614. ret = WOLFSSL_FATAL_ERROR;
  22615. }
  22616. }
  22617. /* Test bad args. */
  22618. if (ret == 0) {
  22619. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  22620. if (cliSalt != NULL) {
  22621. ret = WOLFSSL_FATAL_ERROR;
  22622. }
  22623. }
  22624. if (ret == 0) {
  22625. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  22626. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  22627. if (cliSalt == NULL || servSalt == NULL) {
  22628. ret = WOLFSSL_FATAL_ERROR;
  22629. }
  22630. }
  22631. if (ret == 0) {
  22632. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  22633. }
  22634. /* Test bad args. */
  22635. if (ret == 0) {
  22636. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  22637. if (ret == BAD_FUNC_ARG) {
  22638. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  22639. }
  22640. if (ret == BAD_FUNC_ARG) {
  22641. ret = 0;
  22642. }
  22643. else if (ret == 0) {
  22644. ret = WOLFSSL_FATAL_ERROR;
  22645. }
  22646. }
  22647. if (wc_FreeRng(&rng) && ret == 0) {
  22648. ret = WOLFSSL_FATAL_ERROR;
  22649. }
  22650. wc_ecc_ctx_free(cliCtx);
  22651. wc_ecc_ctx_free(servCtx);
  22652. res = TEST_RES_CHECK(ret == 0);
  22653. #endif
  22654. return res;
  22655. } /* END test_wc_ecc_ctx_set_peer_salt */
  22656. /*
  22657. * Testing wc_ecc_ctx_set_info()
  22658. */
  22659. static int test_wc_ecc_ctx_set_info(void)
  22660. {
  22661. int res = TEST_SKIPPED;
  22662. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22663. ecEncCtx* ctx = NULL;
  22664. WC_RNG rng;
  22665. int ret;
  22666. const char* optInfo = "Optional Test Info.";
  22667. int optInfoSz = (int)XSTRLEN(optInfo);
  22668. const char* badOptInfo = NULL;
  22669. ret = wc_InitRng(&rng);
  22670. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  22671. ret = WOLFSSL_FATAL_ERROR;
  22672. }
  22673. if (ret == 0) {
  22674. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  22675. }
  22676. /* Test bad args. */
  22677. if (ret == 0) {
  22678. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  22679. if (ret == BAD_FUNC_ARG) {
  22680. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  22681. }
  22682. if (ret == BAD_FUNC_ARG) {
  22683. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  22684. }
  22685. if (ret == BAD_FUNC_ARG) {
  22686. ret = 0;
  22687. }
  22688. else if (ret == 0) {
  22689. ret = WOLFSSL_FATAL_ERROR;
  22690. }
  22691. }
  22692. if (wc_FreeRng(&rng) && ret == 0) {
  22693. ret = WOLFSSL_FATAL_ERROR;
  22694. }
  22695. wc_ecc_ctx_free(ctx);
  22696. res = TEST_RES_CHECK(ret == 0);
  22697. #endif
  22698. return res;
  22699. } /* END test_wc_ecc_ctx_set_info */
  22700. /*
  22701. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22702. */
  22703. static int test_wc_ecc_encryptDecrypt(void)
  22704. {
  22705. int res = TEST_SKIPPED;
  22706. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22707. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22708. ecc_key srvKey, cliKey, tmpKey;
  22709. WC_RNG rng;
  22710. int ret;
  22711. const char* msg = "EccBlock Size 16";
  22712. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22713. #ifdef WOLFSSL_ECIES_OLD
  22714. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22715. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22716. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22717. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22718. #else
  22719. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22720. #endif
  22721. word32 outSz = (word32)sizeof(out);
  22722. byte plain[sizeof("EccBlock Size 16")];
  22723. word32 plainSz = (word32)sizeof(plain);
  22724. int keySz = KEY20;
  22725. /* Init stack variables. */
  22726. XMEMSET(out, 0, outSz);
  22727. XMEMSET(plain, 0, plainSz);
  22728. XMEMSET(&rng, 0, sizeof(rng));
  22729. XMEMSET(&srvKey, 0, sizeof(srvKey));
  22730. XMEMSET(&cliKey, 0, sizeof(cliKey));
  22731. ret = wc_InitRng(&rng);
  22732. if (ret == 0) {
  22733. ret = wc_ecc_init(&cliKey);
  22734. if (ret == 0) {
  22735. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22736. #if defined(WOLFSSL_ASYNC_CRYPT)
  22737. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22738. #endif
  22739. }
  22740. if (ret == 0) {
  22741. ret = wc_ecc_init(&srvKey);
  22742. }
  22743. if (ret == 0) {
  22744. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22745. #if defined(WOLFSSL_ASYNC_CRYPT)
  22746. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22747. #endif
  22748. }
  22749. if (ret == 0) {
  22750. ret = wc_ecc_init(&tmpKey);
  22751. }
  22752. }
  22753. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22754. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22755. !defined(HAVE_SELFTEST)
  22756. if (ret == 0) {
  22757. ret = wc_ecc_set_rng(&srvKey, &rng);
  22758. }
  22759. if (ret == 0) {
  22760. ret = wc_ecc_set_rng(&cliKey, &rng);
  22761. }
  22762. #endif
  22763. if (ret == 0) {
  22764. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22765. &outSz, NULL);
  22766. }
  22767. if (ret == 0) {
  22768. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  22769. &outSz, NULL);
  22770. if (ret == BAD_FUNC_ARG) {
  22771. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  22772. &outSz, NULL);
  22773. }
  22774. if (ret == BAD_FUNC_ARG) {
  22775. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  22776. &outSz, NULL);
  22777. }
  22778. if (ret == BAD_FUNC_ARG) {
  22779. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22780. &outSz, NULL);
  22781. }
  22782. if (ret == BAD_FUNC_ARG) {
  22783. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22784. NULL, NULL);
  22785. }
  22786. if (ret == BAD_FUNC_ARG) {
  22787. ret = 0;
  22788. }
  22789. else if (ret == 0) {
  22790. ret = WOLFSSL_FATAL_ERROR;
  22791. }
  22792. }
  22793. #ifdef WOLFSSL_ECIES_OLD
  22794. if (ret == 0) {
  22795. tmpKey.dp = cliKey.dp;
  22796. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  22797. }
  22798. #endif
  22799. if (ret == 0) {
  22800. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  22801. &plainSz, NULL);
  22802. }
  22803. if (ret == 0) {
  22804. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  22805. &plainSz, NULL);
  22806. #ifdef WOLFSSL_ECIES_OLD
  22807. /* NULL parameter allowed in new implementations - public key comes from
  22808. * the message. */
  22809. if (ret == BAD_FUNC_ARG) {
  22810. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  22811. &plainSz, NULL);
  22812. }
  22813. #endif
  22814. if (ret == BAD_FUNC_ARG) {
  22815. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  22816. &plainSz, NULL);
  22817. }
  22818. if (ret == BAD_FUNC_ARG) {
  22819. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  22820. &plainSz, NULL);
  22821. }
  22822. if (ret == BAD_FUNC_ARG) {
  22823. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  22824. plain, NULL, NULL);
  22825. }
  22826. if (ret == BAD_FUNC_ARG) {
  22827. ret = 0;
  22828. }
  22829. else if (ret == 0) {
  22830. ret = WOLFSSL_FATAL_ERROR;
  22831. }
  22832. }
  22833. if (XMEMCMP(msg, plain, msgSz) != 0) {
  22834. ret = WOLFSSL_FATAL_ERROR;
  22835. }
  22836. if (wc_FreeRng(&rng) && ret == 0) {
  22837. ret = WOLFSSL_FATAL_ERROR;
  22838. }
  22839. wc_ecc_free(&tmpKey);
  22840. wc_ecc_free(&cliKey);
  22841. wc_ecc_free(&srvKey);
  22842. res = TEST_RES_CHECK(ret == 0);
  22843. #endif
  22844. return res;
  22845. } /* END test_wc_ecc_encryptDecrypt */
  22846. /*
  22847. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22848. */
  22849. static int test_wc_ecc_del_point(void)
  22850. {
  22851. int res = TEST_SKIPPED;
  22852. #if defined(HAVE_ECC)
  22853. ecc_point* pt;
  22854. pt = wc_ecc_new_point();
  22855. wc_ecc_del_point(pt);
  22856. res = TEST_RES_CHECK(pt != NULL);
  22857. #endif
  22858. return res;
  22859. } /* END test_wc_ecc_del_point */
  22860. /*
  22861. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22862. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22863. * and wc_ecc_cmp_point()
  22864. */
  22865. static int test_wc_ecc_pointFns(void)
  22866. {
  22867. int res = TEST_SKIPPED;
  22868. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22869. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22870. !defined(WOLFSSL_ATECC608A)
  22871. ecc_key key;
  22872. WC_RNG rng;
  22873. int ret;
  22874. ecc_point* point = NULL;
  22875. ecc_point* cpypt = NULL;
  22876. int idx = 0;
  22877. int keySz = KEY32;
  22878. byte der[DER_SZ(KEY32)];
  22879. word32 derlenChk = 0;
  22880. word32 derSz = DER_SZ(KEY32);
  22881. /* Init stack variables. */
  22882. XMEMSET(der, 0, derSz);
  22883. XMEMSET(&rng, 0, sizeof(rng));
  22884. XMEMSET(&key, 0, sizeof(key));
  22885. ret = wc_InitRng(&rng);
  22886. if (ret == 0) {
  22887. ret = wc_ecc_init(&key);
  22888. if (ret == 0) {
  22889. ret = wc_ecc_make_key(&rng, keySz, &key);
  22890. #if defined(WOLFSSL_ASYNC_CRYPT)
  22891. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22892. #endif
  22893. }
  22894. }
  22895. if (ret == 0) {
  22896. point = wc_ecc_new_point();
  22897. if (!point) {
  22898. ret = WOLFSSL_FATAL_ERROR;
  22899. }
  22900. }
  22901. if (ret == 0) {
  22902. cpypt = wc_ecc_new_point();
  22903. if (!cpypt) {
  22904. ret = WOLFSSL_FATAL_ERROR;
  22905. }
  22906. }
  22907. /* Export */
  22908. if (ret == 0) {
  22909. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22910. NULL, &derlenChk);
  22911. /* Check length value. */
  22912. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  22913. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22914. der, &derSz);
  22915. }
  22916. }
  22917. /* Test bad args. */
  22918. if (ret == 0) {
  22919. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  22920. if (ret == ECC_BAD_ARG_E) {
  22921. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  22922. }
  22923. if (ret == ECC_BAD_ARG_E) {
  22924. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22925. der, NULL);
  22926. }
  22927. if (ret == ECC_BAD_ARG_E) {
  22928. ret = 0;
  22929. }
  22930. else if (ret == 0) {
  22931. ret = WOLFSSL_FATAL_ERROR;
  22932. }
  22933. }
  22934. /* Import */
  22935. if (ret == 0) {
  22936. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  22937. /* Condition double checks wc_ecc_cmp_point(). */
  22938. if (ret == 0 &&
  22939. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  22940. ret = wc_ecc_cmp_point(&key.pubkey, point);
  22941. }
  22942. }
  22943. /* Test bad args. */
  22944. if (ret == 0) {
  22945. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  22946. if (ret == ECC_BAD_ARG_E) {
  22947. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  22948. }
  22949. if (ret == ECC_BAD_ARG_E) {
  22950. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  22951. }
  22952. if (ret == ECC_BAD_ARG_E) {
  22953. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  22954. }
  22955. if (ret == ECC_BAD_ARG_E) {
  22956. ret = 0;
  22957. }
  22958. else if (ret == 0) {
  22959. ret = WOLFSSL_FATAL_ERROR;
  22960. }
  22961. }
  22962. /* Copy */
  22963. if (ret == 0) {
  22964. ret = wc_ecc_copy_point(point, cpypt);
  22965. }
  22966. /* Test bad args. */
  22967. if (ret == 0) {
  22968. ret = wc_ecc_copy_point(NULL, cpypt);
  22969. if (ret == ECC_BAD_ARG_E) {
  22970. ret = wc_ecc_copy_point(point, NULL);
  22971. }
  22972. if (ret == ECC_BAD_ARG_E) {
  22973. ret = 0;
  22974. }
  22975. else if (ret == 0) {
  22976. ret = WOLFSSL_FATAL_ERROR;
  22977. }
  22978. }
  22979. /* Compare point */
  22980. if (ret == 0) {
  22981. ret = wc_ecc_cmp_point(point, cpypt);
  22982. }
  22983. /* Test bad args. */
  22984. if (ret == 0) {
  22985. ret = wc_ecc_cmp_point(NULL, cpypt);
  22986. if (ret == BAD_FUNC_ARG) {
  22987. ret = wc_ecc_cmp_point(point, NULL);
  22988. }
  22989. if (ret == BAD_FUNC_ARG) {
  22990. ret = 0;
  22991. }
  22992. else if (ret == 0) {
  22993. ret = WOLFSSL_FATAL_ERROR;
  22994. }
  22995. }
  22996. /* At infinity if return == 1, otherwise return == 0. */
  22997. if (ret == 0) {
  22998. ret = wc_ecc_point_is_at_infinity(point);
  22999. }
  23000. /* Test bad args. */
  23001. if (ret == 0) {
  23002. ret = wc_ecc_point_is_at_infinity(NULL);
  23003. if (ret == BAD_FUNC_ARG) {
  23004. ret = 0;
  23005. }
  23006. else if (ret == 0) {
  23007. ret = WOLFSSL_FATAL_ERROR;
  23008. }
  23009. }
  23010. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  23011. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  23012. #ifdef USE_ECC_B_PARAM
  23013. /* On curve if ret == 0 */
  23014. if (ret == 0) {
  23015. ret = wc_ecc_point_is_on_curve(point, idx);
  23016. }
  23017. /* Test bad args. */
  23018. if (ret == 0) {
  23019. ret = wc_ecc_point_is_on_curve(NULL, idx);
  23020. if (ret == BAD_FUNC_ARG) {
  23021. ret = wc_ecc_point_is_on_curve(point, 1000);
  23022. }
  23023. if (ret == ECC_BAD_ARG_E) {
  23024. ret = 0;
  23025. }
  23026. else if (ret == 0) {
  23027. ret = WOLFSSL_FATAL_ERROR;
  23028. }
  23029. }
  23030. #endif /* USE_ECC_B_PARAM */
  23031. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  23032. /* Free */
  23033. wc_ecc_del_point(point);
  23034. wc_ecc_del_point(cpypt);
  23035. wc_ecc_free(&key);
  23036. if (wc_FreeRng(&rng) && ret == 0) {
  23037. ret = WOLFSSL_FATAL_ERROR;
  23038. }
  23039. res = TEST_RES_CHECK(ret == 0);
  23040. #endif
  23041. return res;
  23042. } /* END test_wc_ecc_pointFns */
  23043. /*
  23044. * Testing wc_ecc_sahred_secret_ssh()
  23045. */
  23046. static int test_wc_ecc_shared_secret_ssh(void)
  23047. {
  23048. int res = TEST_SKIPPED;
  23049. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  23050. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23051. !defined(WOLFSSL_ATECC608A)
  23052. ecc_key key, key2;
  23053. WC_RNG rng;
  23054. int ret;
  23055. int keySz = KEY32;
  23056. int key2Sz = KEY24;
  23057. byte secret[KEY32];
  23058. word32 secretLen = keySz;
  23059. /* Init stack variables. */
  23060. XMEMSET(secret, 0, secretLen);
  23061. XMEMSET(&rng, 0, sizeof(rng));
  23062. XMEMSET(&key, 0, sizeof(key));
  23063. XMEMSET(&key2, 0, sizeof(key2));
  23064. /* Make keys */
  23065. ret = wc_InitRng(&rng);
  23066. if (ret == 0) {
  23067. ret = wc_ecc_init(&key);
  23068. if (ret == 0) {
  23069. ret = wc_ecc_make_key(&rng, keySz, &key);
  23070. #if defined(WOLFSSL_ASYNC_CRYPT)
  23071. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23072. #endif
  23073. }
  23074. if (wc_FreeRng(&rng) && ret == 0) {
  23075. ret = WOLFSSL_FATAL_ERROR;
  23076. }
  23077. }
  23078. if (ret == 0) {
  23079. ret = wc_InitRng(&rng);
  23080. if (ret == 0) {
  23081. ret = wc_ecc_init(&key2);
  23082. }
  23083. if (ret == 0) {
  23084. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  23085. #if defined(WOLFSSL_ASYNC_CRYPT)
  23086. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  23087. #endif
  23088. }
  23089. }
  23090. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23091. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23092. !defined(HAVE_SELFTEST)
  23093. if (ret == 0) {
  23094. ret = wc_ecc_set_rng(&key, &rng);
  23095. }
  23096. #endif
  23097. if (ret == 0) {
  23098. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23099. }
  23100. /* Pass in bad args. */
  23101. if (ret == 0) {
  23102. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  23103. if (ret == BAD_FUNC_ARG) {
  23104. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  23105. }
  23106. if (ret == BAD_FUNC_ARG) {
  23107. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  23108. }
  23109. if (ret == BAD_FUNC_ARG) {
  23110. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  23111. }
  23112. if (ret == BAD_FUNC_ARG) {
  23113. key.type = ECC_PUBLICKEY;
  23114. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23115. if (ret == ECC_BAD_ARG_E) {
  23116. ret = 0;
  23117. }
  23118. else if (ret == 0) {
  23119. ret = WOLFSSL_FATAL_ERROR;
  23120. }
  23121. }
  23122. else if (ret == 0) {
  23123. ret = WOLFSSL_FATAL_ERROR;
  23124. }
  23125. }
  23126. if (wc_FreeRng(&rng) && ret == 0) {
  23127. ret = WOLFSSL_FATAL_ERROR;
  23128. }
  23129. wc_ecc_free(&key);
  23130. wc_ecc_free(&key2);
  23131. #ifdef FP_ECC
  23132. wc_ecc_fp_free();
  23133. #endif
  23134. res = TEST_RES_CHECK(ret == 0);
  23135. #endif
  23136. return res;
  23137. } /* END test_wc_ecc_shared_secret_ssh */
  23138. /*
  23139. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  23140. */
  23141. static int test_wc_ecc_verify_hash_ex(void)
  23142. {
  23143. int res = TEST_SKIPPED;
  23144. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  23145. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23146. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  23147. ecc_key key;
  23148. WC_RNG rng;
  23149. int ret;
  23150. mp_int r;
  23151. mp_int s;
  23152. mp_int z;
  23153. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  23154. unsigned char iHash[] = "Everyone gets Friday off.......";
  23155. unsigned char shortHash[] = TEST_STRING;
  23156. word32 hashlen = sizeof(hash);
  23157. word32 iHashLen = sizeof(iHash);
  23158. word32 shortHashLen = sizeof(shortHash);
  23159. int keySz = KEY32;
  23160. int sig = WOLFSSL_FATAL_ERROR;
  23161. int ver = WOLFSSL_FATAL_ERROR;
  23162. int verify_ok = 0;
  23163. /* Initialize r and s. */
  23164. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  23165. if (ret != MP_OKAY) {
  23166. return MP_INIT_E;
  23167. }
  23168. ret = wc_InitRng(&rng);
  23169. if (ret == 0) {
  23170. ret = wc_ecc_init(&key);
  23171. if (ret == 0) {
  23172. ret = wc_ecc_make_key(&rng, keySz, &key);
  23173. #if defined(WOLFSSL_ASYNC_CRYPT)
  23174. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23175. #endif
  23176. }
  23177. }
  23178. if (ret == 0) {
  23179. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  23180. if (ret == 0) {
  23181. /* verify_ok should be 1. */
  23182. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  23183. if (verify_ok != 1 && ret == 0) {
  23184. ret = WOLFSSL_FATAL_ERROR;
  23185. }
  23186. }
  23187. if (ret == 0) {
  23188. /* verify_ok should be 0 */
  23189. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  23190. &verify_ok, &key);
  23191. if (verify_ok != 0 && ret == 0) {
  23192. ret = WOLFSSL_FATAL_ERROR;
  23193. }
  23194. }
  23195. if (ret == 0) {
  23196. /* verify_ok should be 0. */
  23197. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23198. &verify_ok, &key);
  23199. if (verify_ok != 0 && ret == 0) {
  23200. ret = WOLFSSL_FATAL_ERROR;
  23201. }
  23202. }
  23203. }
  23204. /* Test bad args. */
  23205. if (ret == 0) {
  23206. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  23207. == ECC_BAD_ARG_E) {
  23208. sig = 0;
  23209. }
  23210. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  23211. != ECC_BAD_ARG_E) {
  23212. sig = WOLFSSL_FATAL_ERROR;
  23213. }
  23214. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  23215. != ECC_BAD_ARG_E) {
  23216. sig = WOLFSSL_FATAL_ERROR;
  23217. }
  23218. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  23219. != ECC_BAD_ARG_E) {
  23220. sig = WOLFSSL_FATAL_ERROR;
  23221. }
  23222. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  23223. != ECC_BAD_ARG_E) {
  23224. sig = WOLFSSL_FATAL_ERROR;
  23225. }
  23226. }
  23227. /* Test bad args. */
  23228. if (ret == 0) {
  23229. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  23230. == ECC_BAD_ARG_E) {
  23231. ver = 0;
  23232. }
  23233. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  23234. &verify_ok, &key) != ECC_BAD_ARG_E) {
  23235. ver = WOLFSSL_FATAL_ERROR;
  23236. }
  23237. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  23238. != MP_ZERO_E) {
  23239. ver = WOLFSSL_FATAL_ERROR;
  23240. }
  23241. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  23242. != MP_ZERO_E) {
  23243. ver = WOLFSSL_FATAL_ERROR;
  23244. }
  23245. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  23246. != MP_ZERO_E) {
  23247. ver = WOLFSSL_FATAL_ERROR;
  23248. }
  23249. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  23250. &key) != ECC_BAD_ARG_E) {
  23251. ver = WOLFSSL_FATAL_ERROR;
  23252. }
  23253. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23254. NULL, &key) != ECC_BAD_ARG_E) {
  23255. ver = WOLFSSL_FATAL_ERROR;
  23256. }
  23257. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23258. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  23259. ver = WOLFSSL_FATAL_ERROR;
  23260. }
  23261. }
  23262. wc_ecc_free(&key);
  23263. mp_free(&r);
  23264. mp_free(&s);
  23265. if (wc_FreeRng(&rng)) {
  23266. return WOLFSSL_FATAL_ERROR;
  23267. }
  23268. if (ret == 0 && (sig != 0 || ver != 0)) {
  23269. ret = WOLFSSL_FATAL_ERROR;
  23270. }
  23271. res = TEST_RES_CHECK(ret == 0);
  23272. #endif
  23273. return res;
  23274. } /* END test_wc_ecc_verify_hash_ex */
  23275. /*
  23276. * Testing wc_ecc_mulmod()
  23277. */
  23278. static int test_wc_ecc_mulmod(void)
  23279. {
  23280. int res = TEST_SKIPPED;
  23281. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  23282. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  23283. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23284. ecc_key key1, key2, key3;
  23285. WC_RNG rng;
  23286. int ret = 0;
  23287. ret = wc_InitRng(&rng);
  23288. if (ret == 0) {
  23289. ret = wc_ecc_init(&key1);
  23290. if (ret == 0) {
  23291. ret = wc_ecc_init(&key2);
  23292. }
  23293. if (ret == 0) {
  23294. ret = wc_ecc_init(&key3);
  23295. }
  23296. if (ret == 0) {
  23297. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  23298. #if defined(WOLFSSL_ASYNC_CRYPT)
  23299. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  23300. #endif
  23301. }
  23302. wc_FreeRng(&rng);
  23303. }
  23304. if (ret == 0) {
  23305. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  23306. ECC_SECP256R1);
  23307. if (ret == 0) {
  23308. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  23309. key1.dp->prime, ECC_SECP256R1);
  23310. }
  23311. }
  23312. if (ret == 0) {
  23313. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  23314. &key3.k, 1);
  23315. }
  23316. /* Test bad args. */
  23317. if (ret == 0) {
  23318. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  23319. &key3.k, 1);
  23320. if (ret == ECC_BAD_ARG_E) {
  23321. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  23322. &key3.k, 1);
  23323. }
  23324. if (ret == ECC_BAD_ARG_E) {
  23325. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  23326. &key3.k, 1);
  23327. }
  23328. if (ret == ECC_BAD_ARG_E) {
  23329. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  23330. &key2.k, NULL, 1);
  23331. }
  23332. if (ret == ECC_BAD_ARG_E) {
  23333. ret = 0;
  23334. }
  23335. else if (ret == 0) {
  23336. ret = WOLFSSL_FATAL_ERROR;
  23337. }
  23338. }
  23339. wc_ecc_free(&key1);
  23340. wc_ecc_free(&key2);
  23341. wc_ecc_free(&key3);
  23342. #ifdef FP_ECC
  23343. wc_ecc_fp_free();
  23344. #endif
  23345. res = TEST_RES_CHECK(ret == 0);
  23346. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  23347. return res;
  23348. } /* END test_wc_ecc_mulmod */
  23349. /*
  23350. * Testing wc_ecc_is_valid_idx()
  23351. */
  23352. static int test_wc_ecc_is_valid_idx(void)
  23353. {
  23354. int res = TEST_SKIPPED;
  23355. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23356. ecc_key key;
  23357. WC_RNG rng;
  23358. int ret;
  23359. int iVal = -2;
  23360. int iVal2 = 3000;
  23361. XMEMSET(&rng, 0, sizeof(rng));
  23362. XMEMSET(&key, 0, sizeof(key));
  23363. ret = wc_InitRng(&rng);
  23364. if (ret == 0) {
  23365. ret = wc_ecc_init(&key);
  23366. if (ret == 0) {
  23367. ret = wc_ecc_make_key(&rng, 32, &key);
  23368. #if defined(WOLFSSL_ASYNC_CRYPT)
  23369. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23370. #endif
  23371. }
  23372. }
  23373. if (ret == 0) {
  23374. ret = wc_ecc_is_valid_idx(key.idx);
  23375. if (ret == 1) {
  23376. ret = 0;
  23377. }
  23378. else {
  23379. ret = WOLFSSL_FATAL_ERROR;
  23380. }
  23381. }
  23382. /* Test bad args. */
  23383. if (ret == 0) {
  23384. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  23385. if (ret == 0) {
  23386. ret = wc_ecc_is_valid_idx(iVal2);
  23387. }
  23388. if (ret != 0) {
  23389. ret = WOLFSSL_FATAL_ERROR;
  23390. }
  23391. }
  23392. if (wc_FreeRng(&rng) && ret == 0) {
  23393. ret = WOLFSSL_FATAL_ERROR;
  23394. }
  23395. wc_ecc_free(&key);
  23396. #ifdef FP_ECC
  23397. wc_ecc_fp_free();
  23398. #endif
  23399. res = TEST_RES_CHECK(ret == 0);
  23400. #endif
  23401. return res;
  23402. } /* END test_wc_ecc_is_valid_idx */
  23403. /*
  23404. * Testing wc_ecc_get_curve_id_from_oid()
  23405. */
  23406. static int test_wc_ecc_get_curve_id_from_oid(void)
  23407. {
  23408. int res = TEST_SKIPPED;
  23409. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23410. !defined(HAVE_FIPS)
  23411. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  23412. word32 len = sizeof(oid);
  23413. int ret;
  23414. /* Bad Cases */
  23415. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  23416. if (ret == BAD_FUNC_ARG) {
  23417. ret = 0;
  23418. }
  23419. if (ret == 0) {
  23420. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  23421. if (ret == ECC_CURVE_INVALID) {
  23422. ret = 0;
  23423. }
  23424. }
  23425. /* Good Case */
  23426. if (ret == 0) {
  23427. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  23428. if (ret == ECC_SECP256R1) {
  23429. ret = 0;
  23430. }
  23431. }
  23432. res = TEST_RES_CHECK(ret == 0);
  23433. #endif
  23434. return res;
  23435. }/* END test_wc_ecc_get_curve_id_from_oid */
  23436. /*
  23437. * Testing wc_ecc_sig_size_calc()
  23438. */
  23439. static int test_wc_ecc_sig_size_calc(void)
  23440. {
  23441. int res = TEST_SKIPPED;
  23442. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  23443. ecc_key key;
  23444. WC_RNG rng;
  23445. int sz = 0;
  23446. int ret = 0;
  23447. ret = wc_InitRng(&rng);
  23448. if (ret == 0) {
  23449. ret = wc_ecc_init(&key);
  23450. if (ret == 0) {
  23451. ret = wc_ecc_make_key(&rng, 16, &key);
  23452. #if defined(WOLFSSL_ASYNC_CRYPT)
  23453. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23454. #endif
  23455. }
  23456. sz = key.dp->size;
  23457. }
  23458. if (ret == 0) {
  23459. ret = wc_ecc_sig_size_calc(sz);
  23460. if (ret > 0) {
  23461. ret = 0;
  23462. }
  23463. }
  23464. wc_ecc_free(&key);
  23465. wc_FreeRng(&rng);
  23466. res = TEST_RES_CHECK(ret == 0);
  23467. #endif
  23468. return res;
  23469. } /* END test_wc_ecc_sig_size_calc */
  23470. /*
  23471. * Testing ToTraditional
  23472. */
  23473. static int test_ToTraditional(void)
  23474. {
  23475. int res = TEST_SKIPPED;
  23476. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23477. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23478. defined(OPENSSL_EXTRA_X509_SMALL))
  23479. XFILE f;
  23480. byte input[TWOK_BUF];
  23481. word32 sz;
  23482. int ret;
  23483. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  23484. AssertTrue((f != XBADFILE));
  23485. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  23486. XFCLOSE(f);
  23487. /* Good case */
  23488. ret = ToTraditional(input, sz);
  23489. if (ret > 0) {
  23490. ret = 0;
  23491. }
  23492. /* Bad cases */
  23493. if (ret == 0) {
  23494. ret = ToTraditional(NULL, 0);
  23495. if (ret == BAD_FUNC_ARG) {
  23496. ret = 0;
  23497. }
  23498. }
  23499. if (ret == 0) {
  23500. ret = ToTraditional(NULL, sz);
  23501. if (ret == BAD_FUNC_ARG) {
  23502. ret = 0;
  23503. }
  23504. }
  23505. if (ret == 0) {
  23506. ret = ToTraditional(input, 0);
  23507. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  23508. ret = 0;
  23509. }
  23510. }
  23511. res = TEST_RES_CHECK(ret == 0);
  23512. #endif
  23513. return res;
  23514. }/* End test_ToTraditional*/
  23515. /*
  23516. * Testing wc_EccPrivateKeyToDer
  23517. */
  23518. static int test_wc_EccPrivateKeyToDer(void)
  23519. {
  23520. int res = TEST_SKIPPED;
  23521. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23522. byte output[ONEK_BUF];
  23523. ecc_key eccKey;
  23524. WC_RNG rng;
  23525. word32 inLen;
  23526. int ret;
  23527. ret = wc_InitRng(&rng);
  23528. if (ret == 0) {
  23529. ret = wc_ecc_init(&eccKey);
  23530. if (ret == 0) {
  23531. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23532. #if defined(WOLFSSL_ASYNC_CRYPT)
  23533. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23534. #endif
  23535. }
  23536. inLen = (word32)sizeof(output);
  23537. /* Bad Cases */
  23538. if (ret == 0) {
  23539. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  23540. if (ret == BAD_FUNC_ARG) {
  23541. ret = 0;
  23542. }
  23543. }
  23544. if (ret == 0) {
  23545. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  23546. if (ret == BAD_FUNC_ARG) {
  23547. ret = 0;
  23548. }
  23549. }
  23550. if (ret == 0) {
  23551. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  23552. if (ret == LENGTH_ONLY_E) {
  23553. ret = 0;
  23554. }
  23555. }
  23556. if (ret == 0) {
  23557. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  23558. if (ret == BAD_FUNC_ARG) {
  23559. ret = 0;
  23560. }
  23561. }
  23562. /*Good Case */
  23563. if (ret == 0) {
  23564. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  23565. if (ret > 0) {
  23566. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23567. /* test importing private only into a PKEY struct */
  23568. EC_KEY* ec;
  23569. EVP_PKEY* pkey;
  23570. const unsigned char* der = output;
  23571. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  23572. AssertNotNull(pkey);
  23573. der = output;
  23574. ec = d2i_ECPrivateKey(NULL, &der, ret);
  23575. AssertNotNull(ec);
  23576. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23577. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23578. #endif
  23579. ret = 0;
  23580. }
  23581. }
  23582. wc_ecc_free(&eccKey);
  23583. }
  23584. wc_FreeRng(&rng);
  23585. res = TEST_RES_CHECK(ret == 0);
  23586. #endif
  23587. return res;
  23588. }/* End test_wc_EccPrivateKeyToDer*/
  23589. /*
  23590. * Testing wc_DhPublicKeyDecode
  23591. */
  23592. static int test_wc_DhPublicKeyDecode(void)
  23593. {
  23594. int res = TEST_SKIPPED;
  23595. #ifndef NO_DH
  23596. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23597. DhKey key;
  23598. word32 inOutIdx;
  23599. AssertIntEQ(wc_InitDhKey(&key), 0);
  23600. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  23601. BAD_FUNC_ARG);
  23602. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23603. BAD_FUNC_ARG);
  23604. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23605. BAD_FUNC_ARG);
  23606. inOutIdx = 0;
  23607. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23608. BAD_FUNC_ARG);
  23609. inOutIdx = 0;
  23610. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23611. BAD_FUNC_ARG);
  23612. inOutIdx = 0;
  23613. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23614. sizeof_dh_pub_key_der_2048), 0);
  23615. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  23616. key.pub.used != 0 && key.priv.used == 0);
  23617. wc_FreeDhKey(&key);
  23618. res = TEST_RES_CHECK(1);
  23619. #endif
  23620. #endif /* !NO_DH */
  23621. return res;
  23622. }
  23623. /*
  23624. * Testing wc_Ed25519KeyToDer
  23625. */
  23626. static int test_wc_Ed25519KeyToDer(void)
  23627. {
  23628. int res = TEST_SKIPPED;
  23629. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23630. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23631. byte output[ONEK_BUF];
  23632. ed25519_key ed25519Key;
  23633. WC_RNG rng;
  23634. word32 inLen;
  23635. int ret;
  23636. ret = wc_InitRng(&rng);
  23637. if (ret == 0) {
  23638. ret = wc_ed25519_init(&ed25519Key);
  23639. if (ret == 0) {
  23640. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23641. }
  23642. inLen = (word32)sizeof(output);
  23643. /* Bad Cases */
  23644. if (ret == 0) {
  23645. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  23646. if (ret == BAD_FUNC_ARG) {
  23647. ret = 0;
  23648. }
  23649. }
  23650. if (ret == 0) {
  23651. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  23652. if (ret == BAD_FUNC_ARG) {
  23653. ret = 0;
  23654. }
  23655. }
  23656. if (ret == 0) {
  23657. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  23658. if (ret == BAD_FUNC_ARG) {
  23659. ret = 0;
  23660. }
  23661. }
  23662. /* Good Cases */
  23663. if (ret == 0) {
  23664. /* length only */
  23665. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  23666. if (ret > 0) {
  23667. ret = 0;
  23668. }
  23669. }
  23670. if (ret == 0) {
  23671. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  23672. if (ret > 0) {
  23673. ret = 0;
  23674. }
  23675. }
  23676. wc_ed25519_free(&ed25519Key);
  23677. }
  23678. wc_FreeRng(&rng);
  23679. res = TEST_RES_CHECK(ret == 0);
  23680. #endif
  23681. return res;
  23682. }/* End test_wc_Ed25519KeyToDer*/
  23683. /*
  23684. * Testing wc_Ed25519PrivateKeyToDer
  23685. */
  23686. static int test_wc_Ed25519PrivateKeyToDer(void)
  23687. {
  23688. int res = TEST_SKIPPED;
  23689. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23690. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23691. byte output[ONEK_BUF];
  23692. ed25519_key ed25519PrivKey;
  23693. WC_RNG rng;
  23694. word32 inLen;
  23695. int ret;
  23696. ret = wc_InitRng(&rng);
  23697. if (ret == 0) {
  23698. ret = wc_ed25519_init(&ed25519PrivKey);
  23699. if (ret == 0) {
  23700. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  23701. }
  23702. inLen = (word32)sizeof(output);
  23703. /* Bad Cases */
  23704. if (ret == 0) {
  23705. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  23706. if (ret == BAD_FUNC_ARG) {
  23707. ret = 0;
  23708. }
  23709. }
  23710. if (ret == 0) {
  23711. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  23712. if (ret == BAD_FUNC_ARG) {
  23713. ret = 0;
  23714. }
  23715. }
  23716. if (ret == 0) {
  23717. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  23718. if (ret == BAD_FUNC_ARG) {
  23719. ret = 0;
  23720. }
  23721. }
  23722. /* Good Cases */
  23723. if (ret == 0) {
  23724. /* length only */
  23725. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  23726. if (ret > 0) {
  23727. ret = 0;
  23728. }
  23729. }
  23730. if (ret == 0) {
  23731. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  23732. if (ret > 0) {
  23733. ret = 0;
  23734. }
  23735. }
  23736. wc_ed25519_free(&ed25519PrivKey);
  23737. }
  23738. wc_FreeRng(&rng);
  23739. res = TEST_RES_CHECK(ret == 0);
  23740. #endif
  23741. return res;
  23742. }/* End test_wc_Ed25519PrivateKeyToDer*/
  23743. /*
  23744. * Testing wc_Ed448KeyToDer
  23745. */
  23746. static int test_wc_Ed448KeyToDer(void)
  23747. {
  23748. int res = TEST_SKIPPED;
  23749. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23750. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23751. byte output[ONEK_BUF];
  23752. ed448_key ed448Key;
  23753. WC_RNG rng;
  23754. word32 inLen;
  23755. int ret;
  23756. ret = wc_InitRng(&rng);
  23757. if (ret == 0) {
  23758. ret = wc_ed448_init(&ed448Key);
  23759. if (ret == 0) {
  23760. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23761. }
  23762. inLen = sizeof(output);
  23763. /* Bad Cases */
  23764. if (ret == 0) {
  23765. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  23766. if (ret == BAD_FUNC_ARG) {
  23767. ret = 0;
  23768. }
  23769. }
  23770. if (ret == 0) {
  23771. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  23772. if (ret == BAD_FUNC_ARG) {
  23773. ret = 0;
  23774. }
  23775. }
  23776. if (ret == 0) {
  23777. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  23778. if (ret == BAD_FUNC_ARG) {
  23779. ret = 0;
  23780. }
  23781. }
  23782. /* Good Cases */
  23783. if (ret == 0) {
  23784. /* length only */
  23785. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  23786. if (ret > 0) {
  23787. ret = 0;
  23788. }
  23789. }
  23790. if (ret == 0) {
  23791. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  23792. if (ret > 0) {
  23793. ret = 0;
  23794. }
  23795. }
  23796. wc_ed448_free(&ed448Key);
  23797. }
  23798. wc_FreeRng(&rng);
  23799. res = TEST_RES_CHECK(ret == 0);
  23800. #endif
  23801. return res;
  23802. }/* End test_wc_Ed448KeyToDer*/
  23803. /*
  23804. * Testing wc_Ed448PrivateKeyToDer
  23805. */
  23806. static int test_wc_Ed448PrivateKeyToDer(void)
  23807. {
  23808. int res = TEST_SKIPPED;
  23809. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23810. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23811. byte output[ONEK_BUF];
  23812. ed448_key ed448PrivKey;
  23813. WC_RNG rng;
  23814. word32 inLen;
  23815. int ret;
  23816. ret = wc_InitRng(&rng);
  23817. if (ret == 0) {
  23818. ret = wc_ed448_init(&ed448PrivKey);
  23819. if (ret == 0) {
  23820. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  23821. }
  23822. inLen = sizeof(output);
  23823. /* Bad Cases */
  23824. if (ret == 0) {
  23825. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  23826. if (ret == BAD_FUNC_ARG) {
  23827. ret = 0;
  23828. }
  23829. }
  23830. if (ret == 0) {
  23831. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  23832. if (ret == BAD_FUNC_ARG) {
  23833. ret = 0;
  23834. }
  23835. }
  23836. if (ret == 0) {
  23837. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  23838. if (ret == BAD_FUNC_ARG) {
  23839. ret = 0;
  23840. }
  23841. }
  23842. /* Good cases */
  23843. if (ret == 0) {
  23844. /* length only */
  23845. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  23846. if (ret > 0) {
  23847. ret = 0;
  23848. }
  23849. }
  23850. if (ret == 0) {
  23851. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  23852. if (ret > 0) {
  23853. ret = 0;
  23854. }
  23855. }
  23856. wc_ed448_free(&ed448PrivKey);
  23857. }
  23858. wc_FreeRng(&rng);
  23859. res = TEST_RES_CHECK(ret == 0);
  23860. #endif
  23861. return res;
  23862. }/* End test_wc_Ed448PrivateKeyToDer*/
  23863. /*
  23864. * Testing wc_SetSubjectBuffer
  23865. */
  23866. static int test_wc_SetSubjectBuffer(void)
  23867. {
  23868. int res = TEST_SKIPPED;
  23869. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23870. Cert cert;
  23871. FILE* file;
  23872. byte* der;
  23873. word32 derSz;
  23874. int ret = 0;
  23875. derSz = FOURK_BUF;
  23876. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23877. if (der == NULL) {
  23878. ret = -1;
  23879. }
  23880. if (ret == 0) {
  23881. file = XFOPEN("./certs/ca-cert.der", "rb");
  23882. if (file != NULL) {
  23883. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  23884. XFCLOSE(file);
  23885. }
  23886. else {
  23887. ret = -1;
  23888. }
  23889. }
  23890. if (ret == 0) {
  23891. ret = wc_InitCert(&cert);
  23892. }
  23893. if (ret == 0) {
  23894. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  23895. }
  23896. if (ret == 0) {
  23897. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  23898. if (ret == BAD_FUNC_ARG) {
  23899. ret = 0;
  23900. }
  23901. }
  23902. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23903. res = TEST_RES_CHECK(ret == 0);
  23904. #endif
  23905. return res;
  23906. }/* End test_wc_SetSubjectBuffer*/
  23907. /*
  23908. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23909. */
  23910. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23911. {
  23912. int res = TEST_SKIPPED;
  23913. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23914. WC_RNG rng;
  23915. Cert cert;
  23916. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23917. ed25519_key ed25519Key;
  23918. #endif
  23919. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23920. RsaKey rsaKey;
  23921. int bits = 2048;
  23922. #endif
  23923. #if defined(HAVE_ECC)
  23924. ecc_key eccKey;
  23925. #endif
  23926. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23927. ed448_key ed448Key;
  23928. #endif
  23929. int ret = 0;
  23930. #ifndef HAVE_FIPS
  23931. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  23932. #else
  23933. ret = wc_InitRng(&rng);
  23934. #endif
  23935. wc_InitCert(&cert);
  23936. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23937. if (ret == 0) { /*ED25519*/
  23938. ret = wc_ed25519_init(&ed25519Key);
  23939. if (ret == 0) {
  23940. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23941. }
  23942. if (ret == 0) {
  23943. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23944. &ed25519Key);
  23945. }
  23946. wc_ed25519_free(&ed25519Key);
  23947. }
  23948. #endif
  23949. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23950. if (ret == 0) { /*RSA*/
  23951. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23952. if (ret == 0) {
  23953. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23954. }
  23955. if (ret == 0) {
  23956. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23957. }
  23958. wc_FreeRsaKey(&rsaKey);
  23959. }
  23960. #endif
  23961. #if defined(HAVE_ECC)
  23962. if (ret == 0) { /*ECC*/
  23963. ret = wc_ecc_init(&eccKey);
  23964. if (ret == 0) {
  23965. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23966. #if defined(WOLFSSL_ASYNC_CRYPT)
  23967. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23968. #endif
  23969. }
  23970. if (ret == 0) {
  23971. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23972. }
  23973. wc_ecc_free(&eccKey);
  23974. }
  23975. #endif
  23976. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23977. if (ret == 0) { /*ED448*/
  23978. ret = wc_ed448_init(&ed448Key);
  23979. if (ret == 0) {
  23980. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23981. }
  23982. if (ret == 0) {
  23983. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23984. &ed448Key);
  23985. }
  23986. wc_ed448_free(&ed448Key);
  23987. }
  23988. #endif
  23989. wc_FreeRng(&rng);
  23990. res = TEST_RES_CHECK(ret == 0);
  23991. #endif
  23992. return res;
  23993. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23994. /*
  23995. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23996. */
  23997. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23998. {
  23999. int res = TEST_SKIPPED;
  24000. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  24001. WC_RNG rng;
  24002. Cert cert;
  24003. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24004. ed25519_key ed25519Key;
  24005. #endif
  24006. #if !defined(NO_RSA) && defined(HAVE_RSA)
  24007. RsaKey rsaKey;
  24008. int bits = 2048;
  24009. #endif
  24010. #if defined(HAVE_ECC)
  24011. ecc_key eccKey;
  24012. #endif
  24013. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24014. ed448_key ed448Key;
  24015. #endif
  24016. int ret = 0;
  24017. #ifndef HAVE_FIPS
  24018. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  24019. #else
  24020. ret = wc_InitRng(&rng);
  24021. #endif
  24022. wc_InitCert(&cert);
  24023. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24024. if (ret == 0) { /*ED25519*/
  24025. ret = wc_ed25519_init(&ed25519Key);
  24026. if (ret == 0) {
  24027. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24028. }
  24029. if (ret == 0) {
  24030. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  24031. &ed25519Key);
  24032. }
  24033. wc_ed25519_free(&ed25519Key);
  24034. }
  24035. #endif
  24036. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  24037. if (ret == 0) { /*RSA*/
  24038. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  24039. if (ret == 0) {
  24040. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  24041. }
  24042. if (ret == 0) {
  24043. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  24044. }
  24045. wc_FreeRsaKey(&rsaKey);
  24046. }
  24047. #endif
  24048. #if defined(HAVE_ECC)
  24049. if (ret == 0) { /*ECC*/
  24050. ret = wc_ecc_init(&eccKey);
  24051. if (ret == 0) {
  24052. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24053. #if defined(WOLFSSL_ASYNC_CRYPT)
  24054. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24055. #endif
  24056. }
  24057. if (ret == 0) {
  24058. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  24059. }
  24060. wc_ecc_free(&eccKey);
  24061. }
  24062. #endif
  24063. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24064. if (ret == 0) { /*ED448*/
  24065. ret = wc_ed448_init(&ed448Key);
  24066. if (ret == 0) {
  24067. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24068. }
  24069. if (ret == 0) {
  24070. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  24071. &ed448Key);
  24072. }
  24073. wc_ed448_free(&ed448Key);
  24074. }
  24075. #endif
  24076. wc_FreeRng(&rng);
  24077. res = TEST_RES_CHECK(ret == 0);
  24078. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  24079. return res;
  24080. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  24081. /*
  24082. * Testing wc_PKCS7_New()
  24083. */
  24084. static int test_wc_PKCS7_New(void)
  24085. {
  24086. int res = TEST_SKIPPED;
  24087. #if defined(HAVE_PKCS7)
  24088. PKCS7* pkcs7;
  24089. pkcs7 = wc_PKCS7_New(NULL, testDevId);
  24090. wc_PKCS7_Free(pkcs7);
  24091. res = TEST_RES_CHECK(pkcs7 != NULL);
  24092. #endif
  24093. return res;
  24094. } /* END test-wc_PKCS7_New */
  24095. /*
  24096. * Testing wc_PKCS7_Init()
  24097. */
  24098. static int test_wc_PKCS7_Init(void)
  24099. {
  24100. int res = TEST_SKIPPED;
  24101. #if defined(HAVE_PKCS7)
  24102. PKCS7* pkcs7;
  24103. void* heap = NULL;
  24104. pkcs7 = wc_PKCS7_New(heap, testDevId);
  24105. AssertNotNull(pkcs7);
  24106. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  24107. /* Pass in bad args. */
  24108. AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  24109. wc_PKCS7_Free(pkcs7);
  24110. res = TEST_RES_CHECK(1);
  24111. #endif
  24112. return res;
  24113. } /* END test-wc_PKCS7_Init */
  24114. /*
  24115. * Testing wc_PKCS7_InitWithCert()
  24116. */
  24117. static int test_wc_PKCS7_InitWithCert(void)
  24118. {
  24119. int res = TEST_SKIPPED;
  24120. #if defined(HAVE_PKCS7)
  24121. PKCS7* pkcs7;
  24122. #ifndef NO_RSA
  24123. #if defined(USE_CERT_BUFFERS_2048)
  24124. unsigned char cert[sizeof(client_cert_der_2048)];
  24125. int certSz = (int)sizeof(cert);
  24126. XMEMSET(cert, 0, certSz);
  24127. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  24128. #elif defined(USE_CERT_BUFFERS_1024)
  24129. unsigned char cert[sizeof(client_cert_der_1024)];
  24130. int certSz = (int)sizeof(cert);
  24131. XMEMSET(cert, 0, certSz);
  24132. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  24133. #else
  24134. unsigned char cert[ONEK_BUF];
  24135. XFILE fp;
  24136. int certSz;
  24137. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24138. AssertTrue(fp != XBADFILE);
  24139. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24140. XFCLOSE(fp);
  24141. #endif
  24142. #elif defined(HAVE_ECC)
  24143. #if defined(USE_CERT_BUFFERS_256)
  24144. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24145. int certSz = (int)sizeof(cert);
  24146. XMEMSET(cert, 0, certSz);
  24147. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  24148. #else
  24149. unsigned char cert[ONEK_BUF];
  24150. XFILE fp;
  24151. int certSz;
  24152. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24153. AssertTrue(fp != XBADFILE);
  24154. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  24155. XFCLOSE(fp);
  24156. #endif
  24157. #else
  24158. #error PKCS7 requires ECC or RSA
  24159. #endif
  24160. #ifdef HAVE_ECC
  24161. {
  24162. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  24163. static unsigned char certWithInvalidEccKey[] = {
  24164. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  24165. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  24166. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  24167. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  24168. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24169. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  24170. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24171. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24172. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24173. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24174. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24175. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24176. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24177. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24178. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24179. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  24180. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  24181. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  24182. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  24183. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  24184. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  24185. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  24186. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  24187. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  24188. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  24189. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  24190. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24191. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  24192. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24193. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  24194. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  24195. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  24196. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  24197. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  24198. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  24199. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  24200. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  24201. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  24202. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  24203. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  24204. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24205. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  24206. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  24207. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24208. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  24209. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24210. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  24211. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24212. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24213. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24214. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24215. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24216. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24217. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24218. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24219. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24220. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  24221. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  24222. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  24223. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  24224. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  24225. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  24226. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  24227. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  24228. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  24229. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  24230. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  24231. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  24232. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  24233. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  24234. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  24235. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  24236. 0x08, 0xA8, 0xB4};
  24237. #endif
  24238. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24239. /* If initialization is not successful, it's free'd in init func. */
  24240. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  24241. wc_PKCS7_Free(pkcs7);
  24242. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24243. /* Valid initialization usage. */
  24244. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24245. /* Pass in bad args. No need free for null checks, free at end.*/
  24246. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  24247. BAD_FUNC_ARG);
  24248. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  24249. BAD_FUNC_ARG);
  24250. #ifdef HAVE_ECC
  24251. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  24252. sizeof(certWithInvalidEccKey)), 0);
  24253. }
  24254. #endif
  24255. wc_PKCS7_Free(pkcs7);
  24256. res = TEST_RES_CHECK(1);
  24257. #endif
  24258. return res;
  24259. } /* END test_wc_PKCS7_InitWithCert */
  24260. /*
  24261. * Testing wc_PKCS7_EncodeData()
  24262. */
  24263. static int test_wc_PKCS7_EncodeData(void)
  24264. {
  24265. int res = TEST_SKIPPED;
  24266. #if defined(HAVE_PKCS7)
  24267. PKCS7* pkcs7;
  24268. byte output[FOURK_BUF];
  24269. byte data[] = "My encoded DER cert.";
  24270. #ifndef NO_RSA
  24271. #if defined(USE_CERT_BUFFERS_2048)
  24272. unsigned char cert[sizeof(client_cert_der_2048)];
  24273. unsigned char key[sizeof(client_key_der_2048)];
  24274. int certSz = (int)sizeof(cert);
  24275. int keySz = (int)sizeof(key);
  24276. XMEMSET(cert, 0, certSz);
  24277. XMEMSET(key, 0, keySz);
  24278. XMEMCPY(cert, client_cert_der_2048, certSz);
  24279. XMEMCPY(key, client_key_der_2048, keySz);
  24280. #elif defined(USE_CERT_BUFFERS_1024)
  24281. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  24282. unsigned char key[sizeof_client_key_der_1024];
  24283. int certSz = (int)sizeof(cert);
  24284. int keySz = (int)sizeof(key);
  24285. XMEMSET(cert, 0, certSz);
  24286. XMEMSET(key, 0, keySz);
  24287. XMEMCPY(cert, client_cert_der_1024, certSz);
  24288. XMEMCPY(key, client_key_der_1024, keySz);
  24289. #else
  24290. unsigned char cert[ONEK_BUF];
  24291. unsigned char key[ONEK_BUF];
  24292. XFILE fp;
  24293. int certSz;
  24294. int keySz;
  24295. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24296. AssertTrue(fp != XBADFILE);
  24297. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24298. XFCLOSE(fp);
  24299. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24300. AssertTrue(fp != XBADFILE);
  24301. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24302. XFCLOSE(fp);
  24303. #endif
  24304. #elif defined(HAVE_ECC)
  24305. #if defined(USE_CERT_BUFFERS_256)
  24306. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24307. unsigned char key[sizeof(ecc_clikey_der_256)];
  24308. int certSz = (int)sizeof(cert);
  24309. int keySz = (int)sizeof(key);
  24310. XMEMSET(cert, 0, certSz);
  24311. XMEMSET(key, 0, keySz);
  24312. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24313. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24314. #else
  24315. unsigned char cert[ONEK_BUF];
  24316. unsigned char key[ONEK_BUF];
  24317. XFILE fp;
  24318. int certSz, keySz;
  24319. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24320. AssertTrue(fp != XBADFILE);
  24321. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24322. XFCLOSE(fp);
  24323. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24324. AssertTrue(fp != XBADFILE);
  24325. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24326. XFCLOSE(fp);
  24327. #endif
  24328. #endif
  24329. XMEMSET(output, 0, sizeof(output));
  24330. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24331. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24332. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  24333. pkcs7->content = data;
  24334. pkcs7->contentSz = sizeof(data);
  24335. pkcs7->privateKey = key;
  24336. pkcs7->privateKeySz = keySz;
  24337. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  24338. /* Test bad args. */
  24339. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  24340. BAD_FUNC_ARG);
  24341. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  24342. BAD_FUNC_ARG);
  24343. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  24344. wc_PKCS7_Free(pkcs7);
  24345. res = TEST_RES_CHECK(1);
  24346. #endif
  24347. return res;
  24348. } /* END test_wc_PKCS7_EncodeData */
  24349. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24350. !defined(NO_RSA) && !defined(NO_SHA256)
  24351. /* RSA sign raw digest callback */
  24352. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  24353. byte* out, word32 outSz, byte* privateKey,
  24354. word32 privateKeySz, int devid, int hashOID)
  24355. {
  24356. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  24357. byte digInfoEncoding[] = {
  24358. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  24359. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  24360. 0x00, 0x04, 0x20
  24361. };
  24362. int ret;
  24363. byte digestInfo[ONEK_BUF];
  24364. byte sig[FOURK_BUF];
  24365. word32 digestInfoSz = 0;
  24366. word32 idx = 0;
  24367. RsaKey rsa;
  24368. /* SHA-256 required only for this example callback due to above
  24369. * digInfoEncoding[] */
  24370. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24371. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24372. (hashOID != SHA256h)) {
  24373. return -1;
  24374. }
  24375. /* build DigestInfo */
  24376. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24377. digestInfoSz += sizeof(digInfoEncoding);
  24378. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24379. digestInfoSz += digestSz;
  24380. /* set up RSA key */
  24381. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24382. if (ret != 0) {
  24383. return ret;
  24384. }
  24385. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24386. /* sign DigestInfo */
  24387. if (ret == 0) {
  24388. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24389. &rsa, pkcs7->rng);
  24390. if (ret > 0) {
  24391. if (ret > (int)outSz) {
  24392. /* output buffer too small */
  24393. ret = -1;
  24394. }
  24395. else {
  24396. /* success, ret holds sig size */
  24397. XMEMCPY(out, sig, ret);
  24398. }
  24399. }
  24400. }
  24401. wc_FreeRsaKey(&rsa);
  24402. return ret;
  24403. }
  24404. #endif
  24405. /*
  24406. * Testing wc_PKCS7_EncodeSignedData()
  24407. */
  24408. static int test_wc_PKCS7_EncodeSignedData(void)
  24409. {
  24410. int res = TEST_SKIPPED;
  24411. #if defined(HAVE_PKCS7)
  24412. PKCS7* pkcs7;
  24413. WC_RNG rng;
  24414. byte output[FOURK_BUF];
  24415. byte badOut[1];
  24416. word32 outputSz = (word32)sizeof(output);
  24417. word32 badOutSz = 0;
  24418. byte data[] = "Test data to encode.";
  24419. #ifndef NO_RSA
  24420. #if defined(USE_CERT_BUFFERS_2048)
  24421. byte key[sizeof(client_key_der_2048)];
  24422. byte cert[sizeof(client_cert_der_2048)];
  24423. word32 keySz = (word32)sizeof(key);
  24424. word32 certSz = (word32)sizeof(cert);
  24425. XMEMSET(key, 0, keySz);
  24426. XMEMSET(cert, 0, certSz);
  24427. XMEMCPY(key, client_key_der_2048, keySz);
  24428. XMEMCPY(cert, client_cert_der_2048, certSz);
  24429. #elif defined(USE_CERT_BUFFERS_1024)
  24430. byte key[sizeof_client_key_der_1024];
  24431. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24432. word32 keySz = (word32)sizeof(key);
  24433. word32 certSz = (word32)sizeof(cert);
  24434. XMEMSET(key, 0, keySz);
  24435. XMEMSET(cert, 0, certSz);
  24436. XMEMCPY(key, client_key_der_1024, keySz);
  24437. XMEMCPY(cert, client_cert_der_1024, certSz);
  24438. #else
  24439. unsigned char cert[ONEK_BUF];
  24440. unsigned char key[ONEK_BUF];
  24441. XFILE fp;
  24442. int certSz;
  24443. int keySz;
  24444. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24445. AssertTrue(fp != XBADFILE);
  24446. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24447. XFCLOSE(fp);
  24448. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24449. AssertTrue(fp != XBADFILE);
  24450. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24451. XFCLOSE(fp);
  24452. #endif
  24453. #elif defined(HAVE_ECC)
  24454. #if defined(USE_CERT_BUFFERS_256)
  24455. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24456. unsigned char key[sizeof(ecc_clikey_der_256)];
  24457. int certSz = (int)sizeof(cert);
  24458. int keySz = (int)sizeof(key);
  24459. XMEMSET(cert, 0, certSz);
  24460. XMEMSET(key, 0, keySz);
  24461. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24462. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24463. #else
  24464. unsigned char cert[ONEK_BUF];
  24465. unsigned char key[ONEK_BUF];
  24466. XFILE fp;
  24467. int certSz, keySz;
  24468. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  24469. AssertTrue(fp != XBADFILE);
  24470. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  24471. XFCLOSE(fp);
  24472. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24473. AssertTrue(fp != XBADFILE);
  24474. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  24475. XFCLOSE(fp);
  24476. #endif
  24477. #endif
  24478. XMEMSET(output, 0, outputSz);
  24479. AssertIntEQ(wc_InitRng(&rng), 0);
  24480. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24481. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24482. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24483. pkcs7->content = data;
  24484. pkcs7->contentSz = (word32)sizeof(data);
  24485. pkcs7->privateKey = key;
  24486. pkcs7->privateKeySz = (word32)sizeof(key);
  24487. pkcs7->encryptOID = RSAk;
  24488. #ifdef NO_SHA
  24489. pkcs7->hashOID = SHA256h;
  24490. #else
  24491. pkcs7->hashOID = SHAh;
  24492. #endif
  24493. pkcs7->rng = &rng;
  24494. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24495. wc_PKCS7_Free(pkcs7);
  24496. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24497. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24498. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24499. /* Pass in bad args. */
  24500. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  24501. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24502. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24503. badOutSz), BAD_FUNC_ARG);
  24504. pkcs7->hashOID = 0; /* bad hashOID */
  24505. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  24506. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24507. !defined(NO_RSA) && !defined(NO_SHA256)
  24508. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24509. * Example callback assumes SHA-256, so only run test if compiled in. */
  24510. wc_PKCS7_Free(pkcs7);
  24511. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24512. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24513. pkcs7->content = data;
  24514. pkcs7->contentSz = (word32)sizeof(data);
  24515. pkcs7->privateKey = key;
  24516. pkcs7->privateKeySz = (word32)sizeof(key);
  24517. pkcs7->encryptOID = RSAk;
  24518. pkcs7->hashOID = SHA256h;
  24519. pkcs7->rng = &rng;
  24520. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24521. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24522. #endif
  24523. wc_PKCS7_Free(pkcs7);
  24524. wc_FreeRng(&rng);
  24525. res = TEST_RES_CHECK(1);
  24526. #endif
  24527. return res;
  24528. } /* END test_wc_PKCS7_EncodeSignedData */
  24529. /*
  24530. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24531. */
  24532. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24533. {
  24534. int res = TEST_SKIPPED;
  24535. #if defined(HAVE_PKCS7)
  24536. int ret, i;
  24537. PKCS7* pkcs7;
  24538. WC_RNG rng;
  24539. byte outputHead[FOURK_BUF/2];
  24540. byte outputFoot[FOURK_BUF/2];
  24541. word32 outputHeadSz = (word32)sizeof(outputHead);
  24542. word32 outputFootSz = (word32)sizeof(outputFoot);
  24543. byte data[FOURK_BUF];
  24544. wc_HashAlg hash;
  24545. #ifdef NO_SHA
  24546. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24547. #else
  24548. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24549. #endif
  24550. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24551. word32 hashSz = wc_HashGetDigestSize(hashType);
  24552. #ifndef NO_RSA
  24553. #if defined(USE_CERT_BUFFERS_2048)
  24554. byte key[sizeof(client_key_der_2048)];
  24555. byte cert[sizeof(client_cert_der_2048)];
  24556. word32 keySz = (word32)sizeof(key);
  24557. word32 certSz = (word32)sizeof(cert);
  24558. XMEMSET(key, 0, keySz);
  24559. XMEMSET(cert, 0, certSz);
  24560. XMEMCPY(key, client_key_der_2048, keySz);
  24561. XMEMCPY(cert, client_cert_der_2048, certSz);
  24562. #elif defined(USE_CERT_BUFFERS_1024)
  24563. byte key[sizeof_client_key_der_1024];
  24564. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24565. word32 keySz = (word32)sizeof(key);
  24566. word32 certSz = (word32)sizeof(cert);
  24567. XMEMSET(key, 0, keySz);
  24568. XMEMSET(cert, 0, certSz);
  24569. XMEMCPY(key, client_key_der_1024, keySz);
  24570. XMEMCPY(cert, client_cert_der_1024, certSz);
  24571. #else
  24572. unsigned char cert[ONEK_BUF];
  24573. unsigned char key[ONEK_BUF];
  24574. XFILE fp;
  24575. int certSz;
  24576. int keySz;
  24577. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24578. AssertTrue((fp != XBADFILE));
  24579. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24580. XFCLOSE(fp);
  24581. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24582. AssertTrue(fp != XBADFILE);
  24583. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24584. XFCLOSE(fp);
  24585. #endif
  24586. #elif defined(HAVE_ECC)
  24587. #if defined(USE_CERT_BUFFERS_256)
  24588. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24589. unsigned char key[sizeof(ecc_clikey_der_256)];
  24590. int certSz = (int)sizeof(cert);
  24591. int keySz = (int)sizeof(key);
  24592. XMEMSET(cert, 0, certSz);
  24593. XMEMSET(key, 0, keySz);
  24594. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24595. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24596. #else
  24597. unsigned char cert[ONEK_BUF];
  24598. unsigned char key[ONEK_BUF];
  24599. XFILE fp;
  24600. int certSz, keySz;
  24601. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24602. AssertTrue(fp != XBADFILE);
  24603. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24604. XFCLOSE(fp);
  24605. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24606. AssertTrue(fp != XBADFILE);
  24607. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24608. XFCLOSE(fp);
  24609. #endif
  24610. #endif
  24611. /* initialize large data with sequence */
  24612. for (i=0; i<(int)sizeof(data); i++)
  24613. data[i] = i & 0xff;
  24614. XMEMSET(outputHead, 0, outputHeadSz);
  24615. XMEMSET(outputFoot, 0, outputFootSz);
  24616. AssertIntEQ(wc_InitRng(&rng), 0);
  24617. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24618. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24619. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24620. pkcs7->content = NULL; /* not used for ex */
  24621. pkcs7->contentSz = (word32)sizeof(data);
  24622. pkcs7->privateKey = key;
  24623. pkcs7->privateKeySz = (word32)sizeof(key);
  24624. pkcs7->encryptOID = RSAk;
  24625. #ifdef NO_SHA
  24626. pkcs7->hashOID = SHA256h;
  24627. #else
  24628. pkcs7->hashOID = SHAh;
  24629. #endif
  24630. pkcs7->rng = &rng;
  24631. /* calculate hash for content */
  24632. ret = wc_HashInit(&hash, hashType);
  24633. if (ret == 0) {
  24634. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24635. if (ret == 0) {
  24636. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24637. }
  24638. wc_HashFree(&hash, hashType);
  24639. }
  24640. AssertIntEQ(ret, 0);
  24641. /* Perform PKCS7 sign using hash directly */
  24642. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24643. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24644. AssertIntGT(outputHeadSz, 0);
  24645. AssertIntGT(outputFootSz, 0);
  24646. wc_PKCS7_Free(pkcs7);
  24647. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24648. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24649. /* required parameter even on verify when using _ex, if using outputHead
  24650. * and outputFoot */
  24651. pkcs7->contentSz = (word32)sizeof(data);
  24652. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24653. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24654. wc_PKCS7_Free(pkcs7);
  24655. /* assembly complete PKCS7 sign and use normal verify */
  24656. {
  24657. byte* output = (byte*)XMALLOC(
  24658. outputHeadSz + sizeof(data) + outputFootSz,
  24659. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24660. word32 outputSz = 0;
  24661. AssertNotNull(output);
  24662. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24663. outputSz += outputHeadSz;
  24664. XMEMCPY(&output[outputSz], data, sizeof(data));
  24665. outputSz += sizeof(data);
  24666. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24667. outputSz += outputFootSz;
  24668. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24669. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24670. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24671. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24672. }
  24673. /* Pass in bad args. */
  24674. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24675. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24676. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24677. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24678. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24679. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24680. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24681. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24682. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24683. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24684. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24685. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24686. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24687. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24688. pkcs7->hashOID = 0; /* bad hashOID */
  24689. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24690. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24691. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24692. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24693. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24694. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24695. #ifndef NO_PKCS7_STREAM
  24696. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24697. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24698. #else
  24699. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24700. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24701. #endif
  24702. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24703. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24704. #ifndef NO_PKCS7_STREAM
  24705. /* can pass in 0 buffer length with streaming API */
  24706. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24707. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24708. #else
  24709. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24710. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24711. #endif
  24712. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24713. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24714. #ifndef NO_PKCS7_STREAM
  24715. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24716. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24717. #else
  24718. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24719. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  24720. #endif
  24721. wc_PKCS7_Free(pkcs7);
  24722. wc_FreeRng(&rng);
  24723. res = TEST_RES_CHECK(1);
  24724. #endif
  24725. return res;
  24726. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24727. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24728. /**
  24729. * Loads certs/keys from files or buffers into the argument buffers,
  24730. * helper function called by CreatePKCS7SignedData().
  24731. *
  24732. * Returns 0 on success, negative on error.
  24733. */
  24734. static int LoadPKCS7SignedDataCerts(
  24735. int useIntermediateCertChain, int pkAlgoType,
  24736. byte* intCARoot, word32* intCARootSz,
  24737. byte* intCA1, word32* intCA1Sz,
  24738. byte* intCA2, word32* intCA2Sz,
  24739. byte* cert, word32* certSz,
  24740. byte* key, word32* keySz)
  24741. {
  24742. int ret = 0;
  24743. FILE* fp = NULL;
  24744. #ifndef NO_RSA
  24745. const char* intCARootRSA = "./certs/ca-cert.der";
  24746. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24747. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24748. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24749. const char* intServKeyRSA = "./certs/server-key.der";
  24750. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24751. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24752. const char* cli1024Key = "./certs/1024/client-key.der";
  24753. #endif
  24754. #endif
  24755. #ifdef HAVE_ECC
  24756. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24757. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24758. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24759. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24760. const char* intServKeyECC = "./certs/ecc-key.der";
  24761. #ifndef USE_CERT_BUFFERS_256
  24762. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24763. const char* cliEccKey = "./certs/client-ecc-key.der";
  24764. #endif
  24765. #endif
  24766. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24767. ((useIntermediateCertChain == 1) &&
  24768. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24769. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24770. return BAD_FUNC_ARG;
  24771. }
  24772. /* Read/load certs and keys to use for signing based on PK type and chain */
  24773. switch (pkAlgoType) {
  24774. #ifndef NO_RSA
  24775. case RSA_TYPE:
  24776. if (useIntermediateCertChain == 1) {
  24777. fp = XFOPEN(intCARootRSA, "rb");
  24778. AssertNotNull(fp);
  24779. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24780. XFCLOSE(fp);
  24781. AssertIntGT(*intCARootSz, 0);
  24782. fp = XFOPEN(intCA1RSA, "rb");
  24783. AssertNotNull(fp);
  24784. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24785. XFCLOSE(fp);
  24786. AssertIntGT(*intCA1Sz, 0);
  24787. fp = XFOPEN(intCA2RSA, "rb");
  24788. AssertNotNull(fp);
  24789. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24790. XFCLOSE(fp);
  24791. AssertIntGT(*intCA2Sz, 0);
  24792. fp = XFOPEN(intServCertRSA, "rb");
  24793. AssertNotNull(fp);
  24794. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24795. XFCLOSE(fp);
  24796. AssertIntGT(*certSz, 0);
  24797. fp = XFOPEN(intServKeyRSA, "rb");
  24798. AssertNotNull(fp);
  24799. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24800. XFCLOSE(fp);
  24801. AssertIntGT(*keySz, 0);
  24802. }
  24803. else {
  24804. #if defined(USE_CERT_BUFFERS_2048)
  24805. *keySz = sizeof_client_key_der_2048;
  24806. *certSz = sizeof_client_cert_der_2048;
  24807. XMEMCPY(key, client_key_der_2048, *keySz);
  24808. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24809. #elif defined(USE_CERT_BUFFERS_1024)
  24810. *keySz = sizeof_client_key_der_1024;
  24811. *certSz = sizeof_client_cert_der_1024;
  24812. XMEMCPY(key, client_key_der_1024, *keySz);
  24813. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24814. #else
  24815. fp = XFOPEN(cli1024Key, "rb");
  24816. AssertNotNull(fp);
  24817. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24818. XFCLOSE(fp);
  24819. AssertIntGT(*keySz, 0);
  24820. fp = XFOPEN(cli1024Cert, "rb");
  24821. AssertNotNull(fp);
  24822. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24823. XFCLOSE(fp);
  24824. AssertIntGT(*certSz, 0);
  24825. #endif /* USE_CERT_BUFFERS_2048 */
  24826. }
  24827. break;
  24828. #endif /* !NO_RSA */
  24829. #ifdef HAVE_ECC
  24830. case ECC_TYPE:
  24831. if (useIntermediateCertChain == 1) {
  24832. fp = XFOPEN(intCARootECC, "rb");
  24833. AssertNotNull(fp);
  24834. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24835. XFCLOSE(fp);
  24836. AssertIntGT(*intCARootSz, 0);
  24837. fp = XFOPEN(intCA1ECC, "rb");
  24838. AssertNotNull(fp);
  24839. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24840. XFCLOSE(fp);
  24841. AssertIntGT(*intCA1Sz, 0);
  24842. fp = XFOPEN(intCA2ECC, "rb");
  24843. AssertNotNull(fp);
  24844. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24845. XFCLOSE(fp);
  24846. AssertIntGT(*intCA2Sz, 0);
  24847. fp = XFOPEN(intServCertECC, "rb");
  24848. AssertNotNull(fp);
  24849. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24850. XFCLOSE(fp);
  24851. AssertIntGT(*certSz, 0);
  24852. fp = XFOPEN(intServKeyECC, "rb");
  24853. AssertNotNull(fp);
  24854. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24855. XFCLOSE(fp);
  24856. AssertIntGT(*keySz, 0);
  24857. }
  24858. else {
  24859. #if defined(USE_CERT_BUFFERS_256)
  24860. *keySz = sizeof_ecc_clikey_der_256;
  24861. *certSz = sizeof_cliecc_cert_der_256;
  24862. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24863. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24864. #else
  24865. fp = XFOPEN(cliEccKey, "rb");
  24866. AssertNotNull(fp);
  24867. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24868. XFCLOSE(fp);
  24869. AssertIntGT(*keySz, 0);
  24870. fp = XFOPEN(cliEccCert, "rb");
  24871. AssertNotNull(fp);
  24872. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24873. XFCLOSE(fp);
  24874. AssertIntGT(*certSz, 0);
  24875. #endif /* USE_CERT_BUFFERS_256 */
  24876. }
  24877. break;
  24878. #endif /* HAVE_ECC */
  24879. default:
  24880. WOLFSSL_MSG("Unsupported SignedData PK type");
  24881. ret = BAD_FUNC_ARG;
  24882. break;
  24883. }
  24884. return ret;
  24885. }
  24886. /**
  24887. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24888. *
  24889. * output output buffer to place SignedData
  24890. * outputSz size of output buffer
  24891. * data data buffer to be signed
  24892. * dataSz size of data buffer
  24893. * withAttribs [1/0] include attributes in SignedData message
  24894. * detachedSig [1/0] create detached signature, no content
  24895. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24896. * root CAs in bundle
  24897. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24898. *
  24899. * Return size of bundle created on success, negative on error */
  24900. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24901. byte* data, word32 dataSz,
  24902. int withAttribs, int detachedSig,
  24903. int useIntermediateCertChain,
  24904. int pkAlgoType)
  24905. {
  24906. int ret = 0;
  24907. WC_RNG rng;
  24908. PKCS7* pkcs7 = NULL;
  24909. static byte messageTypeOid[] =
  24910. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24911. 0x09, 0x02 };
  24912. static byte messageType[] = { 0x13, 2, '1', '9' };
  24913. PKCS7Attrib attribs[] =
  24914. {
  24915. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24916. sizeof(messageType) }
  24917. };
  24918. byte intCARoot[TWOK_BUF];
  24919. byte intCA1[TWOK_BUF];
  24920. byte intCA2[TWOK_BUF];
  24921. byte cert[TWOK_BUF];
  24922. byte key[TWOK_BUF];
  24923. word32 intCARootSz = sizeof(intCARoot);
  24924. word32 intCA1Sz = sizeof(intCA1);
  24925. word32 intCA2Sz = sizeof(intCA2);
  24926. word32 certSz = sizeof(cert);
  24927. word32 keySz = sizeof(key);
  24928. XMEMSET(intCARoot, 0, intCARootSz);
  24929. XMEMSET(intCA1, 0, intCA1Sz);
  24930. XMEMSET(intCA2, 0, intCA2Sz);
  24931. XMEMSET(cert, 0, certSz);
  24932. XMEMSET(key, 0, keySz);
  24933. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24934. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24935. cert, &certSz, key, &keySz);
  24936. AssertIntEQ(ret, 0);
  24937. XMEMSET(output, 0, outputSz);
  24938. AssertIntEQ(wc_InitRng(&rng), 0);
  24939. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24940. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24941. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24942. if (useIntermediateCertChain == 1) {
  24943. /* Add intermediate and root CA certs into SignedData Certs SET */
  24944. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24945. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24946. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24947. }
  24948. pkcs7->content = data;
  24949. pkcs7->contentSz = dataSz;
  24950. pkcs7->privateKey = key;
  24951. pkcs7->privateKeySz = (word32)sizeof(key);
  24952. if (pkAlgoType == RSA_TYPE) {
  24953. pkcs7->encryptOID = RSAk;
  24954. }
  24955. else {
  24956. pkcs7->encryptOID = ECDSAk;
  24957. }
  24958. #ifdef NO_SHA
  24959. pkcs7->hashOID = SHA256h;
  24960. #else
  24961. pkcs7->hashOID = SHAh;
  24962. #endif
  24963. pkcs7->rng = &rng;
  24964. if (withAttribs) {
  24965. /* include a signed attribute */
  24966. pkcs7->signedAttribs = attribs;
  24967. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24968. }
  24969. if (detachedSig) {
  24970. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24971. }
  24972. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24973. AssertIntGT(outputSz, 0);
  24974. wc_PKCS7_Free(pkcs7);
  24975. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24976. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24977. if (detachedSig) {
  24978. pkcs7->content = data;
  24979. pkcs7->contentSz = dataSz;
  24980. }
  24981. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24982. wc_PKCS7_Free(pkcs7);
  24983. wc_FreeRng(&rng);
  24984. return outputSz;
  24985. }
  24986. #endif
  24987. /*
  24988. * Testing wc_PKCS_VerifySignedData()
  24989. */
  24990. static int test_wc_PKCS7_VerifySignedData(void)
  24991. {
  24992. int res = TEST_SKIPPED;
  24993. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24994. PKCS7* pkcs7;
  24995. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24996. word32 outputSz = sizeof(output);
  24997. byte data[] = "Test data to encode.";
  24998. byte badOut[1];
  24999. word32 badOutSz = 0;
  25000. byte badContent[] = "This is different content than was signed";
  25001. int ret;
  25002. wc_HashAlg hash;
  25003. #ifdef NO_SHA
  25004. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25005. #else
  25006. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25007. #endif
  25008. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25009. word32 hashSz = wc_HashGetDigestSize(hashType);
  25010. #ifndef NO_RSA
  25011. /* Success test with RSA certs/key */
  25012. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25013. (word32)sizeof(data),
  25014. 0, 0, 0, RSA_TYPE)), 0);
  25015. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25016. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25017. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25018. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25019. #endif
  25020. #ifdef HAVE_ECC
  25021. #ifndef NO_RSA
  25022. wc_PKCS7_Free(pkcs7);
  25023. #endif
  25024. /* Success test with ECC certs/key */
  25025. outputSz = sizeof(output);
  25026. XMEMSET(output, 0, outputSz);
  25027. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25028. (word32)sizeof(data),
  25029. 0, 0, 0, ECC_TYPE)), 0);
  25030. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25031. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25032. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25033. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25034. #endif
  25035. /* Test bad args. */
  25036. #if !defined(NO_RSA) || defined(HAVE_ECC)
  25037. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  25038. BAD_FUNC_ARG);
  25039. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  25040. BAD_FUNC_ARG);
  25041. #ifndef NO_PKCS7_STREAM
  25042. /* can pass in 0 buffer length with streaming API */
  25043. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25044. badOutSz), WC_PKCS7_WANT_READ_E);
  25045. #else
  25046. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25047. badOutSz), BAD_FUNC_ARG);
  25048. #endif
  25049. wc_PKCS7_Free(pkcs7);
  25050. #endif /* !NO_RSA || HAVE_ECC */
  25051. /* Invalid content should error, use detached signature so we can
  25052. * easily change content */
  25053. #ifndef NO_RSA
  25054. /* Try RSA certs/key/sig first */
  25055. outputSz = sizeof(output);
  25056. XMEMSET(output, 0, outputSz);
  25057. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25058. (word32)sizeof(data),
  25059. 1, 1, 0, RSA_TYPE)), 0);
  25060. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25061. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25062. pkcs7->content = badContent;
  25063. pkcs7->contentSz = sizeof(badContent);
  25064. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25065. SIG_VERIFY_E);
  25066. wc_PKCS7_Free(pkcs7);
  25067. /* Test success case with detached signature and valid content */
  25068. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25069. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25070. pkcs7->content = data;
  25071. pkcs7->contentSz = sizeof(data);
  25072. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25073. wc_PKCS7_Free(pkcs7);
  25074. /* verify using pre-computed content digest only (no content) */
  25075. {
  25076. /* calculate hash for content */
  25077. ret = wc_HashInit(&hash, hashType);
  25078. if (ret == 0) {
  25079. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25080. if (ret == 0) {
  25081. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25082. }
  25083. wc_HashFree(&hash, hashType);
  25084. }
  25085. AssertIntEQ(ret, 0);
  25086. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25087. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25088. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25089. output, outputSz,
  25090. NULL, 0), 0);
  25091. wc_PKCS7_Free(pkcs7);
  25092. }
  25093. #endif /* !NO_RSA */
  25094. #ifdef HAVE_ECC
  25095. /* Try ECC certs/key/sig next */
  25096. outputSz = sizeof(output);
  25097. XMEMSET(output, 0, outputSz);
  25098. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25099. (word32)sizeof(data),
  25100. 1, 1, 0, ECC_TYPE)), 0);
  25101. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25102. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25103. pkcs7->content = badContent;
  25104. pkcs7->contentSz = sizeof(badContent);
  25105. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25106. SIG_VERIFY_E);
  25107. wc_PKCS7_Free(pkcs7);
  25108. /* Test success case with detached signature and valid content */
  25109. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25110. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25111. pkcs7->content = data;
  25112. pkcs7->contentSz = sizeof(data);
  25113. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25114. wc_PKCS7_Free(pkcs7);
  25115. /* verify using pre-computed content digest only (no content) */
  25116. {
  25117. /* calculate hash for content */
  25118. ret = wc_HashInit(&hash, hashType);
  25119. if (ret == 0) {
  25120. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25121. if (ret == 0) {
  25122. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25123. }
  25124. wc_HashFree(&hash, hashType);
  25125. }
  25126. AssertIntEQ(ret, 0);
  25127. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25128. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25129. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25130. output, outputSz,
  25131. NULL, 0), 0);
  25132. wc_PKCS7_Free(pkcs7);
  25133. }
  25134. #endif
  25135. /* Test verify on signedData containing intermediate/root CA certs */
  25136. #ifndef NO_RSA
  25137. outputSz = sizeof(output);
  25138. XMEMSET(output, 0, outputSz);
  25139. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25140. (word32)sizeof(data),
  25141. 0, 0, 1, RSA_TYPE)), 0);
  25142. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25143. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25144. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25145. wc_PKCS7_Free(pkcs7);
  25146. #endif /* !NO_RSA */
  25147. #ifdef HAVE_ECC
  25148. outputSz = sizeof(output);
  25149. XMEMSET(output, 0, outputSz);
  25150. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25151. (word32)sizeof(data),
  25152. 0, 0, 1, ECC_TYPE)), 0);
  25153. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25154. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25155. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25156. wc_PKCS7_Free(pkcs7);
  25157. #endif /* HAVE_ECC */
  25158. res = TEST_RES_CHECK(1);
  25159. #endif
  25160. return res;
  25161. } /* END test_wc_PKCS7_VerifySignedData() */
  25162. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25163. !defined(NO_AES_256)
  25164. static const byte defKey[] = {
  25165. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25166. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25167. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25168. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25169. };
  25170. static byte aesHandle[32]; /* simulated hardware key handle */
  25171. /* return 0 on success */
  25172. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25173. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25174. byte* in, int inSz, byte* out, void* usrCtx)
  25175. {
  25176. int ret;
  25177. Aes aes;
  25178. if (usrCtx == NULL) {
  25179. /* no simulated handle passed in */
  25180. return -1;
  25181. }
  25182. switch (encryptOID) {
  25183. case AES256CBCb:
  25184. if (ivSz != AES_BLOCK_SIZE)
  25185. return BAD_FUNC_ARG;
  25186. break;
  25187. default:
  25188. WOLFSSL_MSG("Unsupported content cipher type for test");
  25189. return ALGO_ID_E;
  25190. };
  25191. /* simulate using handle to get key */
  25192. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25193. if (ret == 0) {
  25194. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25195. if (ret == 0)
  25196. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25197. wc_AesFree(&aes);
  25198. }
  25199. (void)aad;
  25200. (void)aadSz;
  25201. (void)authTag;
  25202. (void)authTagSz;
  25203. (void)pkcs7;
  25204. return ret;
  25205. }
  25206. /* returns key size on success */
  25207. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25208. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25209. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25210. {
  25211. int ret = -1;
  25212. if (out == NULL)
  25213. return BAD_FUNC_ARG;
  25214. if (keyId[0] != 0x00) {
  25215. return -1;
  25216. }
  25217. if (type != (int)PKCS7_KEKRI) {
  25218. return -1;
  25219. }
  25220. switch (keyWrapAlgo) {
  25221. case AES256_WRAP:
  25222. /* simulate setting a handle for later decryption but use key
  25223. * as handle in the test case here */
  25224. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25225. aesHandle, sizeof(aesHandle), NULL);
  25226. if (ret < 0)
  25227. return ret;
  25228. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25229. if (ret < 0)
  25230. return ret;
  25231. /* return key size on success */
  25232. return sizeof(defKey);
  25233. default:
  25234. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25235. return BAD_KEYWRAP_ALG_E;
  25236. };
  25237. (void)cekSz;
  25238. (void)cek;
  25239. (void)outSz;
  25240. (void)keyIdSz;
  25241. (void)direction;
  25242. (void)orginKey; /* used with KAKRI */
  25243. (void)orginKeySz;
  25244. return ret;
  25245. }
  25246. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25247. /*
  25248. * Testing wc_PKCS7_EncodeEnvelopedData()
  25249. */
  25250. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25251. {
  25252. int res = TEST_SKIPPED;
  25253. #if defined(HAVE_PKCS7)
  25254. PKCS7* pkcs7;
  25255. #ifdef ECC_TIMING_RESISTANT
  25256. WC_RNG rng;
  25257. #endif
  25258. word32 tempWrd32 = 0;
  25259. byte* tmpBytePtr = NULL;
  25260. const char input[] = "Test data to encode.";
  25261. int i;
  25262. int testSz = 0;
  25263. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25264. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25265. byte* rsaCert = NULL;
  25266. byte* rsaPrivKey = NULL;
  25267. word32 rsaCertSz;
  25268. word32 rsaPrivKeySz;
  25269. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25270. !defined(USE_CERT_BUFFERS_2048) )
  25271. static const char* rsaClientCert = "./certs/client-cert.der";
  25272. static const char* rsaClientKey = "./certs/client-key.der";
  25273. rsaCertSz = (word32)sizeof(rsaClientCert);
  25274. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25275. #endif
  25276. #endif
  25277. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25278. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25279. byte* eccCert = NULL;
  25280. byte* eccPrivKey = NULL;
  25281. word32 eccCertSz;
  25282. word32 eccPrivKeySz;
  25283. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25284. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25285. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25286. #endif
  25287. #endif
  25288. /* Generic buffer size. */
  25289. byte output[ONEK_BUF];
  25290. byte decoded[sizeof(input)/sizeof(char)];
  25291. int decodedSz = 0;
  25292. #ifndef NO_FILESYSTEM
  25293. XFILE certFile;
  25294. XFILE keyFile;
  25295. #endif
  25296. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25297. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25298. /* RSA certs and keys. */
  25299. #if defined(USE_CERT_BUFFERS_1024)
  25300. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25301. /* Allocate buffer space. */
  25302. AssertNotNull(rsaCert =
  25303. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25304. /* Init buffer. */
  25305. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25306. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25307. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25308. DYNAMIC_TYPE_TMP_BUFFER));
  25309. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25310. #elif defined(USE_CERT_BUFFERS_2048)
  25311. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25312. /* Allocate buffer */
  25313. AssertNotNull(rsaCert =
  25314. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25315. /* Init buffer. */
  25316. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25317. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25318. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25319. DYNAMIC_TYPE_TMP_BUFFER));
  25320. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25321. #else
  25322. /* File system. */
  25323. certFile = XFOPEN(rsaClientCert, "rb");
  25324. AssertTrue(certFile != XBADFILE);
  25325. rsaCertSz = (word32)FOURK_BUF;
  25326. AssertNotNull(rsaCert =
  25327. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25328. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  25329. XFCLOSE(certFile);
  25330. keyFile = XFOPEN(rsaClientKey, "rb");
  25331. AssertTrue(keyFile != XBADFILE);
  25332. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25333. DYNAMIC_TYPE_TMP_BUFFER));
  25334. rsaPrivKeySz = (word32)FOURK_BUF;
  25335. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  25336. XFCLOSE(keyFile);
  25337. #endif /* USE_CERT_BUFFERS */
  25338. #endif /* NO_RSA */
  25339. /* ECC */
  25340. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25341. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25342. #ifdef USE_CERT_BUFFERS_256
  25343. AssertNotNull(eccCert =
  25344. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25345. /* Init buffer. */
  25346. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25347. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25348. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25349. DYNAMIC_TYPE_TMP_BUFFER));
  25350. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25351. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25352. #else /* File system. */
  25353. certFile = XFOPEN(eccClientCert, "rb");
  25354. AssertTrue(certFile != XBADFILE);
  25355. eccCertSz = (word32)FOURK_BUF;
  25356. AssertNotNull(eccCert =
  25357. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25358. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  25359. XFCLOSE(certFile);
  25360. keyFile = XFOPEN(eccClientKey, "rb");
  25361. AssertTrue(keyFile != XBADFILE);
  25362. eccPrivKeySz = (word32)FOURK_BUF;
  25363. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25364. DYNAMIC_TYPE_TMP_BUFFER));
  25365. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  25366. XFCLOSE(keyFile);
  25367. #endif /* USE_CERT_BUFFERS_256 */
  25368. #endif /* END HAVE_ECC */
  25369. #ifndef NO_FILESYSTEM
  25370. /* Silence. */
  25371. (void)keyFile;
  25372. (void)certFile;
  25373. #endif
  25374. {
  25375. const pkcs7EnvelopedVector testVectors[] = {
  25376. /* DATA is a global variable defined in the makefile. */
  25377. #if !defined(NO_RSA)
  25378. #ifndef NO_DES3
  25379. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25380. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25381. #endif /* NO_DES3 */
  25382. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25383. #ifndef NO_AES_128
  25384. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25385. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25386. #endif
  25387. #ifndef NO_AES_192
  25388. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25389. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25390. #endif
  25391. #ifndef NO_AES_256
  25392. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25393. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25394. #endif
  25395. #endif /* NO_AES && HAVE_AES_CBC */
  25396. #endif /* NO_RSA */
  25397. #if defined(HAVE_ECC)
  25398. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25399. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25400. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25401. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  25402. eccCertSz, eccPrivKey, eccPrivKeySz},
  25403. #endif
  25404. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25405. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25406. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  25407. eccCertSz, eccPrivKey, eccPrivKeySz},
  25408. #endif
  25409. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25410. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25411. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  25412. eccCertSz, eccPrivKey, eccPrivKeySz},
  25413. #endif
  25414. #endif /* NO_AES && HAVE_AES_CBC*/
  25415. #endif /* END HAVE_ECC */
  25416. }; /* END pkcs7EnvelopedVector */
  25417. #ifdef ECC_TIMING_RESISTANT
  25418. AssertIntEQ(wc_InitRng(&rng), 0);
  25419. #endif
  25420. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25421. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25422. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25423. for (i = 0; i < testSz; i++) {
  25424. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25425. (word32)(testVectors + i)->certSz), 0);
  25426. #ifdef ECC_TIMING_RESISTANT
  25427. pkcs7->rng = &rng;
  25428. #endif
  25429. pkcs7->content = (byte*)(testVectors + i)->content;
  25430. pkcs7->contentSz = (testVectors + i)->contentSz;
  25431. pkcs7->contentOID = (testVectors + i)->contentOID;
  25432. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25433. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25434. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25435. pkcs7->privateKey = (testVectors + i)->privateKey;
  25436. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25437. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25438. (word32)sizeof(output)), 0);
  25439. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25440. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25441. AssertIntGE(decodedSz, 0);
  25442. /* Verify the size of each buffer. */
  25443. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25444. /* Don't free the last time through the loop. */
  25445. if (i < testSz - 1) {
  25446. wc_PKCS7_Free(pkcs7);
  25447. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25448. }
  25449. } /* END test loop. */
  25450. }
  25451. /* Test bad args. */
  25452. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25453. (word32)sizeof(output)), BAD_FUNC_ARG);
  25454. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25455. (word32)sizeof(output)), BAD_FUNC_ARG);
  25456. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25457. /* Decode. */
  25458. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25459. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25460. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25461. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25462. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25463. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25464. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25465. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25466. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25467. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25468. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25469. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25470. /* only a failure for KARI test cases */
  25471. tempWrd32 = pkcs7->singleCertSz;
  25472. pkcs7->singleCertSz = 0;
  25473. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25474. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25475. pkcs7->singleCertSz = tempWrd32;
  25476. tmpBytePtr = pkcs7->singleCert;
  25477. pkcs7->singleCert = NULL;
  25478. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25479. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25480. pkcs7->singleCert = tmpBytePtr;
  25481. #endif
  25482. tempWrd32 = pkcs7->privateKeySz;
  25483. pkcs7->privateKeySz = 0;
  25484. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25485. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25486. pkcs7->privateKeySz = tempWrd32;
  25487. tmpBytePtr = pkcs7->privateKey;
  25488. pkcs7->privateKey = NULL;
  25489. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25490. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25491. pkcs7->privateKey = tmpBytePtr;
  25492. wc_PKCS7_Free(pkcs7);
  25493. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25494. /* test of decrypt callback with KEKRI enveloped data */
  25495. {
  25496. int envelopedSz;
  25497. const byte keyId[] = { 0x00 };
  25498. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25499. pkcs7->content = (byte*)input;
  25500. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25501. pkcs7->contentOID = DATA;
  25502. pkcs7->encryptOID = AES256CBCb;
  25503. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25504. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25505. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25506. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25507. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25508. (word32)sizeof(output))), 0);
  25509. wc_PKCS7_Free(pkcs7);
  25510. /* decode envelopedData */
  25511. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25512. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25513. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25514. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25515. envelopedSz, decoded, sizeof(decoded))), 0);
  25516. wc_PKCS7_Free(pkcs7);
  25517. }
  25518. #endif /* !NO_AES && !NO_AES_256 */
  25519. #ifndef NO_RSA
  25520. if (rsaCert) {
  25521. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25522. }
  25523. if (rsaPrivKey) {
  25524. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25525. }
  25526. #endif /*NO_RSA */
  25527. #ifdef HAVE_ECC
  25528. if (eccCert) {
  25529. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25530. }
  25531. if (eccPrivKey) {
  25532. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25533. }
  25534. #endif /* HAVE_ECC */
  25535. #ifdef ECC_TIMING_RESISTANT
  25536. wc_FreeRng(&rng);
  25537. #endif
  25538. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25539. !defined(NO_RSA) && !defined(NO_SHA)
  25540. {
  25541. byte out[7];
  25542. byte *cms;
  25543. word32 cmsSz;
  25544. XFILE cmsFile;
  25545. XMEMSET(out, 0, sizeof(out));
  25546. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25547. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  25548. AssertTrue(cmsFile != XBADFILE);
  25549. cmsSz = (word32)FOURK_BUF;
  25550. AssertNotNull(cms =
  25551. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25552. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  25553. XFCLOSE(cmsFile);
  25554. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25555. sizeof_client_cert_der_2048), 0);
  25556. pkcs7->privateKey = (byte*)client_key_der_2048;
  25557. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25558. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25559. 2), 0);
  25560. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25561. sizeof(out)), 0);
  25562. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25563. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  25564. wc_PKCS7_Free(pkcs7);
  25565. }
  25566. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25567. res = TEST_RES_CHECK(1);
  25568. #endif /* HAVE_PKCS7 */
  25569. return res;
  25570. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  25571. /*
  25572. * Testing wc_PKCS7_EncodeEncryptedData()
  25573. */
  25574. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25575. {
  25576. int res = TEST_SKIPPED;
  25577. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25578. PKCS7* pkcs7 = NULL;
  25579. byte* tmpBytePtr = NULL;
  25580. byte encrypted[TWOK_BUF];
  25581. byte decoded[TWOK_BUF];
  25582. word32 tmpWrd32 = 0;
  25583. int tmpInt = 0;
  25584. int decodedSz;
  25585. int encryptedSz;
  25586. int testSz;
  25587. int i;
  25588. const byte data[] = { /* Hello World */
  25589. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25590. 0x72,0x6c,0x64
  25591. };
  25592. #ifndef NO_DES3
  25593. byte desKey[] = {
  25594. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25595. };
  25596. byte des3Key[] = {
  25597. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25598. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25599. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25600. };
  25601. #endif
  25602. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25603. #ifndef NO_AES_128
  25604. byte aes128Key[] = {
  25605. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25606. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25607. };
  25608. #endif
  25609. #ifndef NO_AES_192
  25610. byte aes192Key[] = {
  25611. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25612. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25613. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25614. };
  25615. #endif
  25616. #ifndef NO_AES_256
  25617. byte aes256Key[] = {
  25618. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25619. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25620. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25621. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25622. };
  25623. #endif
  25624. #endif /* !NO_AES && HAVE_AES_CBC */
  25625. const pkcs7EncryptedVector testVectors[] =
  25626. {
  25627. #ifndef NO_DES3
  25628. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25629. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25630. #endif /* !NO_DES3 */
  25631. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25632. #ifndef NO_AES_128
  25633. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25634. sizeof(aes128Key)},
  25635. #endif
  25636. #ifndef NO_AES_192
  25637. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25638. sizeof(aes192Key)},
  25639. #endif
  25640. #ifndef NO_AES_256
  25641. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25642. sizeof(aes256Key)},
  25643. #endif
  25644. #endif /* !NO_AES && HAVE_AES_CBC */
  25645. };
  25646. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25647. for (i = 0; i < testSz; i++) {
  25648. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25649. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25650. pkcs7->content = (byte*)testVectors[i].content;
  25651. pkcs7->contentSz = testVectors[i].contentSz;
  25652. pkcs7->contentOID = testVectors[i].contentOID;
  25653. pkcs7->encryptOID = testVectors[i].encryptOID;
  25654. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25655. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25656. pkcs7->heap = HEAP_HINT;
  25657. /* encode encryptedData */
  25658. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25659. sizeof(encrypted));
  25660. AssertIntGT(encryptedSz, 0);
  25661. /* Decode encryptedData */
  25662. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25663. decoded, sizeof(decoded));
  25664. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25665. /* Keep values for last itr. */
  25666. if (i < testSz - 1) {
  25667. wc_PKCS7_Free(pkcs7);
  25668. }
  25669. }
  25670. if (pkcs7 == NULL || testSz == 0) {
  25671. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25672. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25673. }
  25674. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25675. sizeof(encrypted)),BAD_FUNC_ARG);
  25676. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25677. sizeof(encrypted)), BAD_FUNC_ARG);
  25678. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25679. 0), BAD_FUNC_ARG);
  25680. /* Testing the struct. */
  25681. tmpBytePtr = pkcs7->content;
  25682. pkcs7->content = NULL;
  25683. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25684. sizeof(encrypted)), BAD_FUNC_ARG);
  25685. pkcs7->content = tmpBytePtr;
  25686. tmpWrd32 = pkcs7->contentSz;
  25687. pkcs7->contentSz = 0;
  25688. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25689. sizeof(encrypted)), BAD_FUNC_ARG);
  25690. pkcs7->contentSz = tmpWrd32;
  25691. tmpInt = pkcs7->encryptOID;
  25692. pkcs7->encryptOID = 0;
  25693. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25694. sizeof(encrypted)), BAD_FUNC_ARG);
  25695. pkcs7->encryptOID = tmpInt;
  25696. tmpBytePtr = pkcs7->encryptionKey;
  25697. pkcs7->encryptionKey = NULL;
  25698. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25699. sizeof(encrypted)), BAD_FUNC_ARG);
  25700. pkcs7->encryptionKey = tmpBytePtr;
  25701. tmpWrd32 = pkcs7->encryptionKeySz;
  25702. pkcs7->encryptionKeySz = 0;
  25703. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25704. sizeof(encrypted)), BAD_FUNC_ARG);
  25705. pkcs7->encryptionKeySz = tmpWrd32;
  25706. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25707. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25708. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25709. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25710. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25711. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25712. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25713. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25714. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25715. decoded, 0), BAD_FUNC_ARG);
  25716. /* Test struct fields */
  25717. tmpBytePtr = pkcs7->encryptionKey;
  25718. pkcs7->encryptionKey = NULL;
  25719. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25720. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25721. pkcs7->encryptionKey = tmpBytePtr;
  25722. pkcs7->encryptionKeySz = 0;
  25723. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25724. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25725. wc_PKCS7_Free(pkcs7);
  25726. res = TEST_RES_CHECK(1);
  25727. #endif
  25728. return res;
  25729. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25730. /*
  25731. * Testing wc_PKCS7_Degenerate()
  25732. */
  25733. static int test_wc_PKCS7_Degenerate(void)
  25734. {
  25735. int res = TEST_SKIPPED;
  25736. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25737. PKCS7* pkcs7;
  25738. char fName[] = "./certs/test-degenerate.p7b";
  25739. XFILE f;
  25740. byte der[4096];
  25741. word32 derSz;
  25742. int ret;
  25743. AssertNotNull(f = XFOPEN(fName, "rb"));
  25744. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25745. derSz = (word32)ret;
  25746. XFCLOSE(f);
  25747. /* test degenerate success */
  25748. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25749. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25750. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25751. #ifndef NO_RSA
  25752. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25753. #else
  25754. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25755. #endif
  25756. wc_PKCS7_Free(pkcs7);
  25757. /* test with turning off degenerate cases */
  25758. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25759. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25760. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25761. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25762. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  25763. wc_PKCS7_Free(pkcs7);
  25764. res = TEST_RES_CHECK(1);
  25765. #endif
  25766. return res;
  25767. } /* END test_wc_PKCS7_Degenerate() */
  25768. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25769. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  25770. static byte berContent[] = {
  25771. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25772. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  25773. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  25774. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  25775. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  25776. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  25777. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  25778. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  25779. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  25780. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  25781. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  25782. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  25783. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  25784. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  25785. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  25786. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  25787. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  25788. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  25789. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25790. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25791. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  25792. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25793. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25794. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  25795. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  25796. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  25797. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25798. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  25799. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  25800. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  25801. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  25802. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  25803. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  25804. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  25805. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  25806. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  25807. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  25808. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  25809. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  25810. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  25811. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  25812. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  25813. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  25814. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  25815. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25816. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  25817. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  25818. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  25819. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  25820. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  25821. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  25822. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  25823. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  25824. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  25825. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  25826. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  25827. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  25828. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  25829. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  25830. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  25831. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  25832. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  25833. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  25834. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  25835. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  25836. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  25837. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  25838. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  25839. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  25840. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  25841. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  25842. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  25843. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  25844. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  25845. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  25846. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  25847. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  25848. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  25849. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  25850. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  25851. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  25852. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  25853. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  25854. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  25855. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  25856. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  25857. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  25858. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  25859. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  25860. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  25861. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  25862. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  25863. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  25864. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  25865. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  25866. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  25867. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25868. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25869. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25870. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25871. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25872. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25873. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25874. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25875. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25876. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25877. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25878. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25879. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25880. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25881. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25882. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25883. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25884. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25885. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25886. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25887. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25888. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25889. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25890. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25891. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25892. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25893. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25894. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25895. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25896. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25897. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25898. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25899. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25900. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25901. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25902. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25903. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25904. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25905. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25906. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25907. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25908. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25909. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25910. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25911. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25912. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25913. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25914. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25915. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25916. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25917. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25918. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25919. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25920. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25921. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25922. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25923. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25924. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25925. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25926. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25927. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25928. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25929. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25930. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25931. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25932. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25933. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25934. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25935. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25936. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25937. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25938. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25939. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25940. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25941. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25942. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25943. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25944. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25945. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25946. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25947. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25948. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25949. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25950. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25951. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25952. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25953. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25954. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25955. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25956. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25957. 0x00, 0x00, 0x00, 0x00, 0x00
  25958. };
  25959. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  25960. * !NO_DES3 && !NO_SHA
  25961. */
  25962. /*
  25963. * Testing wc_PKCS7_BER()
  25964. */
  25965. static int test_wc_PKCS7_BER(void)
  25966. {
  25967. int res = TEST_SKIPPED;
  25968. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25969. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  25970. PKCS7* pkcs7;
  25971. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25972. XFILE f;
  25973. byte der[4096];
  25974. #ifndef NO_DES3
  25975. byte decoded[2048];
  25976. #endif
  25977. word32 derSz;
  25978. int ret;
  25979. AssertNotNull(f = XFOPEN(fName, "rb"));
  25980. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25981. derSz = (word32)ret;
  25982. XFCLOSE(f);
  25983. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25984. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25985. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25986. #ifndef NO_RSA
  25987. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25988. #else
  25989. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25990. #endif
  25991. wc_PKCS7_Free(pkcs7);
  25992. #ifndef NO_DES3
  25993. /* decode BER content */
  25994. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  25995. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25996. derSz = (word32)ret;
  25997. XFCLOSE(f);
  25998. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25999. #ifndef NO_RSA
  26000. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26001. #else
  26002. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26003. #endif
  26004. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  26005. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26006. derSz = (word32)ret;
  26007. XFCLOSE(f);
  26008. pkcs7->privateKey = der;
  26009. pkcs7->privateKeySz = derSz;
  26010. #ifndef NO_RSA
  26011. #ifdef WOLFSSL_SP_MATH
  26012. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26013. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26014. #else
  26015. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26016. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26017. #endif
  26018. #else
  26019. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26020. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26021. #endif
  26022. wc_PKCS7_Free(pkcs7);
  26023. #endif /* !NO_DES3 */
  26024. res = TEST_RES_CHECK(1);
  26025. #endif
  26026. return res;
  26027. } /* END test_wc_PKCS7_BER() */
  26028. static int test_PKCS7_signed_enveloped(void)
  26029. {
  26030. int res = TEST_SKIPPED;
  26031. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26032. !defined(NO_FILESYSTEM)
  26033. XFILE f;
  26034. PKCS7* pkcs7;
  26035. #ifdef HAVE_AES_CBC
  26036. PKCS7* inner;
  26037. #endif
  26038. void* pt;
  26039. WC_RNG rng;
  26040. unsigned char key[FOURK_BUF/2];
  26041. unsigned char cert[FOURK_BUF/2];
  26042. unsigned char env[FOURK_BUF];
  26043. int envSz = FOURK_BUF;
  26044. int keySz;
  26045. int certSz;
  26046. unsigned char sig[FOURK_BUF * 2];
  26047. int sigSz = FOURK_BUF * 2;
  26048. #ifdef HAVE_AES_CBC
  26049. unsigned char decoded[FOURK_BUF];
  26050. int decodedSz = FOURK_BUF;
  26051. #endif
  26052. /* load cert */
  26053. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  26054. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26055. XFCLOSE(f);
  26056. /* load key */
  26057. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  26058. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26059. XFCLOSE(f);
  26060. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  26061. /* sign cert for envelope */
  26062. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26063. AssertIntEQ(wc_InitRng(&rng), 0);
  26064. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26065. pkcs7->content = cert;
  26066. pkcs7->contentSz = certSz;
  26067. pkcs7->contentOID = DATA;
  26068. pkcs7->privateKey = key;
  26069. pkcs7->privateKeySz = keySz;
  26070. pkcs7->encryptOID = RSAk;
  26071. pkcs7->hashOID = SHA256h;
  26072. pkcs7->rng = &rng;
  26073. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26074. wc_PKCS7_Free(pkcs7);
  26075. wc_FreeRng(&rng);
  26076. #ifdef HAVE_AES_CBC
  26077. /* create envelope */
  26078. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26079. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26080. pkcs7->content = sig;
  26081. pkcs7->contentSz = sigSz;
  26082. pkcs7->contentOID = DATA;
  26083. pkcs7->encryptOID = AES256CBCb;
  26084. pkcs7->privateKey = key;
  26085. pkcs7->privateKeySz = keySz;
  26086. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26087. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26088. wc_PKCS7_Free(pkcs7);
  26089. #endif
  26090. /* create bad signed enveloped data */
  26091. sigSz = FOURK_BUF * 2;
  26092. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26093. AssertIntEQ(wc_InitRng(&rng), 0);
  26094. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26095. pkcs7->content = env;
  26096. pkcs7->contentSz = envSz;
  26097. pkcs7->contentOID = DATA;
  26098. pkcs7->privateKey = key;
  26099. pkcs7->privateKeySz = keySz;
  26100. pkcs7->encryptOID = RSAk;
  26101. pkcs7->hashOID = SHA256h;
  26102. pkcs7->rng = &rng;
  26103. /* Set no certs in bundle for this test. Hang on to the pointer though to
  26104. * free it later. */
  26105. pt = (void*)pkcs7->certList;
  26106. pkcs7->certList = NULL; /* no certs in bundle */
  26107. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26108. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  26109. wc_PKCS7_Free(pkcs7);
  26110. /* check verify fails */
  26111. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26112. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26113. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26114. PKCS7_SIGNEEDS_CHECK);
  26115. /* try verifying the signature manually */
  26116. {
  26117. RsaKey rKey;
  26118. word32 idx = 0;
  26119. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26120. WC_MAX_DIGEST_SIZE];
  26121. int digestSz;
  26122. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26123. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26124. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  26125. digest, sizeof(digest), &rKey);
  26126. AssertIntGT(digestSz, 0);
  26127. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26128. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26129. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  26130. /* verify was success */
  26131. }
  26132. wc_PKCS7_Free(pkcs7);
  26133. /* initializing the PKCS7 struct with the signing certificate should pass */
  26134. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26135. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26136. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26137. wc_PKCS7_Free(pkcs7);
  26138. /* create valid degenerate bundle */
  26139. sigSz = FOURK_BUF * 2;
  26140. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26141. pkcs7->content = env;
  26142. pkcs7->contentSz = envSz;
  26143. pkcs7->contentOID = DATA;
  26144. pkcs7->privateKey = key;
  26145. pkcs7->privateKeySz = keySz;
  26146. pkcs7->encryptOID = RSAk;
  26147. pkcs7->hashOID = SHA256h;
  26148. pkcs7->rng = &rng;
  26149. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26150. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26151. wc_PKCS7_Free(pkcs7);
  26152. wc_FreeRng(&rng);
  26153. /* check verify */
  26154. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26155. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26156. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26157. AssertNotNull(pkcs7->content);
  26158. #ifdef HAVE_AES_CBC
  26159. /* check decode */
  26160. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  26161. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26162. inner->privateKey = key;
  26163. inner->privateKeySz = keySz;
  26164. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26165. pkcs7->contentSz, decoded, decodedSz)), 0);
  26166. wc_PKCS7_Free(inner);
  26167. #endif
  26168. wc_PKCS7_Free(pkcs7);
  26169. #ifdef HAVE_AES_CBC
  26170. /* check cert set */
  26171. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26172. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26173. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26174. AssertNotNull(pkcs7->singleCert);
  26175. AssertIntNE(pkcs7->singleCertSz, 0);
  26176. wc_PKCS7_Free(pkcs7);
  26177. #endif
  26178. res = TEST_RES_CHECK(1);
  26179. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26180. return res;
  26181. }
  26182. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26183. {
  26184. int res = TEST_SKIPPED;
  26185. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26186. && !defined(NO_AES)
  26187. PKCS7* pkcs7;
  26188. void* heap = NULL;
  26189. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26190. AssertNotNull(pkcs7);
  26191. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26192. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26193. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26194. wc_PKCS7_Free(pkcs7);
  26195. res = TEST_RES_CHECK(1);
  26196. #endif
  26197. return res;
  26198. }
  26199. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26200. {
  26201. int res = TEST_SKIPPED;
  26202. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26203. && !defined(NO_AES)
  26204. PKCS7* pkcs7;
  26205. void* heap = NULL;
  26206. WOLFSSL_CTX* ctx;
  26207. ctx = NULL;
  26208. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26209. AssertNotNull(pkcs7);
  26210. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26211. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26212. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26213. wc_PKCS7_Free(pkcs7);
  26214. res = TEST_RES_CHECK(1);
  26215. #endif
  26216. return res;
  26217. }
  26218. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26219. {
  26220. int res = TEST_SKIPPED;
  26221. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26222. && !defined(NO_AES)
  26223. PKCS7* pkcs7;
  26224. void* heap = NULL;
  26225. WOLFSSL_CTX* ctx;
  26226. ctx = NULL;
  26227. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26228. AssertNotNull(pkcs7);
  26229. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26230. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26231. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26232. wc_PKCS7_Free(pkcs7);
  26233. res = TEST_RES_CHECK(1);
  26234. #endif
  26235. return res;
  26236. }
  26237. static int test_wc_PKCS7_DecodeCompressedData(void)
  26238. {
  26239. int res = TEST_SKIPPED;
  26240. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26241. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26242. PKCS7* pkcs7;
  26243. void* heap = NULL;
  26244. byte out[4096];
  26245. byte *decompressed;
  26246. int outSz, decompressedSz;
  26247. const char* cert = "./certs/client-cert.pem";
  26248. byte* cert_buf = NULL;
  26249. size_t cert_sz = 0;
  26250. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26251. AssertNotNull((decompressed =
  26252. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  26253. decompressedSz = (int)cert_sz;
  26254. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26255. pkcs7->content = (byte*)cert_buf;
  26256. pkcs7->contentSz = (word32)cert_sz;
  26257. pkcs7->contentOID = DATA;
  26258. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26259. sizeof(out))), 0);
  26260. wc_PKCS7_Free(pkcs7);
  26261. /* compressed key should be smaller than when started */
  26262. AssertIntLT(outSz, cert_sz);
  26263. /* test decompression */
  26264. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26265. AssertIntEQ(pkcs7->contentOID, 0);
  26266. /* fail case with out buffer too small */
  26267. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26268. decompressed, outSz), 0);
  26269. /* success case */
  26270. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26271. decompressed, decompressedSz), cert_sz);
  26272. AssertIntEQ(pkcs7->contentOID, DATA);
  26273. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26274. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26275. decompressed = NULL;
  26276. /* test decompression function with different 'max' inputs */
  26277. outSz = sizeof(out);
  26278. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26279. 0);
  26280. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26281. out, outSz, 0, heap), 0);
  26282. AssertNull(decompressed);
  26283. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26284. out, outSz, 0, heap), 0);
  26285. AssertNotNull(decompressed);
  26286. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26287. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26288. decompressed = NULL;
  26289. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26290. out, outSz, 0, heap), 0);
  26291. AssertNotNull(decompressed);
  26292. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26293. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26294. if (cert_buf)
  26295. free(cert_buf);
  26296. wc_PKCS7_Free(pkcs7);
  26297. res = TEST_RES_CHECK(1);
  26298. #endif
  26299. return res;
  26300. }
  26301. static int test_wc_i2d_PKCS12(void)
  26302. {
  26303. int res = TEST_SKIPPED;
  26304. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26305. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26306. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26307. WC_PKCS12* pkcs12 = NULL;
  26308. unsigned char der[FOURK_BUF * 2];
  26309. unsigned char* pt;
  26310. int derSz;
  26311. unsigned char out[FOURK_BUF * 2];
  26312. int outSz = FOURK_BUF * 2;
  26313. const char p12_f[] = "./certs/test-servercert.p12";
  26314. XFILE f;
  26315. f = XFOPEN(p12_f, "rb");
  26316. AssertNotNull(f);
  26317. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  26318. AssertIntGT(derSz, 0);
  26319. XFCLOSE(f);
  26320. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26321. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26322. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26323. AssertIntEQ(outSz, derSz);
  26324. outSz = derSz - 1;
  26325. pt = out;
  26326. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26327. outSz = derSz;
  26328. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26329. AssertIntEQ((pt == out), 0);
  26330. pt = NULL;
  26331. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26332. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26333. wc_PKCS12_free(pkcs12);
  26334. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26335. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26336. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26337. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26338. AssertIntEQ(outSz, derSz);
  26339. wc_PKCS12_free(pkcs12);
  26340. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26341. pkcs12 = NULL;
  26342. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26343. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26344. AssertIntEQ(outSz, derSz);
  26345. wc_PKCS12_free(pkcs12);
  26346. res = TEST_RES_CHECK(1);
  26347. #endif
  26348. return res;
  26349. }
  26350. /* Testing wc_SignatureGetSize() for signature type ECC */
  26351. static int test_wc_SignatureGetSize_ecc(void)
  26352. {
  26353. int res = TEST_SKIPPED;
  26354. #ifndef NO_SIG_WRAPPER
  26355. int ret;
  26356. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  26357. enum wc_SignatureType sig_type;
  26358. word32 key_len;
  26359. /* Initialize ECC Key */
  26360. ecc_key ecc;
  26361. const char* qx =
  26362. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26363. const char* qy =
  26364. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26365. const char* d =
  26366. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26367. ret = wc_ecc_init(&ecc);
  26368. if (ret == 0) {
  26369. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  26370. }
  26371. if (ret == 0) {
  26372. /* Input for signature type ECC */
  26373. sig_type = WC_SIGNATURE_TYPE_ECC;
  26374. key_len = sizeof(ecc_key);
  26375. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26376. /* Test bad args */
  26377. if (ret > 0) {
  26378. sig_type = (enum wc_SignatureType) 100;
  26379. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26380. if (ret == BAD_FUNC_ARG) {
  26381. sig_type = WC_SIGNATURE_TYPE_ECC;
  26382. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  26383. }
  26384. if (ret >= 0) {
  26385. key_len = (word32) 0;
  26386. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26387. }
  26388. if (ret == BAD_FUNC_ARG) {
  26389. ret = SIG_TYPE_E;
  26390. }
  26391. }
  26392. }
  26393. else {
  26394. ret = WOLFSSL_FATAL_ERROR;
  26395. }
  26396. wc_ecc_free(&ecc);
  26397. #else
  26398. ret = SIG_TYPE_E;
  26399. #endif
  26400. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  26401. #endif /* NO_SIG_WRAPPER */
  26402. return res;
  26403. }/* END test_wc_SignatureGetSize_ecc() */
  26404. /* Testing wc_SignatureGetSize() for signature type rsa */
  26405. static int test_wc_SignatureGetSize_rsa(void)
  26406. {
  26407. int res = TEST_SKIPPED;
  26408. #ifndef NO_SIG_WRAPPER
  26409. int ret = 0;
  26410. #ifndef NO_RSA
  26411. enum wc_SignatureType sig_type;
  26412. word32 key_len;
  26413. word32 idx = 0;
  26414. /* Initialize RSA Key */
  26415. RsaKey rsa_key;
  26416. byte* tmp = NULL;
  26417. size_t bytes;
  26418. #ifdef USE_CERT_BUFFERS_1024
  26419. bytes = (size_t)sizeof_client_key_der_1024;
  26420. if (bytes < (size_t)sizeof_client_key_der_1024)
  26421. bytes = (size_t)sizeof_client_cert_der_1024;
  26422. #elif defined(USE_CERT_BUFFERS_2048)
  26423. bytes = (size_t)sizeof_client_key_der_2048;
  26424. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26425. bytes = (size_t)sizeof_client_cert_der_2048;
  26426. #else
  26427. bytes = FOURK_BUF;
  26428. #endif
  26429. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26430. if (tmp != NULL) {
  26431. #ifdef USE_CERT_BUFFERS_1024
  26432. XMEMCPY(tmp, client_key_der_1024,
  26433. (size_t)sizeof_client_key_der_1024);
  26434. #elif defined(USE_CERT_BUFFERS_2048)
  26435. XMEMCPY(tmp, client_key_der_2048,
  26436. (size_t)sizeof_client_key_der_2048);
  26437. #elif !defined(NO_FILESYSTEM)
  26438. file = XFOPEN(clientKey, "rb");
  26439. if (file != XBADFILE) {
  26440. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  26441. XFCLOSE(file);
  26442. }
  26443. else {
  26444. ret = WOLFSSL_FATAL_ERROR;
  26445. }
  26446. #else
  26447. ret = WOLFSSL_FATAL_ERROR;
  26448. #endif
  26449. }
  26450. else {
  26451. ret = WOLFSSL_FATAL_ERROR;
  26452. }
  26453. if (ret == 0) {
  26454. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
  26455. }
  26456. if (ret == 0) {
  26457. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  26458. }
  26459. if (ret == 0) {
  26460. /* Input for signature type RSA */
  26461. sig_type = WC_SIGNATURE_TYPE_RSA;
  26462. key_len = sizeof(RsaKey);
  26463. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26464. /* Test bad args */
  26465. if (ret > 0) {
  26466. sig_type = (enum wc_SignatureType) 100;
  26467. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26468. if (ret == BAD_FUNC_ARG) {
  26469. sig_type = WC_SIGNATURE_TYPE_RSA;
  26470. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  26471. }
  26472. #ifndef HAVE_USER_RSA
  26473. if (ret == BAD_FUNC_ARG)
  26474. #else
  26475. if (ret == 0)
  26476. #endif
  26477. {
  26478. key_len = (word32)0;
  26479. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26480. }
  26481. if (ret == BAD_FUNC_ARG) {
  26482. ret = SIG_TYPE_E;
  26483. }
  26484. }
  26485. }
  26486. else {
  26487. ret = WOLFSSL_FATAL_ERROR;
  26488. }
  26489. wc_FreeRsaKey(&rsa_key);
  26490. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26491. #else
  26492. ret = SIG_TYPE_E;
  26493. #endif
  26494. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  26495. #endif /* NO_SIG_WRAPPER */
  26496. return res;
  26497. }/* END test_wc_SignatureGetSize_rsa(void) */
  26498. /*----------------------------------------------------------------------------*
  26499. | hash.h Tests
  26500. *----------------------------------------------------------------------------*/
  26501. static int test_wc_HashInit(void)
  26502. {
  26503. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  26504. wc_HashAlg hash;
  26505. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26506. enum wc_HashType enumArray[] = {
  26507. #ifndef NO_MD5
  26508. WC_HASH_TYPE_MD5,
  26509. #endif
  26510. #ifndef NO_SHA
  26511. WC_HASH_TYPE_SHA,
  26512. #endif
  26513. #ifndef WOLFSSL_SHA224
  26514. WC_HASH_TYPE_SHA224,
  26515. #endif
  26516. #ifndef NO_SHA256
  26517. WC_HASH_TYPE_SHA256,
  26518. #endif
  26519. #ifndef WOLFSSL_SHA384
  26520. WC_HASH_TYPE_SHA384,
  26521. #endif
  26522. #ifndef WOLFSSL_SHA512
  26523. WC_HASH_TYPE_SHA512,
  26524. #endif
  26525. };
  26526. /* dynamically finds the length */
  26527. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26528. /* For loop to test various arguments... */
  26529. for (i = 0; i < enumlen; i++) {
  26530. /* check for bad args */
  26531. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  26532. ret = 1;
  26533. break;
  26534. }
  26535. wc_HashFree(&hash, enumArray[i]);
  26536. /* check for null ptr */
  26537. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  26538. ret = 1;
  26539. break;
  26540. }
  26541. } /* end of for loop */
  26542. return TEST_RES_CHECK(ret == 0);
  26543. } /* end of test_wc_HashInit */
  26544. /*
  26545. * Unit test function for wc_HashSetFlags()
  26546. */
  26547. static int test_wc_HashSetFlags(void)
  26548. {
  26549. int res = TEST_SKIPPED;
  26550. #ifdef WOLFSSL_HASH_FLAGS
  26551. wc_HashAlg hash;
  26552. int ret = 0;
  26553. word32 flags = 0;
  26554. int i, j;
  26555. int notSupportedLen;
  26556. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26557. enum wc_HashType enumArray[] = {
  26558. #ifndef NO_MD5
  26559. WC_HASH_TYPE_MD5,
  26560. #endif
  26561. #ifndef NO_SHA
  26562. WC_HASH_TYPE_SHA,
  26563. #endif
  26564. #ifdef WOLFSSL_SHA224
  26565. WC_HASH_TYPE_SHA224,
  26566. #endif
  26567. #ifndef NO_SHA256
  26568. WC_HASH_TYPE_SHA256,
  26569. #endif
  26570. #ifdef WOLFSSL_SHA384
  26571. WC_HASH_TYPE_SHA384,
  26572. #endif
  26573. #ifdef WOLFSSL_SHA512
  26574. WC_HASH_TYPE_SHA512,
  26575. #endif
  26576. #ifdef WOLFSSL_SHA3
  26577. WC_HASH_TYPE_SHA3_224,
  26578. #endif
  26579. };
  26580. enum wc_HashType notSupported[] = {
  26581. WC_HASH_TYPE_MD5_SHA,
  26582. WC_HASH_TYPE_MD2,
  26583. WC_HASH_TYPE_MD4,
  26584. WC_HASH_TYPE_BLAKE2B,
  26585. WC_HASH_TYPE_BLAKE2S,
  26586. WC_HASH_TYPE_NONE,
  26587. };
  26588. /* dynamically finds the length */
  26589. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26590. /* For loop to test various arguments... */
  26591. for (i = 0; i < enumlen; i++) {
  26592. ret = wc_HashInit(&hash, enumArray[i]);
  26593. if (ret == 0) {
  26594. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  26595. }
  26596. if (ret == 0) {
  26597. if (flags & WC_HASH_FLAG_ISCOPY) {
  26598. ret = 0;
  26599. }
  26600. }
  26601. if (ret == 0) {
  26602. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  26603. if (ret == BAD_FUNC_ARG) {
  26604. ret = 0;
  26605. }
  26606. }
  26607. wc_HashFree(&hash, enumArray[i]);
  26608. }
  26609. /* For loop to test not supported cases */
  26610. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26611. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  26612. ret = wc_HashInit(&hash, notSupported[j]);
  26613. if (ret == 0) {
  26614. ret = -1;
  26615. }
  26616. else if (ret == BAD_FUNC_ARG) {
  26617. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  26618. if (ret == 0) {
  26619. ret = -1;
  26620. }
  26621. else if (ret == BAD_FUNC_ARG) {
  26622. ret = 0;
  26623. }
  26624. }
  26625. if (ret == 0) {
  26626. ret = wc_HashFree(&hash, notSupported[j]);
  26627. if (ret == 0) {
  26628. ret = -1;
  26629. }
  26630. else if (ret == BAD_FUNC_ARG) {
  26631. ret = 0;
  26632. }
  26633. }
  26634. }
  26635. res = TEST_RES_CHECK(ret == 0);
  26636. #endif
  26637. return res;
  26638. } /* END test_wc_HashSetFlags */
  26639. /*
  26640. * Unit test function for wc_HashGetFlags()
  26641. */
  26642. static int test_wc_HashGetFlags(void)
  26643. {
  26644. int res = TEST_SKIPPED;
  26645. #ifdef WOLFSSL_HASH_FLAGS
  26646. wc_HashAlg hash;
  26647. int ret = 0;
  26648. word32 flags = 0;
  26649. int i, j;
  26650. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26651. enum wc_HashType enumArray[] = {
  26652. #ifndef NO_MD5
  26653. WC_HASH_TYPE_MD5,
  26654. #endif
  26655. #ifndef NO_SHA
  26656. WC_HASH_TYPE_SHA,
  26657. #endif
  26658. #ifdef WOLFSSL_SHA224
  26659. WC_HASH_TYPE_SHA224,
  26660. #endif
  26661. #ifndef NO_SHA256
  26662. WC_HASH_TYPE_SHA256,
  26663. #endif
  26664. #ifdef WOLFSSL_SHA384
  26665. WC_HASH_TYPE_SHA384,
  26666. #endif
  26667. #ifdef WOLFSSL_SHA512
  26668. WC_HASH_TYPE_SHA512,
  26669. #endif
  26670. #ifdef WOLFSSL_SHA3
  26671. WC_HASH_TYPE_SHA3_224,
  26672. #endif
  26673. };
  26674. enum wc_HashType notSupported[] = {
  26675. WC_HASH_TYPE_MD5_SHA,
  26676. WC_HASH_TYPE_MD2,
  26677. WC_HASH_TYPE_MD4,
  26678. WC_HASH_TYPE_BLAKE2B,
  26679. WC_HASH_TYPE_BLAKE2S,
  26680. WC_HASH_TYPE_NONE,
  26681. };
  26682. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26683. int notSupportedLen;
  26684. /* For loop to test various arguments... */
  26685. for (i = 0; i < enumlen; i++) {
  26686. ret = wc_HashInit(&hash, enumArray[i]);
  26687. if (ret == 0) {
  26688. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  26689. }
  26690. if (ret == 0) {
  26691. if (flags & WC_HASH_FLAG_ISCOPY) {
  26692. ret = 0;
  26693. }
  26694. }
  26695. if (ret == 0) {
  26696. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  26697. if (ret == BAD_FUNC_ARG) {
  26698. ret = 0;
  26699. }
  26700. }
  26701. wc_HashFree(&hash, enumArray[i]);
  26702. if (ret != 0) {
  26703. break;
  26704. }
  26705. }
  26706. /* For loop to test not supported cases */
  26707. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26708. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  26709. ret = wc_HashInit(&hash, notSupported[j]);
  26710. if (ret == 0) {
  26711. ret = -1;
  26712. }
  26713. else if (ret == BAD_FUNC_ARG) {
  26714. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  26715. if (ret == 0) {
  26716. ret = -1;
  26717. }
  26718. else if (ret == BAD_FUNC_ARG) {
  26719. ret = 0;
  26720. }
  26721. }
  26722. if (ret == 0) {
  26723. ret = wc_HashFree(&hash, notSupported[j]);
  26724. if (ret == 0) {
  26725. ret = -1;
  26726. }
  26727. if (ret == BAD_FUNC_ARG) {
  26728. ret = 0;
  26729. }
  26730. }
  26731. }
  26732. res = TEST_RES_CHECK(ret == 0);
  26733. #endif
  26734. return res;
  26735. } /* END test_wc_HashGetFlags */
  26736. /*----------------------------------------------------------------------------*
  26737. | Compatibility Tests
  26738. *----------------------------------------------------------------------------*/
  26739. static int test_wolfSSL_lhash(void)
  26740. {
  26741. int res = TEST_SKIPPED;
  26742. #ifdef OPENSSL_ALL
  26743. const char testStr[] = "Like a true nature's child\n"
  26744. "We were born\n"
  26745. "Born to be wild";
  26746. #ifdef NO_SHA
  26747. AssertIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  26748. #else
  26749. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  26750. #endif
  26751. res = TEST_RES_CHECK(1);
  26752. #endif
  26753. return res;
  26754. }
  26755. static int test_wolfSSL_X509_NAME(void)
  26756. {
  26757. int res = TEST_SKIPPED;
  26758. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  26759. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26760. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  26761. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  26762. defined(OPENSSL_EXTRA))
  26763. X509* x509;
  26764. const unsigned char* c;
  26765. unsigned char buf[4096];
  26766. int bytes;
  26767. XFILE f;
  26768. const X509_NAME* a;
  26769. const X509_NAME* b;
  26770. X509_NAME* d2i_name = NULL;
  26771. int sz;
  26772. unsigned char* tmp;
  26773. char file[] = "./certs/ca-cert.der";
  26774. #ifndef OPENSSL_EXTRA_X509_SMALL
  26775. byte empty[] = { /* CN=empty emailAddress= */
  26776. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  26777. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  26778. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  26779. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26780. 0x01, 0x16, 0x00
  26781. };
  26782. #endif
  26783. #ifndef OPENSSL_EXTRA_X509_SMALL
  26784. /* test compile of deprecated function, returns 0 */
  26785. AssertIntEQ(CRYPTO_thread_id(), 0);
  26786. #endif
  26787. AssertNotNull(a = X509_NAME_new());
  26788. X509_NAME_free((X509_NAME*)a);
  26789. f = XFOPEN(file, "rb");
  26790. AssertTrue(f != XBADFILE);
  26791. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  26792. XFCLOSE(f);
  26793. c = buf;
  26794. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  26795. /* test cmp function */
  26796. AssertNotNull(a = X509_get_issuer_name(x509));
  26797. AssertNotNull(b = X509_get_subject_name(x509));
  26798. #ifndef OPENSSL_EXTRA_X509_SMALL
  26799. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  26800. #endif
  26801. tmp = buf;
  26802. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  26803. if (sz > 0 && tmp == buf) {
  26804. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  26805. __LINE__);
  26806. fprintf(stderr, " Expected pointer to be incremented\n");
  26807. abort();
  26808. }
  26809. #ifndef OPENSSL_EXTRA_X509_SMALL
  26810. tmp = buf;
  26811. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26812. #endif
  26813. /* if output parameter is NULL, should still return required size. */
  26814. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  26815. /* retry but with the function creating a buffer */
  26816. tmp = NULL;
  26817. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26818. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26819. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  26820. #ifndef OPENSSL_EXTRA_X509_SMALL
  26821. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  26822. #endif
  26823. X509_NAME_free((X509_NAME*)b);
  26824. X509_NAME_free(d2i_name);
  26825. X509_free(x509);
  26826. #ifndef OPENSSL_EXTRA_X509_SMALL
  26827. /* test with an empty domain component */
  26828. tmp = empty;
  26829. sz = sizeof(empty);
  26830. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26831. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  26832. /* size of empty emailAddress will be 0 */
  26833. tmp = buf;
  26834. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  26835. (char*)tmp, sizeof(buf)), 0);
  26836. /* should contain no organization name */
  26837. tmp = buf;
  26838. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  26839. (char*)tmp, sizeof(buf)), -1);
  26840. X509_NAME_free(d2i_name);
  26841. #endif
  26842. res = TEST_RES_CHECK(1);
  26843. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26844. return res;
  26845. }
  26846. static int test_wolfSSL_X509_NAME_hash(void)
  26847. {
  26848. int res = TEST_SKIPPED;
  26849. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  26850. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  26851. BIO* bio;
  26852. X509* x509 = NULL;
  26853. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26854. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26855. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26856. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  26857. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  26858. X509_free(x509);
  26859. BIO_free(bio);
  26860. res = TEST_RES_CHECK(1);
  26861. #endif
  26862. return res;
  26863. }
  26864. static int test_wolfSSL_X509_NAME_print_ex(void)
  26865. {
  26866. int res = TEST_SKIPPED;
  26867. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  26868. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  26869. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  26870. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  26871. !defined(NO_BIO) && !defined(NO_RSA)
  26872. int memSz;
  26873. byte* mem = NULL;
  26874. BIO* bio = NULL;
  26875. BIO* membio = NULL;
  26876. X509* x509 = NULL;
  26877. X509_NAME* name = NULL;
  26878. const char* expNormal = "C=US, CN=wolfssl.com";
  26879. const char* expReverse = "CN=wolfssl.com, C=US";
  26880. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  26881. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  26882. const char* expRFC5523 =
  26883. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  26884. /* Test with real cert (svrCertFile) first */
  26885. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26886. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26887. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26888. AssertNotNull(name = X509_get_subject_name(x509));
  26889. /* Test without flags */
  26890. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26891. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26892. BIO_free(membio);
  26893. /* Test flag: XN_FLAG_RFC2253 */
  26894. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26895. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26896. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26897. BIO_free(membio);
  26898. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  26899. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26900. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26901. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26902. BIO_free(membio);
  26903. X509_free(x509);
  26904. BIO_free(bio);
  26905. /* Test normal case without escaped characters */
  26906. {
  26907. /* Create name: "/C=US/CN=wolfssl.com" */
  26908. AssertNotNull(name = X509_NAME_new());
  26909. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26910. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  26911. WOLFSSL_SUCCESS);
  26912. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26913. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  26914. WOLFSSL_SUCCESS);
  26915. /* Test without flags */
  26916. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26917. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26918. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26919. AssertIntEQ(memSz, XSTRLEN(expNormal));
  26920. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  26921. BIO_free(membio);
  26922. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  26923. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26924. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26925. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26926. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26927. AssertIntEQ(memSz, XSTRLEN(expReverse));
  26928. BIO_free(membio);
  26929. /* Test flags: XN_FLAG_DN_REV - reversed */
  26930. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26931. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26932. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26933. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26934. AssertIntEQ(memSz, XSTRLEN(expReverse));
  26935. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  26936. BIO_free(membio);
  26937. X509_NAME_free(name);
  26938. }
  26939. /* Test RFC2253 characters are escaped with backslashes */
  26940. {
  26941. AssertNotNull(name = X509_NAME_new());
  26942. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26943. /* space at beginning and end, and: ,+"\ */
  26944. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  26945. WOLFSSL_SUCCESS);
  26946. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26947. /* # at beginning, and: <>;*/
  26948. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  26949. WOLFSSL_SUCCESS);
  26950. /* Test without flags */
  26951. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26952. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26953. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26954. AssertIntEQ(memSz, XSTRLEN(expNotEscaped));
  26955. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  26956. XSTRLEN(expNotEscaped)), 0);
  26957. BIO_free(membio);
  26958. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  26959. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26960. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26961. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26962. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26963. AssertIntEQ(memSz, XSTRLEN(expRFC5523));
  26964. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  26965. BIO_free(membio);
  26966. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  26967. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26968. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26969. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26970. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26971. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  26972. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  26973. XSTRLEN(expNotEscapedRev)), 0);
  26974. BIO_free(membio);
  26975. X509_NAME_free(name);
  26976. }
  26977. res = TEST_RES_CHECK(1);
  26978. #endif
  26979. return res;
  26980. }
  26981. #ifndef NO_BIO
  26982. static int test_wolfSSL_X509_INFO_multiple_info(void)
  26983. {
  26984. int res = TEST_SKIPPED;
  26985. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26986. STACK_OF(X509_INFO) *info_stack;
  26987. X509_INFO *info;
  26988. int len;
  26989. int i;
  26990. const char* files[] = {
  26991. cliCertFile,
  26992. cliKeyFile,
  26993. /* This needs to be the order as svrCertFile contains the
  26994. * intermediate cert as well. */
  26995. svrKeyFile,
  26996. svrCertFile,
  26997. NULL,
  26998. };
  26999. const char** curFile;
  27000. BIO *fileBIO;
  27001. BIO *concatBIO = NULL;
  27002. byte tmp[FOURK_BUF];
  27003. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  27004. * to group objects together. */
  27005. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  27006. for (curFile = files; *curFile != NULL; curFile++) {
  27007. int fileLen;
  27008. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  27009. fileLen = wolfSSL_BIO_get_len(fileBIO);
  27010. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  27011. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  27012. fileLen -= len;
  27013. }
  27014. /* Make sure we read the entire file */
  27015. AssertIntEQ(fileLen, 0);
  27016. BIO_free(fileBIO);
  27017. }
  27018. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  27019. NULL));
  27020. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  27021. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  27022. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  27023. AssertNotNull(info->x509);
  27024. AssertNull(info->crl);
  27025. if (i != 0) {
  27026. AssertNotNull(info->x_pkey);
  27027. AssertIntEQ(X509_check_private_key(info->x509,
  27028. info->x_pkey->dec_pkey), 1);
  27029. }
  27030. else {
  27031. AssertNull(info->x_pkey);
  27032. }
  27033. }
  27034. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27035. BIO_free(concatBIO);
  27036. res = TEST_RES_CHECK(1);
  27037. #endif
  27038. return res;
  27039. }
  27040. #endif
  27041. #ifndef NO_BIO
  27042. static int test_wolfSSL_X509_INFO(void)
  27043. {
  27044. int res = TEST_SKIPPED;
  27045. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  27046. STACK_OF(X509_INFO) *info_stack;
  27047. X509_INFO *info;
  27048. BIO *cert;
  27049. int i;
  27050. /* PEM in hex format to avoid null terminator */
  27051. byte data[] = {
  27052. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  27053. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  27054. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  27055. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  27056. 0x2d, 0x2d, 0x2d
  27057. };
  27058. /* PEM in hex format to avoid null terminator */
  27059. byte data2[] = {
  27060. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  27061. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  27062. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  27063. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  27064. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  27065. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  27066. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  27067. };
  27068. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  27069. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27070. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  27071. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  27072. AssertNotNull(info->x509);
  27073. AssertNull(info->crl);
  27074. AssertNull(info->x_pkey);
  27075. }
  27076. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27077. BIO_free(cert);
  27078. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  27079. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27080. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27081. BIO_free(cert);
  27082. /* This case should fail due to invalid input. */
  27083. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  27084. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  27085. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27086. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27087. BIO_free(cert);
  27088. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  27089. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  27090. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27091. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27092. BIO_free(cert);
  27093. res = TEST_RES_CHECK(1);
  27094. #endif
  27095. return res;
  27096. }
  27097. #endif
  27098. static int test_wolfSSL_X509_subject_name_hash(void)
  27099. {
  27100. int res = TEST_SKIPPED;
  27101. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27102. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  27103. X509* x509;
  27104. X509_NAME* subjectName = NULL;
  27105. unsigned long ret1 = 0;
  27106. unsigned long ret2 = 0;
  27107. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27108. SSL_FILETYPE_PEM));
  27109. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  27110. /* These two
  27111. * - X509_subject_name_hash(x509)
  27112. * - X509_NAME_hash(X509_get_subject_name(x509))
  27113. * should give the same hash, if !defined(NO_SHA) is true. */
  27114. ret1 = X509_subject_name_hash(x509);
  27115. AssertIntNE(ret1, 0);
  27116. #if !defined(NO_SHA)
  27117. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  27118. AssertIntNE(ret2, 0);
  27119. AssertIntEQ(ret1, ret2);
  27120. #else
  27121. (void) ret2;
  27122. #endif
  27123. X509_free(x509);
  27124. res = TEST_RES_CHECK(1);
  27125. #endif
  27126. return res;
  27127. }
  27128. static int test_wolfSSL_X509_issuer_name_hash(void)
  27129. {
  27130. int res = TEST_SKIPPED;
  27131. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27132. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  27133. X509* x509;
  27134. X509_NAME* issuertName = NULL;
  27135. unsigned long ret1 = 0;
  27136. unsigned long ret2 = 0;
  27137. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27138. SSL_FILETYPE_PEM));
  27139. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  27140. /* These two
  27141. * - X509_issuer_name_hash(x509)
  27142. * - X509_NAME_hash(X509_get_issuer_name(x509))
  27143. * should give the same hash, if !defined(NO_SHA) is true. */
  27144. ret1 = X509_issuer_name_hash(x509);
  27145. AssertIntNE(ret1, 0);
  27146. #if !defined(NO_SHA)
  27147. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  27148. AssertIntNE(ret2, 0);
  27149. AssertIntEQ(ret1, ret2);
  27150. #else
  27151. (void) ret2;
  27152. #endif
  27153. X509_free(x509);
  27154. res = TEST_RES_CHECK(1);
  27155. #endif
  27156. return res;
  27157. }
  27158. static int test_wolfSSL_X509_check_host(void)
  27159. {
  27160. int res = TEST_SKIPPED;
  27161. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27162. && !defined(NO_SHA) && !defined(NO_RSA)
  27163. X509* x509;
  27164. const char altName[] = "example.com";
  27165. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27166. SSL_FILETYPE_PEM));
  27167. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  27168. WOLFSSL_SUCCESS);
  27169. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  27170. WOLFSSL_FAILURE);
  27171. X509_free(x509);
  27172. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  27173. WOLFSSL_FAILURE);
  27174. res = TEST_RES_CHECK(1);
  27175. #endif
  27176. return res;
  27177. }
  27178. static int test_wolfSSL_X509_check_email(void)
  27179. {
  27180. int res = TEST_SKIPPED;
  27181. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  27182. X509* x509;
  27183. const char goodEmail[] = "info@wolfssl.com";
  27184. const char badEmail[] = "disinfo@wolfssl.com";
  27185. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27186. SSL_FILETYPE_PEM));
  27187. /* Should fail on non-matching email address */
  27188. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  27189. WOLFSSL_FAILURE);
  27190. /* Should succeed on matching email address */
  27191. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  27192. WOLFSSL_SUCCESS);
  27193. /* Should compute length internally when not provided */
  27194. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  27195. WOLFSSL_SUCCESS);
  27196. /* Should fail when email address is NULL */
  27197. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  27198. WOLFSSL_FAILURE);
  27199. X509_free(x509);
  27200. /* Should fail when x509 is NULL */
  27201. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  27202. WOLFSSL_FAILURE);
  27203. res = TEST_RES_CHECK(1);
  27204. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  27205. return res;
  27206. }
  27207. static int test_wolfSSL_DES(void)
  27208. {
  27209. int res = TEST_SKIPPED;
  27210. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  27211. const_DES_cblock myDes;
  27212. DES_cblock iv;
  27213. DES_key_schedule key;
  27214. word32 i;
  27215. DES_LONG dl;
  27216. unsigned char msg[] = "hello wolfssl";
  27217. DES_check_key(1);
  27218. DES_set_key(&myDes, &key);
  27219. /* check, check of odd parity */
  27220. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  27221. XMEMSET(key, 5, sizeof(DES_key_schedule));
  27222. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  27223. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  27224. /* set odd parity for success case */
  27225. DES_set_odd_parity(&myDes);
  27226. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  27227. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  27228. myDes[3]);
  27229. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  27230. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27231. AssertIntEQ(key[i], myDes[i]);
  27232. }
  27233. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  27234. /* check weak key */
  27235. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  27236. XMEMSET(key, 5, sizeof(DES_key_schedule));
  27237. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  27238. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  27239. /* now do unchecked copy of a weak key over */
  27240. DES_set_key_unchecked(&myDes, &key);
  27241. /* compare arrays, should be the same */
  27242. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27243. AssertIntEQ(key[i], myDes[i]);
  27244. }
  27245. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  27246. /* check DES_key_sched API */
  27247. XMEMSET(key, 1, sizeof(DES_key_schedule));
  27248. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  27249. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  27250. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  27251. /* compare arrays, should be the same */
  27252. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27253. AssertIntEQ(key[i], myDes[i]);
  27254. }
  27255. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  27256. * DES_cbc_encrypt on the input */
  27257. XMEMSET(iv, 0, sizeof(DES_cblock));
  27258. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  27259. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  27260. AssertIntEQ(dl, 480052723);
  27261. res = TEST_RES_CHECK(1);
  27262. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  27263. return res;
  27264. }
  27265. static int test_wc_PemToDer(void)
  27266. {
  27267. int res = TEST_SKIPPED;
  27268. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  27269. int ret;
  27270. DerBuffer* pDer = NULL;
  27271. const char* ca_cert = "./certs/server-cert.pem";
  27272. byte* cert_buf = NULL;
  27273. size_t cert_sz = 0;
  27274. int eccKey = 0;
  27275. EncryptedInfo info;
  27276. XMEMSET(&info, 0, sizeof(info));
  27277. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27278. if (ret == 0) {
  27279. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  27280. &pDer, NULL, &info, &eccKey);
  27281. AssertIntEQ(ret, 0);
  27282. wc_FreeDer(&pDer);
  27283. }
  27284. if (cert_buf)
  27285. free(cert_buf);
  27286. #ifdef HAVE_ECC
  27287. {
  27288. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  27289. byte key_buf[256] = {0};
  27290. /* Test fail of loading a key with cert type */
  27291. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  27292. key_buf[0] = '\n';
  27293. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  27294. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  27295. &pDer, NULL, &info, &eccKey)), 0);
  27296. #ifdef OPENSSL_EXTRA
  27297. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  27298. &pDer, NULL, &info, &eccKey)), 0);
  27299. #endif
  27300. wc_FreeDer(&pDer);
  27301. if (cert_buf)
  27302. free(cert_buf);
  27303. }
  27304. #endif
  27305. res = TEST_RES_CHECK(1);
  27306. #endif
  27307. return res;
  27308. }
  27309. static int test_wc_AllocDer(void)
  27310. {
  27311. int res = TEST_SKIPPED;
  27312. #if !defined(NO_CERTS)
  27313. int ret;
  27314. DerBuffer* pDer = NULL;
  27315. word32 testSize = 1024;
  27316. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  27317. AssertIntEQ(ret, 0);
  27318. AssertNotNull(pDer);
  27319. wc_FreeDer(&pDer);
  27320. res = TEST_RES_CHECK(1);
  27321. #endif
  27322. return res;
  27323. }
  27324. static int test_wc_CertPemToDer(void)
  27325. {
  27326. int res = TEST_SKIPPED;
  27327. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  27328. int ret;
  27329. const char* ca_cert = "./certs/ca-cert.pem";
  27330. byte* cert_buf = NULL;
  27331. size_t cert_sz = 0, cert_dersz = 0;
  27332. byte* cert_der = NULL;
  27333. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27334. if (ret == 0) {
  27335. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  27336. cert_der = (byte*)malloc(cert_dersz);
  27337. if (cert_der) {
  27338. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  27339. cert_der, (int)cert_dersz, CERT_TYPE);
  27340. AssertIntGE(ret, 0);
  27341. }
  27342. }
  27343. if (cert_der)
  27344. free(cert_der);
  27345. if (cert_buf)
  27346. free(cert_buf);
  27347. res = TEST_RES_CHECK(1);
  27348. #endif
  27349. return res;
  27350. }
  27351. static int test_wc_PubKeyPemToDer(void)
  27352. {
  27353. int res = TEST_SKIPPED;
  27354. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  27355. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  27356. int ret;
  27357. const char* key = "./certs/ecc-client-keyPub.pem";
  27358. byte* cert_buf = NULL;
  27359. size_t cert_sz = 0, cert_dersz = 0;
  27360. byte* cert_der = NULL;
  27361. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  27362. cert_der, (int)cert_dersz);
  27363. AssertIntGE(ret, BAD_FUNC_ARG);
  27364. ret = load_file(key, &cert_buf, &cert_sz);
  27365. if (ret == 0) {
  27366. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  27367. cert_der = (byte*)malloc(cert_dersz);
  27368. if (cert_der) {
  27369. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  27370. cert_der, (int)cert_dersz);
  27371. AssertIntGE(ret, 0);
  27372. }
  27373. }
  27374. if (cert_der)
  27375. free(cert_der);
  27376. if (cert_buf)
  27377. free(cert_buf);
  27378. res = TEST_RES_CHECK(1);
  27379. #endif
  27380. return res;
  27381. }
  27382. static int test_wc_PemPubKeyToDer(void)
  27383. {
  27384. int res = TEST_SKIPPED;
  27385. #if !defined(NO_FILESYSTEM) && \
  27386. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  27387. int ret;
  27388. const char* key = "./certs/ecc-client-keyPub.pem";
  27389. size_t cert_dersz = 1024;
  27390. byte* cert_der = (byte*)malloc(cert_dersz);
  27391. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  27392. AssertIntGE(ret, BAD_FUNC_ARG);
  27393. if (cert_der) {
  27394. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  27395. AssertIntGE(ret, 0);
  27396. free(cert_der);
  27397. }
  27398. res = TEST_RES_CHECK(1);
  27399. #endif
  27400. return res;
  27401. }
  27402. static int test_wc_GetPubKeyDerFromCert(void)
  27403. {
  27404. int res = TEST_SKIPPED;
  27405. #if !defined(NO_RSA) || defined(HAVE_ECC)
  27406. int ret;
  27407. word32 idx = 0;
  27408. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27409. word32 keyDerSz = (word32)sizeof(keyDer);
  27410. DecodedCert decoded;
  27411. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  27412. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  27413. word32 certBufSz = sizeof(certBuf);
  27414. #endif
  27415. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  27416. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  27417. XFILE fp;
  27418. #endif
  27419. #ifndef NO_RSA
  27420. RsaKey rsaKey;
  27421. #if defined(USE_CERT_BUFFERS_2048)
  27422. byte* rsaCertDer = (byte*)client_cert_der_2048;
  27423. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  27424. #elif defined(USE_CERT_BUFFERS_1024)
  27425. byte* rsaCertDer = (byte*)client_cert_der_1024;
  27426. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  27427. #else
  27428. unsigned char rsaCertDer[TWOK_BUF];
  27429. word32 rsaCertDerSz;
  27430. #endif
  27431. #endif
  27432. #ifdef HAVE_ECC
  27433. ecc_key eccKey;
  27434. #if defined(USE_CERT_BUFFERS_256)
  27435. byte* eccCert = (byte*)cliecc_cert_der_256;
  27436. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  27437. #else
  27438. unsigned char eccCert[ONEK_BUF];
  27439. word32 eccCertSz;
  27440. XFILE fp2;
  27441. #endif
  27442. #endif
  27443. #ifndef NO_RSA
  27444. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  27445. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  27446. AssertTrue((fp != XBADFILE));
  27447. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  27448. XFCLOSE(fp);
  27449. #endif
  27450. /* good test case - RSA DER cert */
  27451. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  27452. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27453. AssertIntEQ(ret, 0);
  27454. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27455. AssertIntEQ(ret, 0);
  27456. AssertIntGT(keyDerSz, 0);
  27457. /* sanity check, verify we can import DER public key */
  27458. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  27459. AssertIntEQ(ret, 0);
  27460. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  27461. AssertIntEQ(ret, 0);
  27462. wc_FreeRsaKey(&rsaKey);
  27463. /* test LENGTH_ONLY_E case */
  27464. keyDerSz = 0;
  27465. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  27466. AssertIntEQ(ret, LENGTH_ONLY_E);
  27467. AssertIntGT(keyDerSz, 0);
  27468. /* bad args: DecodedCert NULL */
  27469. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  27470. AssertIntEQ(ret, BAD_FUNC_ARG);
  27471. /* bad args: output key buff size */
  27472. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  27473. AssertIntEQ(ret, BAD_FUNC_ARG);
  27474. /* bad args: zero size output key buffer */
  27475. keyDerSz = 0;
  27476. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27477. AssertIntEQ(ret, BAD_FUNC_ARG);
  27478. wc_FreeDecodedCert(&decoded);
  27479. /* Certificate Request Tests */
  27480. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  27481. {
  27482. XMEMSET(certBuf, 0, sizeof(certBuf));
  27483. fp = XFOPEN("./certs/csr.signed.der", "rb");
  27484. AssertTrue((fp != XBADFILE));
  27485. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  27486. XFCLOSE(fp);
  27487. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  27488. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
  27489. AssertIntEQ(ret, 0);
  27490. /* good test case - RSA DER certificate request */
  27491. keyDerSz = sizeof(keyDer);
  27492. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27493. AssertIntEQ(ret, 0);
  27494. AssertIntGT(keyDerSz, 0);
  27495. /* sanity check, verify we can import DER public key */
  27496. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  27497. AssertIntEQ(ret, 0);
  27498. idx = 0;
  27499. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  27500. AssertIntEQ(ret, 0);
  27501. wc_FreeRsaKey(&rsaKey);
  27502. wc_FreeDecodedCert(&decoded);
  27503. }
  27504. #endif /* WOLFSSL_CERT_REQ */
  27505. #endif /* NO_RSA */
  27506. #ifdef HAVE_ECC
  27507. #ifndef USE_CERT_BUFFERS_256
  27508. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  27509. AssertTrue((fp2 != XBADFILE));
  27510. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  27511. XFCLOSE(fp2);
  27512. #endif
  27513. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  27514. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27515. AssertIntEQ(ret, 0);
  27516. /* good test case - ECC */
  27517. XMEMSET(keyDer, 0, sizeof(keyDer));
  27518. keyDerSz = sizeof(keyDer);
  27519. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27520. AssertIntEQ(ret, 0);
  27521. AssertIntGT(keyDerSz, 0);
  27522. /* sanity check, verify we can import DER public key */
  27523. ret = wc_ecc_init(&eccKey);
  27524. AssertIntEQ(ret, 0);
  27525. idx = 0; /* reset idx to 0, used above in RSA case */
  27526. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  27527. AssertIntEQ(ret, 0);
  27528. wc_ecc_free(&eccKey);
  27529. /* test LENGTH_ONLY_E case */
  27530. keyDerSz = 0;
  27531. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  27532. AssertIntEQ(ret, LENGTH_ONLY_E);
  27533. AssertIntGT(keyDerSz, 0);
  27534. wc_FreeDecodedCert(&decoded);
  27535. #endif
  27536. res = TEST_RES_CHECK(1);
  27537. #endif /* !NO_RSA || HAVE_ECC */
  27538. return res;
  27539. }
  27540. static int test_wc_CheckCertSigPubKey(void)
  27541. {
  27542. int res = TEST_SKIPPED;
  27543. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27544. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  27545. int ret;
  27546. const char* ca_cert = "./certs/ca-cert.pem";
  27547. byte* cert_buf = NULL;
  27548. size_t cert_sz = 0;
  27549. byte* cert_der = NULL;
  27550. word32 cert_dersz = 0;
  27551. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27552. word32 keyDerSz = (word32)sizeof(keyDer);
  27553. DecodedCert decoded;
  27554. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27555. if (ret == 0) {
  27556. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  27557. cert_der = (byte*)malloc(cert_dersz);
  27558. if (cert_der) {
  27559. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  27560. cert_der, (int)cert_dersz, CERT_TYPE);
  27561. AssertIntGE(ret, 0);
  27562. }
  27563. }
  27564. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  27565. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27566. AssertIntEQ(ret, 0);
  27567. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27568. AssertIntEQ(ret, 0);
  27569. AssertIntGT(keyDerSz, 0);
  27570. /* Good test case. */
  27571. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  27572. RSAk);
  27573. AssertIntEQ(ret, 0);
  27574. /* No certificate. */
  27575. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  27576. ECDSAk);
  27577. AssertIntEQ(ret, BAD_FUNC_ARG);
  27578. /* Bad cert size. */
  27579. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  27580. RSAk);
  27581. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  27582. /* No public key. */
  27583. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  27584. RSAk);
  27585. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  27586. /* Bad public key size. */
  27587. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  27588. RSAk);
  27589. AssertIntEQ(ret, BAD_FUNC_ARG);
  27590. /* Wrong aglo. */
  27591. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  27592. ECDSAk);
  27593. AssertIntEQ(ret, ASN_PARSE_E);
  27594. wc_FreeDecodedCert(&decoded);
  27595. if (cert_der)
  27596. free(cert_der);
  27597. if (cert_buf)
  27598. free(cert_buf);
  27599. res = TEST_RES_CHECK(1);
  27600. #endif
  27601. return res;
  27602. }
  27603. static int test_wolfSSL_certs(void)
  27604. {
  27605. int res = TEST_SKIPPED;
  27606. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27607. !defined(NO_RSA)
  27608. X509* x509ext;
  27609. #ifdef OPENSSL_ALL
  27610. X509* x509;
  27611. WOLFSSL_X509_EXTENSION* ext;
  27612. ASN1_OBJECT* obj;
  27613. #endif
  27614. WOLFSSL* ssl;
  27615. WOLFSSL_CTX* ctx;
  27616. STACK_OF(ASN1_OBJECT)* sk;
  27617. ASN1_STRING* asn1_str;
  27618. AUTHORITY_KEYID* akey;
  27619. BASIC_CONSTRAINTS* bc;
  27620. int crit;
  27621. #ifndef NO_WOLFSSL_SERVER
  27622. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  27623. #else
  27624. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  27625. #endif
  27626. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  27627. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27628. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  27629. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27630. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  27631. #endif
  27632. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27633. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27634. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  27635. #endif
  27636. AssertNotNull(ssl = SSL_new(ctx));
  27637. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27638. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27639. #endif
  27640. #ifdef HAVE_PK_CALLBACKS
  27641. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  27642. #endif /* HAVE_PK_CALLBACKS */
  27643. /* create and use x509 */
  27644. #ifdef OPENSSL_ALL
  27645. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  27646. AssertNotNull(x509);
  27647. #endif
  27648. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  27649. AssertNotNull(x509ext);
  27650. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  27651. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27652. /* with loading in a new cert the check on private key should now fail */
  27653. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27654. #endif
  27655. #if defined(USE_CERT_BUFFERS_2048)
  27656. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  27657. (unsigned char*)server_cert_der_2048,
  27658. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  27659. #endif
  27660. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  27661. /************* Get Digest of Certificate ******************/
  27662. {
  27663. byte digest[64]; /* max digest size */
  27664. word32 digestSz;
  27665. XMEMSET(digest, 0, sizeof(digest));
  27666. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  27667. WOLFSSL_SUCCESS);
  27668. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  27669. WOLFSSL_SUCCESS);
  27670. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  27671. WOLFSSL_FAILURE);
  27672. }
  27673. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  27674. /* test and checkout X509 extensions */
  27675. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  27676. &crit, NULL);
  27677. AssertNotNull(bc);
  27678. AssertIntEQ(crit, 0);
  27679. #ifdef OPENSSL_ALL
  27680. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  27681. AssertNotNull(ext);
  27682. X509_EXTENSION_free(ext);
  27683. AssertNotNull(ext = X509_EXTENSION_new());
  27684. X509_EXTENSION_set_critical(ext, 1);
  27685. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  27686. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  27687. ASN1_OBJECT_free(obj);
  27688. X509_EXTENSION_free(ext);
  27689. AssertNotNull(ext = X509_EXTENSION_new());
  27690. X509_EXTENSION_set_critical(ext, 0);
  27691. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  27692. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  27693. NULL);
  27694. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  27695. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  27696. * and X509_get_ext_d2i has created new */
  27697. X509_EXTENSION_free(ext);
  27698. #endif
  27699. BASIC_CONSTRAINTS_free(bc);
  27700. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  27701. AssertNotNull(asn1_str);
  27702. AssertIntEQ(crit, 1);
  27703. AssertIntEQ(asn1_str->type, NID_key_usage);
  27704. #ifdef OPENSSL_ALL
  27705. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  27706. AssertNotNull(ext);
  27707. X509_EXTENSION_free(ext);
  27708. #endif
  27709. ASN1_STRING_free(asn1_str);
  27710. #ifdef OPENSSL_ALL
  27711. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  27712. &crit, NULL);
  27713. AssertNotNull(sk);
  27714. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  27715. AssertNotNull(ext);
  27716. X509_EXTENSION_free(ext);
  27717. sk_ASN1_OBJECT_pop_free(sk, NULL);
  27718. #else
  27719. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  27720. &crit, NULL);
  27721. AssertNull(sk);
  27722. #endif
  27723. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  27724. NID_authority_key_identifier, &crit, NULL);
  27725. AssertNotNull(akey);
  27726. #ifdef OPENSSL_ALL
  27727. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  27728. AssertNotNull(ext);
  27729. X509_EXTENSION_free(ext);
  27730. #endif
  27731. wolfSSL_AUTHORITY_KEYID_free(akey);
  27732. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27733. NID_private_key_usage_period, &crit, NULL);
  27734. /* AssertNotNull(sk); NID not yet supported */
  27735. AssertIntEQ(crit, -1);
  27736. sk_ASN1_OBJECT_free(sk);
  27737. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  27738. &crit, NULL);
  27739. {
  27740. int i;
  27741. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  27742. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  27743. AssertIntEQ(gen->type, GEN_DNS);
  27744. AssertIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  27745. }
  27746. }
  27747. /* AssertNotNull(sk); no alt names set */
  27748. sk_GENERAL_NAME_free(sk);
  27749. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  27750. &crit, NULL);
  27751. /* AssertNotNull(sk); NID not yet supported */
  27752. AssertIntEQ(crit, -1);
  27753. sk_ASN1_OBJECT_free(sk);
  27754. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  27755. NULL);
  27756. /* AssertNotNull(sk); no auth info set */
  27757. sk_ASN1_OBJECT_free(sk);
  27758. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  27759. &crit, NULL);
  27760. /* AssertNotNull(sk); NID not yet supported */
  27761. AssertIntEQ(crit, -1);
  27762. sk_ASN1_OBJECT_free(sk);
  27763. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  27764. &crit, NULL);
  27765. /* AssertNotNull(sk); NID not yet supported */
  27766. AssertIntEQ(crit, -1);
  27767. sk_ASN1_OBJECT_free(sk);
  27768. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27769. NID_certificate_policies, &crit, NULL);
  27770. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  27771. AssertNull(sk);
  27772. #else
  27773. /* AssertNotNull(sk); no cert policy set */
  27774. #endif
  27775. sk_ASN1_OBJECT_free(sk);
  27776. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  27777. &crit, NULL);
  27778. /* AssertNotNull(sk); NID not yet supported */
  27779. AssertIntEQ(crit, -1);
  27780. sk_ASN1_OBJECT_free(sk);
  27781. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  27782. &crit, NULL);
  27783. /* AssertNotNull(sk); NID not yet supported */
  27784. AssertIntEQ(crit, -1);
  27785. sk_ASN1_OBJECT_free(sk);
  27786. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  27787. &crit, NULL);
  27788. /* AssertNotNull(sk); NID not yet supported */
  27789. AssertIntEQ(crit, -1);
  27790. sk_ASN1_OBJECT_free(sk);
  27791. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  27792. NULL);
  27793. /* AssertNotNull(sk); NID not yet supported */
  27794. AssertIntEQ(crit, -1);
  27795. sk_ASN1_OBJECT_free(sk);
  27796. /* test invalid cases */
  27797. crit = 0;
  27798. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  27799. AssertNull(sk);
  27800. AssertIntEQ(crit, -1);
  27801. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  27802. NULL, NULL);
  27803. AssertNull(sk);
  27804. AssertIntEQ(SSL_get_hit(ssl), 0);
  27805. #ifdef OPENSSL_ALL
  27806. X509_free(x509);
  27807. #endif
  27808. X509_free(x509ext);
  27809. SSL_free(ssl);
  27810. SSL_CTX_free(ctx);
  27811. res = TEST_RES_CHECK(1);
  27812. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  27813. return res;
  27814. }
  27815. static int test_wolfSSL_X509_check_private_key(void)
  27816. {
  27817. int res = TEST_SKIPPED;
  27818. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  27819. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  27820. X509* x509;
  27821. EVP_PKEY* pkey = NULL;
  27822. const byte* key;
  27823. /* Check with correct key */
  27824. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  27825. SSL_FILETYPE_PEM)));
  27826. key = client_key_der_2048;
  27827. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27828. &key, (long)sizeof_client_key_der_2048));
  27829. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  27830. EVP_PKEY_free(pkey);
  27831. pkey = NULL;
  27832. /* Check with wrong key */
  27833. key = server_key_der_2048;
  27834. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27835. &key, (long)sizeof_server_key_der_2048));
  27836. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  27837. /* test for incorrect parameter */
  27838. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  27839. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  27840. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  27841. EVP_PKEY_free(pkey);
  27842. X509_free(x509);
  27843. res = TEST_RES_CHECK(1);
  27844. #endif
  27845. return res;
  27846. }
  27847. static int test_wolfSSL_ASN1_TIME_print(void)
  27848. {
  27849. int res = TEST_SKIPPED;
  27850. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  27851. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27852. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048) && \
  27853. !defined(NO_BIO)
  27854. BIO* bio;
  27855. X509* x509;
  27856. const unsigned char* der = client_cert_der_2048;
  27857. ASN1_TIME* t;
  27858. unsigned char buf[25];
  27859. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27860. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27861. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27862. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  27863. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27864. AssertIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  27865. /* create a bad time and test results */
  27866. AssertNotNull(t = X509_get_notAfter(x509));
  27867. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  27868. t->data[8] = 0;
  27869. t->data[3] = 0;
  27870. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  27871. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27872. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27873. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  27874. BIO_free(bio);
  27875. X509_free(x509);
  27876. res = TEST_RES_CHECK(1);
  27877. #endif
  27878. return res;
  27879. }
  27880. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27881. {
  27882. int res = TEST_SKIPPED;
  27883. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27884. BIO* bio;
  27885. ASN1_UTCTIME* utc = NULL;
  27886. unsigned char buf[25];
  27887. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27888. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27889. /* NULL parameter check */
  27890. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27891. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27892. BIO_free(bio);
  27893. /* Valid date */
  27894. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27895. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27896. DYNAMIC_TYPE_ASN1));
  27897. utc->type = ASN_UTC_TIME;
  27898. utc->length = ASN_UTC_TIME_SIZE;
  27899. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27900. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27901. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27902. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27903. XMEMSET(buf, 0, sizeof(buf));
  27904. BIO_free(bio);
  27905. /* Invalid format */
  27906. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27907. utc->type = ASN_UTC_TIME;
  27908. utc->length = ASN_UTC_TIME_SIZE;
  27909. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27910. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27911. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27912. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27913. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27914. BIO_free(bio);
  27915. res = TEST_RES_CHECK(1);
  27916. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27917. return res;
  27918. }
  27919. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27920. {
  27921. int res = TEST_SKIPPED;
  27922. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27923. ASN1_TIME* fromTime;
  27924. ASN1_TIME* toTime;
  27925. int daysDiff;
  27926. int secsDiff;
  27927. AssertNotNull((fromTime = ASN1_TIME_new()));
  27928. /* Feb 22, 2003, 21:15:15 */
  27929. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), WOLFSSL_SUCCESS);
  27930. AssertNotNull((toTime = ASN1_TIME_new()));
  27931. /* Dec 19, 2010, 18:10:11 */
  27932. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), WOLFSSL_SUCCESS);
  27933. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), WOLFSSL_SUCCESS);
  27934. /* Error conditions. */
  27935. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime),
  27936. WOLFSSL_FAILURE);
  27937. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime),
  27938. WOLFSSL_FAILURE);
  27939. /* If both times are NULL, difference is 0. */
  27940. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL),
  27941. WOLFSSL_SUCCESS);
  27942. AssertIntEQ(daysDiff, 0);
  27943. AssertIntEQ(secsDiff, 0);
  27944. /* If one time is NULL, it defaults to the current time. */
  27945. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime),
  27946. WOLFSSL_SUCCESS);
  27947. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL),
  27948. WOLFSSL_SUCCESS);
  27949. /* Normal operation. Both times non-NULL. */
  27950. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27951. WOLFSSL_SUCCESS);
  27952. AssertIntEQ(daysDiff, 2856);
  27953. AssertIntEQ(secsDiff, 75296);
  27954. /* Swapping the times should return negative values. */
  27955. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime),
  27956. WOLFSSL_SUCCESS);
  27957. AssertIntEQ(daysDiff, -2856);
  27958. AssertIntEQ(secsDiff, -75296);
  27959. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27960. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27961. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27962. /* Compare regression test: No seconds difference, just difference in days.
  27963. */
  27964. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27965. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27966. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27967. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27968. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27969. /* Edge case with Unix epoch. */
  27970. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27971. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27972. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27973. WOLFSSL_SUCCESS);
  27974. AssertIntEQ(daysDiff, 3652);
  27975. AssertIntEQ(secsDiff, 0);
  27976. /* Edge case with year > 2038 (year 2038 problem). */
  27977. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27978. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27979. WOLFSSL_SUCCESS);
  27980. AssertIntEQ(daysDiff, 2932896);
  27981. AssertIntEQ(secsDiff, 86399);
  27982. ASN1_TIME_free(fromTime);
  27983. ASN1_TIME_free(toTime);
  27984. res = TEST_RES_CHECK(1);
  27985. #endif
  27986. return res;
  27987. }
  27988. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27989. {
  27990. int res = TEST_SKIPPED;
  27991. #if defined(OPENSSL_EXTRA)
  27992. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  27993. unsigned char nullstr[32];
  27994. XMEMSET(nullstr, 0, 32);
  27995. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27996. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  27997. DYNAMIC_TYPE_TMP_BUFFER);
  27998. if (asn1_gtime) {
  27999. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  28000. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28001. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  28002. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28003. }
  28004. res = TEST_RES_CHECK(1);
  28005. #endif /* OPENSSL_EXTRA */
  28006. return res;
  28007. }
  28008. static int test_wolfSSL_private_keys(void)
  28009. {
  28010. int res = TEST_SKIPPED;
  28011. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28012. !defined(NO_FILESYSTEM)
  28013. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28014. WOLFSSL* ssl;
  28015. WOLFSSL_CTX* ctx;
  28016. EVP_PKEY* pkey = NULL;
  28017. OpenSSL_add_all_digests();
  28018. OpenSSL_add_all_algorithms();
  28019. #ifndef NO_RSA
  28020. #ifndef NO_WOLFSSL_SERVER
  28021. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28022. #else
  28023. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28024. #endif
  28025. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28026. /* Have to load a cert before you can check the private key against that
  28027. * certificates public key! */
  28028. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28029. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  28030. #endif
  28031. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28032. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28033. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28034. #endif
  28035. AssertNotNull(ssl = SSL_new(ctx));
  28036. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28037. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28038. #endif
  28039. #ifdef USE_CERT_BUFFERS_2048
  28040. {
  28041. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28042. unsigned char buf[FOURK_BUF];
  28043. word32 bufSz;
  28044. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  28045. (unsigned char*)client_key_der_2048,
  28046. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28047. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28048. /* Should mismatch now that a different private key loaded */
  28049. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28050. #endif
  28051. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  28052. (unsigned char*)server_key,
  28053. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28054. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28055. /* After loading back in DER format of original key, should match */
  28056. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28057. #endif
  28058. /* test loading private key to the WOLFSSL_CTX */
  28059. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28060. (unsigned char*)client_key_der_2048,
  28061. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28062. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28063. /* Should mismatch now that a different private key loaded */
  28064. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28065. #endif
  28066. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28067. (unsigned char*)server_key,
  28068. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28069. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28070. /* After loading back in DER format of original key, should match */
  28071. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28072. #endif
  28073. /* pkey not set yet, expecting to fail */
  28074. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  28075. /* set PKEY and test again */
  28076. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28077. &server_key, (long)sizeof_server_key_der_2048));
  28078. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28079. /* reuse PKEY structure and test
  28080. * this should be checked with a memory management sanity checker */
  28081. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  28082. server_key = (const unsigned char*)server_key_der_2048;
  28083. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28084. &server_key, (long)sizeof_server_key_der_2048));
  28085. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28086. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  28087. bufSz = FOURK_BUF;
  28088. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  28089. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  28090. RSAk, NULL, 0)), 0);
  28091. server_key = (const unsigned char*)buf;
  28092. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  28093. (long)bufSz));
  28094. }
  28095. #endif
  28096. EVP_PKEY_free(pkey);
  28097. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  28098. SSL_CTX_free(ctx);
  28099. #endif /* end of RSA private key match tests */
  28100. #ifdef HAVE_ECC
  28101. #ifndef NO_WOLFSSL_SERVER
  28102. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28103. #else
  28104. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28105. #endif
  28106. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  28107. WOLFSSL_FILETYPE_PEM));
  28108. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  28109. WOLFSSL_FILETYPE_PEM));
  28110. AssertNotNull(ssl = SSL_new(ctx));
  28111. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28112. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28113. #endif
  28114. SSL_free(ssl);
  28115. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  28116. WOLFSSL_FILETYPE_PEM));
  28117. AssertNotNull(ssl = SSL_new(ctx));
  28118. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  28119. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28120. #endif
  28121. SSL_free(ssl);
  28122. SSL_CTX_free(ctx);
  28123. #endif /* end of ECC private key match tests */
  28124. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  28125. #ifndef NO_WOLFSSL_SERVER
  28126. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28127. #else
  28128. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28129. #endif
  28130. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  28131. WOLFSSL_FILETYPE_PEM));
  28132. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  28133. WOLFSSL_FILETYPE_PEM));
  28134. AssertNotNull(ssl = SSL_new(ctx));
  28135. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28136. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28137. #endif
  28138. SSL_free(ssl);
  28139. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  28140. WOLFSSL_FILETYPE_PEM));
  28141. AssertNotNull(ssl = SSL_new(ctx));
  28142. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28143. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28144. #endif
  28145. SSL_free(ssl);
  28146. SSL_CTX_free(ctx);
  28147. #endif /* end of Ed25519 private key match tests */
  28148. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  28149. #ifndef NO_WOLFSSL_SERVER
  28150. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28151. #else
  28152. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28153. #endif
  28154. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  28155. WOLFSSL_FILETYPE_PEM));
  28156. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  28157. WOLFSSL_FILETYPE_PEM));
  28158. AssertNotNull(ssl = SSL_new(ctx));
  28159. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28160. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28161. #endif
  28162. SSL_free(ssl);
  28163. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  28164. WOLFSSL_FILETYPE_PEM));
  28165. AssertNotNull(ssl = SSL_new(ctx));
  28166. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28167. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28168. #endif
  28169. SSL_free(ssl);
  28170. SSL_CTX_free(ctx);
  28171. #endif /* end of Ed448 private key match tests */
  28172. EVP_cleanup();
  28173. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  28174. CONF_modules_free();
  28175. ENGINE_cleanup();
  28176. CONF_modules_unload();
  28177. (void)ssl;
  28178. (void)ctx;
  28179. (void)pkey;
  28180. res = TEST_RES_CHECK(1);
  28181. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28182. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28183. return res;
  28184. }
  28185. static int test_wolfSSL_PEM_read_PrivateKey(void)
  28186. {
  28187. int res = TEST_SKIPPED;
  28188. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  28189. && !defined(NO_FILESYSTEM)
  28190. XFILE file;
  28191. const char* fname = "./certs/server-key.pem";
  28192. EVP_PKEY* pkey;
  28193. RSA* rsa;
  28194. WOLFSSL_EVP_PKEY_CTX* ctx;
  28195. unsigned char* sig;
  28196. size_t sigLen;
  28197. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  28198. size_t tbsLen = sizeof(tbs);
  28199. /* Check error case. */
  28200. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  28201. /* Read in an RSA key. */
  28202. file = XFOPEN(fname, "rb");
  28203. AssertTrue(file != XBADFILE);
  28204. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  28205. XFCLOSE(file);
  28206. /* Make sure the key is usable by signing some data with it. */
  28207. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  28208. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  28209. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  28210. DYNAMIC_TYPE_TMP_BUFFER));
  28211. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  28212. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  28213. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  28214. WOLFSSL_SUCCESS);
  28215. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28216. EVP_PKEY_CTX_free(ctx);
  28217. EVP_PKEY_free(pkey);
  28218. res = TEST_RES_CHECK(1);
  28219. #endif
  28220. return res;
  28221. }
  28222. static int test_wolfSSL_PEM_read_PUBKEY(void)
  28223. {
  28224. int res = TEST_SKIPPED;
  28225. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  28226. && !defined(NO_FILESYSTEM)
  28227. XFILE file;
  28228. const char* fname = "./certs/client-keyPub.pem";
  28229. EVP_PKEY* pkey;
  28230. /* Check error case. */
  28231. AssertNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  28232. /* Read in an RSA key. */
  28233. file = XFOPEN(fname, "rb");
  28234. AssertTrue(file != XBADFILE);
  28235. AssertNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  28236. EVP_PKEY_free(pkey);
  28237. XFCLOSE(file);
  28238. res = TEST_RES_CHECK(1);
  28239. #endif
  28240. return res;
  28241. }
  28242. static int test_wolfSSL_PEM_PrivateKey(void)
  28243. {
  28244. int res = TEST_SKIPPED;
  28245. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28246. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  28247. #ifndef NO_BIO
  28248. BIO* bio = NULL;
  28249. #endif
  28250. EVP_PKEY* pkey = NULL;
  28251. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28252. #ifndef NO_BIO
  28253. /* test creating new EVP_PKEY with bad arg */
  28254. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  28255. /* test loading RSA key using BIO */
  28256. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28257. {
  28258. XFILE file;
  28259. const char* fname = "./certs/server-key.pem";
  28260. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  28261. size_t sz;
  28262. byte* buf;
  28263. EVP_PKEY* pkey2;
  28264. EVP_PKEY* pkey3;
  28265. RSA* rsa_key = NULL;
  28266. file = XFOPEN(fname, "rb");
  28267. AssertTrue((file != XBADFILE));
  28268. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28269. sz = XFTELL(file);
  28270. XREWIND(file);
  28271. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28272. if (buf) {
  28273. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28274. }
  28275. XFCLOSE(file);
  28276. /* Test using BIO new mem and loading PEM private key */
  28277. bio = BIO_new_mem_buf(buf, (int)sz);
  28278. AssertNotNull(bio);
  28279. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28280. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28281. BIO_free(bio);
  28282. bio = NULL;
  28283. AssertNotNull(pkey2 = EVP_PKEY_new());
  28284. pkey2->type = EVP_PKEY_RSA;
  28285. /* Test parameter copy */
  28286. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  28287. EVP_PKEY_free(pkey2);
  28288. EVP_PKEY_free(pkey);
  28289. pkey = NULL;
  28290. /* Qt unit test case : rsa pkcs8 key */
  28291. file = XFOPEN(fname_rsa_p8, "rb");
  28292. AssertTrue((file != XBADFILE));
  28293. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28294. sz = XFTELL(file);
  28295. XREWIND(file);
  28296. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28297. if (buf)
  28298. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28299. XFCLOSE(file);
  28300. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28301. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28302. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28303. BIO_free(bio);
  28304. bio = NULL;
  28305. AssertNotNull(pkey3 = EVP_PKEY_new());
  28306. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  28307. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  28308. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28309. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28310. #else
  28311. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28312. #endif
  28313. RSA_free(rsa_key);
  28314. EVP_PKEY_free(pkey3);
  28315. EVP_PKEY_free(pkey);
  28316. pkey = NULL;
  28317. }
  28318. #endif
  28319. /* test loading ECC key using BIO */
  28320. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28321. {
  28322. XFILE file;
  28323. const char* fname = "./certs/ecc-key.pem";
  28324. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  28325. size_t sz;
  28326. byte* buf;
  28327. EVP_PKEY* pkey2;
  28328. EVP_PKEY* pkey3;
  28329. EC_KEY* ec_key;
  28330. int nid = 0;
  28331. file = XFOPEN(fname, "rb");
  28332. AssertTrue((file != XBADFILE));
  28333. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28334. sz = XFTELL(file);
  28335. XREWIND(file);
  28336. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28337. if (buf)
  28338. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28339. XFCLOSE(file);
  28340. /* Test using BIO new mem and loading PEM private key */
  28341. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28342. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28343. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28344. BIO_free(bio);
  28345. bio = NULL;
  28346. AssertNotNull(pkey2 = EVP_PKEY_new());
  28347. AssertNotNull(pkey3 = EVP_PKEY_new());
  28348. pkey2->type = EVP_PKEY_EC;
  28349. /* Test parameter copy */
  28350. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  28351. /* Qt unit test case 1*/
  28352. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28353. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  28354. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28355. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28356. #else
  28357. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28358. #endif
  28359. /* Test default digest */
  28360. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  28361. AssertIntEQ(nid, NID_sha256);
  28362. EC_KEY_free(ec_key);
  28363. EVP_PKEY_free(pkey3);
  28364. EVP_PKEY_free(pkey2);
  28365. EVP_PKEY_free(pkey);
  28366. pkey = NULL;
  28367. /* Qt unit test case ec pkcs8 key */
  28368. file = XFOPEN(fname_ecc_p8, "rb");
  28369. AssertTrue((file != XBADFILE));
  28370. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28371. sz = XFTELL(file);
  28372. XREWIND(file);
  28373. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28374. if (buf)
  28375. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28376. XFCLOSE(file);
  28377. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28378. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28379. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28380. BIO_free(bio);
  28381. bio = NULL;
  28382. AssertNotNull(pkey3 = EVP_PKEY_new());
  28383. /* Qt unit test case */
  28384. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28385. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  28386. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28387. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28388. #else
  28389. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28390. #endif
  28391. EC_KEY_free(ec_key);
  28392. EVP_PKEY_free(pkey3);
  28393. EVP_PKEY_free(pkey);
  28394. pkey = NULL;
  28395. }
  28396. #endif
  28397. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  28398. defined(WOLFSSL_CERT_GEN))
  28399. {
  28400. #define BIO_PEM_TEST_CHAR 'a'
  28401. EVP_PKEY* pkey2 = NULL;
  28402. unsigned char extra[10];
  28403. int i;
  28404. BIO* pub_bio = NULL;
  28405. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  28406. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28407. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  28408. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28409. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  28410. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  28411. &server_key, (long)sizeof_server_key_der_2048));
  28412. AssertNull(pkey);
  28413. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28414. &server_key, (long)sizeof_server_key_der_2048));
  28415. AssertIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  28416. NULL), WOLFSSL_FAILURE);
  28417. AssertIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  28418. NULL), WOLFSSL_FAILURE);
  28419. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  28420. NULL), WOLFSSL_SUCCESS);
  28421. AssertIntGT(BIO_pending(bio), 0);
  28422. AssertIntEQ(BIO_pending(bio), 1679);
  28423. /* Check if the pubkey API writes only the public key */
  28424. #ifdef WOLFSSL_KEY_GEN
  28425. AssertIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  28426. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  28427. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  28428. AssertIntGT(BIO_pending(pub_bio), 0);
  28429. /* Previously both the private key and the pubkey calls would write
  28430. * out the private key and the PEM header was the only difference.
  28431. * The public PEM should be significantly shorter than the
  28432. * private key versison. */
  28433. AssertIntEQ(BIO_pending(pub_bio), 451);
  28434. #endif
  28435. /* test creating new EVP_PKEY with good args */
  28436. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28437. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  28438. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  28439. /* test of reuse of EVP_PKEY */
  28440. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  28441. AssertIntEQ(BIO_pending(bio), 0);
  28442. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  28443. SSL_SUCCESS);
  28444. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  28445. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  28446. AssertNotNull(pkey);
  28447. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  28448. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  28449. }
  28450. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  28451. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  28452. for (i = 0; i < 10; i++) {
  28453. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  28454. }
  28455. BIO_free(pub_bio);
  28456. BIO_free(bio);
  28457. bio = NULL;
  28458. EVP_PKEY_free(pkey);
  28459. pkey = NULL;
  28460. EVP_PKEY_free(pkey2);
  28461. }
  28462. #endif
  28463. /* key is DES encrypted */
  28464. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  28465. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  28466. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  28467. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  28468. {
  28469. XFILE f;
  28470. wc_pem_password_cb* passwd_cb;
  28471. void* passwd_cb_userdata;
  28472. SSL_CTX* ctx;
  28473. char passwd[] = "bad password";
  28474. #ifndef WOLFSSL_NO_TLS12
  28475. #ifndef NO_WOLFSSL_SERVER
  28476. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  28477. #else
  28478. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  28479. #endif
  28480. #else
  28481. #ifndef NO_WOLFSSL_SERVER
  28482. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  28483. #else
  28484. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  28485. #endif
  28486. #endif
  28487. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  28488. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  28489. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  28490. AssertNull(passwd_cb_userdata =
  28491. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  28492. /* fail case with password call back */
  28493. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  28494. (void*)passwd));
  28495. BIO_free(bio);
  28496. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  28497. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  28498. (void*)passwd));
  28499. BIO_free(bio);
  28500. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  28501. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  28502. /* use callback that works */
  28503. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  28504. (void*)"yassl123"));
  28505. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  28506. EVP_PKEY_free(pkey);
  28507. pkey = NULL;
  28508. BIO_free(bio);
  28509. bio = NULL;
  28510. SSL_CTX_free(ctx);
  28511. }
  28512. #endif /* !defined(NO_DES3) */
  28513. #endif /* !NO_BIO */
  28514. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28515. {
  28516. unsigned char buf[2048];
  28517. size_t bytes;
  28518. XFILE f;
  28519. SSL_CTX* ctx;
  28520. #ifndef WOLFSSL_NO_TLS12
  28521. #ifndef NO_WOLFSSL_SERVER
  28522. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  28523. #else
  28524. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  28525. #endif
  28526. #else
  28527. #ifndef NO_WOLFSSL_SERVER
  28528. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  28529. #else
  28530. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  28531. #endif
  28532. #endif
  28533. f = XFOPEN("./certs/ecc-key.der", "rb");
  28534. AssertTrue((f != XBADFILE));
  28535. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  28536. XFCLOSE(f);
  28537. server_key = buf;
  28538. pkey = NULL;
  28539. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  28540. AssertNull(pkey);
  28541. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  28542. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  28543. EVP_PKEY_free(pkey);
  28544. pkey = NULL;
  28545. SSL_CTX_free(ctx);
  28546. }
  28547. #endif
  28548. res = TEST_RES_CHECK(1);
  28549. #ifndef NO_BIO
  28550. (void)bio;
  28551. #endif
  28552. (void)pkey;
  28553. (void)server_key;
  28554. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  28555. return res;
  28556. }
  28557. static int test_wolfSSL_PEM_file_RSAKey(void)
  28558. {
  28559. int res = TEST_SKIPPED;
  28560. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28561. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  28562. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  28563. RSA* rsa = NULL;
  28564. XFILE fp;
  28565. AssertTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  28566. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  28567. XFCLOSE(fp);
  28568. AssertIntEQ(RSA_size(rsa), 256);
  28569. AssertIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  28570. AssertIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  28571. AssertIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  28572. AssertIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  28573. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  28574. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  28575. RSA_free(rsa);
  28576. res = TEST_RES_CHECK(1);
  28577. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28578. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28579. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28580. return res;
  28581. }
  28582. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  28583. {
  28584. int res = TEST_SKIPPED;
  28585. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  28586. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  28587. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  28588. RSA* rsa = NULL;
  28589. XFILE f = NULL;
  28590. f = XFOPEN(svrKeyFile, "r");
  28591. AssertTrue((f != XBADFILE));
  28592. AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  28593. AssertIntEQ(RSA_size(rsa), 256);
  28594. AssertIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  28595. NULL), WOLFSSL_FAILURE);
  28596. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  28597. NULL), WOLFSSL_FAILURE);
  28598. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  28599. WOLFSSL_SUCCESS);
  28600. RSA_free(rsa);
  28601. XFCLOSE(f);
  28602. #ifdef HAVE_ECC
  28603. f = XFOPEN(eccKeyFile, "r");
  28604. AssertTrue((f != XBADFILE));
  28605. AssertNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  28606. XFCLOSE(f);
  28607. #endif /* HAVE_ECC */
  28608. res = TEST_RES_CHECK(1);
  28609. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28610. return res;
  28611. }
  28612. #ifndef NO_BIO
  28613. static int test_wolfSSL_PEM_bio_RSAKey(void)
  28614. {
  28615. int res = TEST_SKIPPED;
  28616. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28617. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  28618. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  28619. RSA* rsa = NULL;
  28620. BIO* bio = NULL;
  28621. /* PrivateKey */
  28622. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28623. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  28624. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  28625. AssertNotNull(rsa);
  28626. AssertIntEQ(RSA_size(rsa), 256);
  28627. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  28628. NULL), WOLFSSL_FAILURE);
  28629. BIO_free(bio);
  28630. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28631. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  28632. NULL), WOLFSSL_SUCCESS);
  28633. BIO_free(bio);
  28634. RSA_free(rsa);
  28635. /* PUBKEY */
  28636. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  28637. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28638. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28639. AssertIntEQ(RSA_size(rsa), 256);
  28640. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28641. BIO_free(bio);
  28642. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28643. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  28644. BIO_free(bio);
  28645. RSA_free(rsa);
  28646. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  28647. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  28648. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28649. BIO_free(bio);
  28650. RSA_free(rsa);
  28651. #ifdef HAVE_ECC
  28652. /* ensure that non-rsa keys do not work */
  28653. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28654. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28655. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28656. BIO_free(bio);
  28657. RSA_free(rsa);
  28658. #endif /* HAVE_ECC */
  28659. res = TEST_RES_CHECK(1);
  28660. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28661. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28662. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28663. return res;
  28664. }
  28665. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  28666. {
  28667. int res = TEST_SKIPPED;
  28668. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28669. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28670. RSA* rsa = NULL;
  28671. RSA* rsa_dup = NULL;
  28672. BIO* bio = NULL;
  28673. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28674. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28675. AssertIntEQ(RSA_size(rsa), 256);
  28676. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  28677. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  28678. /* Test duplicating empty key. */
  28679. rsa_dup = RSA_new();
  28680. AssertNull(RSAPublicKey_dup(rsa_dup));
  28681. RSA_free(rsa_dup);
  28682. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  28683. AssertPtrNE(rsa_dup, rsa);
  28684. #endif
  28685. /* test if valgrind complains about unreleased memory */
  28686. RSA_up_ref(rsa);
  28687. RSA_free(rsa);
  28688. BIO_free(bio);
  28689. RSA_free(rsa);
  28690. RSA_free(rsa_dup);
  28691. #ifdef HAVE_ECC
  28692. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  28693. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28694. BIO_free(bio);
  28695. #endif /* HAVE_ECC */
  28696. res = TEST_RES_CHECK(1);
  28697. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28698. return res;
  28699. }
  28700. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  28701. {
  28702. int res = TEST_SKIPPED;
  28703. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28704. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28705. XFILE file;
  28706. const char* fname = "./certs/client-keyPub.pem";
  28707. RSA *rsa;
  28708. AssertNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  28709. file = XFOPEN(fname, "rb");
  28710. AssertTrue((file != XBADFILE));
  28711. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  28712. AssertIntEQ(RSA_size(rsa), 256);
  28713. RSA_free(rsa);
  28714. XFCLOSE(file);
  28715. res = TEST_RES_CHECK(1);
  28716. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28717. return res;
  28718. }
  28719. static int test_wolfSSL_PEM_bio_DSAKey(void)
  28720. {
  28721. int res = TEST_SKIPPED;
  28722. #ifndef HAVE_SELFTEST
  28723. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  28724. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  28725. DSA* dsa = NULL;
  28726. BIO* bio = NULL;
  28727. /* PrivateKey */
  28728. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  28729. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  28730. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28731. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  28732. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  28733. WOLFSSL_FAILURE);
  28734. BIO_free(bio);
  28735. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28736. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  28737. WOLFSSL_SUCCESS);
  28738. BIO_free(bio);
  28739. DSA_free(dsa);
  28740. /* PUBKEY */
  28741. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  28742. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28743. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28744. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  28745. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28746. BIO_free(bio);
  28747. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28748. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  28749. BIO_free(bio);
  28750. DSA_free(dsa);
  28751. #ifdef HAVE_ECC
  28752. /* ensure that non-dsa keys do not work */
  28753. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28754. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28755. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28756. BIO_free(bio);
  28757. DSA_free(dsa);
  28758. #endif /* HAVE_ECC */
  28759. res = TEST_RES_CHECK(1);
  28760. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  28761. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  28762. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  28763. #endif /* HAVE_SELFTEST */
  28764. return res;
  28765. }
  28766. static int test_wolfSSL_PEM_bio_ECKey(void)
  28767. {
  28768. int res = TEST_SKIPPED;
  28769. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28770. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  28771. EC_KEY* ec = NULL;
  28772. EC_KEY* ec2;
  28773. BIO* bio = NULL;
  28774. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  28775. unsigned char* pem = NULL;
  28776. int pLen;
  28777. #endif
  28778. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  28779. "MAA=\n"
  28780. "-----END PUBLIC KEY-----";
  28781. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  28782. "MAA=\n"
  28783. "-----END EC PRIVATE KEY-----";
  28784. /* PrivateKey */
  28785. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28786. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  28787. ec2 = NULL;
  28788. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  28789. AssertIntEQ(ec == ec2, 1);
  28790. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28791. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  28792. NULL), WOLFSSL_FAILURE);
  28793. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  28794. NULL), WOLFSSL_FAILURE);
  28795. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  28796. WOLFSSL_FAILURE);
  28797. BIO_free(bio);
  28798. /* Public key data - fail. */
  28799. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28800. AssertNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  28801. BIO_free(bio);
  28802. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28803. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  28804. NULL), WOLFSSL_SUCCESS);
  28805. BIO_free(bio);
  28806. AssertIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  28807. NULL),WOLFSSL_FAILURE);
  28808. AssertIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  28809. WOLFSSL_FAILURE);
  28810. AssertIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  28811. WOLFSSL_FAILURE);
  28812. AssertIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  28813. WOLFSSL_SUCCESS);
  28814. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  28815. NULL), 0);
  28816. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  28817. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  28818. NULL), 0);
  28819. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  28820. NULL), 0);
  28821. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  28822. &pLen), 0);
  28823. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  28824. &pLen), 0);
  28825. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  28826. &pLen), 0);
  28827. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  28828. NULL), 0);
  28829. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  28830. &pLen), 1);
  28831. AssertIntGT(pLen, 0);
  28832. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28833. #endif
  28834. EC_KEY_free(ec);
  28835. /* PUBKEY */
  28836. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28837. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  28838. ec2 = NULL;
  28839. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  28840. AssertIntEQ(ec == ec2, 1);
  28841. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28842. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28843. BIO_free(bio);
  28844. /* Test 0x30, 0x00 fails. */
  28845. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  28846. sizeof(ec_key_bad_1)));
  28847. AssertNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  28848. BIO_free(bio);
  28849. /* Private key data - fail. */
  28850. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28851. AssertNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  28852. BIO_free(bio);
  28853. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28854. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  28855. BIO_free(bio);
  28856. /* Same test as above, but with a file pointer rather than a BIO. */
  28857. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28858. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  28859. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  28860. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  28861. EC_KEY_free(ec);
  28862. #ifndef NO_RSA
  28863. /* ensure that non-ec keys do not work */
  28864. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  28865. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28866. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28867. BIO_free(bio);
  28868. EC_KEY_free(ec);
  28869. #endif /* HAVE_ECC */
  28870. /* Test 0x30, 0x00 fails. */
  28871. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  28872. sizeof(ec_priv_key_bad_1)));
  28873. AssertNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  28874. BIO_free(bio);
  28875. res = TEST_RES_CHECK(1);
  28876. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28877. return res;
  28878. }
  28879. static int test_wolfSSL_PEM_PUBKEY(void)
  28880. {
  28881. int res = TEST_SKIPPED;
  28882. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28883. BIO* bio = NULL;
  28884. EVP_PKEY* pkey = NULL;
  28885. /* test creating new EVP_PKEY with bad arg */
  28886. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  28887. /* test loading ECC key using BIO */
  28888. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28889. {
  28890. XFILE file;
  28891. const char* fname = "./certs/ecc-client-keyPub.pem";
  28892. size_t sz;
  28893. byte* buf;
  28894. EVP_PKEY* pkey2;
  28895. EC_KEY* ec_key;
  28896. file = XFOPEN(fname, "rb");
  28897. AssertTrue((file != XBADFILE));
  28898. AssertIntGE(XFSEEK(file, 0, XSEEK_END), 0);
  28899. sz = XFTELL(file);
  28900. XREWIND(file);
  28901. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28902. if (buf)
  28903. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28904. XFCLOSE(file);
  28905. /* Test using BIO new mem and loading PEM private key */
  28906. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28907. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  28908. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28909. BIO_free(bio);
  28910. bio = NULL;
  28911. /* Qt unit test case*/
  28912. AssertNotNull(pkey2 = EVP_PKEY_new());
  28913. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28914. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  28915. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28916. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  28917. #else
  28918. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  28919. #endif
  28920. EC_KEY_free(ec_key);
  28921. EVP_PKEY_free(pkey2);
  28922. EVP_PKEY_free(pkey);
  28923. pkey = NULL;
  28924. }
  28925. #endif
  28926. (void)bio;
  28927. (void)pkey;
  28928. res = TEST_RES_CHECK(1);
  28929. #endif
  28930. return res;
  28931. }
  28932. #endif /* !NO_BIO */
  28933. static int test_DSA_do_sign_verify(void)
  28934. {
  28935. int res = TEST_SKIPPED;
  28936. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  28937. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28938. !defined(NO_DSA)
  28939. unsigned char digest[WC_SHA_DIGEST_SIZE];
  28940. DSA_SIG* sig;
  28941. DSA* dsa;
  28942. word32 bytes;
  28943. byte sigBin[DSA_SIG_SIZE];
  28944. int dsacheck;
  28945. #ifdef USE_CERT_BUFFERS_1024
  28946. byte tmp[ONEK_BUF];
  28947. XMEMSET(tmp, 0, sizeof(tmp));
  28948. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  28949. bytes = sizeof_dsa_key_der_1024;
  28950. #elif defined(USE_CERT_BUFFERS_2048)
  28951. byte tmp[TWOK_BUF];
  28952. XMEMSET(tmp, 0, sizeof(tmp));
  28953. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  28954. bytes = sizeof_dsa_key_der_2048;
  28955. #else
  28956. byte tmp[TWOK_BUF];
  28957. XMEMSET(tmp, 0, sizeof(tmp));
  28958. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  28959. if (fp == XBADFILE) {
  28960. return WOLFSSL_BAD_FILE;
  28961. }
  28962. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  28963. XFCLOSE(fp);
  28964. #endif /* END USE_CERT_BUFFERS_1024 */
  28965. XMEMSET(digest, 202, sizeof(digest));
  28966. AssertNotNull(dsa = DSA_new());
  28967. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  28968. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  28969. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  28970. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  28971. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  28972. DSA_SIG_free(sig);
  28973. DSA_free(dsa);
  28974. res = TEST_RES_CHECK(1);
  28975. #endif
  28976. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  28977. return res;
  28978. }
  28979. static int test_wolfSSL_tmp_dh(void)
  28980. {
  28981. int res = TEST_SKIPPED;
  28982. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28983. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  28984. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28985. byte buff[6000];
  28986. char file[] = "./certs/dsaparams.pem";
  28987. XFILE f;
  28988. int bytes;
  28989. DSA* dsa;
  28990. DH* dh;
  28991. #if defined(WOLFSSL_DH_EXTRA) && \
  28992. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28993. DH* dh2;
  28994. #endif
  28995. BIO* bio;
  28996. SSL* ssl;
  28997. SSL_CTX* ctx;
  28998. #ifndef NO_WOLFSSL_SERVER
  28999. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29000. #else
  29001. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29002. #endif
  29003. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  29004. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  29005. AssertNotNull(ssl = SSL_new(ctx));
  29006. f = XFOPEN(file, "rb");
  29007. AssertTrue((f != XBADFILE));
  29008. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  29009. XFCLOSE(f);
  29010. bio = BIO_new_mem_buf((void*)buff, bytes);
  29011. AssertNotNull(bio);
  29012. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  29013. AssertNotNull(dsa);
  29014. dh = wolfSSL_DSA_dup_DH(dsa);
  29015. AssertNotNull(dh);
  29016. #if defined(WOLFSSL_DH_EXTRA) && \
  29017. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29018. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  29019. #endif
  29020. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  29021. #ifndef NO_WOLFSSL_SERVER
  29022. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  29023. #else
  29024. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  29025. #endif
  29026. BIO_free(bio);
  29027. DSA_free(dsa);
  29028. DH_free(dh);
  29029. #if defined(WOLFSSL_DH_EXTRA) && \
  29030. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29031. DH_free(dh2);
  29032. #endif
  29033. SSL_free(ssl);
  29034. SSL_CTX_free(ctx);
  29035. res = TEST_RES_CHECK(1);
  29036. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29037. #endif
  29038. return res;
  29039. }
  29040. static int test_wolfSSL_ctrl(void)
  29041. {
  29042. int res = TEST_SKIPPED;
  29043. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  29044. byte buff[6000];
  29045. BIO* bio;
  29046. int bytes;
  29047. BUF_MEM* ptr = NULL;
  29048. XMEMSET(buff, 0, sizeof(buff));
  29049. bytes = sizeof(buff);
  29050. bio = BIO_new_mem_buf((void*)buff, bytes);
  29051. AssertNotNull(bio);
  29052. AssertNotNull(BIO_s_socket());
  29053. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  29054. /* needs tested after stubs filled out @TODO
  29055. SSL_ctrl
  29056. SSL_CTX_ctrl
  29057. */
  29058. BIO_free(bio);
  29059. res = TEST_RES_CHECK(1);
  29060. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  29061. return res;
  29062. }
  29063. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  29064. {
  29065. int res = TEST_SKIPPED;
  29066. #ifdef OPENSSL_EXTRA
  29067. static const unsigned char pw[] = "password";
  29068. static const int pwSz = sizeof(pw) - 1;
  29069. size_t checkPwSz = 0;
  29070. const unsigned char* checkPw = NULL;
  29071. WOLFSSL_EVP_PKEY* key = NULL;
  29072. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  29073. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  29074. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  29075. if (key) {
  29076. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  29077. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  29078. AssertIntEQ(key->pkey_sz, pwSz);
  29079. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  29080. }
  29081. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  29082. AssertIntEQ((int)checkPwSz, pwSz);
  29083. if (checkPw) {
  29084. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  29085. }
  29086. wolfSSL_EVP_PKEY_free(key);
  29087. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  29088. if (key) {
  29089. AssertIntEQ(key->pkey_sz, 0);
  29090. }
  29091. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  29092. (void)checkPw;
  29093. AssertIntEQ((int)checkPwSz, 0);
  29094. wolfSSL_EVP_PKEY_free(key);
  29095. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  29096. if (key) {
  29097. AssertIntEQ(key->pkey_sz, 0);
  29098. }
  29099. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  29100. (void)checkPw;
  29101. AssertIntEQ((int)checkPwSz, 0);
  29102. wolfSSL_EVP_PKEY_free(key);
  29103. res = TEST_RES_CHECK(1);
  29104. #endif /* OPENSSL_EXTRA */
  29105. return res;
  29106. }
  29107. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  29108. {
  29109. int res = TEST_SKIPPED;
  29110. #ifdef OPENSSL_EXTRA
  29111. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  29112. const char *priv = "ABCDEFGHIJKLMNOP";
  29113. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  29114. WOLFSSL_EVP_PKEY* key = NULL;
  29115. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29116. NULL, NULL, AES_128_KEY_SIZE, cipher));
  29117. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29118. NULL, (const unsigned char *)priv, 0, cipher));
  29119. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29120. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  29121. AssertNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29122. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  29123. wolfSSL_EVP_PKEY_free(key);
  29124. res = TEST_RES_CHECK(1);
  29125. #endif /* defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) */
  29126. #endif /* OPENSSL_EXTRA */
  29127. return res;
  29128. }
  29129. static int test_wolfSSL_EVP_Digest(void)
  29130. {
  29131. int res = TEST_SKIPPED;
  29132. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  29133. const char* in = "abc";
  29134. int inLen = (int)XSTRLEN(in);
  29135. byte out[WC_SHA256_DIGEST_SIZE];
  29136. unsigned int outLen;
  29137. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  29138. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  29139. "\x15\xAD";
  29140. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  29141. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  29142. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  29143. res = TEST_RES_CHECK(1);
  29144. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  29145. return res;
  29146. }
  29147. static int test_wolfSSL_EVP_Digest_all(void)
  29148. {
  29149. int res = TEST_SKIPPED;
  29150. #ifdef OPENSSL_EXTRA
  29151. const char* digests[] = {
  29152. #ifndef NO_MD5
  29153. "MD5",
  29154. #endif
  29155. #ifndef NO_SHA
  29156. "SHA",
  29157. #endif
  29158. #ifdef WOLFSSL_SHA224
  29159. "SHA224",
  29160. #endif
  29161. #ifndef NO_SHA256
  29162. "SHA256",
  29163. #endif
  29164. #ifdef WOLFSSL_SHA384
  29165. "SHA384",
  29166. #endif
  29167. #ifdef WOLFSSL_SHA512
  29168. "SHA512",
  29169. #endif
  29170. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  29171. "SHA512_224",
  29172. #endif
  29173. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  29174. "SHA512_256",
  29175. #endif
  29176. #ifdef WOLFSSL_SHA3
  29177. #ifndef WOLFSSL_NOSHA3_224
  29178. "SHA3_224",
  29179. #endif
  29180. #ifndef WOLFSSL_NOSHA3_256
  29181. "SHA3_256",
  29182. #endif
  29183. "SHA3_384",
  29184. #ifndef WOLFSSL_NOSHA3_512
  29185. "SHA3_512",
  29186. #endif
  29187. #endif /* WOLFSSL_SHA3 */
  29188. NULL
  29189. };
  29190. const char** d;
  29191. const unsigned char in[] = "abc";
  29192. int inLen = XSTR_SIZEOF(in);
  29193. byte out[WC_MAX_DIGEST_SIZE];
  29194. unsigned int outLen;
  29195. for (d = digests; *d != NULL; d++) {
  29196. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  29197. AssertIntGT(outLen, 0);
  29198. AssertIntEQ(EVP_MD_size(*d), outLen);
  29199. }
  29200. res = TEST_RES_CHECK(1);
  29201. #endif
  29202. return res;
  29203. }
  29204. static int test_wolfSSL_EVP_MD_size(void)
  29205. {
  29206. int res = TEST_SKIPPED;
  29207. #ifdef OPENSSL_EXTRA
  29208. WOLFSSL_EVP_MD_CTX mdCtx;
  29209. #ifdef WOLFSSL_SHA3
  29210. #ifndef WOLFSSL_NOSHA3_224
  29211. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29212. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  29213. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  29214. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  29215. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29216. #endif
  29217. #ifndef WOLFSSL_NOSHA3_256
  29218. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29219. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  29220. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  29221. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  29222. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29223. #endif
  29224. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29225. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  29226. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  29227. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  29228. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29229. #ifndef WOLFSSL_NOSHA3_512
  29230. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29231. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  29232. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  29233. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  29234. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29235. #endif
  29236. #endif /* WOLFSSL_SHA3 */
  29237. #ifndef NO_SHA256
  29238. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29239. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  29240. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  29241. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  29242. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  29243. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  29244. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29245. #endif
  29246. #ifndef NO_MD5
  29247. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29248. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  29249. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  29250. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  29251. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  29252. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  29253. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29254. #endif
  29255. #ifdef WOLFSSL_SHA224
  29256. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29257. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  29258. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  29259. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  29260. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  29261. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  29262. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29263. #endif
  29264. #ifdef WOLFSSL_SHA384
  29265. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29266. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  29267. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  29268. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  29269. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  29270. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  29271. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29272. #endif
  29273. #ifdef WOLFSSL_SHA512
  29274. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29275. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  29276. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  29277. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  29278. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  29279. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  29280. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29281. #endif
  29282. #ifndef NO_SHA
  29283. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29284. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  29285. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  29286. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  29287. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  29288. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  29289. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29290. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29291. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  29292. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  29293. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  29294. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  29295. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  29296. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29297. #endif
  29298. /* error case */
  29299. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29300. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  29301. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  29302. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  29303. /* Cleanup is valid on uninit'ed struct */
  29304. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29305. res = TEST_RES_CHECK(1);
  29306. #endif /* OPENSSL_EXTRA */
  29307. return res;
  29308. }
  29309. static int test_wolfSSL_EVP_MD_pkey_type(void)
  29310. {
  29311. int res = TEST_SKIPPED;
  29312. #ifdef OPENSSL_EXTRA
  29313. const WOLFSSL_EVP_MD* md;
  29314. #ifndef NO_MD5
  29315. AssertNotNull(md = EVP_md5());
  29316. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  29317. #endif
  29318. #ifndef NO_SHA
  29319. AssertNotNull(md = EVP_sha1());
  29320. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  29321. #endif
  29322. #ifdef WOLFSSL_SHA224
  29323. AssertNotNull(md = EVP_sha224());
  29324. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  29325. #endif
  29326. AssertNotNull(md = EVP_sha256());
  29327. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  29328. #ifdef WOLFSSL_SHA384
  29329. AssertNotNull(md = EVP_sha384());
  29330. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  29331. #endif
  29332. #ifdef WOLFSSL_SHA512
  29333. AssertNotNull(md = EVP_sha512());
  29334. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  29335. #endif
  29336. res = TEST_RES_CHECK(1);
  29337. #endif
  29338. return res;
  29339. }
  29340. #ifdef OPENSSL_EXTRA
  29341. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  29342. size_t testKeySz, const char* testData, size_t testDataSz,
  29343. const byte* testResult, size_t testResultSz)
  29344. {
  29345. unsigned char check[WC_MAX_DIGEST_SIZE];
  29346. size_t checkSz = -1;
  29347. WOLFSSL_EVP_PKEY* key;
  29348. WOLFSSL_EVP_MD_CTX mdCtx;
  29349. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  29350. testKey, (int)testKeySz));
  29351. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29352. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  29353. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29354. (unsigned int)testDataSz), 1);
  29355. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29356. AssertIntEQ((int)checkSz, (int)testResultSz);
  29357. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29358. AssertIntEQ((int)checkSz,(int)testResultSz);
  29359. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  29360. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29361. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  29362. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29363. (unsigned int)testDataSz), 1);
  29364. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  29365. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29366. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29367. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  29368. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29369. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29370. AssertIntEQ((int)checkSz, (int)testResultSz);
  29371. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29372. AssertIntEQ((int)checkSz,(int)testResultSz);
  29373. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29374. (unsigned int)testDataSz - 4), 1);
  29375. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29376. AssertIntEQ((int)checkSz,(int)testResultSz);
  29377. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  29378. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29379. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  29380. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29381. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29382. (unsigned int)testDataSz - 4), 1);
  29383. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  29384. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29385. wolfSSL_EVP_PKEY_free(key);
  29386. }
  29387. #endif
  29388. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  29389. {
  29390. int res = TEST_SKIPPED;
  29391. #ifdef OPENSSL_EXTRA
  29392. static const unsigned char testKey[] =
  29393. {
  29394. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29395. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29396. 0x0b, 0x0b, 0x0b, 0x0b
  29397. };
  29398. static const char testData[] = "Hi There";
  29399. #ifdef WOLFSSL_SHA224
  29400. static const unsigned char testResultSha224[] =
  29401. {
  29402. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  29403. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  29404. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  29405. 0x53, 0x68, 0x4b, 0x22
  29406. };
  29407. #endif
  29408. #ifndef NO_SHA256
  29409. static const unsigned char testResultSha256[] =
  29410. {
  29411. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  29412. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  29413. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  29414. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  29415. };
  29416. #endif
  29417. #ifdef WOLFSSL_SHA384
  29418. static const unsigned char testResultSha384[] =
  29419. {
  29420. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  29421. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  29422. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  29423. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  29424. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  29425. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  29426. };
  29427. #endif
  29428. #ifdef WOLFSSL_SHA512
  29429. static const unsigned char testResultSha512[] =
  29430. {
  29431. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  29432. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  29433. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  29434. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  29435. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  29436. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  29437. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  29438. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  29439. };
  29440. #endif
  29441. #ifdef WOLFSSL_SHA3
  29442. #ifndef WOLFSSL_NOSHA3_224
  29443. static const unsigned char testResultSha3_224[] =
  29444. {
  29445. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  29446. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  29447. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  29448. 0xf3, 0xc8, 0x60, 0xf7
  29449. };
  29450. #endif
  29451. #ifndef WOLFSSL_NOSHA3_256
  29452. static const unsigned char testResultSha3_256[] =
  29453. {
  29454. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  29455. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  29456. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  29457. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  29458. };
  29459. #endif
  29460. #ifndef WOLFSSL_NOSHA3_384
  29461. static const unsigned char testResultSha3_384[] =
  29462. {
  29463. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  29464. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  29465. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  29466. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  29467. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  29468. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  29469. };
  29470. #endif
  29471. #ifndef WOLFSSL_NOSHA3_512
  29472. static const unsigned char testResultSha3_512[] =
  29473. {
  29474. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  29475. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  29476. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  29477. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  29478. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  29479. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  29480. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  29481. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  29482. };
  29483. #endif
  29484. #endif
  29485. #ifndef NO_SHA256
  29486. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  29487. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  29488. #endif
  29489. #ifdef WOLFSSL_SHA224
  29490. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  29491. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  29492. #endif
  29493. #ifdef WOLFSSL_SHA384
  29494. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  29495. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  29496. #endif
  29497. #ifdef WOLFSSL_SHA512
  29498. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  29499. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  29500. #endif
  29501. #ifdef WOLFSSL_SHA3
  29502. #ifndef WOLFSSL_NOSHA3_224
  29503. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  29504. testData, XSTRLEN(testData), testResultSha3_224,
  29505. sizeof(testResultSha3_224));
  29506. #endif
  29507. #ifndef WOLFSSL_NOSHA3_256
  29508. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  29509. testData, XSTRLEN(testData), testResultSha3_256,
  29510. sizeof(testResultSha3_256));
  29511. #endif
  29512. #ifndef WOLFSSL_NOSHA3_384
  29513. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  29514. testData, XSTRLEN(testData), testResultSha3_384,
  29515. sizeof(testResultSha3_384));
  29516. #endif
  29517. #ifndef WOLFSSL_NOSHA3_512
  29518. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  29519. testData, XSTRLEN(testData), testResultSha3_512,
  29520. sizeof(testResultSha3_512));
  29521. #endif
  29522. #endif
  29523. res = TEST_RES_CHECK(1);
  29524. #endif /* OPENSSL_EXTRA */
  29525. return res;
  29526. }
  29527. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  29528. {
  29529. int res = TEST_SKIPPED;
  29530. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  29531. defined(USE_CERT_BUFFERS_2048)
  29532. WOLFSSL_EVP_PKEY* privKey;
  29533. WOLFSSL_EVP_PKEY* pubKey;
  29534. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  29535. const char testData[] = "Hi There";
  29536. WOLFSSL_EVP_MD_CTX mdCtx;
  29537. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  29538. size_t checkSz = -1;
  29539. int sz = 2048 / 8;
  29540. const unsigned char* cp;
  29541. const unsigned char* p;
  29542. unsigned char check[2048/8];
  29543. size_t i;
  29544. int paddings[] = {
  29545. RSA_PKCS1_PADDING,
  29546. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  29547. RSA_PKCS1_PSS_PADDING,
  29548. #endif
  29549. };
  29550. cp = client_key_der_2048;
  29551. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  29552. sizeof_client_key_der_2048)));
  29553. p = client_keypub_der_2048;
  29554. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29555. sizeof_client_keypub_der_2048)));
  29556. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29557. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  29558. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29559. NULL, privKey), 1);
  29560. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29561. (unsigned int)XSTRLEN(testData)), 1);
  29562. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29563. AssertIntEQ((int)checkSz, sz);
  29564. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29565. AssertIntEQ((int)checkSz,sz);
  29566. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  29567. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  29568. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy), 1);
  29569. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29570. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29571. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29572. NULL, pubKey), 1);
  29573. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29574. (unsigned int)XSTRLEN(testData)),
  29575. 1);
  29576. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29577. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29578. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29579. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29580. NULL, privKey), 1);
  29581. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29582. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29583. AssertIntEQ((int)checkSz, sz);
  29584. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29585. AssertIntEQ((int)checkSz, sz);
  29586. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29587. (unsigned int)XSTRLEN(testData) - 4), 1);
  29588. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29589. AssertIntEQ((int)checkSz, sz);
  29590. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29591. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29592. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29593. NULL, pubKey), 1);
  29594. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29595. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29596. (unsigned int)XSTRLEN(testData) - 4),
  29597. 1);
  29598. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29599. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29600. /* Check all signing padding types */
  29601. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  29602. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29603. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  29604. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  29605. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29606. paddings[i]), 1);
  29607. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29608. (unsigned int)XSTRLEN(testData)), 1);
  29609. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29610. AssertIntEQ((int)checkSz, sz);
  29611. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29612. AssertIntEQ((int)checkSz,sz);
  29613. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29614. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29615. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  29616. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  29617. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29618. paddings[i]), 1);
  29619. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29620. (unsigned int)XSTRLEN(testData)), 1);
  29621. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29622. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29623. }
  29624. wolfSSL_EVP_PKEY_free(pubKey);
  29625. wolfSSL_EVP_PKEY_free(privKey);
  29626. res = TEST_RES_CHECK(1);
  29627. #endif
  29628. return res;
  29629. }
  29630. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  29631. {
  29632. int res = TEST_SKIPPED;
  29633. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29634. WOLFSSL_EVP_PKEY* privKey;
  29635. WOLFSSL_EVP_PKEY* pubKey;
  29636. const char testData[] = "Hi There";
  29637. WOLFSSL_EVP_MD_CTX mdCtx;
  29638. size_t checkSz = -1;
  29639. const unsigned char* cp;
  29640. const unsigned char* p;
  29641. unsigned char check[2048/8];
  29642. cp = ecc_clikey_der_256;
  29643. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  29644. sizeof_ecc_clikey_der_256);
  29645. AssertNotNull(privKey);
  29646. p = ecc_clikeypub_der_256;
  29647. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29648. sizeof_ecc_clikeypub_der_256)));
  29649. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29650. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29651. NULL, privKey), 1);
  29652. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29653. (unsigned int)XSTRLEN(testData)), 1);
  29654. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29655. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29656. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29657. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29658. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29659. NULL, pubKey), 1);
  29660. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29661. (unsigned int)XSTRLEN(testData)),
  29662. 1);
  29663. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29664. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29665. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29666. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29667. NULL, privKey), 1);
  29668. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29669. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29670. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29671. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29672. (unsigned int)XSTRLEN(testData) - 4), 1);
  29673. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29674. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29675. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29676. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29677. NULL, pubKey), 1);
  29678. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29679. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29680. (unsigned int)XSTRLEN(testData) - 4),
  29681. 1);
  29682. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29683. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29684. wolfSSL_EVP_PKEY_free(pubKey);
  29685. wolfSSL_EVP_PKEY_free(privKey);
  29686. res = TEST_RES_CHECK(1);
  29687. #endif
  29688. return res;
  29689. }
  29690. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  29691. {
  29692. int res = TEST_SKIPPED;
  29693. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29694. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  29695. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29696. char caFile[] = "./certs/client-ca.pem";
  29697. char clientFile[] = "./certs/client-cert.pem";
  29698. SSL_CTX* ctx;
  29699. X509* x509;
  29700. BIO *bio = NULL;
  29701. X509 *cert = NULL;
  29702. X509 *ca;
  29703. STACK_OF(X509) *chain = NULL;
  29704. STACK_OF(X509) *chain2 = NULL;
  29705. #ifndef NO_WOLFSSL_SERVER
  29706. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29707. #else
  29708. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29709. #endif
  29710. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  29711. AssertNotNull(x509);
  29712. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  29713. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  29714. AssertNotNull(x509);
  29715. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  29716. /* additional test of getting EVP_PKEY key size from X509
  29717. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  29718. * allowed with user RSA */
  29719. {
  29720. EVP_PKEY* pkey;
  29721. #if defined(HAVE_ECC)
  29722. X509* ecX509;
  29723. #endif /* HAVE_ECC */
  29724. AssertNotNull(pkey = X509_get_pubkey(x509));
  29725. /* current RSA key is 2048 bit (256 bytes) */
  29726. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  29727. EVP_PKEY_free(pkey);
  29728. #if defined(HAVE_ECC)
  29729. #if defined(USE_CERT_BUFFERS_256)
  29730. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  29731. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  29732. SSL_FILETYPE_ASN1));
  29733. #else
  29734. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  29735. SSL_FILETYPE_PEM));
  29736. #endif
  29737. pkey = X509_get_pubkey(ecX509);
  29738. AssertNotNull(pkey);
  29739. /* current ECC key is 256 bit (32 bytes) */
  29740. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  29741. X509_free(ecX509);
  29742. EVP_PKEY_free(pkey);
  29743. #endif /* HAVE_ECC */
  29744. }
  29745. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  29746. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  29747. #ifdef WOLFSSL_ENCRYPTED_KEYS
  29748. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  29749. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29750. #endif
  29751. SSL_CTX_free(ctx);
  29752. #ifndef NO_WOLFSSL_SERVER
  29753. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29754. #else
  29755. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29756. #endif
  29757. /* Test haproxy use case */
  29758. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  29759. /* Read Certificate */
  29760. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  29761. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  29762. AssertNotNull(chain = sk_X509_new_null());
  29763. AssertIntEQ(sk_X509_push(chain, ca), 1);
  29764. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  29765. AssertNotNull(ca = sk_X509_shift(chain2));
  29766. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  29767. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  29768. BIO_free(bio);
  29769. X509_free(cert);
  29770. sk_X509_pop_free(chain, X509_free);
  29771. sk_X509_pop_free(chain2, X509_free);
  29772. SSL_CTX_free(ctx);
  29773. res = TEST_RES_CHECK(1);
  29774. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29775. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29776. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  29777. return res;
  29778. }
  29779. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  29780. static int test_wolfSSL_ERR_peek_last_error_line(void)
  29781. {
  29782. int res = TEST_SKIPPED;
  29783. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29784. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29785. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  29786. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  29787. tcp_ready ready;
  29788. func_args client_args;
  29789. func_args server_args;
  29790. #ifndef SINGLE_THREADED
  29791. THREAD_TYPE serverThread;
  29792. #endif
  29793. callback_functions client_cb;
  29794. callback_functions server_cb;
  29795. int line = 0;
  29796. int flag = ERR_TXT_STRING;
  29797. const char* file = NULL;
  29798. const char* data = NULL;
  29799. /* create a failed connection and inspect the error */
  29800. #ifdef WOLFSSL_TIRTOS
  29801. fdOpenSession(Task_self());
  29802. #endif
  29803. XMEMSET(&client_args, 0, sizeof(func_args));
  29804. XMEMSET(&server_args, 0, sizeof(func_args));
  29805. StartTCP();
  29806. InitTcpReady(&ready);
  29807. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29808. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29809. client_cb.method = wolfTLSv1_1_client_method;
  29810. server_cb.method = wolfTLSv1_2_server_method;
  29811. server_args.signal = &ready;
  29812. server_args.callbacks = &server_cb;
  29813. client_args.signal = &ready;
  29814. client_args.callbacks = &client_cb;
  29815. #ifndef SINGLE_THREADED
  29816. start_thread(test_server_nofail, &server_args, &serverThread);
  29817. wait_tcp_ready(&server_args);
  29818. test_client_nofail(&client_args, NULL);
  29819. join_thread(serverThread);
  29820. #endif
  29821. FreeTcpReady(&ready);
  29822. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  29823. AssertNotNull(data);
  29824. /* check clearing error state */
  29825. ERR_remove_state(0);
  29826. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29827. ERR_peek_last_error_line(NULL, &line);
  29828. AssertIntEQ(line, 0);
  29829. ERR_peek_last_error_line(&file, NULL);
  29830. AssertNull(file);
  29831. /* retry connection to fill error queue */
  29832. XMEMSET(&client_args, 0, sizeof(func_args));
  29833. XMEMSET(&server_args, 0, sizeof(func_args));
  29834. StartTCP();
  29835. InitTcpReady(&ready);
  29836. client_cb.method = wolfTLSv1_1_client_method;
  29837. server_cb.method = wolfTLSv1_2_server_method;
  29838. server_args.signal = &ready;
  29839. server_args.callbacks = &server_cb;
  29840. client_args.signal = &ready;
  29841. client_args.callbacks = &client_cb;
  29842. start_thread(test_server_nofail, &server_args, &serverThread);
  29843. wait_tcp_ready(&server_args);
  29844. test_client_nofail(&client_args, NULL);
  29845. join_thread(serverThread);
  29846. FreeTcpReady(&ready);
  29847. /* check that error code was stored */
  29848. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29849. ERR_peek_last_error_line(NULL, &line);
  29850. AssertIntNE(line, 0);
  29851. ERR_peek_last_error_line(&file, NULL);
  29852. AssertNotNull(file);
  29853. #ifdef WOLFSSL_TIRTOS
  29854. fdOpenSession(Task_self());
  29855. #endif
  29856. fprintf(stderr, "\nTesting error print out\n");
  29857. ERR_print_errors_fp(stderr);
  29858. fprintf(stderr, "Done testing print out\n\n");
  29859. res = TEST_RES_CHECK(1);
  29860. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29861. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  29862. return res;
  29863. }
  29864. #endif
  29865. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29866. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29867. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  29868. {
  29869. (void) ok;
  29870. (void) ctx;
  29871. fprintf(stderr, "ENTER verify_cb\n");
  29872. return SSL_SUCCESS;
  29873. }
  29874. #endif
  29875. static int test_wolfSSL_X509_Name_canon(void)
  29876. {
  29877. int res = TEST_SKIPPED;
  29878. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29879. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  29880. defined(WOLFSSL_CERT_GEN) && \
  29881. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  29882. const long ex_hash1 = 0x0fdb2da4;
  29883. const long ex_hash2 = 0x9f3e8c9e;
  29884. X509_NAME *name = NULL;
  29885. X509 *x509 = NULL;
  29886. FILE* file = NULL;
  29887. unsigned long hash = 0;
  29888. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  29889. byte *pbuf = NULL;
  29890. word32 len = 0;
  29891. (void) ex_hash2;
  29892. file = XFOPEN(caCertFile, "rb");
  29893. AssertNotNull(file);
  29894. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29895. AssertNotNull(name = X509_get_issuer_name(x509));
  29896. /* When output buffer is NULL, should return necessary output buffer
  29897. * length.*/
  29898. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  29899. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29900. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29901. hash = (((unsigned long)digest[3] << 24) |
  29902. ((unsigned long)digest[2] << 16) |
  29903. ((unsigned long)digest[1] << 8) |
  29904. ((unsigned long)digest[0]));
  29905. AssertIntEQ(hash, ex_hash1);
  29906. XFCLOSE(file);
  29907. X509_free(x509);
  29908. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29909. pbuf = NULL;
  29910. file = XFOPEN(cliCertFile, "rb");
  29911. AssertNotNull(file);
  29912. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29913. AssertNotNull(name = X509_get_issuer_name(x509));
  29914. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29915. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29916. hash = (((unsigned long)digest[3] << 24) |
  29917. ((unsigned long)digest[2] << 16) |
  29918. ((unsigned long)digest[1] << 8) |
  29919. ((unsigned long)digest[0]));
  29920. AssertIntEQ(hash, ex_hash2);
  29921. XFCLOSE(file);
  29922. X509_free(x509);
  29923. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29924. res = TEST_RES_CHECK(1);
  29925. #endif
  29926. return res;
  29927. }
  29928. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  29929. {
  29930. int res = TEST_SKIPPED;
  29931. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  29932. const int MAX_DIR = 4;
  29933. const char paths[][32] = {
  29934. "./certs/ed25519",
  29935. "./certs/ecc",
  29936. "./certs/crl",
  29937. "./certs/",
  29938. };
  29939. char CertCrl_path[MAX_FILENAME_SZ];
  29940. char *p;
  29941. X509_STORE* str;
  29942. X509_LOOKUP* lookup;
  29943. WOLFSSL_STACK* sk = NULL;
  29944. int len, total_len, i;
  29945. (void) sk;
  29946. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  29947. /* illegal string */
  29948. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29949. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29950. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  29951. SSL_FILETYPE_PEM,NULL), 0);
  29952. /* free store */
  29953. X509_STORE_free(str);
  29954. /* short folder string */
  29955. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29956. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29957. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  29958. SSL_FILETYPE_PEM,NULL), 1);
  29959. #if defined(WOLFSSL_INT_H)
  29960. /* only available when including internal.h */
  29961. AssertNotNull(sk = lookup->dirs->dir_entry);
  29962. #endif
  29963. /* free store */
  29964. X509_STORE_free(str);
  29965. /* typical function check */
  29966. p = &CertCrl_path[0];
  29967. total_len = 0;
  29968. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  29969. len = (int)XSTRLEN((const char*)&paths[i]);
  29970. total_len += len;
  29971. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  29972. p += len;
  29973. if (i != 0) *(p++) = SEPARATOR_CHAR;
  29974. }
  29975. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29976. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29977. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  29978. SSL_FILETYPE_PEM,NULL), 1);
  29979. #if defined(WOLFSSL_INT_H)
  29980. /* only available when including internal.h */
  29981. AssertNotNull(sk = lookup->dirs->dir_entry);
  29982. #endif
  29983. X509_STORE_free(str);
  29984. res = TEST_RES_CHECK(1);
  29985. #endif
  29986. return res;
  29987. }
  29988. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  29989. {
  29990. int res = TEST_SKIPPED;
  29991. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29992. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29993. defined(WOLFSSL_SIGNER_DER_CERT)
  29994. X509_STORE_CTX* ctx;
  29995. X509_STORE* str;
  29996. X509_LOOKUP* lookup;
  29997. X509* cert1;
  29998. X509* x509Ca;
  29999. X509* x509Svr;
  30000. X509* issuer;
  30001. WOLFSSL_STACK* sk = NULL;
  30002. X509_NAME* caName;
  30003. X509_NAME* issuerName;
  30004. FILE* file1 = NULL;
  30005. int i, cert_count, cmp;
  30006. char der[] = "certs/ca-cert.der";
  30007. #ifdef HAVE_CRL
  30008. char pem[][100] = {
  30009. "./certs/crl/crl.pem",
  30010. "./certs/crl/crl2.pem",
  30011. "./certs/crl/caEccCrl.pem",
  30012. "./certs/crl/eccCliCRL.pem",
  30013. "./certs/crl/eccSrvCRL.pem",
  30014. ""
  30015. };
  30016. #endif
  30017. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  30018. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  30019. fclose(file1);
  30020. AssertNotNull(ctx = X509_STORE_CTX_new());
  30021. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30022. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30023. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  30024. SSL_FILETYPE_PEM,NULL), 1);
  30025. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  30026. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  30027. /* check if CA cert is loaded into the store */
  30028. for (i = 0; i < cert_count; i++) {
  30029. x509Ca = sk_X509_value(sk, i);
  30030. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  30031. }
  30032. AssertNotNull((x509Svr =
  30033. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30034. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  30035. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  30036. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  30037. AssertNotNull(issuer);
  30038. caName = X509_get_subject_name(x509Ca);
  30039. AssertNotNull(caName);
  30040. issuerName = X509_get_subject_name(issuer);
  30041. AssertNotNull(issuerName);
  30042. cmp = X509_NAME_cmp(caName, issuerName);
  30043. AssertIntEQ(cmp, 0);
  30044. /* load der format */
  30045. X509_free(issuer);
  30046. X509_STORE_CTX_free(ctx);
  30047. X509_STORE_free(str);
  30048. sk_X509_pop_free(sk, NULL);
  30049. X509_free(x509Svr);
  30050. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30051. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30052. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  30053. SSL_FILETYPE_ASN1,NULL), 1);
  30054. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  30055. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  30056. /* check if CA cert is loaded into the store */
  30057. for (i = 0; i < cert_count; i++) {
  30058. x509Ca = sk_X509_value(sk, i);
  30059. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  30060. }
  30061. X509_STORE_free(str);
  30062. sk_X509_pop_free(sk, NULL);
  30063. X509_free(cert1);
  30064. #ifdef HAVE_CRL
  30065. AssertNotNull(str = wolfSSL_X509_STORE_new());
  30066. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30067. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  30068. SSL_FILETYPE_PEM,NULL), 1);
  30069. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  30070. "certs/server-revoked-cert.pem",
  30071. SSL_FILETYPE_PEM,NULL), 1);
  30072. if (str) {
  30073. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  30074. WOLFSSL_FILETYPE_PEM), 1);
  30075. /* since store hasn't yet known the revoked cert*/
  30076. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  30077. "certs/server-revoked-cert.pem",
  30078. WOLFSSL_FILETYPE_PEM), 1);
  30079. }
  30080. for (i = 0; pem[i][0] != '\0'; i++)
  30081. {
  30082. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  30083. SSL_FILETYPE_PEM, NULL), 1);
  30084. }
  30085. if (str) {
  30086. /* since store knows crl list */
  30087. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  30088. "certs/server-revoked-cert.pem",
  30089. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  30090. }
  30091. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  30092. X509_STORE_free(str);
  30093. #endif
  30094. res = TEST_RES_CHECK(1);
  30095. #endif
  30096. return res;
  30097. }
  30098. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  30099. {
  30100. int res = TEST_SKIPPED;
  30101. #if defined(OPENSSL_EXTRA)
  30102. X509_STORE_CTX_cleanup(NULL);
  30103. X509_STORE_CTX_trusted_stack(NULL, NULL);
  30104. AssertTrue(1); /* to confirm previous call gives no harm */
  30105. res = TEST_RES_CHECK(1);
  30106. #endif
  30107. return res;
  30108. }
  30109. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  30110. {
  30111. int res = TEST_SKIPPED;
  30112. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30113. #ifdef WOLFSSL_SIGNER_DER_CERT
  30114. int cmp;
  30115. #endif
  30116. X509_STORE_CTX* ctx;
  30117. X509_STORE* str;
  30118. X509* x509Ca;
  30119. X509* x509Svr;
  30120. X509* issuer;
  30121. X509_NAME* caName;
  30122. X509_NAME* issuerName;
  30123. AssertNotNull(ctx = X509_STORE_CTX_new());
  30124. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30125. AssertNotNull((x509Ca =
  30126. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  30127. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  30128. AssertNotNull((x509Svr =
  30129. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30130. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  30131. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  30132. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  30133. AssertNotNull(issuer);
  30134. caName = X509_get_subject_name(x509Ca);
  30135. AssertNotNull(caName);
  30136. issuerName = X509_get_subject_name(issuer);
  30137. AssertNotNull(issuerName);
  30138. #ifdef WOLFSSL_SIGNER_DER_CERT
  30139. cmp = X509_NAME_cmp(caName, issuerName);
  30140. AssertIntEQ(cmp, 0);
  30141. #endif
  30142. X509_free(issuer);
  30143. X509_STORE_CTX_free(ctx);
  30144. X509_free(x509Svr);
  30145. X509_STORE_free(str);
  30146. X509_free(x509Ca);
  30147. res = TEST_RES_CHECK(1);
  30148. #endif
  30149. return res;
  30150. }
  30151. static int test_wolfSSL_PKCS7_certs(void)
  30152. {
  30153. int res = TEST_SKIPPED;
  30154. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  30155. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  30156. STACK_OF(X509)* sk = NULL;
  30157. STACK_OF(X509_INFO)* info_sk = NULL;
  30158. PKCS7 *p7 = NULL;
  30159. BIO* bio;
  30160. const byte* p = NULL;
  30161. int buflen = 0;
  30162. int i;
  30163. /* Test twice. Once with d2i and once without to test
  30164. * that everything is free'd correctly. */
  30165. for (i = 0; i < 2; i++) {
  30166. AssertNotNull(p7 = PKCS7_new());
  30167. p7->version = 1;
  30168. #ifdef NO_SHA
  30169. p7->hashOID = SHA256h;
  30170. #else
  30171. p7->hashOID = SHAh;
  30172. #endif
  30173. AssertNotNull(bio = BIO_new(BIO_s_file()));
  30174. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30175. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  30176. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  30177. AssertNotNull(sk = sk_X509_new_null());
  30178. while (sk_X509_INFO_num(info_sk)) {
  30179. X509_INFO* info;
  30180. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  30181. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  30182. info->x509 = NULL;
  30183. X509_INFO_free(info);
  30184. }
  30185. sk_X509_INFO_free(info_sk);
  30186. BIO_free(bio);
  30187. bio = BIO_new(BIO_s_mem());
  30188. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  30189. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  30190. if (i == 0) {
  30191. PKCS7_free(p7);
  30192. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  30193. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  30194. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  30195. /* PKCS7_free free's the certs */
  30196. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  30197. }
  30198. BIO_free(bio);
  30199. PKCS7_free(p7);
  30200. }
  30201. res = TEST_RES_CHECK(1);
  30202. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30203. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  30204. return res;
  30205. }
  30206. static int test_wolfSSL_X509_STORE_CTX(void)
  30207. {
  30208. int res = TEST_SKIPPED;
  30209. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30210. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30211. X509_STORE_CTX* ctx;
  30212. X509_STORE* str;
  30213. X509* x509;
  30214. #ifdef OPENSSL_ALL
  30215. X509* x5092;
  30216. STACK_OF(X509) *sk, *sk2, *sk3;
  30217. #endif
  30218. AssertNotNull(ctx = X509_STORE_CTX_new());
  30219. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30220. AssertNotNull((x509 =
  30221. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30222. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  30223. #ifdef OPENSSL_ALL
  30224. /* sk_X509_new only in OPENSSL_ALL */
  30225. sk = sk_X509_new_null();
  30226. AssertNotNull(sk);
  30227. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  30228. #else
  30229. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  30230. #endif
  30231. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  30232. X509_STORE_CTX_set_error(ctx, -5);
  30233. X509_STORE_CTX_set_error(NULL, -5);
  30234. X509_STORE_CTX_free(ctx);
  30235. #ifdef OPENSSL_ALL
  30236. sk_X509_pop_free(sk, NULL);
  30237. #endif
  30238. X509_STORE_free(str);
  30239. X509_free(x509);
  30240. AssertNotNull(ctx = X509_STORE_CTX_new());
  30241. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  30242. X509_STORE_CTX_free(ctx);
  30243. #ifdef OPENSSL_ALL
  30244. /* test X509_STORE_CTX_get(1)_chain */
  30245. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  30246. SSL_FILETYPE_PEM)));
  30247. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  30248. SSL_FILETYPE_PEM)));
  30249. AssertNotNull((sk = sk_X509_new_null()));
  30250. AssertIntEQ(sk_X509_push(sk, x509), 1);
  30251. AssertNotNull((str = X509_STORE_new()));
  30252. AssertNotNull((ctx = X509_STORE_CTX_new()));
  30253. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  30254. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  30255. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  30256. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  30257. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  30258. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  30259. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  30260. X509_STORE_CTX_free(ctx);
  30261. X509_STORE_free(str);
  30262. /* CTX certs not freed yet */
  30263. X509_free(x5092);
  30264. sk_X509_pop_free(sk, NULL);
  30265. /* sk3 is dup so free here */
  30266. sk_X509_pop_free(sk3, NULL);
  30267. #endif
  30268. /* test X509_STORE_CTX_get/set_ex_data */
  30269. {
  30270. int i = 0, tmpData = 5;
  30271. void* tmpDataRet;
  30272. AssertNotNull(ctx = X509_STORE_CTX_new());
  30273. #ifdef HAVE_EX_DATA
  30274. for (i = 0; i < MAX_EX_DATA; i++) {
  30275. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  30276. WOLFSSL_SUCCESS);
  30277. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  30278. AssertNotNull(tmpDataRet);
  30279. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  30280. }
  30281. #else
  30282. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  30283. WOLFSSL_FAILURE);
  30284. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  30285. AssertNull(tmpDataRet);
  30286. #endif
  30287. X509_STORE_CTX_free(ctx);
  30288. }
  30289. /* test X509_STORE_get/set_ex_data */
  30290. {
  30291. int i = 0, tmpData = 99;
  30292. void* tmpDataRet;
  30293. AssertNotNull(str = X509_STORE_new());
  30294. #ifdef HAVE_EX_DATA
  30295. for (i = 0; i < MAX_EX_DATA; i++) {
  30296. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  30297. WOLFSSL_SUCCESS);
  30298. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  30299. AssertNotNull(tmpDataRet);
  30300. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  30301. }
  30302. #else
  30303. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  30304. WOLFSSL_FAILURE);
  30305. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  30306. AssertNull(tmpDataRet);
  30307. #endif
  30308. X509_STORE_free(str);
  30309. }
  30310. res = TEST_RES_CHECK(1);
  30311. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30312. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30313. return res;
  30314. }
  30315. static int test_wolfSSL_X509_STORE_set_flags(void)
  30316. {
  30317. int res = TEST_SKIPPED;
  30318. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30319. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30320. X509_STORE* store;
  30321. X509* x509;
  30322. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  30323. AssertNotNull((x509 =
  30324. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  30325. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  30326. #ifdef HAVE_CRL
  30327. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  30328. #else
  30329. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  30330. NOT_COMPILED_IN);
  30331. #endif
  30332. wolfSSL_X509_free(x509);
  30333. wolfSSL_X509_STORE_free(store);
  30334. res = TEST_RES_CHECK(1);
  30335. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30336. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30337. return res;
  30338. }
  30339. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  30340. {
  30341. int res = TEST_SKIPPED;
  30342. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  30343. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  30344. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  30345. WOLFSSL_X509_STORE* store;
  30346. WOLFSSL_X509_LOOKUP* lookup;
  30347. AssertNotNull(store = wolfSSL_X509_STORE_new());
  30348. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  30349. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  30350. X509_FILETYPE_PEM), 1);
  30351. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  30352. X509_FILETYPE_PEM), 1);
  30353. if (store) {
  30354. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  30355. WOLFSSL_FILETYPE_PEM), 1);
  30356. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  30357. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  30358. }
  30359. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  30360. X509_FILETYPE_PEM), 1);
  30361. if (store) {
  30362. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  30363. WOLFSSL_FILETYPE_PEM), 1);
  30364. }
  30365. wolfSSL_X509_STORE_free(store);
  30366. res = TEST_RES_CHECK(1);
  30367. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  30368. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30369. return res;
  30370. }
  30371. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  30372. {
  30373. int res = TEST_SKIPPED;
  30374. #if defined(OPENSSL_EXTRA)
  30375. WOLFSSL_X509_STORE_CTX* ctx;
  30376. time_t c_time;
  30377. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  30378. c_time = 365*24*60*60;
  30379. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  30380. AssertTrue(
  30381. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  30382. AssertTrue(ctx->param->check_time == c_time);
  30383. wolfSSL_X509_STORE_CTX_free(ctx);
  30384. res = TEST_RES_CHECK(1);
  30385. #endif /* OPENSSL_EXTRA */
  30386. return res;
  30387. }
  30388. static int test_wolfSSL_CTX_get0_set1_param(void)
  30389. {
  30390. int res = TEST_SKIPPED;
  30391. #if defined(OPENSSL_EXTRA)
  30392. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30393. int ret;
  30394. SSL_CTX* ctx;
  30395. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30396. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  30397. char testIPv4[] = "127.0.0.1";
  30398. char testhostName[] = "foo.hoge.com";
  30399. #ifndef NO_WOLFSSL_SERVER
  30400. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30401. #else
  30402. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30403. #endif
  30404. AssertNull(SSL_CTX_get0_param(NULL));
  30405. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  30406. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  30407. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  30408. AssertNotNull(pvpm);
  30409. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30410. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  30411. (int)XSTRLEN(testhostName));
  30412. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  30413. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  30414. ret = SSL_CTX_set1_param(ctx, pvpm);
  30415. AssertIntEQ(1, ret);
  30416. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  30417. (int)XSTRLEN(testhostName)));
  30418. AssertIntEQ(0x01, pParam->hostFlags);
  30419. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  30420. /* test for incorrect patameter */
  30421. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  30422. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  30423. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  30424. SSL_CTX_free(ctx);
  30425. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  30426. res = TEST_RES_CHECK(1);
  30427. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30428. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  30429. return res;
  30430. }
  30431. static int test_wolfSSL_get0_param(void)
  30432. {
  30433. int res = TEST_SKIPPED;
  30434. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30435. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30436. SSL_CTX* ctx;
  30437. SSL* ssl;
  30438. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30439. #ifndef NO_WOLFSSL_SERVER
  30440. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30441. #else
  30442. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30443. #endif
  30444. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30445. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30446. AssertNotNull(ssl = SSL_new(ctx));
  30447. pParam = SSL_get0_param(ssl);
  30448. (void)pParam;
  30449. SSL_free(ssl);
  30450. SSL_CTX_free(ctx);
  30451. res = TEST_RES_CHECK(1);
  30452. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30453. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  30454. return res;
  30455. }
  30456. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  30457. {
  30458. int res = TEST_SKIPPED;
  30459. #if defined(OPENSSL_EXTRA)
  30460. const char host[] = "www.example.com";
  30461. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30462. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  30463. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  30464. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  30465. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30466. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  30467. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30468. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30469. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30470. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30471. res = TEST_RES_CHECK(1);
  30472. #endif /* OPENSSL_EXTRA */
  30473. return res;
  30474. }
  30475. static int test_wolfSSL_set1_host(void)
  30476. {
  30477. int res = TEST_SKIPPED;
  30478. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30479. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30480. const char host[] = "www.test_wolfSSL_set1_host.com";
  30481. const char emptyStr[] = "";
  30482. SSL_CTX* ctx;
  30483. SSL* ssl;
  30484. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30485. #ifndef NO_WOLFSSL_SERVER
  30486. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30487. #else
  30488. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30489. #endif
  30490. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30491. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30492. AssertNotNull(ssl = SSL_new(ctx));
  30493. pParam = SSL_get0_param(ssl);
  30494. /* we should get back host string */
  30495. SSL_set1_host(ssl, host);
  30496. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30497. /* we should get back empty string */
  30498. SSL_set1_host(ssl, emptyStr);
  30499. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  30500. /* we should get back host string */
  30501. SSL_set1_host(ssl, host);
  30502. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30503. /* we should get back empty string */
  30504. SSL_set1_host(ssl, NULL);
  30505. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  30506. SSL_free(ssl);
  30507. SSL_CTX_free(ctx);
  30508. res = TEST_RES_CHECK(1);
  30509. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30510. #endif /* OPENSSL_EXTRA */
  30511. return res;
  30512. }
  30513. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  30514. {
  30515. int res = TEST_SKIPPED;
  30516. #if defined(OPENSSL_EXTRA)
  30517. unsigned char buf[16] = {0};
  30518. WOLFSSL_X509_VERIFY_PARAM* param;
  30519. AssertNotNull(param = X509_VERIFY_PARAM_new());
  30520. /* test 127.0.0.1 */
  30521. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  30522. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  30523. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  30524. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  30525. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30526. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  30527. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  30528. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  30529. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30530. AssertIntEQ(XSTRNCMP(param->ipasc,
  30531. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  30532. /* test 2001:db8:: */
  30533. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30534. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30535. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30536. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  30537. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30538. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  30539. /* test ::1234:5678 */
  30540. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  30541. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30542. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30543. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  30544. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30545. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  30546. /* test 2001:db8::1234:5678 */
  30547. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30548. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30549. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30550. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  30551. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30552. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  30553. sizeof(param->ipasc)), 0);
  30554. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  30555. /* 2001:db8:1::ab9:c0a8:102 */
  30556. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30557. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  30558. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  30559. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  30560. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30561. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  30562. sizeof(param->ipasc)), 0);
  30563. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30564. res = TEST_RES_CHECK(1);
  30565. #endif /* OPENSSL_EXTRA */
  30566. return res;
  30567. }
  30568. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  30569. {
  30570. int res = TEST_SKIPPED;
  30571. #if defined(OPENSSL_EXTRA)
  30572. X509_STORE* store;
  30573. X509_STORE_CTX* ctx;
  30574. X509_STORE_CTX* ctx_no_init;
  30575. AssertNotNull((store = X509_STORE_new()));
  30576. AssertNotNull(ctx = X509_STORE_CTX_new());
  30577. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  30578. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  30579. AssertNull(X509_STORE_CTX_get0_store(NULL));
  30580. /* should return NULL if ctx has not bee initialized */
  30581. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  30582. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  30583. wolfSSL_X509_STORE_CTX_free(ctx);
  30584. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  30585. X509_STORE_free(store);
  30586. res = TEST_RES_CHECK(1);
  30587. #endif /* OPENSSL_EXTRA */
  30588. return res;
  30589. }
  30590. static int test_wolfSSL_CTX_set_client_CA_list(void)
  30591. {
  30592. int res = TEST_SKIPPED;
  30593. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30594. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  30595. WOLFSSL_CTX* ctx;
  30596. WOLFSSL* ssl;
  30597. X509_NAME* name = NULL;
  30598. STACK_OF(X509_NAME)* names = NULL;
  30599. STACK_OF(X509_NAME)* ca_list = NULL;
  30600. int i, names_len;
  30601. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30602. /* Send two X501 names in cert request */
  30603. names = SSL_load_client_CA_file(cliCertFile);
  30604. AssertNotNull(names);
  30605. ca_list = SSL_load_client_CA_file(caCertFile);
  30606. AssertNotNull(ca_list);
  30607. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  30608. SSL_CTX_set_client_CA_list(ctx, names);
  30609. /* This should only free the stack structure */
  30610. sk_X509_NAME_free(ca_list);
  30611. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30612. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30613. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30614. for (i=0; i<names_len; i++) {
  30615. AssertNotNull(name = sk_X509_NAME_value(names, i));
  30616. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  30617. }
  30618. /* Needed to be able to create ssl object */
  30619. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30620. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30621. AssertNotNull(ssl = wolfSSL_new(ctx));
  30622. /* load again as old names are responsibility of ctx to free*/
  30623. names = SSL_load_client_CA_file(cliCertFile);
  30624. AssertNotNull(names);
  30625. SSL_set_client_CA_list(ssl, names);
  30626. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  30627. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30628. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30629. for (i=0; i<names_len; i++) {
  30630. AssertNotNull(name = sk_X509_NAME_value(names, i));
  30631. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  30632. }
  30633. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  30634. {
  30635. tcp_ready ready;
  30636. func_args server_args;
  30637. callback_functions server_cb;
  30638. THREAD_TYPE serverThread;
  30639. WOLFSSL* ssl_client;
  30640. WOLFSSL_CTX* ctx_client;
  30641. SOCKET_T sockfd = 0;
  30642. /* wolfSSL_get_client_CA_list() with handshake */
  30643. StartTCP();
  30644. InitTcpReady(&ready);
  30645. XMEMSET(&server_args, 0, sizeof(func_args));
  30646. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30647. server_args.signal = &ready;
  30648. server_args.callbacks = &server_cb;
  30649. /* we are responsible for free'ing WOLFSSL_CTX */
  30650. server_cb.ctx = ctx;
  30651. server_cb.isSharedCtx = 1;
  30652. AssertIntEQ(WOLFSSL_SUCCESS,
  30653. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30654. start_thread(test_server_nofail, &server_args, &serverThread);
  30655. wait_tcp_ready(&server_args);
  30656. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30657. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30658. AssertIntEQ(WOLFSSL_SUCCESS,
  30659. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  30660. AssertIntEQ(WOLFSSL_SUCCESS,
  30661. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  30662. AssertIntEQ(WOLFSSL_SUCCESS,
  30663. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  30664. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  30665. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  30666. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  30667. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  30668. /* We are expecting two cert names to be sent */
  30669. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  30670. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  30671. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  30672. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  30673. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  30674. }
  30675. wolfSSL_shutdown(ssl_client);
  30676. wolfSSL_free(ssl_client);
  30677. wolfSSL_CTX_free(ctx_client);
  30678. join_thread(serverThread);
  30679. FreeTcpReady(&ready);
  30680. }
  30681. #endif
  30682. wolfSSL_free(ssl);
  30683. wolfSSL_CTX_free(ctx);
  30684. res = TEST_RES_CHECK(1);
  30685. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  30686. * !NO_BIO */
  30687. return res;
  30688. }
  30689. static int test_wolfSSL_CTX_add_client_CA(void)
  30690. {
  30691. int res = TEST_SKIPPED;
  30692. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30693. !defined(NO_WOLFSSL_CLIENT)
  30694. WOLFSSL_CTX* ctx;
  30695. WOLFSSL_X509* x509;
  30696. WOLFSSL_X509* x509_a;
  30697. STACK_OF(X509_NAME)* ca_list;
  30698. int ret = 0;
  30699. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30700. /* Add client cert */
  30701. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  30702. AssertNotNull(x509);
  30703. ret = SSL_CTX_add_client_CA(ctx, x509);
  30704. AssertIntEQ(ret, SSL_SUCCESS);
  30705. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30706. /* Add another client cert */
  30707. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  30708. SSL_FILETYPE_PEM));
  30709. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  30710. /* test for incorrect parameter */
  30711. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  30712. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  30713. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  30714. X509_free(x509);
  30715. X509_free(x509_a);
  30716. SSL_CTX_free(ctx);
  30717. res = TEST_RES_CHECK(1);
  30718. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  30719. return res;
  30720. }
  30721. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  30722. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  30723. {
  30724. callback_functions* callbacks = ((func_args*)args)->callbacks;
  30725. WOLFSSL_CTX* ctx = callbacks->ctx;
  30726. WOLFSSL* ssl = NULL;
  30727. SOCKET_T sfd = 0;
  30728. SOCKET_T cfd = 0;
  30729. word16 port;
  30730. char input[1024];
  30731. int idx;
  30732. int ret, err = 0;
  30733. const char* privateName = "ech-private-name.com";
  30734. int privateNameLen = (int)XSTRLEN(privateName);
  30735. ((func_args*)args)->return_code = TEST_FAIL;
  30736. port = ((func_args*)args)->signal->port;
  30737. AssertIntEQ(WOLFSSL_SUCCESS,
  30738. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30739. AssertIntEQ(WOLFSSL_SUCCESS,
  30740. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30741. WOLFSSL_FILETYPE_PEM));
  30742. AssertIntEQ(WOLFSSL_SUCCESS,
  30743. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30744. WOLFSSL_FILETYPE_PEM));
  30745. if (callbacks->ctx_ready)
  30746. callbacks->ctx_ready(ctx);
  30747. ssl = wolfSSL_new(ctx);
  30748. /* set the sni for the server */
  30749. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  30750. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  30751. CloseSocket(sfd);
  30752. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  30753. if (callbacks->ssl_ready)
  30754. callbacks->ssl_ready(ssl);
  30755. do {
  30756. err = 0; /* Reset error */
  30757. ret = wolfSSL_accept(ssl);
  30758. if (ret != WOLFSSL_SUCCESS) {
  30759. err = wolfSSL_get_error(ssl, 0);
  30760. }
  30761. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  30762. if (ret != WOLFSSL_SUCCESS) {
  30763. char buff[WOLFSSL_MAX_ERROR_SZ];
  30764. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  30765. }
  30766. else {
  30767. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  30768. input[idx] = 0;
  30769. printf("Client message: %s\n", input);
  30770. }
  30771. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  30772. privateNameLen));
  30773. ((func_args*)args)->return_code = TEST_SUCCESS;
  30774. }
  30775. if (callbacks->on_result)
  30776. callbacks->on_result(ssl);
  30777. wolfSSL_shutdown(ssl);
  30778. wolfSSL_free(ssl);
  30779. wolfSSL_CTX_free(ctx);
  30780. CloseSocket(cfd);
  30781. #ifdef FP_ECC
  30782. wc_ecc_fp_free();
  30783. #endif
  30784. return 0;
  30785. }
  30786. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  30787. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30788. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  30789. {
  30790. callback_functions* callbacks = ((func_args*)args)->callbacks;
  30791. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  30792. WOLFSSL* ssl = NULL;
  30793. SOCKET_T sfd = 0;
  30794. SOCKET_T cfd = 0;
  30795. word16 port;
  30796. char msg[] = "I hear you fa shizzle!";
  30797. int len = (int) XSTRLEN(msg);
  30798. char input[1024];
  30799. int idx;
  30800. int ret, err = 0;
  30801. #ifdef WOLFSSL_TIRTOS
  30802. fdOpenSession(Task_self());
  30803. #endif
  30804. ((func_args*)args)->return_code = TEST_FAIL;
  30805. port = ((func_args*)args)->signal->port;
  30806. AssertIntEQ(WOLFSSL_SUCCESS,
  30807. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30808. AssertIntEQ(WOLFSSL_SUCCESS,
  30809. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30810. WOLFSSL_FILETYPE_PEM));
  30811. AssertIntEQ(WOLFSSL_SUCCESS,
  30812. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30813. WOLFSSL_FILETYPE_PEM));
  30814. if (callbacks->ctx_ready)
  30815. callbacks->ctx_ready(ctx);
  30816. ssl = wolfSSL_new(ctx);
  30817. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  30818. CloseSocket(sfd);
  30819. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  30820. if (callbacks->ssl_ready)
  30821. callbacks->ssl_ready(ssl);
  30822. do {
  30823. err = 0; /* Reset error */
  30824. ret = wolfSSL_accept(ssl);
  30825. if (ret != WOLFSSL_SUCCESS) {
  30826. err = wolfSSL_get_error(ssl, 0);
  30827. }
  30828. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  30829. if (ret != WOLFSSL_SUCCESS) {
  30830. char buff[WOLFSSL_MAX_ERROR_SZ];
  30831. fprintf(stderr, "error = %d, %s\n", err,
  30832. wolfSSL_ERR_error_string(err, buff));
  30833. }
  30834. else {
  30835. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  30836. input[idx] = 0;
  30837. fprintf(stderr, "Client message: %s\n", input);
  30838. }
  30839. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  30840. #ifdef WOLFSSL_TIRTOS
  30841. Task_yield();
  30842. #endif
  30843. ((func_args*)args)->return_code = TEST_SUCCESS;
  30844. }
  30845. if (callbacks->on_result)
  30846. callbacks->on_result(ssl);
  30847. wolfSSL_shutdown(ssl);
  30848. wolfSSL_free(ssl);
  30849. wolfSSL_CTX_free(ctx);
  30850. CloseSocket(cfd);
  30851. #ifdef WOLFSSL_TIRTOS
  30852. fdCloseSession(Task_self());
  30853. #endif
  30854. #ifndef WOLFSSL_TIRTOS
  30855. return 0;
  30856. #endif
  30857. }
  30858. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  30859. {
  30860. AssertNotNull(ssl);
  30861. AssertNotNull(line);
  30862. XFILE fp;
  30863. const byte lf = '\n';
  30864. fp = XFOPEN("./MyKeyLog.txt", "a");
  30865. XFWRITE( line, 1, strlen(line),fp);
  30866. XFWRITE( (void*)&lf,1,1,fp);
  30867. XFCLOSE(fp);
  30868. }
  30869. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30870. static int test_wolfSSL_CTX_set_keylog_callback(void)
  30871. {
  30872. int res = TEST_SKIPPED;
  30873. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30874. !defined(NO_WOLFSSL_CLIENT)
  30875. SSL_CTX* ctx;
  30876. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30877. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30878. SSL_CTX_free(ctx);
  30879. SSL_CTX_set_keylog_callback(NULL, NULL);
  30880. res = TEST_RES_CHECK(1);
  30881. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30882. return res;
  30883. }
  30884. static int test_wolfSSL_CTX_get_keylog_callback(void)
  30885. {
  30886. int res = TEST_SKIPPED;
  30887. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30888. !defined(NO_WOLFSSL_CLIENT)
  30889. SSL_CTX* ctx;
  30890. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30891. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30892. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30893. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  30894. SSL_CTX_set_keylog_callback(ctx, NULL );
  30895. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30896. SSL_CTX_free(ctx);
  30897. res = TEST_RES_CHECK(1);
  30898. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30899. return res;
  30900. }
  30901. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  30902. {
  30903. int res = TEST_SKIPPED;
  30904. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30905. /* This test is intended for checking whether keylog callback is called
  30906. * in client during TLS handshake between the client and a server.
  30907. */
  30908. tcp_ready ready;
  30909. func_args client_args;
  30910. func_args server_args;
  30911. THREAD_TYPE serverThread;
  30912. callback_functions server_cbf;
  30913. callback_functions client_cbf;
  30914. SOCKET_T sockfd = 0;
  30915. WOLFSSL_CTX* ctx;
  30916. WOLFSSL* ssl;
  30917. XFILE fp;
  30918. char msg[64] = "hello wolfssl!";
  30919. char reply[1024];
  30920. int msgSz = (int)XSTRLEN(msg);
  30921. #ifdef WOLFSSL_TIRTOS
  30922. fdOpenSession(Task_self());
  30923. #endif
  30924. InitTcpReady(&ready);
  30925. ready.port = 22222;
  30926. XMEMSET(&client_args, 0, sizeof(func_args));
  30927. XMEMSET(&server_args, 0, sizeof(func_args));
  30928. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30929. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30930. server_cbf.method = wolfTLSv1_2_server_method;
  30931. server_args.callbacks = &server_cbf;
  30932. server_args.signal = &ready;
  30933. /* clean up keylog file */
  30934. fp = XFOPEN("./MyKeyLog.txt", "w");
  30935. XFCLOSE(fp);
  30936. /* start server task */
  30937. start_thread(server_task, &server_args, &serverThread);
  30938. wait_tcp_ready(&server_args);
  30939. /* run as a TLS1.2 client */
  30940. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30941. AssertIntEQ(WOLFSSL_SUCCESS,
  30942. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30943. AssertIntEQ(WOLFSSL_SUCCESS,
  30944. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30945. AssertIntEQ(WOLFSSL_SUCCESS,
  30946. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30947. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30948. /* set keylog callback */
  30949. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  30950. /* get connected the server task */
  30951. AssertNotNull(ssl = wolfSSL_new(ctx));
  30952. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30953. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30954. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  30955. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  30956. wolfSSL_shutdown(ssl);
  30957. wolfSSL_free(ssl);
  30958. wolfSSL_CTX_free(ctx);
  30959. CloseSocket(sockfd);
  30960. join_thread(serverThread);
  30961. FreeTcpReady(&ready);
  30962. #ifdef WOLFSSL_TIRTOS
  30963. fdOpenSession(Task_self());
  30964. #endif
  30965. /* check if the keylog file exists */
  30966. char buff[300] = {0};
  30967. int found = 0;
  30968. fp = XFOPEN("./MyKeyLog.txt", "r");
  30969. AssertNotNull(fp);
  30970. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  30971. if (0 == strncmp(buff,"CLIENT_RANDOM ",
  30972. sizeof("CLIENT_RANDOM ")-1)) {
  30973. found = 1;
  30974. break;
  30975. }
  30976. }
  30977. XFCLOSE(fp);
  30978. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  30979. AssertNotNull( found );
  30980. res = TEST_RES_CHECK(1);
  30981. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30982. return res;
  30983. }
  30984. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  30985. {
  30986. int res = TEST_SKIPPED;
  30987. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  30988. defined(HAVE_SECRET_CALLBACK)
  30989. /* This test is intended for checking whether keylog callback is called
  30990. * in client during TLS handshake between the client and a server.
  30991. */
  30992. tcp_ready ready;
  30993. func_args client_args;
  30994. func_args server_args;
  30995. THREAD_TYPE serverThread;
  30996. callback_functions server_cbf;
  30997. callback_functions client_cbf;
  30998. SOCKET_T sockfd = 0;
  30999. WOLFSSL_CTX* ctx;
  31000. WOLFSSL* ssl;
  31001. XFILE fp;
  31002. char msg[64] = "hello wolfssl!";
  31003. char reply[1024];
  31004. int msgSz = (int)XSTRLEN(msg);
  31005. #ifdef WOLFSSL_TIRTOS
  31006. fdOpenSession(Task_self());
  31007. #endif
  31008. InitTcpReady(&ready);
  31009. ready.port = 22222;
  31010. XMEMSET(&client_args, 0, sizeof(func_args));
  31011. XMEMSET(&server_args, 0, sizeof(func_args));
  31012. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  31013. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  31014. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  31015. server_args.callbacks = &server_cbf;
  31016. server_args.signal = &ready;
  31017. /* clean up keylog file */
  31018. fp = XFOPEN("./MyKeyLog.txt", "w");
  31019. XFCLOSE(fp);
  31020. /* start server task */
  31021. start_thread(server_task, &server_args, &serverThread);
  31022. wait_tcp_ready(&server_args);
  31023. /* run as a TLS1.3 client */
  31024. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  31025. AssertIntEQ(WOLFSSL_SUCCESS,
  31026. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  31027. AssertIntEQ(WOLFSSL_SUCCESS,
  31028. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  31029. AssertIntEQ(WOLFSSL_SUCCESS,
  31030. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  31031. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  31032. /* set keylog callback */
  31033. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  31034. /* get connected the server task */
  31035. AssertNotNull(ssl = wolfSSL_new(ctx));
  31036. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  31037. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  31038. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  31039. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  31040. wolfSSL_free(ssl);
  31041. wolfSSL_CTX_free(ctx);
  31042. join_thread(serverThread);
  31043. FreeTcpReady(&ready);
  31044. #ifdef WOLFSSL_TIRTOS
  31045. fdOpenSession(Task_self());
  31046. #endif
  31047. /* check if the keylog file exists */
  31048. {
  31049. char buff[300] = {0};
  31050. int found[4] = {0};
  31051. int numfnd = 0;
  31052. int i;
  31053. fp = XFOPEN("./MyKeyLog.txt", "r");
  31054. AssertNotNull(fp);
  31055. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  31056. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  31057. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  31058. found[0] = 1;
  31059. continue;
  31060. }
  31061. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  31062. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  31063. found[1] = 1;
  31064. continue;
  31065. }
  31066. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  31067. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  31068. found[2] = 1;
  31069. continue;
  31070. }
  31071. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  31072. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  31073. found[3] = 1;
  31074. continue;
  31075. }
  31076. }
  31077. XFCLOSE(fp);
  31078. for (i = 0; i < 4; i++) {
  31079. if (found[i] != 0)
  31080. numfnd++;
  31081. }
  31082. AssertIntEQ(numfnd, 4);
  31083. }
  31084. res = TEST_RES_CHECK(1);
  31085. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  31086. return res;
  31087. }
  31088. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  31089. static int test_wolfSSL_Tls13_ECH_params(void)
  31090. {
  31091. #if !defined(NO_WOLFSSL_CLIENT)
  31092. word32 outputLen = 0;
  31093. byte testBuf[72];
  31094. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  31095. WOLFSSL *ssl = wolfSSL_new(ctx);
  31096. AssertNotNull(ctx);
  31097. AssertNotNull(ssl);
  31098. /* invalid ctx */
  31099. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  31100. "ech-public-name.com", 0, 0, 0));
  31101. /* invalid public name */
  31102. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  31103. 0, 0));
  31104. /* invalid algorithms */
  31105. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  31106. "ech-public-name.com", 1000, 1000, 1000));
  31107. /* invalid ctx */
  31108. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  31109. &outputLen));
  31110. /* invalid output len */
  31111. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  31112. /* invalid ssl */
  31113. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  31114. (char*)testBuf, sizeof(testBuf)));
  31115. /* invalid configs64 */
  31116. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  31117. sizeof(testBuf)));
  31118. /* invalid size */
  31119. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  31120. (char*)testBuf, 0));
  31121. /* invalid ssl */
  31122. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  31123. sizeof(testBuf)));
  31124. /* invalid configs */
  31125. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  31126. sizeof(testBuf)));
  31127. /* invalid size */
  31128. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  31129. /* invalid ssl */
  31130. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  31131. /* invalid size */
  31132. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  31133. wolfSSL_free(ssl);
  31134. wolfSSL_CTX_free(ctx);
  31135. #endif /* !NO_WOLFSSL_CLIENT */
  31136. return TEST_SUCCESS;
  31137. }
  31138. static int test_wolfSSL_Tls13_ECH(void)
  31139. {
  31140. tcp_ready ready;
  31141. func_args client_args;
  31142. func_args server_args;
  31143. THREAD_TYPE serverThread;
  31144. callback_functions server_cbf;
  31145. callback_functions client_cbf;
  31146. SOCKET_T sockfd = 0;
  31147. WOLFSSL_CTX* ctx;
  31148. WOLFSSL* ssl;
  31149. const char* publicName = "ech-public-name.com";
  31150. const char* privateName = "ech-private-name.com";
  31151. int privateNameLen = 20;
  31152. char reply[1024];
  31153. int replyLen = 0;
  31154. byte rawEchConfig[128];
  31155. word32 rawEchConfigLen = sizeof(rawEchConfig);
  31156. InitTcpReady(&ready);
  31157. ready.port = 22222;
  31158. XMEMSET(&client_args, 0, sizeof(func_args));
  31159. XMEMSET(&server_args, 0, sizeof(func_args));
  31160. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  31161. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  31162. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  31163. /* create the server context here so we can get the ech config */
  31164. AssertNotNull(server_cbf.ctx =
  31165. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  31166. /* generate ech config */
  31167. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  31168. publicName, 0, 0, 0));
  31169. /* get the config for the client to use */
  31170. AssertIntEQ(WOLFSSL_SUCCESS,
  31171. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  31172. &rawEchConfigLen));
  31173. server_args.callbacks = &server_cbf;
  31174. server_args.signal = &ready;
  31175. /* start server task */
  31176. start_thread(server_task_ech, &server_args, &serverThread);
  31177. wait_tcp_ready(&server_args);
  31178. /* run as a TLS1.3 client */
  31179. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  31180. AssertIntEQ(WOLFSSL_SUCCESS,
  31181. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  31182. AssertIntEQ(WOLFSSL_SUCCESS,
  31183. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  31184. AssertIntEQ(WOLFSSL_SUCCESS,
  31185. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  31186. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  31187. /* get connected the server task */
  31188. AssertNotNull(ssl = wolfSSL_new(ctx));
  31189. /* set the ech configs for the client */
  31190. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  31191. rawEchConfigLen));
  31192. /* set the sni for the client */
  31193. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  31194. privateName, privateNameLen));
  31195. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  31196. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  31197. AssertIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  31198. privateNameLen);
  31199. AssertIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  31200. /* add th null terminator for string compare */
  31201. reply[replyLen] = 0;
  31202. /* check that the server replied with the private name */
  31203. AssertStrEQ(privateName, reply);
  31204. wolfSSL_free(ssl);
  31205. wolfSSL_CTX_free(ctx);
  31206. join_thread(serverThread);
  31207. FreeTcpReady(&ready);
  31208. return TEST_SUCCESS;
  31209. }
  31210. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  31211. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31212. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31213. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  31214. static void post_auth_version_cb(WOLFSSL* ssl)
  31215. {
  31216. /* do handshake and then test version error */
  31217. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  31218. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  31219. }
  31220. static void post_auth_version_client_cb(WOLFSSL* ssl)
  31221. {
  31222. /* do handshake and then test version error */
  31223. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  31224. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  31225. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  31226. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  31227. /* check was added to error queue */
  31228. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  31229. /* check the string matches expected string */
  31230. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  31231. "WRONG_SSL_VERSION");
  31232. #endif
  31233. }
  31234. static void post_auth_cb(WOLFSSL* ssl)
  31235. {
  31236. WOLFSSL_X509* x509;
  31237. /* do handshake and then test version error */
  31238. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  31239. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  31240. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  31241. wolfSSL_X509_free(x509);
  31242. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  31243. }
  31244. static void set_post_auth_cb(WOLFSSL* ssl)
  31245. {
  31246. if (!wolfSSL_is_server(ssl)) {
  31247. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  31248. }
  31249. else {
  31250. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  31251. }
  31252. }
  31253. #endif
  31254. static int test_wolfSSL_Tls13_postauth(void)
  31255. {
  31256. int res = TEST_SKIPPED;
  31257. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31258. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31259. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  31260. tcp_ready ready;
  31261. func_args client_args;
  31262. func_args server_args;
  31263. callback_functions server_cbf;
  31264. callback_functions client_cbf;
  31265. THREAD_TYPE serverThread;
  31266. XMEMSET(&client_args, 0, sizeof(func_args));
  31267. XMEMSET(&server_args, 0, sizeof(func_args));
  31268. StartTCP();
  31269. InitTcpReady(&ready);
  31270. #if defined(USE_WINDOWS_API)
  31271. /* use RNG to get random port if using windows */
  31272. ready.port = GetRandomPort();
  31273. #endif
  31274. server_args.signal = &ready;
  31275. client_args.signal = &ready;
  31276. /* test version failure doing post auth with TLS 1.2 connection */
  31277. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  31278. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  31279. server_cbf.method = wolfTLSv1_2_server_method;
  31280. server_cbf.ssl_ready = set_post_auth_cb;
  31281. server_cbf.on_result = post_auth_version_cb;
  31282. client_cbf.ssl_ready = set_post_auth_cb;
  31283. client_cbf.on_result = post_auth_version_client_cb;
  31284. server_args.callbacks = &server_cbf;
  31285. client_args.callbacks = &client_cbf;
  31286. start_thread(test_server_nofail, &server_args, &serverThread);
  31287. wait_tcp_ready(&server_args);
  31288. test_client_nofail(&client_args, NULL);
  31289. join_thread(serverThread);
  31290. /* tests on post auth with TLS 1.3 */
  31291. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  31292. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  31293. server_cbf.method = wolfTLSv1_3_server_method;
  31294. server_cbf.ssl_ready = set_post_auth_cb;
  31295. client_cbf.ssl_ready = set_post_auth_cb;
  31296. server_cbf.on_result = post_auth_cb;
  31297. client_cbf.on_result = NULL;
  31298. server_args.callbacks = &server_cbf;
  31299. client_args.callbacks = &client_cbf;
  31300. start_thread(test_server_nofail, &server_args, &serverThread);
  31301. wait_tcp_ready(&server_args);
  31302. test_client_nofail(&client_args, NULL);
  31303. join_thread(serverThread);
  31304. FreeTcpReady(&ready);
  31305. res = TEST_RES_CHECK(1);
  31306. #endif
  31307. return res;
  31308. }
  31309. static int test_wolfSSL_X509_NID(void)
  31310. {
  31311. int res = TEST_SKIPPED;
  31312. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  31313. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  31314. int sigType;
  31315. int nameSz;
  31316. X509* cert;
  31317. EVP_PKEY* pubKeyTmp;
  31318. X509_NAME* name;
  31319. char commonName[80];
  31320. char countryName[80];
  31321. char localityName[80];
  31322. char stateName[80];
  31323. char orgName[80];
  31324. char orgUnit[80];
  31325. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  31326. /* convert cert from DER to internal WOLFSSL_X509 struct */
  31327. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  31328. sizeof_client_cert_der_2048));
  31329. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  31330. /* extract PUBLIC KEY from cert */
  31331. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  31332. /* extract signatureType */
  31333. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  31334. /* extract subjectName info */
  31335. AssertNotNull(name = X509_get_subject_name(cert));
  31336. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  31337. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31338. NULL, 0)), 0);
  31339. AssertIntEQ(nameSz, 15);
  31340. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31341. commonName, sizeof(commonName))), 0);
  31342. AssertIntEQ(nameSz, 15);
  31343. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  31344. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31345. commonName, 9)), 0);
  31346. AssertIntEQ(nameSz, 8);
  31347. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  31348. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  31349. countryName, sizeof(countryName))), 0);
  31350. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  31351. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  31352. localityName, sizeof(localityName))), 0);
  31353. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  31354. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  31355. stateName, sizeof(stateName))), 0);
  31356. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  31357. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  31358. orgName, sizeof(orgName))), 0);
  31359. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  31360. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  31361. orgUnit, sizeof(orgUnit))), 0);
  31362. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  31363. EVP_PKEY_free(pubKeyTmp);
  31364. X509_free(cert);
  31365. res = TEST_RES_CHECK(1);
  31366. #endif
  31367. return res;
  31368. }
  31369. static int test_wolfSSL_CTX_set_srp_username(void)
  31370. {
  31371. int res = TEST_SKIPPED;
  31372. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  31373. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  31374. WOLFSSL_CTX* ctx;
  31375. WOLFSSL* ssl;
  31376. const char *username = "TESTUSER";
  31377. const char *password = "TESTPASSWORD";
  31378. int r;
  31379. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31380. AssertNotNull(ctx);
  31381. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31382. AssertIntEQ(r,SSL_SUCCESS);
  31383. wolfSSL_CTX_free(ctx);
  31384. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31385. AssertNotNull(ctx);
  31386. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31387. AssertIntEQ(r,SSL_SUCCESS);
  31388. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31389. AssertIntEQ(r,SSL_SUCCESS);
  31390. AssertNotNull(ssl = SSL_new(ctx));
  31391. AssertNotNull(SSL_get_srp_username(ssl));
  31392. AssertStrEQ(SSL_get_srp_username(ssl), username);
  31393. wolfSSL_free(ssl);
  31394. wolfSSL_CTX_free(ctx);
  31395. res = TEST_RES_CHECK(1);
  31396. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  31397. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  31398. return res;
  31399. }
  31400. static int test_wolfSSL_CTX_set_srp_password(void)
  31401. {
  31402. int res = TEST_SKIPPED;
  31403. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  31404. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  31405. WOLFSSL_CTX* ctx;
  31406. const char *username = "TESTUSER";
  31407. const char *password = "TESTPASSWORD";
  31408. int r;
  31409. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31410. AssertNotNull(ctx);
  31411. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31412. AssertIntEQ(r,SSL_SUCCESS);
  31413. wolfSSL_CTX_free(ctx);
  31414. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31415. AssertNotNull(ctx);
  31416. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31417. AssertIntEQ(r,SSL_SUCCESS);
  31418. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31419. AssertIntEQ(r,SSL_SUCCESS);
  31420. wolfSSL_CTX_free(ctx);
  31421. res = TEST_RES_CHECK(1);
  31422. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  31423. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  31424. return res;
  31425. }
  31426. static int test_wolfSSL_X509_STORE(void)
  31427. {
  31428. int res = TEST_SKIPPED;
  31429. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31430. X509_STORE *store;
  31431. #ifdef HAVE_CRL
  31432. X509_STORE_CTX *storeCtx;
  31433. X509_CRL *crl;
  31434. X509 *ca, *cert;
  31435. const char crlPem[] = "./certs/crl/crl.revoked";
  31436. const char srvCert[] = "./certs/server-revoked-cert.pem";
  31437. const char caCert[] = "./certs/ca-cert.pem";
  31438. XFILE fp;
  31439. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31440. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  31441. SSL_FILETYPE_PEM)));
  31442. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  31443. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  31444. SSL_FILETYPE_PEM)));
  31445. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  31446. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  31447. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  31448. X509_STORE_free(store);
  31449. X509_STORE_CTX_free(storeCtx);
  31450. X509_free(cert);
  31451. X509_free(ca);
  31452. /* should fail to verify now after adding in CRL */
  31453. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31454. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  31455. SSL_FILETYPE_PEM)));
  31456. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  31457. fp = XFOPEN(crlPem, "rb");
  31458. AssertTrue((fp != XBADFILE));
  31459. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  31460. NULL, NULL));
  31461. XFCLOSE(fp);
  31462. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  31463. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  31464. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  31465. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  31466. SSL_FILETYPE_PEM)));
  31467. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  31468. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  31469. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  31470. X509_CRL_free(crl);
  31471. X509_STORE_free(store);
  31472. X509_STORE_CTX_free(storeCtx);
  31473. X509_free(cert);
  31474. X509_free(ca);
  31475. #endif /* HAVE_CRL */
  31476. #ifndef WOLFCRYPT_ONLY
  31477. {
  31478. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31479. SSL_CTX* ctx;
  31480. SSL* ssl;
  31481. int i;
  31482. for (i = 0; i < 2; i++) {
  31483. #ifndef NO_WOLFSSL_SERVER
  31484. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31485. #else
  31486. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31487. #endif
  31488. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31489. SSL_CTX_set_cert_store(ctx, store);
  31490. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31491. SSL_CTX_set_cert_store(ctx, store);
  31492. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31493. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31494. SSL_FILETYPE_PEM), SSL_SUCCESS);
  31495. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31496. SSL_FILETYPE_PEM), SSL_SUCCESS);
  31497. AssertNotNull(ssl = SSL_new(ctx));
  31498. if (i == 0) {
  31499. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  31500. }
  31501. else {
  31502. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  31503. X509_STORE_free(store);
  31504. }
  31505. SSL_free(ssl);
  31506. SSL_CTX_free(ctx);
  31507. }
  31508. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31509. }
  31510. #endif
  31511. res = TEST_RES_CHECK(1);
  31512. #endif
  31513. return res;
  31514. }
  31515. static int test_wolfSSL_X509_STORE_load_locations(void)
  31516. {
  31517. int res = TEST_SKIPPED;
  31518. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  31519. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  31520. SSL_CTX *ctx;
  31521. X509_STORE *store;
  31522. const char ca_file[] = "./certs/ca-cert.pem";
  31523. const char client_pem_file[] = "./certs/client-cert.pem";
  31524. const char client_der_file[] = "./certs/client-cert.der";
  31525. const char ecc_file[] = "./certs/ecc-key.pem";
  31526. const char certs_path[] = "./certs/";
  31527. const char bad_path[] = "./bad-path/";
  31528. #ifdef HAVE_CRL
  31529. const char crl_path[] = "./certs/crl/";
  31530. const char crl_file[] = "./certs/crl/crl.pem";
  31531. #endif
  31532. #ifndef NO_WOLFSSL_SERVER
  31533. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31534. #else
  31535. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31536. #endif
  31537. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  31538. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  31539. /* Test bad arguments */
  31540. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  31541. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  31542. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  31543. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  31544. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  31545. #ifdef HAVE_CRL
  31546. /* Test with CRL */
  31547. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  31548. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  31549. #endif
  31550. /* Test with CA */
  31551. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  31552. /* Test with client_cert and certs path */
  31553. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  31554. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  31555. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  31556. /* Clear nodes */
  31557. ERR_clear_error();
  31558. #endif
  31559. SSL_CTX_free(ctx);
  31560. res = TEST_RES_CHECK(1);
  31561. #endif
  31562. return res;
  31563. }
  31564. static int test_X509_STORE_get0_objects(void)
  31565. {
  31566. int res = TEST_SKIPPED;
  31567. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  31568. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  31569. X509_STORE *store;
  31570. X509_STORE *store_cpy;
  31571. SSL_CTX *ctx;
  31572. X509_OBJECT *obj;
  31573. STACK_OF(X509_OBJECT) *objs;
  31574. int i;
  31575. /* Setup store */
  31576. #ifndef NO_WOLFSSL_SERVER
  31577. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31578. #else
  31579. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31580. #endif
  31581. AssertNotNull(store_cpy = X509_STORE_new());
  31582. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  31583. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  31584. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  31585. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  31586. #ifdef HAVE_CRL
  31587. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  31588. #endif
  31589. /* Store ready */
  31590. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  31591. AssertNotNull(objs = X509_STORE_get0_objects(store));
  31592. #ifdef HAVE_CRL
  31593. #ifdef WOLFSSL_SIGNER_DER_CERT
  31594. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  31595. #else
  31596. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  31597. #endif
  31598. #else
  31599. #ifdef WOLFSSL_SIGNER_DER_CERT
  31600. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  31601. #else
  31602. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  31603. #endif
  31604. #endif
  31605. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  31606. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  31607. switch (X509_OBJECT_get_type(obj)) {
  31608. case X509_LU_X509:
  31609. AssertNotNull(X509_OBJECT_get0_X509(obj));
  31610. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  31611. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  31612. break;
  31613. case X509_LU_CRL:
  31614. #ifdef HAVE_CRL
  31615. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  31616. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  31617. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  31618. break;
  31619. #endif
  31620. case X509_LU_NONE:
  31621. default:
  31622. Fail(("X509_OBJECT_get_type should return x509 or crl "
  31623. "(when built with crl support)"),
  31624. ("Unrecognized X509_OBJECT type or none"));
  31625. }
  31626. }
  31627. X509_STORE_free(store_cpy);
  31628. SSL_CTX_free(ctx);
  31629. res = TEST_RES_CHECK(1);
  31630. #endif
  31631. return res;
  31632. }
  31633. static int test_wolfSSL_BN(void)
  31634. {
  31635. int res = TEST_SKIPPED;
  31636. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  31637. BIGNUM* a;
  31638. BIGNUM* b;
  31639. BIGNUM* c;
  31640. BIGNUM* d;
  31641. ASN1_INTEGER* ai;
  31642. AssertNotNull(b = BN_new());
  31643. AssertNotNull(c = BN_new());
  31644. AssertNotNull(d = BN_new());
  31645. ai = ASN1_INTEGER_new();
  31646. AssertNotNull(ai);
  31647. /* at the moment hard setting since no set function */
  31648. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  31649. ai->data[1] = 0x01; /* length of integer */
  31650. ai->data[2] = 0x03;
  31651. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  31652. ASN1_INTEGER_free(ai);
  31653. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  31654. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  31655. /* a + 3 = */
  31656. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  31657. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  31658. /* check result 3 + 3*/
  31659. AssertIntEQ(BN_get_word(a), 6);
  31660. /* set a back to 3 */
  31661. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31662. /* a - 3 = */
  31663. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  31664. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  31665. /* check result 3 - 3*/
  31666. AssertIntEQ(BN_get_word(a), 0);
  31667. /* set a back to 3 */
  31668. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31669. /* a^b mod c = */
  31670. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  31671. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  31672. /* check result 3^2 mod 5 */
  31673. AssertIntEQ(BN_get_word(d), 4);
  31674. /* a*b = */
  31675. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  31676. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  31677. /* check result 3*2 */
  31678. AssertIntEQ(BN_get_word(d), 6);
  31679. /* c/b => db + a */
  31680. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  31681. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  31682. /* check result 5/2 */
  31683. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  31684. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  31685. /* set a back to 3 */
  31686. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31687. /* a*b mod c = */
  31688. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  31689. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  31690. /* check result 3*2 mod 5 */
  31691. AssertIntEQ(BN_get_word(d), 1);
  31692. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  31693. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  31694. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  31695. /* gcd of a and b */
  31696. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  31697. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  31698. /* check result gcd(16, 24) */
  31699. AssertIntEQ(BN_get_word(d), 8);
  31700. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  31701. AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
  31702. AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
  31703. AssertIntEQ(BN_is_zero(b), 0);
  31704. AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
  31705. AssertIntEQ(BN_is_zero(b), 1);
  31706. AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
  31707. AssertIntEQ(BN_is_zero(b), 0);
  31708. /* set b back to 2 */
  31709. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  31710. {
  31711. /* BN_mod_inverse test */
  31712. BIGNUM *r = BN_new();
  31713. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  31714. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  31715. BN_free(val);
  31716. }
  31717. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  31718. defined(WOLFSSL_SP_INT_NEGATIVE))
  31719. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  31720. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  31721. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  31722. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  31723. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  31724. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31725. {
  31726. /* Do additional tests on negative BN conversions. */
  31727. char * ret;
  31728. ASN1_INTEGER * asn1;
  31729. BIGNUM * tmp;
  31730. /* Sanity check we have a negative BN. */
  31731. AssertIntEQ(BN_is_negative(c), 1);
  31732. AssertNotNull(ret = BN_bn2dec(c));
  31733. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  31734. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  31735. /* Convert to ASN1_INTEGER and back to BN. */
  31736. AssertNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  31737. AssertNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  31738. /* After converting back BN should be negative and correct. */
  31739. AssertIntEQ(BN_is_negative(tmp), 1);
  31740. AssertNotNull(ret = BN_bn2dec(tmp));
  31741. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  31742. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  31743. ASN1_INTEGER_free(asn1);
  31744. BN_free(tmp);
  31745. }
  31746. #endif
  31747. AssertIntEQ(BN_get_word(c), 4);
  31748. #endif
  31749. BN_free(a);
  31750. BN_free(b);
  31751. BN_free(c);
  31752. BN_clear_free(d);
  31753. /* check that converting NULL and the null string returns an error */
  31754. a = NULL;
  31755. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  31756. AssertIntLE(BN_hex2bn(&a, ""), 0);
  31757. AssertNull(a);
  31758. /* check that getting a string and a bin of the same number are equal,
  31759. * and that the comparison works EQ, LT and GT */
  31760. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  31761. AssertNotNull(b = BN_new());
  31762. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  31763. AssertNotNull(c = BN_new());
  31764. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  31765. AssertIntEQ(BN_cmp(a, b), 0);
  31766. AssertIntLT(BN_cmp(a, c), 0);
  31767. AssertIntGT(BN_cmp(c, b), 0);
  31768. AssertIntEQ(BN_set_word(a, 0), 1);
  31769. AssertIntEQ(BN_is_zero(a), 1);
  31770. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  31771. AssertIntEQ(BN_is_zero(a), 0);
  31772. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  31773. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  31774. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  31775. AssertIntEQ(BN_is_zero(a), 1);
  31776. BN_free(a);
  31777. BN_free(b);
  31778. BN_free(c);
  31779. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  31780. {
  31781. BIGNUM *ap;
  31782. BIGNUM bv;
  31783. BIGNUM cv;
  31784. BIGNUM dv;
  31785. AssertNotNull(ap = BN_new());
  31786. BN_init(&bv);
  31787. BN_init(&cv);
  31788. BN_init(&dv);
  31789. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  31790. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  31791. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  31792. /* a^b mod c = */
  31793. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  31794. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  31795. /* check result 3^2 mod 5 */
  31796. AssertIntEQ(BN_get_word(&dv), 4);
  31797. /* a*b mod c = */
  31798. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  31799. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  31800. /* check result 3*2 mod 5 */
  31801. AssertIntEQ(BN_get_word(&dv), 1);
  31802. BN_free(ap);
  31803. }
  31804. #endif
  31805. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  31806. AssertNotNull(a = BN_new());
  31807. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  31808. SSL_SUCCESS);
  31809. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  31810. BN_free(a);
  31811. #endif
  31812. res = TEST_RES_CHECK(1);
  31813. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31814. return res;
  31815. }
  31816. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31817. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31818. #define TEST_ARG 0x1234
  31819. static void msg_cb(int write_p, int version, int content_type,
  31820. const void *buf, size_t len, SSL *ssl, void *arg)
  31821. {
  31822. (void)write_p;
  31823. (void)version;
  31824. (void)content_type;
  31825. (void)buf;
  31826. (void)len;
  31827. (void)ssl;
  31828. AssertTrue(arg == (void*)TEST_ARG);
  31829. }
  31830. #endif
  31831. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31832. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31833. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  31834. !defined(NO_WOLFSSL_SERVER)
  31835. #ifndef SINGLE_THREADED
  31836. #if defined(SESSION_CERTS)
  31837. #include "wolfssl/internal.h"
  31838. #endif
  31839. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  31840. {
  31841. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31842. STACK_OF(X509)* sk;
  31843. X509* x509;
  31844. int i, num;
  31845. BIO* bio;
  31846. #endif
  31847. (void) ctx;
  31848. fprintf(stderr, "\n===== msgcb called ====\n");
  31849. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  31850. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  31851. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  31852. AssertNotNull(SSL_get0_verified_chain(ssl));
  31853. #else
  31854. (void) ssl;
  31855. #endif
  31856. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31857. bio = BIO_new(BIO_s_file());
  31858. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  31859. sk = SSL_get_peer_cert_chain(ssl);
  31860. AssertNotNull(sk);
  31861. if (!sk) {
  31862. BIO_free(bio);
  31863. return SSL_FAILURE;
  31864. }
  31865. num = sk_X509_num(sk);
  31866. AssertTrue(num > 0);
  31867. for (i = 0; i < num; i++) {
  31868. x509 = sk_X509_value(sk,i);
  31869. AssertNotNull(x509);
  31870. if (!x509)
  31871. break;
  31872. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  31873. X509_print(bio,x509);
  31874. fprintf(stderr, "\n\n");
  31875. }
  31876. BIO_free(bio);
  31877. #endif
  31878. return SSL_SUCCESS;
  31879. }
  31880. #endif
  31881. #endif
  31882. static int test_wolfSSL_msgCb(void)
  31883. {
  31884. int res = TEST_SKIPPED;
  31885. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31886. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31887. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  31888. !defined(NO_WOLFSSL_SERVER)
  31889. tcp_ready ready;
  31890. func_args client_args;
  31891. func_args server_args;
  31892. #ifndef SINGLE_THREADED
  31893. THREAD_TYPE serverThread;
  31894. #endif
  31895. callback_functions client_cb;
  31896. callback_functions server_cb;
  31897. /* create a failed connection and inspect the error */
  31898. #ifdef WOLFSSL_TIRTOS
  31899. fdOpenSession(Task_self());
  31900. #endif
  31901. XMEMSET(&client_args, 0, sizeof(func_args));
  31902. XMEMSET(&server_args, 0, sizeof(func_args));
  31903. StartTCP();
  31904. InitTcpReady(&ready);
  31905. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31906. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31907. #ifndef WOLFSSL_NO_TLS12
  31908. client_cb.method = wolfTLSv1_2_client_method;
  31909. server_cb.method = wolfTLSv1_2_server_method;
  31910. #else
  31911. client_cb.method = wolfTLSv1_3_client_method;
  31912. server_cb.method = wolfTLSv1_3_server_method;
  31913. #endif
  31914. server_args.signal = &ready;
  31915. server_args.callbacks = &server_cb;
  31916. client_args.signal = &ready;
  31917. client_args.callbacks = &client_cb;
  31918. client_args.return_code = TEST_FAIL;
  31919. #ifndef SINGLE_THREADED
  31920. start_thread(test_server_nofail, &server_args, &serverThread);
  31921. wait_tcp_ready(&server_args);
  31922. test_client_nofail(&client_args, msgCb);
  31923. join_thread(serverThread);
  31924. #endif
  31925. FreeTcpReady(&ready);
  31926. #ifndef SINGLE_THREADED
  31927. AssertTrue(client_args.return_code);
  31928. AssertTrue(server_args.return_code);
  31929. #endif
  31930. #ifdef WOLFSSL_TIRTOS
  31931. fdOpenSession(Task_self());
  31932. #endif
  31933. res = TEST_RES_CHECK(1);
  31934. #endif
  31935. return res;
  31936. }
  31937. static int test_wolfSSL_either_side(void)
  31938. {
  31939. int res = TEST_SKIPPED;
  31940. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31941. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31942. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  31943. tcp_ready ready;
  31944. func_args client_args;
  31945. func_args server_args;
  31946. #ifndef SINGLE_THREADED
  31947. THREAD_TYPE serverThread;
  31948. #endif
  31949. callback_functions client_cb;
  31950. callback_functions server_cb;
  31951. /* create a failed connection and inspect the error */
  31952. #ifdef WOLFSSL_TIRTOS
  31953. fdOpenSession(Task_self());
  31954. #endif
  31955. XMEMSET(&client_args, 0, sizeof(func_args));
  31956. XMEMSET(&server_args, 0, sizeof(func_args));
  31957. StartTCP();
  31958. InitTcpReady(&ready);
  31959. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31960. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31961. /* Use different CTX for client and server */
  31962. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31963. AssertNotNull(client_cb.ctx);
  31964. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31965. AssertNotNull(server_cb.ctx);
  31966. /* we are responsible for free'ing WOLFSSL_CTX */
  31967. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31968. server_args.signal = &ready;
  31969. server_args.callbacks = &server_cb;
  31970. client_args.signal = &ready;
  31971. client_args.callbacks = &client_cb;
  31972. client_args.return_code = TEST_FAIL;
  31973. #ifndef SINGLE_THREADED
  31974. start_thread(test_server_nofail, &server_args, &serverThread);
  31975. wait_tcp_ready(&server_args);
  31976. test_client_nofail(&client_args, NULL);
  31977. join_thread(serverThread);
  31978. #endif
  31979. wolfSSL_CTX_free(client_cb.ctx);
  31980. wolfSSL_CTX_free(server_cb.ctx);
  31981. FreeTcpReady(&ready);
  31982. #ifndef SINGLE_THREADED
  31983. AssertTrue(client_args.return_code);
  31984. AssertTrue(server_args.return_code);
  31985. #endif
  31986. #ifdef WOLFSSL_TIRTOS
  31987. fdOpenSession(Task_self());
  31988. #endif
  31989. res = TEST_RES_CHECK(1);
  31990. #endif
  31991. return res;
  31992. }
  31993. static int test_wolfSSL_DTLS_either_side(void)
  31994. {
  31995. int res = TEST_SKIPPED;
  31996. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31997. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31998. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  31999. defined(WOLFSSL_DTLS)
  32000. tcp_ready ready;
  32001. func_args client_args;
  32002. func_args server_args;
  32003. #ifndef SINGLE_THREADED
  32004. THREAD_TYPE serverThread;
  32005. #endif
  32006. callback_functions client_cb;
  32007. callback_functions server_cb;
  32008. /* create a failed connection and inspect the error */
  32009. #ifdef WOLFSSL_TIRTOS
  32010. fdOpenSession(Task_self());
  32011. #endif
  32012. XMEMSET(&client_args, 0, sizeof(func_args));
  32013. XMEMSET(&server_args, 0, sizeof(func_args));
  32014. StartTCP();
  32015. InitTcpReady(&ready);
  32016. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32017. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32018. /* Use different CTX for client and server */
  32019. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  32020. AssertNotNull(client_cb.ctx);
  32021. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  32022. AssertNotNull(server_cb.ctx);
  32023. /* we are responsible for free'ing WOLFSSL_CTX */
  32024. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  32025. server_args.signal = &ready;
  32026. server_args.callbacks = &server_cb;
  32027. client_args.signal = &ready;
  32028. client_args.callbacks = &client_cb;
  32029. client_args.return_code = TEST_FAIL;
  32030. #ifndef SINGLE_THREADED
  32031. start_thread(test_server_nofail, &server_args, &serverThread);
  32032. wait_tcp_ready(&server_args);
  32033. test_client_nofail(&client_args, NULL);
  32034. join_thread(serverThread);
  32035. #endif
  32036. wolfSSL_CTX_free(client_cb.ctx);
  32037. wolfSSL_CTX_free(server_cb.ctx);
  32038. FreeTcpReady(&ready);
  32039. #ifndef SINGLE_THREADED
  32040. AssertTrue(client_args.return_code);
  32041. AssertTrue(server_args.return_code);
  32042. #endif
  32043. #ifdef WOLFSSL_TIRTOS
  32044. fdOpenSession(Task_self());
  32045. #endif
  32046. res = TEST_RES_CHECK(1);
  32047. #endif
  32048. return res;
  32049. }
  32050. static int test_generate_cookie(void)
  32051. {
  32052. int res = TEST_SKIPPED;
  32053. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  32054. SSL_CTX* ctx;
  32055. SSL* ssl;
  32056. byte buf[FOURK_BUF] = {0};
  32057. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  32058. AssertNotNull(ssl = SSL_new(ctx));
  32059. /* Test unconnected */
  32060. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  32061. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  32062. wolfSSL_SetCookieCtx(ssl, ctx);
  32063. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  32064. AssertNull(wolfSSL_GetCookieCtx(NULL));
  32065. SSL_free(ssl);
  32066. SSL_CTX_free(ctx);
  32067. res = TEST_RES_CHECK(1);
  32068. #endif
  32069. return res;
  32070. }
  32071. static int test_wolfSSL_set_options(void)
  32072. {
  32073. int res = TEST_SKIPPED;
  32074. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32075. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32076. WOLFSSL* ssl;
  32077. WOLFSSL_CTX* ctx;
  32078. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  32079. char appData[] = "extra msg";
  32080. #endif
  32081. #ifdef OPENSSL_EXTRA
  32082. unsigned char protos[] = {
  32083. 7, 't', 'l', 's', '/', '1', '.', '2',
  32084. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  32085. };
  32086. unsigned int len = sizeof(protos);
  32087. void *arg = (void *)TEST_ARG;
  32088. #endif
  32089. #ifndef NO_WOLFSSL_SERVER
  32090. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  32091. #else
  32092. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32093. #endif
  32094. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  32095. WOLFSSL_FILETYPE_PEM));
  32096. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32097. WOLFSSL_FILETYPE_PEM));
  32098. AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  32099. == WOLFSSL_OP_NO_TLSv1);
  32100. AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  32101. AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  32102. WOLFSSL_OP_NO_SSLv2)), 0);
  32103. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  32104. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  32105. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  32106. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  32107. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  32108. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  32109. AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  32110. WOLFSSL_OP_NO_COMPRESSION));
  32111. wolfSSL_CTX_free(ctx);
  32112. #ifndef NO_WOLFSSL_SERVER
  32113. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  32114. AssertNotNull(ctx);
  32115. #else
  32116. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  32117. AssertNotNull(ctx);
  32118. #endif
  32119. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  32120. WOLFSSL_FILETYPE_PEM));
  32121. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32122. WOLFSSL_FILETYPE_PEM));
  32123. #ifdef OPENSSL_EXTRA
  32124. AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  32125. #endif
  32126. AssertNotNull(ssl = wolfSSL_new(ctx));
  32127. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  32128. #ifdef HAVE_EX_DATA
  32129. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  32130. AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  32131. if (ssl) {
  32132. AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  32133. appData, sizeof(appData)), 0);
  32134. }
  32135. #else
  32136. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  32137. AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  32138. #endif
  32139. #endif
  32140. AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  32141. WOLFSSL_OP_NO_TLSv1);
  32142. AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  32143. AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  32144. WOLFSSL_OP_NO_SSLv2)), 0);
  32145. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  32146. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  32147. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  32148. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  32149. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  32150. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  32151. #ifdef OPENSSL_EXTRA
  32152. AssertFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  32153. WOLFSSL_OP_NO_COMPRESSION));
  32154. #endif
  32155. #ifdef OPENSSL_EXTRA
  32156. AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  32157. wolfSSL_set_msg_callback_arg(ssl, arg);
  32158. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32159. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  32160. #else
  32161. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  32162. #endif
  32163. #endif
  32164. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  32165. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  32166. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  32167. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  32168. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32169. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  32170. #else
  32171. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  32172. #endif
  32173. #endif /* HAVE_ALPN && !NO_BIO */
  32174. #endif
  32175. wolfSSL_free(ssl);
  32176. wolfSSL_CTX_free(ctx);
  32177. res = TEST_RES_CHECK(1);
  32178. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32179. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32180. return res;
  32181. }
  32182. static int test_wolfSSL_sk_SSL_CIPHER(void)
  32183. {
  32184. int res = TEST_SKIPPED;
  32185. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32186. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32187. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32188. SSL* ssl;
  32189. SSL_CTX* ctx;
  32190. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  32191. #ifndef NO_WOLFSSL_SERVER
  32192. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32193. #else
  32194. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32195. #endif
  32196. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  32197. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32198. AssertNotNull(ssl = SSL_new(ctx));
  32199. AssertNotNull(sk = SSL_get_ciphers(ssl));
  32200. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  32201. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  32202. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  32203. /* error case because connection has not been established yet */
  32204. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  32205. sk_SSL_CIPHER_free(dupSk);
  32206. /* sk is pointer to internal struct that should be free'd in SSL_free */
  32207. SSL_free(ssl);
  32208. SSL_CTX_free(ctx);
  32209. res = TEST_RES_CHECK(1);
  32210. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32211. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32212. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32213. return res;
  32214. }
  32215. static int test_wolfSSL_set1_curves_list(void)
  32216. {
  32217. int res = TEST_SKIPPED;
  32218. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  32219. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32220. SSL* ssl = NULL;
  32221. SSL_CTX* ctx = NULL;
  32222. #ifndef NO_WOLFSSL_SERVER
  32223. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32224. #else
  32225. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32226. #endif
  32227. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  32228. SSL_FILETYPE_PEM));
  32229. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  32230. AssertNotNull(ssl = SSL_new(ctx));
  32231. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  32232. #ifdef HAVE_ECC
  32233. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  32234. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  32235. #endif
  32236. #ifdef HAVE_CURVE25519
  32237. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  32238. #else
  32239. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  32240. #endif
  32241. #ifdef HAVE_CURVE448
  32242. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  32243. #else
  32244. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  32245. #endif
  32246. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  32247. #ifdef HAVE_ECC
  32248. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  32249. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  32250. #endif
  32251. #ifdef HAVE_CURVE25519
  32252. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  32253. #else
  32254. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  32255. #endif
  32256. #ifdef HAVE_CURVE448
  32257. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  32258. #else
  32259. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  32260. #endif
  32261. SSL_free(ssl);
  32262. SSL_CTX_free(ctx);
  32263. res = TEST_RES_CHECK(1);
  32264. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32265. #endif
  32266. return res;
  32267. }
  32268. static int test_wolfSSL_set1_sigalgs_list(void)
  32269. {
  32270. int res = TEST_SKIPPED;
  32271. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  32272. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32273. SSL* ssl;
  32274. SSL_CTX* ctx;
  32275. #ifndef NO_WOLFSSL_SERVER
  32276. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32277. #else
  32278. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32279. #endif
  32280. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32281. SSL_FILETYPE_PEM));
  32282. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32283. AssertNotNull(ssl = SSL_new(ctx));
  32284. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  32285. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  32286. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  32287. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  32288. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  32289. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  32290. #ifndef NO_RSA
  32291. #ifndef NO_SHA256
  32292. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  32293. WOLFSSL_FAILURE);
  32294. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  32295. WOLFSSL_FAILURE);
  32296. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  32297. WOLFSSL_SUCCESS);
  32298. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  32299. WOLFSSL_SUCCESS);
  32300. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  32301. WOLFSSL_FAILURE);
  32302. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  32303. WOLFSSL_FAILURE);
  32304. #ifdef WC_RSA_PSS
  32305. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  32306. WOLFSSL_SUCCESS);
  32307. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  32308. WOLFSSL_SUCCESS);
  32309. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  32310. WOLFSSL_SUCCESS);
  32311. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  32312. WOLFSSL_SUCCESS);
  32313. #endif
  32314. #ifdef WOLFSSL_SHA512
  32315. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32316. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  32317. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32318. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  32319. #elif defined(WOLFSSL_SHA384)
  32320. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32321. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32322. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32323. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32324. #endif
  32325. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  32326. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  32327. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  32328. WOLFSSL_FAILURE);
  32329. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  32330. WOLFSSL_FAILURE);
  32331. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  32332. WOLFSSL_FAILURE);
  32333. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  32334. WOLFSSL_FAILURE);
  32335. #endif
  32336. #endif
  32337. #ifdef HAVE_ECC
  32338. #ifndef NO_SHA256
  32339. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  32340. WOLFSSL_SUCCESS);
  32341. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  32342. #ifdef WOLFSSL_SHA512
  32343. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32344. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32345. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32346. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32347. #elif defined(WOLFSSL_SHA384)
  32348. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32349. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32350. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32351. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32352. #endif
  32353. #endif
  32354. #endif
  32355. #ifdef HAVE_ED25519
  32356. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  32357. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  32358. #endif
  32359. #ifdef HAVE_ED448
  32360. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  32361. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  32362. #endif
  32363. #ifndef NO_DSA
  32364. #ifndef NO_SHA256
  32365. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  32366. WOLFSSL_SUCCESS);
  32367. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  32368. WOLFSSL_SUCCESS);
  32369. #endif
  32370. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  32371. defined(WOLFSSL_ALLOW_TLS_SHA1))
  32372. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  32373. WOLFSSL_SUCCESS);
  32374. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  32375. WOLFSSL_SUCCESS);
  32376. #endif
  32377. #endif
  32378. SSL_free(ssl);
  32379. SSL_CTX_free(ctx);
  32380. res = TEST_RES_CHECK(1);
  32381. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32382. #endif
  32383. return res;
  32384. }
  32385. /* Testing wolfSSL_set_tlsext_status_type function.
  32386. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  32387. */
  32388. static int test_wolfSSL_set_tlsext_status_type(void)
  32389. {
  32390. int res = TEST_SKIPPED;
  32391. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  32392. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  32393. SSL* ssl;
  32394. SSL_CTX* ctx;
  32395. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32396. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  32397. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32398. AssertNotNull(ssl = SSL_new(ctx));
  32399. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  32400. SSL_SUCCESS);
  32401. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  32402. SSL_free(ssl);
  32403. SSL_CTX_free(ctx);
  32404. res = TEST_RES_CHECK(1);
  32405. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  32406. return res;
  32407. }
  32408. #ifndef NO_BIO
  32409. static int test_wolfSSL_PEM_read_bio(void)
  32410. {
  32411. int res = TEST_SKIPPED;
  32412. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32413. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32414. byte buff[6000];
  32415. XFILE f;
  32416. int bytes;
  32417. X509* x509;
  32418. BIO* bio = NULL;
  32419. BUF_MEM* buf;
  32420. f = XFOPEN(cliCertFile, "rb");
  32421. AssertTrue((f != XBADFILE));
  32422. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  32423. XFCLOSE(f);
  32424. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32425. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  32426. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  32427. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32428. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  32429. /* BIO should return the set EOF value */
  32430. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  32431. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  32432. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  32433. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  32434. BIO_free(bio);
  32435. BUF_MEM_free(buf);
  32436. X509_free(x509);
  32437. res = TEST_RES_CHECK(1);
  32438. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32439. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32440. return res;
  32441. }
  32442. #if defined(OPENSSL_EXTRA)
  32443. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  32444. long argl, long ret)
  32445. {
  32446. (void)bio;
  32447. (void)cmd;
  32448. (void)argp;
  32449. (void)argi;
  32450. (void)argl;
  32451. return ret;
  32452. }
  32453. #endif
  32454. static int test_wolfSSL_BIO(void)
  32455. {
  32456. int res = TEST_SKIPPED;
  32457. #if defined(OPENSSL_EXTRA)
  32458. const unsigned char* p;
  32459. byte buff[20];
  32460. BIO* bio1;
  32461. BIO* bio2;
  32462. BIO* bio3;
  32463. char* bufPt;
  32464. int i;
  32465. for (i = 0; i < 20; i++) {
  32466. buff[i] = i;
  32467. }
  32468. /* test BIO_free with NULL */
  32469. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  32470. /* Creating and testing type BIO_s_bio */
  32471. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  32472. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  32473. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  32474. /* read/write before set up */
  32475. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32476. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32477. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  32478. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  32479. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  32480. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  32481. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  32482. XMEMCPY(bufPt, buff, 10);
  32483. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  32484. /* write buffer full */
  32485. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  32486. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  32487. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  32488. /* write the other direction with pair */
  32489. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  32490. XMEMCPY(bufPt, buff, 8);
  32491. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  32492. /* try read */
  32493. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  32494. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  32495. /* try read using ctrl function */
  32496. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  32497. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  32498. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  32499. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  32500. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  32501. for (i = 0; i < 20; i++) {
  32502. AssertIntEQ((int)bufPt[i], i);
  32503. }
  32504. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32505. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  32506. for (i = 0; i < 8; i++) {
  32507. AssertIntEQ((int)bufPt[i], i);
  32508. }
  32509. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32510. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  32511. /* new pair */
  32512. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  32513. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  32514. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  32515. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32516. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  32517. /* test wrap around... */
  32518. AssertIntEQ(BIO_reset(bio1), 0);
  32519. AssertIntEQ(BIO_reset(bio3), 0);
  32520. /* fill write buffer, read only small amount then write again */
  32521. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32522. XMEMCPY(bufPt, buff, 20);
  32523. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  32524. for (i = 0; i < 4; i++) {
  32525. AssertIntEQ(bufPt[i], i);
  32526. }
  32527. /* try writing over read index */
  32528. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  32529. XMEMSET(bufPt, 0, 4);
  32530. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  32531. /* read and write 0 bytes */
  32532. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32533. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  32534. /* should read only to end of write buffer then need to read again */
  32535. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  32536. for (i = 0; i < 16; i++) {
  32537. AssertIntEQ(bufPt[i], buff[4 + i]);
  32538. }
  32539. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  32540. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  32541. for (i = 0; i < 4; i++) {
  32542. AssertIntEQ(bufPt[i], 0);
  32543. }
  32544. /* read index should not have advanced with nread0 */
  32545. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  32546. for (i = 0; i < 4; i++) {
  32547. AssertIntEQ(bufPt[i], 0);
  32548. }
  32549. /* write and fill up buffer checking reset of index state */
  32550. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32551. XMEMCPY(bufPt, buff, 20);
  32552. /* test reset on data in bio1 write buffer */
  32553. AssertIntEQ(BIO_reset(bio1), 0);
  32554. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32555. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  32556. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32557. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  32558. AssertNotNull(p);
  32559. XMEMCPY(bufPt, buff, 20);
  32560. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  32561. for (i = 0; i < 6; i++) {
  32562. AssertIntEQ(bufPt[i], i);
  32563. }
  32564. /* test case of writing twice with offset read index */
  32565. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  32566. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  32567. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32568. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32569. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32570. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  32571. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  32572. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32573. BIO_free(bio1);
  32574. BIO_free(bio3);
  32575. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  32576. {
  32577. BIO* bioA = NULL;
  32578. BIO* bioB = NULL;
  32579. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  32580. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  32581. BIO_free(bioA);
  32582. bioA = NULL;
  32583. BIO_free(bioB);
  32584. bioB = NULL;
  32585. }
  32586. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  32587. /* BIOs with file pointers */
  32588. #if !defined(NO_FILESYSTEM)
  32589. {
  32590. XFILE f1;
  32591. XFILE f2;
  32592. BIO* f_bio1;
  32593. BIO* f_bio2;
  32594. unsigned char cert[300];
  32595. char testFile[] = "tests/bio_write_test.txt";
  32596. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  32597. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  32598. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  32599. /* Failure due to wrong BIO type */
  32600. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32601. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  32602. f1 = XFOPEN(svrCertFile, "rwb");
  32603. AssertTrue((f1 != XBADFILE));
  32604. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  32605. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  32606. WOLFSSL_SUCCESS);
  32607. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32608. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  32609. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  32610. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  32611. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  32612. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  32613. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  32614. AssertIntEQ(BIO_reset(f_bio2), 0);
  32615. AssertIntEQ(BIO_tell(NULL),-1);
  32616. AssertIntEQ(BIO_tell(f_bio2),0);
  32617. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  32618. AssertIntEQ(BIO_tell(f_bio2),4);
  32619. BIO_free(f_bio1);
  32620. BIO_free(f_bio2);
  32621. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  32622. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32623. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32624. BIO_free(f_bio1);
  32625. }
  32626. #endif /* !defined(NO_FILESYSTEM) */
  32627. /* BIO info callback */
  32628. {
  32629. const char* testArg = "test";
  32630. BIO* cb_bio;
  32631. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  32632. BIO_set_callback(cb_bio, bioCallback);
  32633. AssertNotNull(BIO_get_callback(cb_bio));
  32634. BIO_set_callback(cb_bio, NULL);
  32635. AssertNull(BIO_get_callback(cb_bio));
  32636. BIO_set_callback_arg(cb_bio, (char*)testArg);
  32637. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  32638. AssertNull(BIO_get_callback_arg(NULL));
  32639. BIO_free(cb_bio);
  32640. }
  32641. /* BIO_vfree */
  32642. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  32643. BIO_vfree(NULL);
  32644. BIO_vfree(bio1);
  32645. res = TEST_RES_CHECK(1);
  32646. #endif
  32647. return res;
  32648. }
  32649. #endif /* !NO_BIO */
  32650. static int test_wolfSSL_ASN1_STRING(void)
  32651. {
  32652. int res = TEST_SKIPPED;
  32653. #if defined(OPENSSL_EXTRA)
  32654. ASN1_STRING* str = NULL;
  32655. const char data[] = "hello wolfSSL";
  32656. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  32657. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  32658. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  32659. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  32660. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  32661. ASN1_STRING_free(str);
  32662. res = TEST_RES_CHECK(1);
  32663. #endif
  32664. return res;
  32665. }
  32666. static int test_wolfSSL_ASN1_BIT_STRING(void)
  32667. {
  32668. int res = TEST_SKIPPED;
  32669. #ifdef OPENSSL_ALL
  32670. ASN1_BIT_STRING* str;
  32671. AssertNotNull(str = ASN1_BIT_STRING_new());
  32672. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  32673. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  32674. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  32675. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  32676. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  32677. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  32678. ASN1_BIT_STRING_free(str);
  32679. res = TEST_RES_CHECK(1);
  32680. #endif
  32681. return res;
  32682. }
  32683. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  32684. {
  32685. int res = TEST_SKIPPED;
  32686. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  32687. BIO *bio, *out;
  32688. ASN1_INTEGER* ai;
  32689. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  32690. char tmp[1024];
  32691. int tmpSz;
  32692. const char expected1[] = "123456";
  32693. const char expected2[] = "112345678912345678901234567890";
  32694. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  32695. AssertNotNull(out = BIO_new(BIO_s_mem()));
  32696. AssertNotNull(ai = ASN1_INTEGER_new());
  32697. /* read first line */
  32698. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32699. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  32700. XMEMSET(tmp, 0, 1024);
  32701. tmpSz = BIO_read(out, tmp, 1024);
  32702. AssertIntEQ(tmpSz, 6);
  32703. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  32704. /* fail on second line (not % 2) */
  32705. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32706. /* read 3rd long line */
  32707. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32708. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  32709. XMEMSET(tmp, 0, 1024);
  32710. tmpSz = BIO_read(out, tmp, 1024);
  32711. AssertIntEQ(tmpSz, 30);
  32712. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  32713. BIO_free(out);
  32714. BIO_free(bio);
  32715. ASN1_INTEGER_free(ai);
  32716. res = TEST_RES_CHECK(1);
  32717. #endif
  32718. return res;
  32719. }
  32720. static int test_wolfSSL_a2i_IPADDRESS(void)
  32721. {
  32722. int res = TEST_SKIPPED;
  32723. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  32724. const unsigned char* data;
  32725. int dataSz = 0;
  32726. ASN1_OCTET_STRING *st;
  32727. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  32728. const unsigned char ipv6_exp[] = {
  32729. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  32730. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  32731. };
  32732. const unsigned char ipv6_home[] = {
  32733. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32734. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  32735. };
  32736. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  32737. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  32738. data = ASN1_STRING_get0_data(st);
  32739. dataSz = ASN1_STRING_length(st);
  32740. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  32741. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  32742. ASN1_STRING_free(st);
  32743. AssertNotNull(st = a2i_IPADDRESS("::1"));
  32744. data = ASN1_STRING_get0_data(st);
  32745. dataSz = ASN1_STRING_length(st);
  32746. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  32747. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  32748. ASN1_STRING_free(st);
  32749. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  32750. data = ASN1_STRING_get0_data(st);
  32751. dataSz = ASN1_STRING_length(st);
  32752. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  32753. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  32754. ASN1_STRING_free(st);
  32755. res = TEST_RES_CHECK(1);
  32756. #endif
  32757. return res;
  32758. }
  32759. static int test_wolfSSL_DES_ecb_encrypt(void)
  32760. {
  32761. int res = TEST_SKIPPED;
  32762. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  32763. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  32764. WOLFSSL_DES_key_schedule key;
  32765. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  32766. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  32767. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  32768. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  32769. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  32770. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  32771. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  32772. /* Encrypt messages */
  32773. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  32774. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  32775. {
  32776. /* Decrypt messages */
  32777. int ret1 = 0;
  32778. int ret2 = 0;
  32779. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  32780. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  32781. AssertIntEQ(ret1,0);
  32782. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  32783. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  32784. AssertIntEQ(ret2,0);
  32785. }
  32786. res = TEST_RES_CHECK(1);
  32787. #endif
  32788. return res;
  32789. }
  32790. static int test_wolfSSL_ASN1_TIME_adj(void)
  32791. {
  32792. int res = TEST_SKIPPED;
  32793. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32794. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32795. const int year = 365*24*60*60;
  32796. const int day = 24*60*60;
  32797. const int hour = 60*60;
  32798. const int mini = 60;
  32799. const byte asn_utc_time = ASN_UTC_TIME;
  32800. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  32801. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  32802. #endif
  32803. WOLFSSL_ASN1_TIME *asn_time, *s;
  32804. int offset_day;
  32805. long offset_sec;
  32806. char date_str[CTC_DATE_SIZE + 1];
  32807. time_t t;
  32808. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  32809. /* UTC notation test */
  32810. /* 2000/2/15 20:30:00 */
  32811. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32812. offset_day = 7;
  32813. offset_sec = 45 * mini;
  32814. /* offset_sec = -45 * min;*/
  32815. AssertNotNull(asn_time =
  32816. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  32817. AssertTrue(asn_time->type == asn_utc_time);
  32818. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32819. date_str[CTC_DATE_SIZE] = '\0';
  32820. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  32821. /* negative offset */
  32822. offset_sec = -45 * mini;
  32823. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32824. AssertNotNull(asn_time);
  32825. AssertTrue(asn_time->type == asn_utc_time);
  32826. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32827. date_str[CTC_DATE_SIZE] = '\0';
  32828. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  32829. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  32830. XMEMSET(date_str, 0, sizeof(date_str));
  32831. /* Generalized time will overflow time_t if not long */
  32832. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  32833. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  32834. DYNAMIC_TYPE_OPENSSL);
  32835. /* GeneralizedTime notation test */
  32836. /* 2055/03/01 09:00:00 */
  32837. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  32838. offset_day = 12;
  32839. offset_sec = 10 * mini;
  32840. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32841. AssertTrue(asn_time->type == asn_gen_time);
  32842. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32843. date_str[CTC_DATE_SIZE] = '\0';
  32844. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  32845. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  32846. XMEMSET(date_str, 0, sizeof(date_str));
  32847. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  32848. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  32849. s = NULL;
  32850. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  32851. offset_day = 7;
  32852. offset_sec = 45 * mini;
  32853. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32854. AssertTrue(asn_time->type == asn_utc_time);
  32855. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32856. date_str[CTC_DATE_SIZE] = '\0';
  32857. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  32858. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  32859. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  32860. AssertTrue(asn_time->type == asn_utc_time);
  32861. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32862. date_str[CTC_DATE_SIZE] = '\0';
  32863. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  32864. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  32865. res = TEST_RES_CHECK(1);
  32866. #endif
  32867. return res;
  32868. }
  32869. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  32870. {
  32871. int res = TEST_SKIPPED;
  32872. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  32873. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  32874. && !defined(NO_ASN_TIME)
  32875. ASN1_TIME asnTime;
  32876. struct tm tm;
  32877. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  32878. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  32879. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  32880. AssertIntEQ(tm.tm_sec, 15);
  32881. AssertIntEQ(tm.tm_min, 15);
  32882. AssertIntEQ(tm.tm_hour, 21);
  32883. AssertIntEQ(tm.tm_mday, 22);
  32884. AssertIntEQ(tm.tm_mon, 1);
  32885. AssertIntEQ(tm.tm_year, 100);
  32886. AssertIntEQ(tm.tm_isdst, 0);
  32887. #ifdef XMKTIME
  32888. AssertIntEQ(tm.tm_wday, 2);
  32889. AssertIntEQ(tm.tm_yday, 52);
  32890. #endif
  32891. res = TEST_RES_CHECK(1);
  32892. #endif
  32893. return res;
  32894. }
  32895. static int test_wolfSSL_X509_cmp_time(void)
  32896. {
  32897. int res = TEST_SKIPPED;
  32898. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32899. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32900. WOLFSSL_ASN1_TIME asn_time;
  32901. time_t t;
  32902. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  32903. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  32904. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  32905. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  32906. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  32907. res = TEST_RES_CHECK(1);
  32908. #endif
  32909. return res;
  32910. }
  32911. static int test_wolfSSL_X509_time_adj(void)
  32912. {
  32913. int res = TEST_SKIPPED;
  32914. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  32915. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  32916. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  32917. !defined(NO_ASN_TIME)
  32918. X509* x509;
  32919. time_t t, not_before, not_after;
  32920. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  32921. client_cert_der_2048, sizeof_client_cert_der_2048,
  32922. WOLFSSL_FILETYPE_ASN1));
  32923. t = 0;
  32924. not_before = wc_Time(0);
  32925. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  32926. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  32927. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  32928. /* Check X509_gmtime_adj, too. */
  32929. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  32930. X509_free(x509);
  32931. res = TEST_RES_CHECK(1);
  32932. #endif
  32933. return res;
  32934. }
  32935. static int test_wolfSSL_X509(void)
  32936. {
  32937. int res = TEST_SKIPPED;
  32938. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  32939. && !defined(NO_RSA)
  32940. X509* x509;
  32941. #ifndef NO_BIO
  32942. BIO* bio;
  32943. X509_STORE_CTX* ctx;
  32944. X509_STORE* store;
  32945. #endif
  32946. char der[] = "certs/ca-cert.der";
  32947. XFILE fp;
  32948. AssertNotNull(x509 = X509_new());
  32949. X509_free(x509);
  32950. #ifndef NO_BIO
  32951. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  32952. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32953. #ifdef WOLFSSL_CERT_GEN
  32954. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  32955. #endif
  32956. AssertNotNull(ctx = X509_STORE_CTX_new());
  32957. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  32958. AssertNotNull(store = X509_STORE_new());
  32959. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  32960. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  32961. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  32962. X509_STORE_CTX_free(ctx);
  32963. X509_STORE_free(store);
  32964. X509_free(x509);
  32965. BIO_free(bio);
  32966. #endif
  32967. /** d2i_X509_fp test **/
  32968. fp = XFOPEN(der, "rb");
  32969. AssertTrue((fp != XBADFILE));
  32970. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  32971. AssertNotNull(x509);
  32972. X509_free(x509);
  32973. XFCLOSE(fp);
  32974. fp = XFOPEN(der, "rb");
  32975. AssertTrue((fp != XBADFILE));
  32976. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  32977. AssertNotNull(x509);
  32978. X509_free(x509);
  32979. XFCLOSE(fp);
  32980. /* X509_up_ref test */
  32981. AssertIntEQ(X509_up_ref(NULL), 0);
  32982. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  32983. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  32984. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  32985. X509_free(x509); /* refCount = 2 */
  32986. X509_free(x509); /* refCount = 1 */
  32987. X509_free(x509); /* refCount = 0, free */
  32988. res = TEST_RES_CHECK(1);
  32989. #endif
  32990. return res;
  32991. }
  32992. static int test_wolfSSL_X509_get_ext_count(void)
  32993. {
  32994. int res = TEST_SKIPPED;
  32995. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32996. !defined(NO_RSA)
  32997. int ret = 0;
  32998. WOLFSSL_X509* x509;
  32999. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  33000. FILE* f;
  33001. /* NULL parameter check */
  33002. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  33003. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33004. SSL_FILETYPE_PEM));
  33005. AssertIntEQ(X509_get_ext_count(x509), 5);
  33006. wolfSSL_X509_free(x509);
  33007. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  33008. SSL_FILETYPE_PEM));
  33009. AssertIntEQ(X509_get_ext_count(x509), 5);
  33010. wolfSSL_X509_free(x509);
  33011. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  33012. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  33013. fclose(f);
  33014. /* wolfSSL_X509_get_ext_count() valid input */
  33015. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  33016. /* wolfSSL_X509_get_ext_count() NULL argument */
  33017. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  33018. wolfSSL_X509_free(x509);
  33019. res = TEST_RES_CHECK(1);
  33020. #endif
  33021. return res;
  33022. }
  33023. static int test_wolfSSL_X509_sign2(void)
  33024. {
  33025. int res = TEST_SKIPPED;
  33026. /* test requires WOLFSSL_AKID_NAME to match expected output */
  33027. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33028. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  33029. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  33030. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  33031. WOLFSSL_X509 *x509, *ca;
  33032. const unsigned char *der;
  33033. const unsigned char *pt;
  33034. WOLFSSL_EVP_PKEY *priv;
  33035. WOLFSSL_X509_NAME *name;
  33036. int derSz;
  33037. #ifndef NO_ASN_TIME
  33038. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  33039. const int year = 365*24*60*60;
  33040. const int day = 24*60*60;
  33041. const int hour = 60*60;
  33042. const int mini = 60;
  33043. time_t t;
  33044. #endif
  33045. const unsigned char expected[] = {
  33046. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  33047. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  33048. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  33049. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  33050. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  33051. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  33052. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  33053. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  33054. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  33055. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  33056. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  33057. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  33058. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  33059. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  33060. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33061. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  33062. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  33063. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  33064. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  33065. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  33066. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  33067. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  33068. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  33069. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  33070. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  33071. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  33072. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  33073. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  33074. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  33075. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  33076. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  33077. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  33078. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  33079. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33080. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  33081. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  33082. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  33083. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  33084. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  33085. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  33086. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  33087. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  33088. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  33089. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  33090. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  33091. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  33092. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  33093. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  33094. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  33095. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  33096. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  33097. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  33098. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  33099. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  33100. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  33101. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  33102. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  33103. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  33104. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  33105. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  33106. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  33107. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  33108. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  33109. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  33110. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  33111. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  33112. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  33113. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  33114. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  33115. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  33116. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  33117. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  33118. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  33119. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  33120. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  33121. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  33122. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  33123. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  33124. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  33125. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  33126. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  33127. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  33128. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  33129. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  33130. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  33131. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  33132. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  33133. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  33134. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  33135. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  33136. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  33137. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  33138. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  33139. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  33140. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  33141. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  33142. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  33143. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  33144. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  33145. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  33146. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  33147. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  33148. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  33149. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  33150. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  33151. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  33152. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  33153. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  33154. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  33155. };
  33156. pt = ca_key_der_2048;
  33157. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  33158. sizeof_ca_key_der_2048));
  33159. pt = client_cert_der_2048;
  33160. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  33161. sizeof_client_cert_der_2048));
  33162. pt = ca_cert_der_2048;
  33163. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  33164. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  33165. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  33166. #ifndef NO_ASN_TIME
  33167. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  33168. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  33169. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  33170. AssertIntEQ(notAfter->length, 13);
  33171. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  33172. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  33173. #endif
  33174. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  33175. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  33176. AssertIntEQ(derSz, sizeof(expected));
  33177. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  33178. wolfSSL_X509_free(ca);
  33179. wolfSSL_X509_free(x509);
  33180. wolfSSL_EVP_PKEY_free(priv);
  33181. wolfSSL_ASN1_TIME_free(notBefore);
  33182. wolfSSL_ASN1_TIME_free(notAfter);
  33183. res = TEST_RES_CHECK(1);
  33184. #endif
  33185. return res;
  33186. }
  33187. static int test_wolfSSL_X509_sign(void)
  33188. {
  33189. int res = TEST_SKIPPED;
  33190. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33191. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  33192. int ret;
  33193. char *cn;
  33194. word32 cnSz;
  33195. X509_NAME *name;
  33196. X509 *x509, *ca;
  33197. DecodedCert dCert;
  33198. EVP_PKEY *pub;
  33199. EVP_PKEY *priv;
  33200. EVP_MD_CTX *mctx;
  33201. #if defined(USE_CERT_BUFFERS_1024)
  33202. const unsigned char* rsaPriv = client_key_der_1024;
  33203. const unsigned char* rsaPub = client_keypub_der_1024;
  33204. const unsigned char* certIssuer = client_cert_der_1024;
  33205. long clientKeySz = (long)sizeof_client_key_der_1024;
  33206. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  33207. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  33208. #elif defined(USE_CERT_BUFFERS_2048)
  33209. const unsigned char* rsaPriv = client_key_der_2048;
  33210. const unsigned char* rsaPub = client_keypub_der_2048;
  33211. const unsigned char* certIssuer = client_cert_der_2048;
  33212. long clientKeySz = (long)sizeof_client_key_der_2048;
  33213. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  33214. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  33215. #endif
  33216. byte sn[16];
  33217. int snSz = sizeof(sn);
  33218. /* Set X509_NAME fields */
  33219. AssertNotNull(name = X509_NAME_new());
  33220. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  33221. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  33222. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  33223. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  33224. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  33225. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  33226. /* Get private and public keys */
  33227. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  33228. clientKeySz));
  33229. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  33230. AssertNotNull(x509 = X509_new());
  33231. /* Set version 3 */
  33232. AssertIntNE(X509_set_version(x509, 2L), 0);
  33233. /* Set subject name, add pubkey, and sign certificate */
  33234. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  33235. X509_NAME_free(name);
  33236. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  33237. #ifdef WOLFSSL_ALT_NAMES
  33238. /* Add some subject alt names */
  33239. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  33240. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  33241. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  33242. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  33243. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  33244. "sphygmomanometer",
  33245. ASN_DNS_TYPE), SSL_SUCCESS);
  33246. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  33247. "supercalifragilisticexpialidocious",
  33248. ASN_DNS_TYPE), SSL_SUCCESS);
  33249. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  33250. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  33251. ASN_DNS_TYPE), SSL_SUCCESS);
  33252. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  33253. {
  33254. unsigned char ip4_type[] = {127,128,0,255};
  33255. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  33256. 0xff, 0xee, 0x99, 0x88,
  33257. 0x77, 0x66, 0x55, 0x44,
  33258. 0x00, 0x33, 0x22, 0x11};
  33259. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  33260. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  33261. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  33262. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  33263. }
  33264. #endif
  33265. #endif /* WOLFSSL_ALT_NAMES */
  33266. /* test valid sign case */
  33267. ret = X509_sign(x509, priv, EVP_sha256());
  33268. /* test valid X509_sign_ctx case */
  33269. AssertNotNull(mctx = EVP_MD_CTX_new());
  33270. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  33271. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  33272. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  33273. AssertIntEQ(X509_get_ext_count(x509), 1);
  33274. #endif
  33275. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  33276. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  33277. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  33278. #endif
  33279. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  33280. WOLFSSL_SUCCESS);
  33281. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  33282. /* Variation in size depends on ASN.1 encoding when MSB is set.
  33283. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  33284. * with the MSB set. See GenerateInteger in asn.c */
  33285. #ifndef USE_CERT_BUFFERS_1024
  33286. #ifndef WOLFSSL_ALT_NAMES
  33287. /* Valid case - size should be 781-786 with 16 byte serial number */
  33288. AssertTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  33289. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  33290. /* Valid case - size should be 955-960 with 16 byte serial number */
  33291. AssertTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  33292. #else
  33293. /* Valid case - size should be 926-931 with 16 byte serial number */
  33294. AssertTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  33295. #endif
  33296. #else
  33297. #ifndef WOLFSSL_ALT_NAMES
  33298. /* Valid case - size should be 537-542 with 16 byte serial number */
  33299. AssertTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  33300. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  33301. /* Valid case - size should be 695-670 with 16 byte serial number */
  33302. AssertTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  33303. #else
  33304. /* Valid case - size should be 666-671 with 16 byte serial number */
  33305. AssertTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  33306. #endif
  33307. #endif
  33308. /* check that issuer name is as expected after signature */
  33309. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  33310. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  33311. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  33312. AssertNotNull(name = X509_get_subject_name(ca));
  33313. cnSz = X509_NAME_get_sz(name);
  33314. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33315. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  33316. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  33317. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33318. #ifdef WOLFSSL_MULTI_ATTRIB
  33319. /* test adding multiple OU's to the signer */
  33320. AssertNotNull(name = X509_get_subject_name(ca));
  33321. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  33322. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  33323. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  33324. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  33325. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  33326. #endif
  33327. AssertNotNull(name = X509_get_subject_name(ca));
  33328. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  33329. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33330. AssertNotNull(name = X509_get_issuer_name(x509));
  33331. cnSz = X509_NAME_get_sz(name);
  33332. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33333. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  33334. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  33335. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  33336. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33337. FreeDecodedCert(&dCert);
  33338. /* Test invalid parameters */
  33339. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  33340. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  33341. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  33342. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  33343. EVP_MD_CTX_free(mctx);
  33344. AssertNotNull(mctx = EVP_MD_CTX_new());
  33345. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  33346. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  33347. /* test invalid version number */
  33348. #if defined(OPENSSL_ALL)
  33349. AssertIntNE(X509_set_version(x509, 6L), 0);
  33350. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33351. /* uses ParseCert which fails on bad version number */
  33352. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  33353. #endif
  33354. EVP_MD_CTX_free(mctx);
  33355. EVP_PKEY_free(priv);
  33356. EVP_PKEY_free(pub);
  33357. X509_free(x509);
  33358. X509_free(ca);
  33359. res = TEST_RES_CHECK(1);
  33360. #endif
  33361. return res;
  33362. }
  33363. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  33364. {
  33365. int res = TEST_SKIPPED;
  33366. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33367. X509* x509 = NULL;
  33368. const X509_ALGOR* alg;
  33369. AssertNotNull(x509 = X509_new());
  33370. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  33371. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  33372. X509_free(x509);
  33373. res = TEST_RES_CHECK(1);
  33374. #endif
  33375. return res;
  33376. }
  33377. static int test_wolfSSL_X509_ALGOR_get0(void)
  33378. {
  33379. int res = TEST_SKIPPED;
  33380. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33381. !defined(NO_SHA256) && !defined(NO_RSA)
  33382. X509* x509 = NULL;
  33383. const ASN1_OBJECT* obj = NULL;
  33384. const X509_ALGOR* alg;
  33385. int pptype = 0;
  33386. const void *ppval = NULL;
  33387. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  33388. SSL_FILETYPE_PEM));
  33389. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  33390. /* Invalid case */
  33391. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  33392. AssertNull(obj);
  33393. /* Valid case */
  33394. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  33395. AssertNotNull(obj);
  33396. AssertNull(ppval);
  33397. AssertIntNE(pptype, 0);
  33398. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  33399. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  33400. X509_free(x509);
  33401. res = TEST_RES_CHECK(1);
  33402. #endif
  33403. return res;
  33404. }
  33405. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  33406. {
  33407. int res = TEST_SKIPPED;
  33408. #if defined(OPENSSL_EXTRA)
  33409. X509_VERIFY_PARAM *paramTo;
  33410. X509_VERIFY_PARAM *paramFrom;
  33411. int ret;
  33412. char testIPv4[] = "127.0.0.1";
  33413. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  33414. char testhostName1[] = "foo.hoge.com";
  33415. char testhostName2[] = "foobar.hoge.com";
  33416. paramTo = X509_VERIFY_PARAM_new();
  33417. AssertNotNull(paramTo);
  33418. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  33419. paramFrom = X509_VERIFY_PARAM_new();
  33420. AssertNotNull(paramFrom);
  33421. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  33422. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  33423. (int)XSTRLEN(testhostName1));
  33424. AssertIntEQ(1, ret);
  33425. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  33426. (int)XSTRLEN(testhostName1)));
  33427. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  33428. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  33429. AssertIntEQ(0x01, paramFrom->hostFlags);
  33430. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  33431. AssertIntEQ(0, ret);
  33432. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  33433. AssertIntEQ(1, ret);
  33434. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33435. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  33436. AssertIntEQ(1, ret);
  33437. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  33438. AssertIntEQ(1, ret);
  33439. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33440. /* null pointer */
  33441. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  33442. AssertIntEQ(WOLFSSL_FAILURE, ret);
  33443. /* in the case of "from" null, returns success */
  33444. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  33445. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  33446. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  33447. AssertIntEQ(WOLFSSL_FAILURE, ret);
  33448. /* inherit flags test : VPARAM_DEFAULT */
  33449. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33450. AssertIntEQ(1, ret);
  33451. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33452. (int)XSTRLEN(testhostName1)));
  33453. AssertIntEQ(0x01, paramTo->hostFlags);
  33454. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33455. /* inherit flags test : VPARAM OVERWRITE */
  33456. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33457. (int)XSTRLEN(testhostName2));
  33458. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33459. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33460. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  33461. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33462. AssertIntEQ(1, ret);
  33463. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33464. (int)XSTRLEN(testhostName1)));
  33465. AssertIntEQ(0x01, paramTo->hostFlags);
  33466. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33467. /* inherit flags test : VPARAM_RESET_FLAGS */
  33468. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33469. (int)XSTRLEN(testhostName2));
  33470. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33471. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  33472. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  33473. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33474. AssertIntEQ(1, ret);
  33475. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33476. (int)XSTRLEN(testhostName1)));
  33477. AssertIntEQ(0x01, paramTo->hostFlags);
  33478. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33479. /* inherit flags test : VPARAM_LOCKED */
  33480. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33481. (int)XSTRLEN(testhostName2));
  33482. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33483. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33484. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  33485. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33486. AssertIntEQ(1, ret);
  33487. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  33488. (int)XSTRLEN(testhostName2)));
  33489. AssertIntEQ(0x00, paramTo->hostFlags);
  33490. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33491. /* test for incorrect parameters */
  33492. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  33493. AssertIntEQ(0, ret);
  33494. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  33495. AssertIntEQ(0, ret);
  33496. /* inherit flags test : VPARAM_ONCE, not testable yet */
  33497. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  33498. AssertIntEQ(1, ret);
  33499. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  33500. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  33501. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  33502. AssertIntEQ(1, ret);
  33503. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  33504. AssertIntEQ(0, ret);
  33505. X509_VERIFY_PARAM_free(paramTo);
  33506. X509_VERIFY_PARAM_free(paramFrom);
  33507. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  33508. res = TEST_RES_CHECK(1);
  33509. #endif
  33510. return res;
  33511. }
  33512. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  33513. static int test_wolfSSL_check_domain_verify_count = 0;
  33514. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  33515. WOLFSSL_X509_STORE_CTX* store)
  33516. {
  33517. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  33518. AssertIntEQ(preverify, 1);
  33519. test_wolfSSL_check_domain_verify_count++;
  33520. return 1;
  33521. }
  33522. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  33523. {
  33524. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  33525. /* Domain check should only be done on the leaf cert */
  33526. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  33527. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  33528. "wolfSSL Server Chain", 0), 1);
  33529. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  33530. test_wolfSSL_check_domain_verify_cb);
  33531. }
  33532. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  33533. {
  33534. /* Use a cert with different domains in chain */
  33535. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  33536. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  33537. }
  33538. static int test_wolfSSL_check_domain(void)
  33539. {
  33540. tcp_ready ready;
  33541. func_args client_args;
  33542. func_args server_args;
  33543. THREAD_TYPE serverThread;
  33544. callback_functions func_cb_client;
  33545. callback_functions func_cb_server;
  33546. XMEMSET(&client_args, 0, sizeof(func_args));
  33547. XMEMSET(&server_args, 0, sizeof(func_args));
  33548. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  33549. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  33550. #ifdef WOLFSSL_TIRTOS
  33551. fdOpenSession(Task_self());
  33552. #endif
  33553. StartTCP();
  33554. InitTcpReady(&ready);
  33555. #if defined(USE_WINDOWS_API)
  33556. /* use RNG to get random port if using windows */
  33557. ready.port = GetRandomPort();
  33558. #endif
  33559. server_args.signal = &ready;
  33560. client_args.signal = &ready;
  33561. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  33562. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  33563. client_args.callbacks = &func_cb_client;
  33564. server_args.callbacks = &func_cb_server;
  33565. start_thread(test_server_nofail, &server_args, &serverThread);
  33566. wait_tcp_ready(&server_args);
  33567. test_client_nofail(&client_args, NULL);
  33568. join_thread(serverThread);
  33569. AssertTrue(client_args.return_code);
  33570. AssertTrue(server_args.return_code);
  33571. FreeTcpReady(&ready);
  33572. /* Should have been called once for each cert in sent chain */
  33573. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  33574. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  33575. #else
  33576. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  33577. #endif
  33578. return TEST_RES_CHECK(1);
  33579. }
  33580. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  33581. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  33582. {
  33583. int res = TEST_SKIPPED;
  33584. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33585. X509* x509 = NULL;
  33586. X509_PUBKEY* pubKey;
  33587. AssertNotNull(x509 = X509_new());
  33588. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  33589. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  33590. X509_free(x509);
  33591. res = TEST_RES_CHECK(1);
  33592. #endif
  33593. return res;
  33594. }
  33595. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  33596. {
  33597. int res = TEST_SKIPPED;
  33598. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33599. !defined(NO_SHA256) && !defined(NO_RSA)
  33600. X509* x509 = NULL;
  33601. ASN1_OBJECT* obj = NULL;
  33602. const ASN1_OBJECT* pa_oid = NULL;
  33603. X509_PUBKEY* pubKey;
  33604. X509_PUBKEY* pubKey2;
  33605. EVP_PKEY* evpKey;
  33606. const unsigned char *pk;
  33607. int ppklen, pptype;
  33608. X509_ALGOR *pa;
  33609. const void *pval;
  33610. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33611. SSL_FILETYPE_PEM));
  33612. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33613. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33614. AssertNotNull(pk);
  33615. AssertNotNull(pa);
  33616. AssertNotNull(pubKey);
  33617. AssertIntGT(ppklen, 0);
  33618. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  33619. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33620. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  33621. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33622. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33623. AssertNotNull(pk);
  33624. AssertNotNull(pa);
  33625. AssertIntGT(ppklen, 0);
  33626. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33627. AssertNotNull(pa_oid);
  33628. AssertNull(pval);
  33629. AssertIntEQ(pptype, V_ASN1_NULL);
  33630. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  33631. X509_PUBKEY_free(pubKey2);
  33632. X509_free(x509);
  33633. EVP_PKEY_free(evpKey);
  33634. res = TEST_RES_CHECK(1);
  33635. #endif
  33636. return res;
  33637. }
  33638. static int test_wolfSSL_X509_PUBKEY_EC(void)
  33639. {
  33640. int res = TEST_SKIPPED;
  33641. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  33642. X509* x509 = NULL;
  33643. ASN1_OBJECT* obj = NULL;
  33644. ASN1_OBJECT* poid;
  33645. const ASN1_OBJECT* pa_oid = NULL;
  33646. X509_PUBKEY* pubKey;
  33647. X509_PUBKEY* pubKey2;
  33648. EVP_PKEY* evpKey;
  33649. const unsigned char *pk;
  33650. int ppklen, pptype;
  33651. X509_ALGOR *pa;
  33652. const void *pval;
  33653. char buf[50];
  33654. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  33655. SSL_FILETYPE_PEM));
  33656. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33657. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33658. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  33659. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33660. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33661. AssertNotNull(pk);
  33662. AssertNotNull(pa);
  33663. AssertIntGT(ppklen, 0);
  33664. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33665. AssertNotNull(pa_oid);
  33666. AssertNotNull(pval);
  33667. AssertIntEQ(pptype, V_ASN1_OBJECT);
  33668. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  33669. poid = (ASN1_OBJECT *)pval;
  33670. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  33671. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  33672. X509_PUBKEY_free(pubKey2);
  33673. X509_free(x509);
  33674. EVP_PKEY_free(evpKey);
  33675. res = TEST_RES_CHECK(1);
  33676. #endif
  33677. return res;
  33678. }
  33679. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  33680. {
  33681. int res = TEST_SKIPPED;
  33682. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  33683. word32 bytes;
  33684. #ifdef USE_CERT_BUFFERS_1024
  33685. byte tmp[ONEK_BUF];
  33686. #elif defined(USE_CERT_BUFFERS_2048)
  33687. byte tmp[TWOK_BUF];
  33688. #else
  33689. byte tmp[TWOK_BUF];
  33690. #endif /* END USE_CERT_BUFFERS_1024 */
  33691. const unsigned char* dsaKeyDer = tmp;
  33692. ASN1_OBJECT* obj = NULL;
  33693. ASN1_STRING* str;
  33694. const ASN1_OBJECT* pa_oid = NULL;
  33695. X509_PUBKEY* pubKey = NULL;
  33696. EVP_PKEY* evpKey = NULL;
  33697. const unsigned char *pk;
  33698. int ppklen, pptype;
  33699. X509_ALGOR *pa;
  33700. const void *pval;
  33701. #ifdef USE_CERT_BUFFERS_1024
  33702. XMEMSET(tmp, 0, sizeof(tmp));
  33703. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  33704. bytes = sizeof_dsa_key_der_1024;
  33705. #elif defined(USE_CERT_BUFFERS_2048)
  33706. XMEMSET(tmp, 0, sizeof(tmp));
  33707. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  33708. bytes = sizeof_dsa_key_der_2048;
  33709. #else
  33710. {
  33711. XFILE fp;
  33712. XMEMSET(tmp, 0, sizeof(tmp));
  33713. fp = XFOPEN("./certs/dsa2048.der", "rb");
  33714. if (fp == XBADFILE) {
  33715. return WOLFSSL_BAD_FILE;
  33716. }
  33717. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  33718. XFCLOSE(fp);
  33719. }
  33720. #endif
  33721. /* Initialize pkey with der format dsa key */
  33722. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  33723. AssertNotNull(pubKey = X509_PUBKEY_new());
  33724. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  33725. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33726. AssertNotNull(pk);
  33727. AssertNotNull(pa);
  33728. AssertIntGT(ppklen, 0);
  33729. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33730. AssertNotNull(pa_oid);
  33731. AssertNotNull(pval);
  33732. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  33733. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  33734. str = (ASN1_STRING *)pval;
  33735. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  33736. #ifdef USE_CERT_BUFFERS_1024
  33737. AssertIntEQ(ASN1_STRING_length(str), 291);
  33738. #else
  33739. AssertIntEQ(ASN1_STRING_length(str), 549);
  33740. #endif /* END USE_CERT_BUFFERS_1024 */
  33741. X509_PUBKEY_free(pubKey);
  33742. EVP_PKEY_free(evpKey);
  33743. res = TEST_RES_CHECK(1);
  33744. #endif
  33745. return res;
  33746. }
  33747. static int test_wolfSSL_RAND(void)
  33748. {
  33749. int res = TEST_SKIPPED;
  33750. #if defined(OPENSSL_EXTRA)
  33751. byte seed[16];
  33752. XMEMSET(seed, 0, sizeof(seed));
  33753. RAND_seed(seed, sizeof(seed));
  33754. AssertIntEQ(RAND_poll(), 1);
  33755. RAND_cleanup();
  33756. AssertIntEQ(RAND_egd(NULL), -1);
  33757. #ifndef NO_FILESYSTEM
  33758. {
  33759. char fname[100];
  33760. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  33761. AssertIntEQ(RAND_write_file(NULL), 0);
  33762. }
  33763. #endif
  33764. res = TEST_RES_CHECK(1);
  33765. #endif
  33766. return res;
  33767. }
  33768. static int test_wolfSSL_BUF(void)
  33769. {
  33770. int res = TEST_SKIPPED;
  33771. #if defined(OPENSSL_EXTRA)
  33772. BUF_MEM* buf;
  33773. AssertNotNull(buf = BUF_MEM_new());
  33774. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  33775. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  33776. BUF_MEM_free(buf);
  33777. res = TEST_RES_CHECK(1);
  33778. #endif
  33779. return res;
  33780. }
  33781. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33782. static int stub_rand_seed(const void *buf, int num)
  33783. {
  33784. (void)buf;
  33785. (void)num;
  33786. return 123;
  33787. }
  33788. static int stub_rand_bytes(unsigned char *buf, int num)
  33789. {
  33790. (void)buf;
  33791. (void)num;
  33792. return 456;
  33793. }
  33794. static byte* was_stub_rand_cleanup_called(void)
  33795. {
  33796. static byte was_called = 0;
  33797. return &was_called;
  33798. }
  33799. static void stub_rand_cleanup(void)
  33800. {
  33801. byte* was_called = was_stub_rand_cleanup_called();
  33802. *was_called = 1;
  33803. return;
  33804. }
  33805. static byte* was_stub_rand_add_called(void)
  33806. {
  33807. static byte was_called = 0;
  33808. return &was_called;
  33809. }
  33810. static int stub_rand_add(const void *buf, int num, double entropy)
  33811. {
  33812. byte* was_called = was_stub_rand_add_called();
  33813. (void)buf;
  33814. (void)num;
  33815. (void)entropy;
  33816. *was_called = 1;
  33817. return 0;
  33818. }
  33819. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  33820. {
  33821. (void)buf;
  33822. (void)num;
  33823. return 9876;
  33824. }
  33825. static int stub_rand_status(void)
  33826. {
  33827. return 5432;
  33828. }
  33829. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33830. static int test_wolfSSL_RAND_set_rand_method(void)
  33831. {
  33832. int res = TEST_SKIPPED;
  33833. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33834. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  33835. unsigned char* buf = NULL;
  33836. int num = 0;
  33837. double entropy = 0;
  33838. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  33839. byte* was_add_called = was_stub_rand_add_called();
  33840. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  33841. DYNAMIC_TYPE_TMP_BUFFER);
  33842. AssertIntNE(wolfSSL_RAND_status(), 5432);
  33843. AssertIntEQ(*was_cleanup_called, 0);
  33844. RAND_cleanup();
  33845. AssertIntEQ(*was_cleanup_called, 0);
  33846. rand_methods.seed = &stub_rand_seed;
  33847. rand_methods.bytes = &stub_rand_bytes;
  33848. rand_methods.cleanup = &stub_rand_cleanup;
  33849. rand_methods.add = &stub_rand_add;
  33850. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  33851. rand_methods.status = &stub_rand_status;
  33852. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  33853. AssertIntEQ(RAND_seed(buf, num), 123);
  33854. AssertIntEQ(RAND_bytes(buf, num), 456);
  33855. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  33856. AssertIntEQ(RAND_status(), 5432);
  33857. AssertIntEQ(*was_add_called, 0);
  33858. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  33859. RAND_add(buf, num, entropy);
  33860. AssertIntEQ(*was_add_called, 1);
  33861. was_add_called = 0;
  33862. AssertIntEQ(*was_cleanup_called, 0);
  33863. RAND_cleanup();
  33864. AssertIntEQ(*was_cleanup_called, 1);
  33865. *was_cleanup_called = 0;
  33866. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  33867. AssertIntNE(RAND_status(), 5432);
  33868. AssertIntEQ(*was_cleanup_called, 0);
  33869. RAND_cleanup();
  33870. AssertIntEQ(*was_cleanup_called, 0);
  33871. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33872. res = TEST_RES_CHECK(1);
  33873. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33874. return res;
  33875. }
  33876. static int test_wolfSSL_RAND_bytes(void)
  33877. {
  33878. int res = TEST_SKIPPED;
  33879. #if defined(OPENSSL_EXTRA)
  33880. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  33881. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  33882. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  33883. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  33884. int max_bufsize;
  33885. byte *my_buf;
  33886. /* sanity check */
  33887. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  33888. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  33889. max_bufsize = size4;
  33890. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  33891. DYNAMIC_TYPE_TMP_BUFFER);
  33892. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  33893. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  33894. AssertNotNull(my_buf);
  33895. XMEMSET(my_buf, 0, max_bufsize);
  33896. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  33897. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  33898. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  33899. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  33900. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33901. res = TEST_RES_CHECK(1);
  33902. #endif
  33903. return res;
  33904. }
  33905. static int test_wolfSSL_BN_rand(void)
  33906. {
  33907. int res = TEST_SKIPPED;
  33908. #if defined(OPENSSL_EXTRA)
  33909. BIGNUM* bn;
  33910. BIGNUM* range;
  33911. /* Error conditions. */
  33912. /* NULL BN. */
  33913. AssertIntEQ(BN_rand(NULL, 0, 0, 0), SSL_FAILURE);
  33914. AssertNotNull(bn = BN_new());
  33915. /* Negative bits. */
  33916. AssertIntEQ(BN_rand(bn, -2, 0, 0), SSL_FAILURE);
  33917. /* 0 bits and top is not -1. */
  33918. AssertIntEQ(BN_rand(bn, 0, 1, 0), SSL_FAILURE);
  33919. /* 0 bits and bottom is not 0. */
  33920. AssertIntEQ(BN_rand(bn, 0, 0, 1), SSL_FAILURE);
  33921. /* 1 bit and top is 1. */
  33922. AssertIntEQ(BN_rand(bn, 1, 1, 0), SSL_FAILURE);
  33923. AssertIntEQ(BN_rand(bn, 0, -1, 0), SSL_SUCCESS);
  33924. AssertIntEQ(BN_num_bits(bn), 0);
  33925. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  33926. AssertIntEQ(BN_num_bits(bn), 8);
  33927. /* When top is 0, top bit should be 1. */
  33928. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  33929. AssertIntEQ(BN_rand(bn, 8, 1, 0), SSL_SUCCESS);
  33930. /* When top is 1, top 2 bits should be 1. */
  33931. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  33932. AssertIntEQ(BN_is_bit_set(bn, 6), SSL_SUCCESS);
  33933. AssertIntEQ(BN_rand(bn, 8, 0, 1), SSL_SUCCESS);
  33934. /* When bottom is 1, bottom bit should be 1. */
  33935. AssertIntEQ(BN_is_bit_set(bn, 0), SSL_SUCCESS);
  33936. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33937. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33938. * case, requesting a 13-bit random number would actually return a 16-bit
  33939. * random number. */
  33940. AssertIntEQ(BN_rand(bn, 13, 0, 0), SSL_SUCCESS);
  33941. AssertIntEQ(BN_num_bits(bn), 13);
  33942. AssertNotNull(range = BN_new());
  33943. AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
  33944. AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
  33945. BN_free(bn);
  33946. BN_free(range);
  33947. res = TEST_RES_CHECK(1);
  33948. #endif
  33949. return res;
  33950. }
  33951. static int test_wolfSSL_pseudo_rand(void)
  33952. {
  33953. int res = TEST_SKIPPED;
  33954. #if defined(OPENSSL_EXTRA)
  33955. BIGNUM* bn;
  33956. unsigned char bin[8];
  33957. int i;
  33958. /* BN_pseudo_rand returns 1 on success 0 on failure
  33959. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  33960. for (i = 0; i < 10; i++) {
  33961. AssertNotNull(bn = BN_new());
  33962. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  33963. AssertIntGT(BN_bn2bin(bn, bin),0);
  33964. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  33965. BN_free(bn);
  33966. }
  33967. for (i = 0; i < 10; i++) {
  33968. AssertNotNull(bn = BN_new());
  33969. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  33970. AssertIntGT(BN_bn2bin(bn, bin),0);
  33971. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  33972. BN_free(bn);
  33973. }
  33974. res = TEST_RES_CHECK(1);
  33975. #endif
  33976. return res;
  33977. }
  33978. static int test_wolfSSL_PKCS8_Compat(void)
  33979. {
  33980. int res = TEST_SKIPPED;
  33981. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  33982. #ifndef NO_BIO
  33983. PKCS8_PRIV_KEY_INFO* pt;
  33984. BIO* bio;
  33985. XFILE f;
  33986. int bytes;
  33987. char pkcs8_buffer[512];
  33988. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33989. EVP_PKEY *pkey = NULL;
  33990. #endif
  33991. /* file from wolfssl/certs/ directory */
  33992. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  33993. AssertTrue(f != XBADFILE);
  33994. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  33995. XFCLOSE(f);
  33996. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33997. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  33998. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33999. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  34000. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  34001. /* gets PKCS8 pointer to pkey */
  34002. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  34003. EVP_PKEY_free(pkey);
  34004. #endif
  34005. BIO_free(bio);
  34006. PKCS8_PRIV_KEY_INFO_free(pt);
  34007. res = TEST_RES_CHECK(1);
  34008. #endif
  34009. #endif
  34010. return res;
  34011. }
  34012. static int test_wolfSSL_PKCS8_d2i(void)
  34013. {
  34014. int res = TEST_SKIPPED;
  34015. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  34016. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  34017. * requires a 12 byte password in FIPS mode. This test ends up
  34018. * trying to use an 8 byte password. */
  34019. #ifndef NO_FILESYSTEM
  34020. unsigned char pkcs8_buffer[2048];
  34021. const unsigned char* p;
  34022. int bytes;
  34023. XFILE file;
  34024. WOLFSSL_EVP_PKEY* pkey = NULL;
  34025. #ifndef NO_BIO
  34026. BIO* bio;
  34027. #if defined(OPENSSL_ALL) && \
  34028. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  34029. defined(HAVE_ECC)) && \
  34030. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  34031. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  34032. #endif
  34033. #endif
  34034. #ifndef NO_RSA
  34035. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  34036. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  34037. #ifndef NO_DES3
  34038. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  34039. #endif
  34040. #endif /* NO_RSA */
  34041. #ifdef HAVE_ECC
  34042. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  34043. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  34044. #ifndef NO_DES3
  34045. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  34046. #endif
  34047. #endif /* HAVE_ECC */
  34048. #endif /* !NO_FILESYSTEM */
  34049. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  34050. #ifndef NO_RSA
  34051. #ifdef USE_CERT_BUFFERS_1024
  34052. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  34053. int rsaSz = sizeof_server_key_der_1024;
  34054. #else
  34055. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  34056. int rsaSz = sizeof_server_key_der_2048;
  34057. #endif
  34058. #endif
  34059. #ifdef HAVE_ECC
  34060. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  34061. int ecSz = sizeof_ecc_key_der_256;
  34062. #endif
  34063. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  34064. #ifndef NO_FILESYSTEM
  34065. (void)pkcs8_buffer;
  34066. (void)p;
  34067. (void)bytes;
  34068. (void)file;
  34069. #ifndef NO_BIO
  34070. (void)bio;
  34071. #endif
  34072. #endif
  34073. #ifdef OPENSSL_ALL
  34074. #ifndef NO_RSA
  34075. /* Try to auto-detect normal RSA private key */
  34076. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  34077. EVP_PKEY_free(pkey);
  34078. #endif
  34079. #ifdef HAVE_ECC
  34080. /* Try to auto-detect normal EC private key */
  34081. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  34082. EVP_PKEY_free(pkey);
  34083. #endif
  34084. #endif /* OPENSSL_ALL */
  34085. #ifndef NO_FILESYSTEM
  34086. #ifndef NO_RSA
  34087. /* Get DER encoded RSA PKCS#8 data. */
  34088. file = XFOPEN(rsaDerPkcs8File, "rb");
  34089. AssertTrue(file != XBADFILE);
  34090. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  34091. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  34092. file)), 0);
  34093. XFCLOSE(file);
  34094. p = pkcs8_buffer;
  34095. #ifdef OPENSSL_ALL
  34096. /* Try to decode - auto-detect key type. */
  34097. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  34098. #else
  34099. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  34100. #endif
  34101. /* Get PEM encoded RSA PKCS#8 data. */
  34102. file = XFOPEN(rsaPemPkcs8File, "rb");
  34103. AssertTrue(file != XBADFILE);
  34104. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  34105. file)), 0);
  34106. XFCLOSE(file);
  34107. #if defined(OPENSSL_ALL) && \
  34108. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  34109. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34110. /* Write PKCS#8 PEM to BIO. */
  34111. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  34112. NULL), bytes);
  34113. /* Compare file and written data */
  34114. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  34115. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  34116. BIO_free(bio);
  34117. #if !defined(NO_DES3) && !defined(NO_SHA)
  34118. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34119. /* Write Encrypted PKCS#8 PEM to BIO. */
  34120. bytes = 1834;
  34121. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  34122. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  34123. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  34124. (void*)"yassl123"));
  34125. EVP_PKEY_free(evpPkey);
  34126. BIO_free(bio);
  34127. #endif /* !NO_DES3 && !NO_SHA */
  34128. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  34129. EVP_PKEY_free(pkey);
  34130. /* PKCS#8 encrypted RSA key */
  34131. #ifndef NO_DES3
  34132. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  34133. AssertTrue(file != XBADFILE);
  34134. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  34135. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  34136. file)), 0);
  34137. XFCLOSE(file);
  34138. #if defined(OPENSSL_ALL) && \
  34139. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  34140. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  34141. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  34142. (void*)"yassl123"));
  34143. EVP_PKEY_free(pkey);
  34144. BIO_free(bio);
  34145. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  34146. #endif /* !NO_DES3 */
  34147. #endif /* NO_RSA */
  34148. #ifdef HAVE_ECC
  34149. /* PKCS#8 encode EC key */
  34150. file = XFOPEN(ecDerPkcs8File, "rb");
  34151. AssertTrue(file != XBADFILE);
  34152. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  34153. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  34154. file)), 0);
  34155. XFCLOSE(file);
  34156. p = pkcs8_buffer;
  34157. #ifdef OPENSSL_ALL
  34158. /* Try to decode - auto-detect key type. */
  34159. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  34160. #else
  34161. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  34162. #endif
  34163. /* Get PEM encoded RSA PKCS#8 data. */
  34164. file = XFOPEN(ecPemPkcs8File, "rb");
  34165. AssertTrue(file != XBADFILE);
  34166. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  34167. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  34168. file)), 0);
  34169. XFCLOSE(file);
  34170. #if defined(OPENSSL_ALL) && \
  34171. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  34172. defined(HAVE_AES_CBC)
  34173. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34174. /* Write PKCS#8 PEM to BIO. */
  34175. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  34176. NULL), bytes);
  34177. /* Compare file and written data */
  34178. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  34179. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  34180. BIO_free(bio);
  34181. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34182. /* Write Encrypted PKCS#8 PEM to BIO. */
  34183. bytes = 379;
  34184. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  34185. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  34186. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  34187. (void*)"yassl123"));
  34188. EVP_PKEY_free(evpPkey);
  34189. BIO_free(bio);
  34190. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  34191. EVP_PKEY_free(pkey);
  34192. /* PKCS#8 encrypted EC key */
  34193. #ifndef NO_DES3
  34194. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  34195. AssertTrue(file != XBADFILE);
  34196. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  34197. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  34198. file)), 0);
  34199. XFCLOSE(file);
  34200. #if defined(OPENSSL_ALL) && \
  34201. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  34202. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  34203. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  34204. (void*)"yassl123"));
  34205. EVP_PKEY_free(pkey);
  34206. BIO_free(bio);
  34207. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  34208. #endif /* !NO_DES3 */
  34209. #endif /* HAVE_ECC */
  34210. #endif /* !NO_FILESYSTEM */
  34211. res = TEST_RES_CHECK(1);
  34212. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  34213. return res;
  34214. }
  34215. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  34216. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  34217. #define LOGGING_THREADS 5
  34218. #define ERROR_COUNT 10
  34219. /* copied from logging.c since this is not exposed otherwise */
  34220. #ifndef ERROR_QUEUE_MAX
  34221. #ifdef ERROR_QUEUE_PER_THREAD
  34222. #define ERROR_QUEUE_MAX 16
  34223. #else
  34224. /* this breaks from compat of unlimited error queue size */
  34225. #define ERROR_QUEUE_MAX 100
  34226. #endif
  34227. #endif
  34228. static volatile int loggingThreadsReady;
  34229. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  34230. {
  34231. const char* file;
  34232. int line;
  34233. unsigned long err;
  34234. int errorCount = 0;
  34235. int i;
  34236. (void)args;
  34237. while (!loggingThreadsReady);
  34238. for (i = 0; i < ERROR_COUNT; i++)
  34239. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  34240. while ((err = ERR_get_error_line(&file, &line))) {
  34241. AssertIntEQ(err, 990 + errorCount);
  34242. errorCount++;
  34243. }
  34244. AssertIntEQ(errorCount, ERROR_COUNT);
  34245. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  34246. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  34247. errorCount = 0;
  34248. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  34249. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  34250. while ((err = ERR_get_error_line(&file, &line))) {
  34251. AssertIntEQ(err, 990 + errorCount);
  34252. errorCount++;
  34253. }
  34254. /* test that the 3 errors over the max were dropped */
  34255. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  34256. return 0;
  34257. }
  34258. #endif
  34259. static int test_error_queue_per_thread(void)
  34260. {
  34261. int res = TEST_SKIPPED;
  34262. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  34263. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  34264. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  34265. int i;
  34266. ERR_clear_error(); /* clear out any error nodes */
  34267. loggingThreadsReady = 0;
  34268. for (i = 0; i < LOGGING_THREADS; i++)
  34269. start_thread(test_logging, NULL, &loggingThreads[i]);
  34270. loggingThreadsReady = 1;
  34271. for (i = 0; i < LOGGING_THREADS; i++)
  34272. join_thread(loggingThreads[i]);
  34273. res = TEST_RES_CHECK(1);
  34274. #endif
  34275. return res;
  34276. }
  34277. static int test_wolfSSL_ERR_put_error(void)
  34278. {
  34279. int res = TEST_SKIPPED;
  34280. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34281. defined(DEBUG_WOLFSSL)
  34282. const char* file;
  34283. int line;
  34284. ERR_clear_error(); /* clear out any error nodes */
  34285. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  34286. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  34287. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  34288. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  34289. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  34290. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  34291. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  34292. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  34293. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  34294. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  34295. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  34296. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  34297. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  34298. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  34299. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  34300. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  34301. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  34302. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  34303. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  34304. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  34305. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  34306. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  34307. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  34308. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  34309. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  34310. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  34311. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  34312. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  34313. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  34314. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  34315. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  34316. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  34317. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  34318. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  34319. "this file", 100);
  34320. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  34321. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  34322. AssertIntEQ(line, 100);
  34323. AssertIntEQ(wolfSSL_ERR_peek_error(),
  34324. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  34325. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  34326. #endif
  34327. /* try reading past end of error queue */
  34328. file = NULL;
  34329. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  34330. AssertNull(file);
  34331. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  34332. PEMerr(4,4);
  34333. AssertIntEQ(ERR_get_error(), 4);
  34334. /* Empty and free up all error nodes */
  34335. ERR_clear_error();
  34336. /* Verify all nodes are cleared */
  34337. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  34338. ERR_clear_error();
  34339. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  34340. res = TEST_RES_CHECK(1);
  34341. #endif
  34342. return res;
  34343. }
  34344. /*
  34345. * This is a regression test for a bug where the peek/get error functions were
  34346. * drawing from the end of the queue rather than the front.
  34347. */
  34348. static int test_wolfSSL_ERR_get_error_order(void)
  34349. {
  34350. int res = TEST_SKIPPED;
  34351. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  34352. /* Empty the queue. */
  34353. wolfSSL_ERR_clear_error();
  34354. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  34355. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  34356. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  34357. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  34358. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  34359. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  34360. res = TEST_RES_CHECK(1);
  34361. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  34362. return res;
  34363. }
  34364. #ifndef NO_BIO
  34365. static int test_wolfSSL_ERR_print_errors(void)
  34366. {
  34367. int res = TEST_SKIPPED;
  34368. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34369. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  34370. BIO* bio;
  34371. char buf[1024];
  34372. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34373. ERR_clear_error(); /* clear out any error nodes */
  34374. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  34375. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  34376. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  34377. ERR_print_errors(bio);
  34378. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  34379. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  34380. buf, 55), 0);
  34381. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  34382. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  34383. buf, 56), 0);
  34384. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  34385. AssertIntEQ(buf[0], '\0');
  34386. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  34387. BIO_free(bio);
  34388. res = TEST_RES_CHECK(1);
  34389. #endif
  34390. return res;
  34391. }
  34392. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34393. defined(DEBUG_WOLFSSL)
  34394. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  34395. {
  34396. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  34397. return 0;
  34398. }
  34399. #endif
  34400. static int test_wolfSSL_ERR_print_errors_cb(void)
  34401. {
  34402. int res = TEST_SKIPPED;
  34403. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34404. defined(DEBUG_WOLFSSL)
  34405. BIO* bio;
  34406. char buf[1024];
  34407. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34408. ERR_clear_error(); /* clear out any error nodes */
  34409. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  34410. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  34411. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  34412. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  34413. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  34414. buf, 53), 0);
  34415. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  34416. buf + 53, 55), 0);
  34417. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  34418. BIO_free(bio);
  34419. res = TEST_RES_CHECK(1);
  34420. #endif
  34421. return res;
  34422. }
  34423. /*
  34424. * Testing WOLFSSL_ERROR_MSG
  34425. */
  34426. static int test_WOLFSSL_ERROR_MSG(void)
  34427. {
  34428. int res = TEST_SKIPPED;
  34429. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  34430. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  34431. const char* msg = TEST_STRING;
  34432. WOLFSSL_ERROR_MSG(msg);
  34433. res = TEST_RES_CHECK(1);
  34434. #endif
  34435. return res;
  34436. }/*End test_WOLFSSL_ERROR_MSG*/
  34437. /*
  34438. * Testing wc_ERR_remove_state
  34439. */
  34440. static int test_wc_ERR_remove_state(void)
  34441. {
  34442. int res = TEST_SKIPPED;
  34443. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34444. wc_ERR_remove_state();
  34445. res = TEST_RES_CHECK(1);
  34446. #endif
  34447. return res;
  34448. }/*End test_wc_ERR_remove_state*/
  34449. /*
  34450. * Testing wc_ERR_print_errors_fp
  34451. */
  34452. static int test_wc_ERR_print_errors_fp(void)
  34453. {
  34454. int res = TEST_SKIPPED;
  34455. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  34456. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  34457. long sz;
  34458. XFILE fp;
  34459. int ret = 0;
  34460. WOLFSSL_ERROR(BAD_FUNC_ARG);
  34461. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  34462. wc_ERR_print_errors_fp(fp);
  34463. #if defined(DEBUG_WOLFSSL)
  34464. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  34465. sz = XFTELL(fp);
  34466. #ifdef NO_ERROR_QUEUE
  34467. /* File should be empty when NO_ERROR_QUEUE is defined */
  34468. if (sz != 0) {
  34469. ret = BAD_FUNC_ARG;
  34470. }
  34471. #else
  34472. if (sz == 0) {
  34473. ret = BAD_FUNC_ARG;
  34474. }
  34475. #endif
  34476. #endif
  34477. XFCLOSE(fp);
  34478. (void)sz;
  34479. res = TEST_RES_CHECK(ret == 0);
  34480. #endif
  34481. return res;
  34482. }/*End test_wc_ERR_print_errors_fp*/
  34483. #ifdef DEBUG_WOLFSSL
  34484. static void Logging_cb(const int logLevel, const char *const logMessage)
  34485. {
  34486. (void)logLevel;
  34487. (void)logMessage;
  34488. }
  34489. #endif
  34490. /*
  34491. * Testing wolfSSL_GetLoggingCb
  34492. */
  34493. static int test_wolfSSL_GetLoggingCb(void)
  34494. {
  34495. int ret = 0;
  34496. #ifdef DEBUG_WOLFSSL
  34497. /* Testing without wolfSSL_SetLoggingCb() */
  34498. if (ret == 0) {
  34499. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  34500. ret = 0;
  34501. }
  34502. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  34503. ret = -1;
  34504. }
  34505. }
  34506. /* Testing with wolfSSL_SetLoggingCb() */
  34507. if (ret == 0) {
  34508. ret = wolfSSL_SetLoggingCb(Logging_cb);
  34509. if (ret == 0) {
  34510. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  34511. ret = -1;
  34512. }
  34513. if (ret == 0) {
  34514. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  34515. ret = 0;
  34516. }
  34517. }
  34518. /* reset logging callback */
  34519. wolfSSL_SetLoggingCb(NULL);
  34520. }
  34521. }
  34522. #endif
  34523. if (ret == 0) {
  34524. if (wolfSSL_GetLoggingCb() != NULL) {
  34525. ret = -1;
  34526. }
  34527. }
  34528. return TEST_RES_CHECK(ret == 0);
  34529. }/*End test_wolfSSL_GetLoggingCb*/
  34530. #endif /* !NO_BIO */
  34531. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34532. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34533. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34534. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  34535. {
  34536. static const unsigned char key[] = "simple test key";
  34537. HMAC_CTX* hmac;
  34538. ENGINE* e = NULL;
  34539. unsigned char hash[WC_MAX_DIGEST_SIZE];
  34540. unsigned int len;
  34541. AssertNotNull(hmac = HMAC_CTX_new());
  34542. HMAC_CTX_init(hmac);
  34543. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  34544. SSL_SUCCESS);
  34545. /* re-using test key as data to hash */
  34546. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  34547. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  34548. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  34549. AssertIntEQ(len, md_len);
  34550. AssertIntEQ(HMAC_size(hmac), md_len);
  34551. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  34552. HMAC_cleanup(hmac);
  34553. HMAC_CTX_free(hmac);
  34554. len = 0;
  34555. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  34556. AssertIntEQ(len, md_len);
  34557. return 0;
  34558. }
  34559. #endif
  34560. static int test_wolfSSL_HMAC(void)
  34561. {
  34562. int res = TEST_SKIPPED;
  34563. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34564. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34565. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34566. #ifndef NO_SHA256
  34567. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  34568. #endif
  34569. #ifdef WOLFSSL_SHA224
  34570. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  34571. #endif
  34572. #ifdef WOLFSSL_SHA384
  34573. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  34574. #endif
  34575. #ifdef WOLFSSL_SHA512
  34576. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  34577. #endif
  34578. #ifdef WOLFSSL_SHA3
  34579. #ifndef WOLFSSL_NOSHA3_224
  34580. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  34581. #endif
  34582. #ifndef WOLFSSL_NOSHA3_256
  34583. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  34584. #endif
  34585. #ifndef WOLFSSL_NOSHA3_384
  34586. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  34587. #endif
  34588. #ifndef WOLFSSL_NOSHA3_512
  34589. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  34590. #endif
  34591. #endif
  34592. #ifndef NO_SHA
  34593. test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE);
  34594. #endif
  34595. res = TEST_RES_CHECK(1);
  34596. #endif
  34597. return res;
  34598. }
  34599. static int test_wolfSSL_CMAC(void)
  34600. {
  34601. int res = TEST_SKIPPED;
  34602. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  34603. defined(WOLFSSL_AES_DIRECT)
  34604. int i;
  34605. byte key[AES_128_KEY_SIZE];
  34606. CMAC_CTX* cmacCtx = NULL;
  34607. byte out[AES_BLOCK_SIZE];
  34608. size_t outLen = AES_BLOCK_SIZE;
  34609. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  34610. key[i] = i;
  34611. }
  34612. AssertNotNull(cmacCtx = CMAC_CTX_new());
  34613. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  34614. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34615. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  34616. NULL), SSL_SUCCESS);
  34617. /* re-using test key as data to hash */
  34618. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  34619. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  34620. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  34621. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  34622. CMAC_CTX_free(cmacCtx);
  34623. /* give a key too small for the cipher, verify we get failure */
  34624. cmacCtx = NULL;
  34625. AssertNotNull(cmacCtx = CMAC_CTX_new());
  34626. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34627. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  34628. NULL), SSL_FAILURE);
  34629. CMAC_CTX_free(cmacCtx);
  34630. res = TEST_RES_CHECK(1);
  34631. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  34632. return res;
  34633. }
  34634. static int test_wolfSSL_OBJ(void)
  34635. {
  34636. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  34637. * mode
  34638. */
  34639. int res = TEST_SKIPPED;
  34640. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  34641. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  34642. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  34643. ASN1_OBJECT *obj = NULL;
  34644. ASN1_OBJECT *obj2 = NULL;
  34645. char buf[50];
  34646. XFILE fp;
  34647. X509 *x509 = NULL;
  34648. X509_NAME *x509Name;
  34649. X509_NAME_ENTRY *x509NameEntry;
  34650. ASN1_OBJECT *asn1Name = NULL;
  34651. int numNames;
  34652. BIO *bio = NULL;
  34653. int nid;
  34654. int i, j;
  34655. const char *f[] = {
  34656. #ifndef NO_RSA
  34657. "./certs/ca-cert.der",
  34658. #endif
  34659. #ifdef HAVE_ECC
  34660. "./certs/ca-ecc-cert.der",
  34661. "./certs/ca-ecc384-cert.der",
  34662. #endif
  34663. NULL};
  34664. ASN1_OBJECT *field_name_obj = NULL;
  34665. int lastpos = -1;
  34666. int tmp = -1;
  34667. ASN1_STRING *asn1 = NULL;
  34668. unsigned char *buf_dyn = NULL;
  34669. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  34670. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34671. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  34672. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  34673. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34674. ASN1_OBJECT_free(obj);
  34675. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  34676. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  34677. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  34678. #ifdef WOLFSSL_CERT_EXT
  34679. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  34680. #endif
  34681. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34682. AssertNotNull(obj2 = OBJ_dup(obj));
  34683. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  34684. ASN1_OBJECT_free(obj);
  34685. ASN1_OBJECT_free(obj2);
  34686. for (i = 0; f[i] != NULL; i++)
  34687. {
  34688. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  34689. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  34690. XFCLOSE(fp);
  34691. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  34692. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34693. /* Get the Common Name by using OBJ_txt2obj */
  34694. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  34695. do
  34696. {
  34697. lastpos = tmp;
  34698. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  34699. } while (tmp > -1);
  34700. AssertIntNE(lastpos, -1);
  34701. ASN1_OBJECT_free(field_name_obj);
  34702. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  34703. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  34704. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  34705. /*
  34706. * All Common Names should be www.wolfssl.com
  34707. * This makes testing easier as we can test for the expected value.
  34708. */
  34709. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  34710. OPENSSL_free(buf_dyn);
  34711. bio = BIO_new(BIO_s_mem());
  34712. AssertTrue(bio != NULL);
  34713. for (j = 0; j < numNames; j++)
  34714. {
  34715. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34716. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  34717. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34718. }
  34719. BIO_free(bio);
  34720. X509_free(x509);
  34721. }
  34722. #ifdef HAVE_PKCS12
  34723. {
  34724. PKCS12 *p12;
  34725. int boolRet;
  34726. EVP_PKEY *pkey = NULL;
  34727. const char *p12_f[] = {
  34728. #if !defined(NO_DES3) && !defined(NO_RSA)
  34729. "./certs/test-servercert.p12",
  34730. #endif
  34731. NULL};
  34732. for (i = 0; p12_f[i] != NULL; i++)
  34733. {
  34734. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  34735. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  34736. XFCLOSE(fp);
  34737. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  34738. &pkey, &x509, NULL)) > 0);
  34739. wc_PKCS12_free(p12);
  34740. EVP_PKEY_free(pkey);
  34741. x509Name = X509_get_issuer_name(x509);
  34742. AssertNotNull(x509Name);
  34743. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34744. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34745. for (j = 0; j < numNames; j++)
  34746. {
  34747. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34748. AssertNotNull(asn1Name =
  34749. X509_NAME_ENTRY_get_object(x509NameEntry));
  34750. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34751. }
  34752. BIO_free(bio);
  34753. X509_free(x509);
  34754. }
  34755. }
  34756. #endif /* HAVE_PKCS12 */
  34757. res = TEST_RES_CHECK(1);
  34758. #endif
  34759. return res;
  34760. }
  34761. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  34762. {
  34763. int res = TEST_SKIPPED;
  34764. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  34765. ASN1_OBJECT *obj = NULL;
  34766. BIO *bio = NULL;
  34767. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  34768. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34769. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  34770. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  34771. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  34772. BIO_free(bio);
  34773. ASN1_OBJECT_free(obj);
  34774. res = TEST_RES_CHECK(1);
  34775. #endif
  34776. return res;
  34777. }
  34778. static int test_wolfSSL_OBJ_cmp(void)
  34779. {
  34780. int res = TEST_SKIPPED;
  34781. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  34782. ASN1_OBJECT *obj = NULL;
  34783. ASN1_OBJECT *obj2 = NULL;
  34784. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34785. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  34786. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  34787. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  34788. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  34789. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  34790. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  34791. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  34792. ASN1_OBJECT_free(obj);
  34793. ASN1_OBJECT_free(obj2);
  34794. res = TEST_RES_CHECK(1);
  34795. #endif
  34796. return res;
  34797. }
  34798. static int test_wolfSSL_OBJ_txt2nid(void)
  34799. {
  34800. int res = TEST_SKIPPED;
  34801. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  34802. int i;
  34803. static const struct {
  34804. const char* sn;
  34805. const char* ln;
  34806. const char* oid;
  34807. int nid;
  34808. } testVals[] = {
  34809. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  34810. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  34811. NID_id_on_dnsSRV },
  34812. { "msUPN", "Microsoft User Principal Name",
  34813. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  34814. { NULL, NULL, NULL, NID_undef }
  34815. };
  34816. /* Invalid cases */
  34817. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  34818. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  34819. /* Valid cases */
  34820. for (i = 0; testVals[i].sn != NULL; i++) {
  34821. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  34822. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  34823. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  34824. }
  34825. res = TEST_RES_CHECK(1);
  34826. #endif
  34827. return res;
  34828. }
  34829. static int test_wolfSSL_OBJ_txt2obj(void)
  34830. {
  34831. int res = TEST_SKIPPED;
  34832. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  34833. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  34834. int i;
  34835. char buf[50];
  34836. ASN1_OBJECT* obj;
  34837. static const struct {
  34838. const char* oidStr;
  34839. const char* sn;
  34840. const char* ln;
  34841. } objs_list[] = {
  34842. #if defined(WOLFSSL_APACHE_HTTPD)
  34843. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  34844. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  34845. #endif
  34846. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  34847. { NULL, NULL, NULL }
  34848. };
  34849. static const struct {
  34850. const char* numeric;
  34851. const char* name;
  34852. } objs_named[] = {
  34853. /* In dictionary but not in normal list. */
  34854. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  34855. /* Made up OID. */
  34856. { "1.3.5.7", "1.3.5.7" },
  34857. { NULL, NULL }
  34858. };
  34859. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  34860. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  34861. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  34862. /* Test numerical value of oid (oidStr) */
  34863. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  34864. /* Convert object back to text to confirm oid is correct */
  34865. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34866. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34867. ASN1_OBJECT_free(obj);
  34868. XMEMSET(buf, 0, sizeof(buf));
  34869. /* Test short name (sn) */
  34870. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  34871. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  34872. /* Convert object back to text to confirm oid is correct */
  34873. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34874. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34875. ASN1_OBJECT_free(obj);
  34876. XMEMSET(buf, 0, sizeof(buf));
  34877. /* Test long name (ln) - should fail when no_name = 1 */
  34878. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  34879. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  34880. /* Convert object back to text to confirm oid is correct */
  34881. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34882. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34883. ASN1_OBJECT_free(obj);
  34884. XMEMSET(buf, 0, sizeof(buf));
  34885. }
  34886. for (i = 0; objs_named[i].numeric != NULL; i++) {
  34887. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  34888. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  34889. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  34890. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34891. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  34892. ASN1_OBJECT_free(obj);
  34893. }
  34894. res = TEST_RES_CHECK(1);
  34895. #endif
  34896. return res;
  34897. }
  34898. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  34899. {
  34900. int res = TEST_SKIPPED;
  34901. #if defined(OPENSSL_EXTRA) && \
  34902. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  34903. char buf[50] = {0};
  34904. ASN1_OBJECT* obj;
  34905. const char* oid = "2.5.29.19";
  34906. const char* ln = "X509v3 Basic Constraints";
  34907. obj = NULL;
  34908. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  34909. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  34910. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  34911. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  34912. XMEMSET(buf, 0, sizeof(buf));
  34913. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  34914. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  34915. ASN1_OBJECT_free(obj);
  34916. res = TEST_RES_CHECK(1);
  34917. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  34918. return res;
  34919. }
  34920. static int test_wolfSSL_PEM_write_bio_X509(void)
  34921. {
  34922. int res = TEST_SKIPPED;
  34923. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  34924. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  34925. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  34926. !defined(NO_FILESYSTEM)
  34927. /* This test contains the hard coded expected
  34928. * lengths. Update if necessary */
  34929. FILE* fp = NULL;
  34930. WOLFSSL_EVP_PKEY *priv = NULL;
  34931. BIO* input = NULL;
  34932. BIO* output = NULL;
  34933. X509* x509a = NULL;
  34934. X509* x509b = NULL;
  34935. ASN1_TIME* notBeforeA = NULL;
  34936. ASN1_TIME* notAfterA = NULL;
  34937. ASN1_TIME* notBeforeB = NULL;
  34938. ASN1_TIME* notAfterB = NULL;
  34939. int expectedLen;
  34940. fp = XFOPEN("certs/server-key.pem", "rb");
  34941. AssertNotNull(fp);
  34942. priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL);
  34943. XFCLOSE(fp);
  34944. fp = NULL;
  34945. AssertNotNull(priv);
  34946. AssertNotNull(input = BIO_new_file(
  34947. "certs/test/cert-ext-multiple.pem", "rb"));
  34948. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  34949. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  34950. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34951. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  34952. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  34953. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  34954. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34955. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34956. /* compare length against expected */
  34957. expectedLen = 2000;
  34958. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34959. /* read exported X509 PEM back into struct, sanity check on export,
  34960. * make sure notBefore/notAfter are the same and certs are identical. */
  34961. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34962. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  34963. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  34964. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  34965. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  34966. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34967. X509_free(x509b);
  34968. /* Reset output buffer */
  34969. BIO_free(output);
  34970. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34971. /* Test forcing the AKID to be generated just from KeyIdentifier */
  34972. if (x509a->authKeyIdSrc != NULL) {
  34973. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  34974. x509a->authKeyId = x509a->authKeyIdSrc;
  34975. x509a->authKeyIdSrc = NULL;
  34976. x509a->authKeyIdSrcSz = 0;
  34977. }
  34978. /* Resign to re-generate the der */
  34979. AssertIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  34980. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34981. /* Check that we generate a smaller output since the AKID will
  34982. * only contain the KeyIdentifier without any additional
  34983. * information */
  34984. /* Here we copy the validity struct from the original */
  34985. expectedLen = 1688;
  34986. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34987. /* Reset buffers and x509 */
  34988. BIO_free(input);
  34989. BIO_free(output);
  34990. X509_free(x509a);
  34991. /* test CA and basicConstSet values are encoded when
  34992. * the cert is a CA */
  34993. AssertNotNull(input = BIO_new_file(
  34994. "certs/server-cert.pem", "rb"));
  34995. /* read PEM into X509 struct */
  34996. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34997. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  34998. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34999. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  35000. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  35001. * values are maintained and certs are identical.*/
  35002. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  35003. AssertIntEQ(x509b->isCa, 1);
  35004. AssertIntEQ(x509b->basicConstSet, 1);
  35005. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  35006. X509_free(x509a);
  35007. X509_free(x509b);
  35008. BIO_free(input);
  35009. BIO_free(output);
  35010. /* test CA and basicConstSet values are encoded when
  35011. * the cert is not CA */
  35012. AssertNotNull(input = BIO_new_file(
  35013. "certs/client-uri-cert.pem", "rb"));
  35014. /* read PEM into X509 struct */
  35015. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  35016. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  35017. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  35018. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  35019. /* read exported X509 PEM back into struct, ensure isCa and
  35020. * basicConstSet values are maintained and certs are identical */
  35021. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  35022. AssertIntEQ(x509b->isCa, 0);
  35023. AssertIntEQ(x509b->basicConstSet, 1);
  35024. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  35025. wolfSSL_EVP_PKEY_free(priv);
  35026. X509_free(x509a);
  35027. X509_free(x509b);
  35028. BIO_free(input);
  35029. BIO_free(output);
  35030. res = TEST_RES_CHECK(1);
  35031. #endif
  35032. return res;
  35033. }
  35034. static int test_wolfSSL_X509_NAME_ENTRY(void)
  35035. {
  35036. int res = TEST_SKIPPED;
  35037. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35038. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  35039. X509* x509;
  35040. #ifndef NO_BIO
  35041. BIO* bio;
  35042. #endif
  35043. X509_NAME* nm;
  35044. X509_NAME_ENTRY* entry;
  35045. unsigned char cn[] = "another name to add";
  35046. #ifdef OPENSSL_ALL
  35047. int i, names_len;
  35048. #endif
  35049. AssertNotNull(x509 =
  35050. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  35051. #ifndef NO_BIO
  35052. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35053. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  35054. #endif
  35055. #ifdef WOLFSSL_CERT_REQ
  35056. {
  35057. X509_REQ* req;
  35058. #ifndef NO_BIO
  35059. BIO* bReq;
  35060. #endif
  35061. AssertNotNull(req =
  35062. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  35063. #ifndef NO_BIO
  35064. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  35065. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  35066. BIO_free(bReq);
  35067. #endif
  35068. X509_free(req);
  35069. }
  35070. #endif
  35071. AssertNotNull(nm = X509_get_subject_name(x509));
  35072. /* Test add entry */
  35073. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  35074. 0x0c, cn, (int)sizeof(cn)));
  35075. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  35076. #ifdef WOLFSSL_CERT_EXT
  35077. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  35078. (byte*)"support@wolfssl.com", 19, -1,
  35079. 1), WOLFSSL_SUCCESS);
  35080. #endif
  35081. X509_NAME_ENTRY_free(entry);
  35082. #ifdef WOLFSSL_CERT_REQ
  35083. {
  35084. unsigned char srv_pkcs9p[] = "Server";
  35085. char* subject;
  35086. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  35087. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  35088. subject = X509_NAME_oneline(nm, 0, 0);
  35089. #ifdef DEBUG_WOLFSSL
  35090. fprintf(stderr, "\n\t%s\n", subject);
  35091. #endif
  35092. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  35093. }
  35094. #endif
  35095. /* Test add entry by text */
  35096. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  35097. 0x0c, cn, (int)sizeof(cn)));
  35098. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  35099. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  35100. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  35101. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  35102. #endif
  35103. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  35104. X509_NAME_ENTRY_free(entry);
  35105. /* Test add entry by NID */
  35106. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  35107. cn, -1, -1, 0), SSL_SUCCESS);
  35108. #ifdef OPENSSL_ALL
  35109. /* stack of name entry */
  35110. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  35111. for (i=0; i<names_len; i++) {
  35112. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  35113. }
  35114. #endif
  35115. #ifndef NO_BIO
  35116. BIO_free(bio);
  35117. #endif
  35118. X509_free(x509); /* free's nm */
  35119. res = TEST_RES_CHECK(1);
  35120. #endif
  35121. return res;
  35122. }
  35123. static int test_wolfSSL_X509_set_name(void)
  35124. {
  35125. int res = TEST_SKIPPED;
  35126. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35127. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  35128. X509* x509;
  35129. X509_NAME* name;
  35130. AssertNotNull(name = X509_NAME_new());
  35131. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  35132. (byte*)"wolfssl.com", 11, 0, 1),
  35133. WOLFSSL_SUCCESS);
  35134. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  35135. (byte*)"support@wolfssl.com", 19, -1,
  35136. 1), WOLFSSL_SUCCESS);
  35137. AssertNotNull(x509 = X509_new());
  35138. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  35139. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  35140. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  35141. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  35142. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  35143. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  35144. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  35145. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  35146. X509_free(x509);
  35147. X509_NAME_free(name);
  35148. res = TEST_RES_CHECK(1);
  35149. #endif /* OPENSSL_ALL && !NO_CERTS */
  35150. return res;
  35151. }
  35152. static int test_wolfSSL_X509_set_notAfter(void)
  35153. {
  35154. int res = TEST_SKIPPED;
  35155. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  35156. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  35157. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  35158. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  35159. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  35160. /* Generalized time will overflow time_t if not long */
  35161. X509* x;
  35162. BIO* bio;
  35163. ASN1_TIME *asn_time, *time_check;
  35164. const int year = 365*24*60*60;
  35165. const int day = 24*60*60;
  35166. const int hour = 60*60;
  35167. const int mini = 60;
  35168. int offset_day;
  35169. unsigned char buf[25];
  35170. time_t t;
  35171. /*
  35172. * Setup asn_time. APACHE HTTPD uses time(NULL)
  35173. */
  35174. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  35175. offset_day = 7;
  35176. /*
  35177. * Free these.
  35178. */
  35179. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  35180. AssertNotNull(asn_time);
  35181. AssertNotNull(x = X509_new());
  35182. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35183. /*
  35184. * Tests
  35185. */
  35186. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  35187. /* time_check is simply (ANS1_TIME*)x->notAfter */
  35188. AssertNotNull(time_check = X509_get_notAfter(x));
  35189. /* ANS1_TIME_check validates by checking if argument can be parsed */
  35190. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  35191. /* Convert to human readable format and compare to intended date */
  35192. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  35193. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  35194. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  35195. /*
  35196. * Cleanup
  35197. */
  35198. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  35199. X509_free(x);
  35200. BIO_free(bio);
  35201. res = TEST_RES_CHECK(1);
  35202. #endif
  35203. return res;
  35204. }
  35205. static int test_wolfSSL_X509_set_notBefore(void)
  35206. {
  35207. int res = TEST_SKIPPED;
  35208. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  35209. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  35210. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  35211. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  35212. X509* x;
  35213. BIO* bio;
  35214. ASN1_TIME *asn_time, *time_check;
  35215. const int year = 365*24*60*60;
  35216. const int day = 24*60*60;
  35217. const int hour = 60*60;
  35218. const int mini = 60;
  35219. int offset_day;
  35220. unsigned char buf[25];
  35221. time_t t;
  35222. /*
  35223. * Setup asn_time. APACHE HTTPD uses time(NULL)
  35224. */
  35225. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  35226. offset_day = 7;
  35227. /*
  35228. * Free these.
  35229. */
  35230. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  35231. AssertNotNull(asn_time);
  35232. AssertNotNull(x = X509_new());
  35233. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35234. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  35235. /*
  35236. * Main Tests
  35237. */
  35238. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  35239. /* time_check == (ANS1_TIME*)x->notBefore */
  35240. AssertNotNull(time_check = X509_get_notBefore(x));
  35241. /* ANS1_TIME_check validates by checking if argument can be parsed */
  35242. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  35243. /* Convert to human readable format and compare to intended date */
  35244. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  35245. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  35246. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  35247. /*
  35248. * Cleanup
  35249. */
  35250. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  35251. X509_free(x);
  35252. BIO_free(bio);
  35253. res = TEST_RES_CHECK(1);
  35254. #endif
  35255. return res;
  35256. }
  35257. static int test_wolfSSL_X509_set_version(void)
  35258. {
  35259. int res = TEST_SKIPPED;
  35260. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35261. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  35262. X509* x509;
  35263. long v = 2L;
  35264. long maxInt = INT_MAX;
  35265. AssertNotNull(x509 = X509_new());
  35266. /* These should pass. */
  35267. AssertTrue(wolfSSL_X509_set_version(x509, v));
  35268. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  35269. /* Fail Case: When v(long) is greater than x509->version(int). */
  35270. v = maxInt+1;
  35271. AssertFalse(wolfSSL_X509_set_version(x509, v));
  35272. /* Cleanup */
  35273. X509_free(x509);
  35274. res = TEST_RES_CHECK(1);
  35275. #endif
  35276. return res;
  35277. }
  35278. #ifndef NO_BIO
  35279. static int test_wolfSSL_BIO_gets(void)
  35280. {
  35281. int res = TEST_SKIPPED;
  35282. #if defined(OPENSSL_EXTRA)
  35283. BIO* bio;
  35284. BIO* bio2;
  35285. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  35286. char emp[] = "";
  35287. char bio_buffer[20];
  35288. int bufferSz = 20;
  35289. /* try with bad args */
  35290. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  35291. /* try with real msg */
  35292. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  35293. XMEMSET(bio_buffer, 0, bufferSz);
  35294. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  35295. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  35296. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  35297. AssertFalse(bio2 != BIO_next(bio));
  35298. /* make buffer filled with no terminating characters */
  35299. XMEMSET(bio_buffer, 1, bufferSz);
  35300. /* BIO_gets reads a line of data */
  35301. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35302. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35303. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35304. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  35305. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35306. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35307. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35308. /* check not null terminated string */
  35309. BIO_free(bio);
  35310. msg[0] = 0x33;
  35311. msg[1] = 0x33;
  35312. msg[2] = 0x33;
  35313. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  35314. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  35315. AssertIntEQ(bio_buffer[0], msg[0]);
  35316. AssertIntEQ(bio_buffer[1], msg[1]);
  35317. AssertIntNE(bio_buffer[2], msg[2]);
  35318. BIO_free(bio);
  35319. msg[3] = 0x33;
  35320. bio_buffer[3] = 0x33;
  35321. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  35322. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  35323. AssertIntEQ(bio_buffer[0], msg[0]);
  35324. AssertIntEQ(bio_buffer[1], msg[1]);
  35325. AssertIntEQ(bio_buffer[2], msg[2]);
  35326. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  35327. /* check reading an empty string */
  35328. BIO_free(bio);
  35329. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  35330. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  35331. AssertStrEQ(emp, bio_buffer);
  35332. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35333. /* check error cases */
  35334. BIO_free(bio);
  35335. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  35336. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35337. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35338. #if !defined(NO_FILESYSTEM)
  35339. {
  35340. BIO* f_bio;
  35341. XFILE f;
  35342. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  35343. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35344. f = XFOPEN(svrCertFile, "rb");
  35345. AssertTrue((f != XBADFILE));
  35346. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  35347. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35348. BIO_free(f_bio);
  35349. }
  35350. #endif /* NO_FILESYSTEM */
  35351. BIO_free(bio);
  35352. BIO_free(bio2);
  35353. /* try with type BIO */
  35354. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  35355. sizeof(msg));
  35356. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  35357. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35358. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  35359. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  35360. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  35361. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  35362. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  35363. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35364. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35365. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35366. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  35367. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35368. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35369. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35370. BIO_free(bio);
  35371. BIO_free(bio2);
  35372. /* check reading an empty string */
  35373. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  35374. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  35375. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35376. AssertStrEQ(emp, bio_buffer);
  35377. BIO_free(bio);
  35378. res = TEST_RES_CHECK(1);
  35379. #endif
  35380. return res;
  35381. }
  35382. static int test_wolfSSL_BIO_puts(void)
  35383. {
  35384. int res = TEST_SKIPPED;
  35385. #if defined(OPENSSL_EXTRA)
  35386. BIO* bio;
  35387. char input[] = "hello\0world\n.....ok\n\0";
  35388. char output[128];
  35389. XMEMSET(output, 0, sizeof(output));
  35390. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35391. AssertIntEQ(BIO_puts(bio, input), 5);
  35392. AssertIntEQ(BIO_pending(bio), 5);
  35393. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  35394. AssertIntEQ(BIO_pending(bio), 19);
  35395. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  35396. AssertStrEQ(output, "helloworld\n");
  35397. AssertIntEQ(BIO_pending(bio), 8);
  35398. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  35399. AssertStrEQ(output, ".....ok\n");
  35400. AssertIntEQ(BIO_pending(bio), 0);
  35401. AssertIntEQ(BIO_puts(bio, ""), -1);
  35402. BIO_free(bio);
  35403. res = TEST_RES_CHECK(1);
  35404. #endif
  35405. return res;
  35406. }
  35407. static int test_wolfSSL_BIO_dump(void)
  35408. {
  35409. int res = TEST_SKIPPED;
  35410. #if defined(OPENSSL_EXTRA)
  35411. BIO* bio;
  35412. static const unsigned char data[] = {
  35413. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  35414. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  35415. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  35416. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  35417. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  35418. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  35419. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  35420. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  35421. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  35422. 0xB4
  35423. };
  35424. /* Generated with OpenSSL. */
  35425. static const char expected[] =
  35426. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  35427. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  35428. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  35429. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  35430. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  35431. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  35432. static const char expectedAll[] =
  35433. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  35434. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  35435. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  35436. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  35437. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  35438. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  35439. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  35440. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  35441. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  35442. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  35443. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  35444. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  35445. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  35446. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  35447. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  35448. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  35449. char output[16 * 80];
  35450. int i;
  35451. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35452. /* Example key dumped. */
  35453. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  35454. sizeof(expected) - 1);
  35455. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  35456. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  35457. /* Try every possible value for a character. */
  35458. for (i = 0; i < 256; i++)
  35459. output[i] = i;
  35460. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  35461. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  35462. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  35463. BIO_free(bio);
  35464. res = TEST_RES_CHECK(1);
  35465. #endif
  35466. return res;
  35467. }
  35468. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35469. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35470. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35471. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  35472. {
  35473. (void)ssl;
  35474. (void)buf;
  35475. (void)sz;
  35476. (void)ctx;
  35477. return WOLFSSL_CBIO_ERR_WANT_READ;
  35478. }
  35479. #endif
  35480. static int test_wolfSSL_BIO_should_retry(void)
  35481. {
  35482. int res = TEST_SKIPPED;
  35483. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35484. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35485. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35486. tcp_ready ready;
  35487. func_args server_args;
  35488. THREAD_TYPE serverThread;
  35489. SOCKET_T sockfd = 0;
  35490. WOLFSSL_CTX* ctx;
  35491. WOLFSSL* ssl;
  35492. char msg[64] = "hello wolfssl!";
  35493. char reply[1024];
  35494. int msgSz = (int)XSTRLEN(msg);
  35495. int ret;
  35496. BIO* bio;
  35497. XMEMSET(&server_args, 0, sizeof(func_args));
  35498. #ifdef WOLFSSL_TIRTOS
  35499. fdOpenSession(Task_self());
  35500. #endif
  35501. StartTCP();
  35502. InitTcpReady(&ready);
  35503. #if defined(USE_WINDOWS_API)
  35504. /* use RNG to get random port if using windows */
  35505. ready.port = GetRandomPort();
  35506. #endif
  35507. server_args.signal = &ready;
  35508. start_thread(test_server_nofail, &server_args, &serverThread);
  35509. wait_tcp_ready(&server_args);
  35510. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35511. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  35512. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  35513. #endif
  35514. AssertIntEQ(WOLFSSL_SUCCESS,
  35515. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35516. AssertIntEQ(WOLFSSL_SUCCESS,
  35517. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35518. AssertIntEQ(WOLFSSL_SUCCESS,
  35519. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35520. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35521. /* force retry */
  35522. ssl = wolfSSL_new(ctx);
  35523. AssertNotNull(ssl);
  35524. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35525. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  35526. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  35527. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  35528. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  35529. AssertIntNE(BIO_should_retry(bio), 0);
  35530. /* now perform successful connection */
  35531. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  35532. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  35533. BIO_read(bio, reply, sizeof(reply));
  35534. ret = wolfSSL_get_error(ssl, -1);
  35535. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  35536. AssertIntNE(BIO_should_retry(bio), 0);
  35537. }
  35538. else {
  35539. AssertIntEQ(BIO_should_retry(bio), 0);
  35540. }
  35541. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  35542. XSTRLEN("I hear you fa shizzle!")), 0);
  35543. BIO_free(bio);
  35544. wolfSSL_CTX_free(ctx);
  35545. join_thread(serverThread);
  35546. FreeTcpReady(&ready);
  35547. #ifdef WOLFSSL_TIRTOS
  35548. fdOpenSession(Task_self());
  35549. #endif
  35550. res = TEST_RES_CHECK(1);
  35551. #endif
  35552. return res;
  35553. }
  35554. static int test_wolfSSL_BIO_connect(void)
  35555. {
  35556. int res = TEST_SKIPPED;
  35557. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35558. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  35559. tcp_ready ready;
  35560. func_args server_args;
  35561. THREAD_TYPE serverThread;
  35562. BIO *tcpBio;
  35563. BIO *sslBio;
  35564. SSL_CTX* ctx;
  35565. SSL *ssl;
  35566. SSL *sslPtr;
  35567. char msg[] = "hello wolfssl!";
  35568. char reply[30];
  35569. char buff[10] = {0};
  35570. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35571. AssertIntEQ(WOLFSSL_SUCCESS,
  35572. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35573. AssertIntEQ(WOLFSSL_SUCCESS,
  35574. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35575. AssertIntEQ(WOLFSSL_SUCCESS,
  35576. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35577. /* Setup server */
  35578. XMEMSET(&server_args, 0, sizeof(func_args));
  35579. StartTCP();
  35580. InitTcpReady(&ready);
  35581. #if defined(USE_WINDOWS_API)
  35582. /* use RNG to get random port if using windows */
  35583. ready.port = GetRandomPort();
  35584. #endif
  35585. server_args.signal = &ready;
  35586. start_thread(test_server_nofail, &server_args, &serverThread);
  35587. wait_tcp_ready(&server_args);
  35588. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35589. /* Start the test proper */
  35590. /* Setup the TCP BIO */
  35591. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  35592. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  35593. /* Setup the SSL object */
  35594. AssertNotNull(ssl = SSL_new(ctx));
  35595. SSL_set_connect_state(ssl);
  35596. /* Setup the SSL BIO */
  35597. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  35598. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  35599. /* Verify that BIO_get_ssl works. */
  35600. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  35601. AssertPtrEq(ssl, sslPtr);
  35602. /* Link BIO's so that sslBio uses tcpBio for IO */
  35603. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  35604. /* Do TCP connect */
  35605. AssertIntEQ(BIO_do_connect(sslBio), 1);
  35606. /* Do TLS handshake */
  35607. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  35608. /* Test writing */
  35609. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35610. /* Expect length of default wolfSSL reply */
  35611. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35612. /* Clean it all up */
  35613. BIO_free_all(sslBio);
  35614. /* Server clean up */
  35615. join_thread(serverThread);
  35616. FreeTcpReady(&ready);
  35617. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  35618. * after. */
  35619. XMEMSET(&server_args, 0, sizeof(func_args));
  35620. StartTCP();
  35621. InitTcpReady(&ready);
  35622. #if defined(USE_WINDOWS_API)
  35623. /* use RNG to get random port if using windows */
  35624. ready.port = GetRandomPort();
  35625. #endif
  35626. server_args.signal = &ready;
  35627. start_thread(test_server_nofail, &server_args, &serverThread);
  35628. wait_tcp_ready(&server_args);
  35629. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35630. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  35631. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  35632. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  35633. AssertIntEQ(BIO_do_connect(sslBio), 1);
  35634. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  35635. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35636. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35637. /* Attempt to close the TLS connection gracefully. */
  35638. BIO_ssl_shutdown(sslBio);
  35639. BIO_free_all(sslBio);
  35640. join_thread(serverThread);
  35641. FreeTcpReady(&ready);
  35642. SSL_CTX_free(ctx);
  35643. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35644. wc_ecc_fp_free(); /* free per thread cache */
  35645. #endif
  35646. res = TEST_RES_CHECK(1);
  35647. #endif
  35648. return res;
  35649. }
  35650. static int test_wolfSSL_BIO_tls(void)
  35651. {
  35652. int res = TEST_SKIPPED;
  35653. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  35654. SSL_CTX* ctx;
  35655. SSL *ssl;
  35656. BIO *readBio;
  35657. BIO *writeBio;
  35658. int ret, err = 0;
  35659. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35660. AssertNotNull(ssl = SSL_new(ctx));
  35661. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  35662. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  35663. /* Qt reads data from write-bio,
  35664. * then writes the read data into plain packet.
  35665. * Qt reads data from plain packet,
  35666. * then writes the read data into read-bio.
  35667. */
  35668. SSL_set_bio(ssl, readBio, writeBio);
  35669. do {
  35670. #ifdef WOLFSSL_ASYNC_CRYPT
  35671. if (err == WC_PENDING_E) {
  35672. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35673. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35674. }
  35675. #endif
  35676. ret = SSL_connect(ssl);
  35677. err = SSL_get_error(ssl, 0);
  35678. } while (err == WC_PENDING_E);
  35679. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  35680. /* in this use case, should return WANT READ
  35681. * so that Qt will read the data from plain packet for next state.
  35682. */
  35683. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  35684. SSL_free(ssl);
  35685. SSL_CTX_free(ctx);
  35686. res = TEST_RES_CHECK(1);
  35687. #endif
  35688. return res;
  35689. }
  35690. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  35691. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  35692. {
  35693. BIO* clientBio;
  35694. SSL* sslClient;
  35695. SSL_CTX* ctx;
  35696. char connectAddr[20]; /* IP + port */;
  35697. (void)args;
  35698. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  35699. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  35700. AssertIntEQ(BIO_do_connect(clientBio), 1);
  35701. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35702. AssertNotNull(sslClient = SSL_new(ctx));
  35703. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  35704. SSL_set_bio(sslClient, clientBio, clientBio);
  35705. AssertIntEQ(SSL_connect(sslClient), 1);
  35706. SSL_free(sslClient);
  35707. SSL_CTX_free(ctx);
  35708. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35709. wc_ecc_fp_free(); /* free per thread cache */
  35710. #endif
  35711. return 0;
  35712. }
  35713. #endif
  35714. static int test_wolfSSL_BIO_accept(void)
  35715. {
  35716. int res = TEST_SKIPPED;
  35717. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  35718. BIO* serverBindBio;
  35719. BIO* serverAcceptBio;
  35720. SSL* sslServer;
  35721. SSL_CTX* ctx;
  35722. func_args args;
  35723. THREAD_TYPE thread;
  35724. char port[10]; /* 10 bytes should be enough to store the string
  35725. * representation of the port */
  35726. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  35727. AssertNotNull(serverBindBio = BIO_new_accept(port));
  35728. /* First BIO_do_accept binds the port */
  35729. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  35730. XMEMSET(&args, 0, sizeof(func_args));
  35731. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  35732. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  35733. /* Let's plug it into SSL to test */
  35734. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35735. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35736. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35737. AssertNotNull(sslServer = SSL_new(ctx));
  35738. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  35739. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  35740. AssertIntEQ(SSL_accept(sslServer), 1);
  35741. join_thread(thread);
  35742. BIO_free(serverBindBio);
  35743. SSL_free(sslServer);
  35744. SSL_CTX_free(ctx);
  35745. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35746. wc_ecc_fp_free(); /* free per thread cache */
  35747. #endif
  35748. res = TEST_RES_CHECK(1);
  35749. #endif
  35750. return res;
  35751. }
  35752. static int test_wolfSSL_BIO_write(void)
  35753. {
  35754. int res = TEST_SKIPPED;
  35755. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  35756. BIO* bio;
  35757. BIO* bio64;
  35758. BIO* ptr;
  35759. int sz;
  35760. char msg[] = "conversion test";
  35761. char out[40];
  35762. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  35763. void* bufPtr = NULL;
  35764. BUF_MEM* buf = NULL;
  35765. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  35766. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  35767. /* now should convert to base64 then write to memory */
  35768. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35769. BIO_flush(bio);
  35770. /* test BIO chain */
  35771. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  35772. AssertNotNull(buf);
  35773. AssertIntEQ(buf->length, 25);
  35774. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  35775. AssertPtrEq(buf->data, bufPtr);
  35776. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  35777. sz = sizeof(out);
  35778. XMEMSET(out, 0, sz);
  35779. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  35780. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  35781. /* write then read should return the same message */
  35782. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35783. sz = sizeof(out);
  35784. XMEMSET(out, 0, sz);
  35785. AssertIntEQ(BIO_read(bio, out, sz), 16);
  35786. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35787. /* now try encoding with no line ending */
  35788. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  35789. #ifdef HAVE_EX_DATA
  35790. BIO_set_ex_data(bio64, 0, (void*) "data");
  35791. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  35792. #endif
  35793. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35794. BIO_flush(bio);
  35795. sz = sizeof(out);
  35796. XMEMSET(out, 0, sz);
  35797. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  35798. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  35799. BIO_free_all(bio); /* frees bio64 also */
  35800. /* test with more than one bio64 in list */
  35801. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  35802. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  35803. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  35804. /* now should convert to base64 when stored and then decode with read */
  35805. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  35806. BIO_flush(bio);
  35807. sz = sizeof(out);
  35808. XMEMSET(out, 0, sz);
  35809. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  35810. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  35811. BIO_clear_flags(bio64, ~0);
  35812. BIO_set_retry_read(bio);
  35813. BIO_free_all(bio); /* frees bio64s also */
  35814. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  35815. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35816. BIO_free(bio);
  35817. res = TEST_RES_CHECK(1);
  35818. #endif
  35819. return res;
  35820. }
  35821. static int test_wolfSSL_BIO_printf(void)
  35822. {
  35823. int res = TEST_SKIPPED;
  35824. #if defined(OPENSSL_ALL)
  35825. BIO* bio;
  35826. int sz = 7;
  35827. char msg[] = "TLS 1.3 for the world";
  35828. char out[60];
  35829. char expected[] = "TLS 1.3 for the world : sz = 7";
  35830. XMEMSET(out, 0, sizeof(out));
  35831. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35832. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  35833. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  35834. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  35835. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  35836. BIO_free(bio);
  35837. res = TEST_RES_CHECK(1);
  35838. #endif
  35839. return res;
  35840. }
  35841. static int test_wolfSSL_BIO_f_md(void)
  35842. {
  35843. int res = TEST_SKIPPED;
  35844. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  35845. BIO *bio, *mem;
  35846. char msg[] = "message to hash";
  35847. char out[60];
  35848. EVP_MD_CTX* ctx;
  35849. const unsigned char testKey[] =
  35850. {
  35851. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35852. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35853. 0x0b, 0x0b, 0x0b, 0x0b
  35854. };
  35855. const char testData[] = "Hi There";
  35856. const unsigned char testResult[] =
  35857. {
  35858. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  35859. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  35860. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  35861. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  35862. };
  35863. const unsigned char expectedHash[] =
  35864. {
  35865. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  35866. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  35867. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  35868. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  35869. };
  35870. const unsigned char emptyHash[] =
  35871. {
  35872. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  35873. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  35874. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  35875. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  35876. };
  35877. unsigned char check[sizeof(testResult) + 1];
  35878. size_t checkSz = -1;
  35879. EVP_PKEY* key;
  35880. XMEMSET(out, 0, sizeof(out));
  35881. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35882. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  35883. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  35884. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  35885. /* should not be able to write/read yet since just digest wrapper and no
  35886. * data is passing through the bio */
  35887. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  35888. AssertIntEQ(BIO_pending(bio), 0);
  35889. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  35890. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  35891. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35892. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  35893. BIO_reset(bio);
  35894. /* append BIO mem to bio in order to read/write */
  35895. AssertNotNull(bio = BIO_push(bio, mem));
  35896. XMEMSET(out, 0, sizeof(out));
  35897. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  35898. AssertIntEQ(BIO_pending(bio), 16);
  35899. /* this just reads the message and does not hash it (gets calls final) */
  35900. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  35901. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35902. /* create a message digest using BIO */
  35903. XMEMSET(out, 0, sizeof(out));
  35904. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  35905. AssertIntEQ(BIO_pending(mem), 16);
  35906. AssertIntEQ(BIO_pending(bio), 16);
  35907. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35908. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  35909. BIO_free(bio);
  35910. BIO_free(mem);
  35911. /* test with HMAC */
  35912. XMEMSET(out, 0, sizeof(out));
  35913. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35914. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  35915. BIO_get_md_ctx(bio, &ctx);
  35916. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  35917. testKey, (int)sizeof(testKey)));
  35918. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  35919. AssertNotNull(bio = BIO_push(bio, mem));
  35920. BIO_write(bio, testData, (int)strlen(testData));
  35921. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  35922. EVP_DigestSignFinal(ctx, check, &checkSz);
  35923. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  35924. EVP_PKEY_free(key);
  35925. BIO_free(bio);
  35926. BIO_free(mem);
  35927. res = TEST_RES_CHECK(1);
  35928. #endif
  35929. return res;
  35930. }
  35931. static int test_wolfSSL_BIO_up_ref(void)
  35932. {
  35933. int res = TEST_SKIPPED;
  35934. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35935. BIO* bio;
  35936. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35937. AssertIntEQ(BIO_up_ref(NULL), 0);
  35938. AssertIntEQ(BIO_up_ref(bio), 1);
  35939. BIO_free(bio);
  35940. AssertIntEQ(BIO_up_ref(bio), 1);
  35941. BIO_free(bio);
  35942. BIO_free(bio);
  35943. res = TEST_RES_CHECK(1);
  35944. #endif
  35945. return res;
  35946. }
  35947. static int test_wolfSSL_BIO_reset(void)
  35948. {
  35949. int res = TEST_SKIPPED;
  35950. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35951. BIO* bio;
  35952. byte buf[16];
  35953. AssertNotNull(bio = BIO_new_mem_buf("secure your data",
  35954. (word32)XSTRLEN("secure your data")));
  35955. AssertIntEQ(BIO_read(bio, buf, 6), 6);
  35956. AssertIntEQ(XMEMCMP(buf, "secure", 6), 0);
  35957. XMEMSET(buf, 0, 16);
  35958. AssertIntEQ(BIO_read(bio, buf, 16), 10);
  35959. AssertIntEQ(XMEMCMP(buf, " your data", 10), 0);
  35960. /* You cannot write to MEM BIO with read-only mode. */
  35961. AssertIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  35962. AssertIntEQ(BIO_read(bio, buf, 16), -1);
  35963. XMEMSET(buf, 0, 16);
  35964. AssertIntEQ(BIO_reset(bio), 0);
  35965. AssertIntEQ(BIO_read(bio, buf, 16), 16);
  35966. AssertIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  35967. BIO_free(bio);
  35968. res = TEST_RES_CHECK(1);
  35969. #endif
  35970. return res;
  35971. }
  35972. #endif /* !NO_BIO */
  35973. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  35974. /* test that the callback arg is correct */
  35975. static int certCbArg = 0;
  35976. static int clientCertCb(WOLFSSL* ssl, void* arg)
  35977. {
  35978. if (ssl == NULL || arg != &certCbArg)
  35979. return 0;
  35980. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  35981. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35982. return 0;
  35983. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  35984. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35985. return 0;
  35986. return 1;
  35987. }
  35988. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  35989. {
  35990. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  35991. }
  35992. /**
  35993. * This is only done because test_client_nofail has no way to stop
  35994. * certificate and key loading
  35995. */
  35996. static void clientCertClearCb(WOLFSSL* ssl)
  35997. {
  35998. /* Clear the loaded certs to force the callbacks to set them up */
  35999. SSL_certs_clear(ssl);
  36000. }
  36001. static int serverCertCb(WOLFSSL* ssl, void* arg)
  36002. {
  36003. if (ssl == NULL || arg != &certCbArg)
  36004. return 0;
  36005. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  36006. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  36007. return 0;
  36008. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  36009. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  36010. return 0;
  36011. return 1;
  36012. }
  36013. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  36014. {
  36015. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  36016. }
  36017. /**
  36018. * This is only done because test_server_nofail has no way to stop
  36019. * certificate and key loading
  36020. */
  36021. static void serverCertClearCb(WOLFSSL* ssl)
  36022. {
  36023. /* Clear the loaded certs to force the callbacks to set them up */
  36024. SSL_certs_clear(ssl);
  36025. }
  36026. #endif
  36027. static int test_wolfSSL_cert_cb(void)
  36028. {
  36029. int res = TEST_SKIPPED;
  36030. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  36031. callback_functions func_cb_client;
  36032. callback_functions func_cb_server;
  36033. tcp_ready ready;
  36034. func_args client_args;
  36035. func_args server_args;
  36036. THREAD_TYPE serverThread;
  36037. XMEMSET(&client_args, 0, sizeof(func_args));
  36038. XMEMSET(&server_args, 0, sizeof(func_args));
  36039. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  36040. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  36041. #ifdef WOLFSSL_TIRTOS
  36042. fdOpenSession(Task_self());
  36043. #endif
  36044. StartTCP();
  36045. InitTcpReady(&ready);
  36046. #if defined(USE_WINDOWS_API)
  36047. /* use RNG to get random port if using windows */
  36048. ready.port = GetRandomPort();
  36049. #endif
  36050. server_args.signal = &ready;
  36051. client_args.signal = &ready;
  36052. client_args.callbacks = &func_cb_client;
  36053. server_args.callbacks = &func_cb_server;
  36054. func_cb_client.ctx_ready = clientCertSetupCb;
  36055. func_cb_client.ssl_ready = clientCertClearCb;
  36056. func_cb_server.ctx_ready = serverCertSetupCb;
  36057. func_cb_server.ssl_ready = serverCertClearCb;
  36058. start_thread(test_server_nofail, &server_args, &serverThread);
  36059. wait_tcp_ready(&server_args);
  36060. test_client_nofail(&client_args, NULL);
  36061. join_thread(serverThread);
  36062. AssertTrue(client_args.return_code);
  36063. AssertTrue(server_args.return_code);
  36064. FreeTcpReady(&ready);
  36065. #ifdef WOLFSSL_TIRTOS
  36066. fdOpenSession(Task_self());
  36067. #endif
  36068. res = TEST_RES_CHECK(1);
  36069. #endif
  36070. return res;
  36071. }
  36072. static int test_wolfSSL_SESSION(void)
  36073. {
  36074. int res = TEST_SKIPPED;
  36075. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36076. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  36077. !defined(NO_SESSION_CACHE)
  36078. WOLFSSL* ssl;
  36079. WOLFSSL_CTX* ctx;
  36080. WOLFSSL_SESSION* sess;
  36081. WOLFSSL_SESSION* sess_copy;
  36082. #ifdef OPENSSL_EXTRA
  36083. unsigned char* sessDer = NULL;
  36084. unsigned char* ptr = NULL;
  36085. const unsigned char context[] = "user app context";
  36086. unsigned int contextSz = (unsigned int)sizeof(context);
  36087. int sz;
  36088. #endif
  36089. int ret, err;
  36090. SOCKET_T sockfd;
  36091. tcp_ready ready;
  36092. func_args server_args;
  36093. THREAD_TYPE serverThread;
  36094. char msg[80];
  36095. const char* sendGET = "GET";
  36096. /* TLS v1.3 requires session tickets */
  36097. /* CHACHA and POLY1305 required for myTicketEncCb */
  36098. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  36099. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  36100. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  36101. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  36102. #else
  36103. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36104. #endif
  36105. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  36106. WOLFSSL_FILETYPE_PEM));
  36107. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  36108. WOLFSSL_FILETYPE_PEM));
  36109. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  36110. WOLFSSL_SUCCESS);
  36111. #ifdef WOLFSSL_ENCRYPTED_KEYS
  36112. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  36113. #endif
  36114. #ifdef HAVE_SESSION_TICKET
  36115. /* Use session tickets, for ticket tests below */
  36116. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  36117. #endif
  36118. XMEMSET(&server_args, 0, sizeof(func_args));
  36119. #ifdef WOLFSSL_TIRTOS
  36120. fdOpenSession(Task_self());
  36121. #endif
  36122. StartTCP();
  36123. InitTcpReady(&ready);
  36124. #if defined(USE_WINDOWS_API)
  36125. /* use RNG to get random port if using windows */
  36126. ready.port = GetRandomPort();
  36127. #endif
  36128. server_args.signal = &ready;
  36129. start_thread(test_server_nofail, &server_args, &serverThread);
  36130. wait_tcp_ready(&server_args);
  36131. /* client connection */
  36132. ssl = wolfSSL_new(ctx);
  36133. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  36134. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  36135. #ifdef WOLFSSL_ASYNC_CRYPT
  36136. err = 0; /* Reset error */
  36137. #endif
  36138. do {
  36139. #ifdef WOLFSSL_ASYNC_CRYPT
  36140. if (err == WC_PENDING_E) {
  36141. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36142. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36143. }
  36144. #endif
  36145. ret = wolfSSL_connect(ssl);
  36146. err = wolfSSL_get_error(ssl, 0);
  36147. } while (err == WC_PENDING_E);
  36148. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  36149. #ifdef WOLFSSL_ASYNC_CRYPT
  36150. err = 0; /* Reset error */
  36151. #endif
  36152. do {
  36153. #ifdef WOLFSSL_ASYNC_CRYPT
  36154. if (err == WC_PENDING_E) {
  36155. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36156. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36157. }
  36158. #endif
  36159. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  36160. err = wolfSSL_get_error(ssl, 0);
  36161. } while (err == WC_PENDING_E);
  36162. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  36163. #ifdef WOLFSSL_ASYNC_CRYPT
  36164. err = 0; /* Reset error */
  36165. #endif
  36166. do {
  36167. #ifdef WOLFSSL_ASYNC_CRYPT
  36168. if (err == WC_PENDING_E) {
  36169. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36170. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36171. }
  36172. #endif
  36173. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  36174. err = wolfSSL_get_error(ssl, 0);
  36175. } while (err == WC_PENDING_E);
  36176. AssertIntEQ(ret, 23);
  36177. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  36178. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  36179. #ifdef HAVE_EXT_CACHE
  36180. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  36181. * HAVE_EXT_CACHE we get new objects each time */
  36182. #endif
  36183. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  36184. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  36185. sess = wolfSSL_get_session(ssl);
  36186. #ifdef OPENSSL_EXTRA
  36187. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  36188. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  36189. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  36190. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  36191. #ifdef HAVE_SESSION_TICKET
  36192. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  36193. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  36194. SESSION_TICKET_HINT_DEFAULT);
  36195. #else
  36196. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  36197. #endif
  36198. #else
  36199. (void)sess;
  36200. #endif /* OPENSSL_EXTRA */
  36201. /* Retain copy of the session for later testing */
  36202. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  36203. wolfSSL_shutdown(ssl);
  36204. wolfSSL_free(ssl);
  36205. join_thread(serverThread);
  36206. FreeTcpReady(&ready);
  36207. #ifdef WOLFSSL_TIRTOS
  36208. fdOpenSession(Task_self());
  36209. #endif
  36210. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  36211. {
  36212. X509 *x509;
  36213. char buf[30];
  36214. int bufSz;
  36215. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  36216. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  36217. X509_get_subject_name(x509), NID_organizationalUnitName,
  36218. buf, sizeof(buf))), 0);
  36219. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  36220. if (bufSz == 7) {
  36221. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  36222. }
  36223. if (bufSz == 16) {
  36224. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  36225. }
  36226. }
  36227. #endif
  36228. #ifdef HAVE_EXT_CACHE
  36229. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  36230. wolfSSL_SESSION_free(sess_copy);
  36231. sess_copy = NULL;
  36232. #endif
  36233. #ifdef OPENSSL_EXTRA
  36234. /* get session from DER and update the timeout */
  36235. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  36236. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  36237. wolfSSL_SESSION_free(sess);
  36238. sess = NULL;
  36239. ptr = sessDer;
  36240. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  36241. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  36242. (const unsigned char**)&ptr, sz));
  36243. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  36244. sessDer = NULL;
  36245. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  36246. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  36247. #endif
  36248. /* successful set session test */
  36249. AssertNotNull(ssl = wolfSSL_new(ctx));
  36250. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  36251. #ifdef HAVE_SESSION_TICKET
  36252. /* Test set/get session ticket */
  36253. {
  36254. const char* ticket = "This is a session ticket";
  36255. char buf[64] = {0};
  36256. word32 bufSz = (word32)sizeof(buf);
  36257. AssertIntEQ(SSL_SUCCESS,
  36258. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  36259. (word32)XSTRLEN(ticket)));
  36260. AssertIntEQ(SSL_SUCCESS,
  36261. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  36262. AssertStrEQ(ticket, buf);
  36263. }
  36264. #endif
  36265. #ifdef OPENSSL_EXTRA
  36266. /* session timeout case */
  36267. /* make the session to be expired */
  36268. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  36269. XSLEEP_MS(1200);
  36270. /* SSL_set_session should reject specified session but return success
  36271. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  36272. */
  36273. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  36274. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  36275. #else
  36276. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  36277. #endif
  36278. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  36279. /* fail case with miss match session context IDs (use compatibility API) */
  36280. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  36281. SSL_SUCCESS);
  36282. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  36283. wolfSSL_free(ssl);
  36284. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  36285. SSL_FAILURE);
  36286. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  36287. SSL_SUCCESS);
  36288. AssertNotNull(ssl = wolfSSL_new(ctx));
  36289. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  36290. #endif /* OPENSSL_EXTRA */
  36291. wolfSSL_free(ssl);
  36292. wolfSSL_SESSION_free(sess);
  36293. wolfSSL_CTX_free(ctx);
  36294. res = TEST_RES_CHECK(1);
  36295. #endif
  36296. return res;
  36297. }
  36298. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  36299. defined(HAVE_EX_DATA)
  36300. static int clientSessRemCountMalloc = 0;
  36301. static int serverSessRemCountMalloc = 0;
  36302. static int clientSessRemCountFree = 0;
  36303. static int serverSessRemCountFree = 0;
  36304. static WOLFSSL_CTX* serverSessCtx = NULL;
  36305. static WOLFSSL_SESSION* serverSess = NULL;
  36306. #ifndef NO_SESSION_CACHE_REF
  36307. static WOLFSSL_CTX* clientSessCtx = NULL;
  36308. static WOLFSSL_SESSION* clientSess = NULL;
  36309. #endif
  36310. static int serverSessRemIdx = 3;
  36311. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  36312. {
  36313. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  36314. (void)ctx;
  36315. AssertNotNull(mallocedData);
  36316. if (!*mallocedData)
  36317. clientSessRemCountFree++;
  36318. else
  36319. serverSessRemCountFree++;
  36320. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  36321. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  36322. }
  36323. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  36324. {
  36325. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  36326. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  36327. !defined(NO_SESSION_CACHE_REF)
  36328. /* Allow downgrade, set min version, and disable TLS 1.3.
  36329. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  36330. * reference to the session cache. But with WOLFSSL_TLS13 and without
  36331. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  36332. * session in the cache. In this case we need to downgrade to previous
  36333. * versions to just use the legacy session ID field. */
  36334. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  36335. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  36336. #endif
  36337. }
  36338. static void SessRemSslSetupCb(WOLFSSL* ssl)
  36339. {
  36340. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  36341. AssertNotNull(mallocedData);
  36342. *mallocedData = SSL_is_server(ssl);
  36343. if (!*mallocedData) {
  36344. clientSessRemCountMalloc++;
  36345. #ifndef NO_SESSION_CACHE_REF
  36346. AssertNotNull(clientSess = SSL_get1_session(ssl));
  36347. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  36348. SSL_SUCCESS);
  36349. #endif
  36350. }
  36351. else {
  36352. serverSessRemCountMalloc++;
  36353. AssertNotNull(serverSess = SSL_get1_session(ssl));
  36354. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  36355. SSL_SUCCESS);
  36356. }
  36357. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  36358. mallocedData), SSL_SUCCESS);
  36359. }
  36360. #endif
  36361. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  36362. {
  36363. int res = TEST_SKIPPED;
  36364. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  36365. defined(HAVE_EX_DATA)
  36366. /* Check that the remove callback gets called for external data in a
  36367. * session object */
  36368. callback_functions func_cb;
  36369. tcp_ready ready;
  36370. func_args client_args;
  36371. func_args server_args;
  36372. THREAD_TYPE serverThread;
  36373. XMEMSET(&client_args, 0, sizeof(func_args));
  36374. XMEMSET(&server_args, 0, sizeof(func_args));
  36375. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  36376. #ifdef WOLFSSL_TIRTOS
  36377. fdOpenSession(Task_self());
  36378. #endif
  36379. StartTCP();
  36380. InitTcpReady(&ready);
  36381. #if defined(USE_WINDOWS_API)
  36382. /* use RNG to get random port if using windows */
  36383. ready.port = GetRandomPort();
  36384. #endif
  36385. server_args.signal = &ready;
  36386. client_args.signal = &ready;
  36387. client_args.callbacks = &func_cb;
  36388. server_args.callbacks = &func_cb;
  36389. func_cb.ctx_ready = SessRemCtxSetupCb;
  36390. func_cb.on_result = SessRemSslSetupCb;
  36391. start_thread(test_server_nofail, &server_args, &serverThread);
  36392. wait_tcp_ready(&server_args);
  36393. test_client_nofail(&client_args, NULL);
  36394. join_thread(serverThread);
  36395. AssertTrue(client_args.return_code);
  36396. AssertTrue(server_args.return_code);
  36397. FreeTcpReady(&ready);
  36398. #ifdef WOLFSSL_TIRTOS
  36399. fdOpenSession(Task_self());
  36400. #endif
  36401. /* Both should have been allocated */
  36402. AssertIntEQ(clientSessRemCountMalloc, 1);
  36403. AssertIntEQ(serverSessRemCountMalloc, 1);
  36404. #ifdef NO_SESSION_CACHE_REF
  36405. /* Client session should not be added to cache so this should be free'd when
  36406. * the SSL object was being free'd */
  36407. AssertIntEQ(clientSessRemCountFree, 1);
  36408. #else
  36409. /* Client session is in cache due to requiring a persistent reference */
  36410. AssertIntEQ(clientSessRemCountFree, 0);
  36411. /* Force a cache lookup */
  36412. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36413. /* Force a cache update */
  36414. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  36415. /* This should set the timeout to 0 and call the remove callback from within
  36416. * the session cache. */
  36417. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  36418. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36419. AssertIntEQ(clientSessRemCountFree, 1);
  36420. #endif
  36421. /* Server session is in the cache so ex_data isn't free'd with the SSL
  36422. * object */
  36423. AssertIntEQ(serverSessRemCountFree, 0);
  36424. /* Force a cache lookup */
  36425. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36426. /* Force a cache update */
  36427. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  36428. /* This should set the timeout to 0 and call the remove callback from within
  36429. * the session cache. */
  36430. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  36431. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36432. AssertIntEQ(serverSessRemCountFree, 1);
  36433. /* Need to free the references that we kept */
  36434. SSL_CTX_free(serverSessCtx);
  36435. SSL_SESSION_free(serverSess);
  36436. #ifndef NO_SESSION_CACHE_REF
  36437. SSL_CTX_free(clientSessCtx);
  36438. SSL_SESSION_free(clientSess);
  36439. #endif
  36440. res = TEST_RES_CHECK(1);
  36441. #endif
  36442. return res;
  36443. }
  36444. static int test_wolfSSL_ticket_keys(void)
  36445. {
  36446. int res = TEST_SKIPPED;
  36447. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  36448. !defined(NO_WOLFSSL_SERVER)
  36449. WOLFSSL_CTX* ctx;
  36450. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  36451. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36452. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  36453. WOLFSSL_FAILURE);
  36454. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  36455. WOLFSSL_FAILURE);
  36456. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  36457. WOLFSSL_FAILURE);
  36458. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  36459. WOLFSSL_FAILURE);
  36460. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36461. WOLFSSL_FAILURE);
  36462. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36463. WOLFSSL_FAILURE);
  36464. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36465. WOLFSSL_FAILURE);
  36466. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  36467. WOLFSSL_FAILURE);
  36468. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  36469. WOLFSSL_FAILURE);
  36470. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  36471. WOLFSSL_FAILURE);
  36472. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  36473. WOLFSSL_FAILURE);
  36474. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36475. WOLFSSL_FAILURE);
  36476. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36477. WOLFSSL_FAILURE);
  36478. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36479. WOLFSSL_FAILURE);
  36480. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36481. WOLFSSL_SUCCESS);
  36482. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36483. WOLFSSL_SUCCESS);
  36484. wolfSSL_CTX_free(ctx);
  36485. res = TEST_RES_CHECK(1);
  36486. #endif
  36487. return res;
  36488. }
  36489. #ifndef NO_BIO
  36490. static int test_wolfSSL_d2i_PUBKEY(void)
  36491. {
  36492. int res = TEST_SKIPPED;
  36493. #if defined(OPENSSL_EXTRA)
  36494. BIO* bio;
  36495. EVP_PKEY* pkey;
  36496. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36497. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  36498. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  36499. /* RSA PUBKEY test */
  36500. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  36501. sizeof_client_keypub_der_2048), 0);
  36502. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36503. EVP_PKEY_free(pkey);
  36504. #endif
  36505. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  36506. /* ECC PUBKEY test */
  36507. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  36508. sizeof_ecc_clikeypub_der_256), 0);
  36509. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36510. EVP_PKEY_free(pkey);
  36511. #endif
  36512. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  36513. /* DSA PUBKEY test */
  36514. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  36515. sizeof_dsa_pub_key_der_2048), 0);
  36516. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36517. EVP_PKEY_free(pkey);
  36518. #endif
  36519. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  36520. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  36521. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  36522. (HAVE_FIPS_VERSION > 2))
  36523. /* DH PUBKEY test */
  36524. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  36525. sizeof_dh_pub_key_der_2048), 0);
  36526. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36527. EVP_PKEY_free(pkey);
  36528. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36529. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  36530. BIO_free(bio);
  36531. (void)pkey;
  36532. res = TEST_RES_CHECK(1);
  36533. #endif
  36534. return res;
  36535. }
  36536. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  36537. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  36538. {
  36539. BIO* bio = NULL;
  36540. EVP_PKEY* pkey = NULL;
  36541. #ifndef NO_RSA
  36542. #endif
  36543. WOLFSSL_CTX* ctx;
  36544. #if defined(WOLFSSL_KEY_GEN)
  36545. unsigned char buff[4096];
  36546. unsigned char* bufPtr = buff;
  36547. #endif
  36548. /* test creating new EVP_PKEY with bad arg */
  36549. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  36550. /* test loading RSA key using BIO */
  36551. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36552. {
  36553. XFILE file;
  36554. const char* fname = "./certs/server-key.der";
  36555. size_t sz;
  36556. byte* buf;
  36557. file = XFOPEN(fname, "rb");
  36558. AssertTrue((file != XBADFILE));
  36559. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36560. sz = XFTELL(file);
  36561. XREWIND(file);
  36562. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36563. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  36564. XFCLOSE(file);
  36565. /* Test using BIO new mem and loading DER private key */
  36566. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36567. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36568. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36569. BIO_free(bio);
  36570. bio = NULL;
  36571. EVP_PKEY_free(pkey);
  36572. pkey = NULL;
  36573. }
  36574. #endif
  36575. /* test loading ECC key using BIO */
  36576. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  36577. {
  36578. XFILE file;
  36579. const char* fname = "./certs/ecc-key.der";
  36580. size_t sz;
  36581. byte* buf;
  36582. file = XFOPEN(fname, "rb");
  36583. AssertTrue((file != XBADFILE));
  36584. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36585. sz = XFTELL(file);
  36586. XREWIND(file);
  36587. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36588. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  36589. XFCLOSE(file);
  36590. /* Test using BIO new mem and loading DER private key */
  36591. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36592. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36593. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36594. BIO_free(bio);
  36595. bio = NULL;
  36596. EVP_PKEY_free(pkey);
  36597. pkey = NULL;
  36598. }
  36599. #endif
  36600. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36601. #ifndef NO_WOLFSSL_SERVER
  36602. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36603. #else
  36604. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36605. #endif
  36606. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  36607. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  36608. {
  36609. RSA* rsa = NULL;
  36610. /* Tests bad parameters */
  36611. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  36612. /* RSA not set yet, expecting to fail*/
  36613. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  36614. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  36615. /* set RSA using bio*/
  36616. AssertIntGT(BIO_write(bio, client_key_der_2048,
  36617. sizeof_client_key_der_2048), 0);
  36618. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  36619. AssertNotNull(rsa);
  36620. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  36621. /*i2d RSAprivate key tests */
  36622. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  36623. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  36624. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36625. sizeof_client_key_der_2048);
  36626. bufPtr -= sizeof_client_key_der_2048;
  36627. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36628. sizeof_client_key_der_2048), 0);
  36629. bufPtr = NULL;
  36630. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36631. sizeof_client_key_der_2048);
  36632. AssertNotNull(bufPtr);
  36633. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36634. sizeof_client_key_der_2048), 0);
  36635. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  36636. RSA_free(rsa);
  36637. rsa = RSA_new();
  36638. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  36639. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  36640. RSA_free(rsa);
  36641. }
  36642. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  36643. SSL_CTX_free(ctx);
  36644. ctx = NULL;
  36645. BIO_free(bio);
  36646. bio = NULL;
  36647. return TEST_RES_CHECK(1);
  36648. }
  36649. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  36650. #endif /* !NO_BIO */
  36651. static int test_wolfSSL_sk_GENERAL_NAME(void)
  36652. {
  36653. int res = TEST_SKIPPED;
  36654. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36655. !defined(NO_RSA)
  36656. X509* x509;
  36657. GENERAL_NAME* gn;
  36658. unsigned char buf[4096];
  36659. const unsigned char* bufPt;
  36660. int bytes, i;
  36661. int j;
  36662. XFILE f;
  36663. STACK_OF(GENERAL_NAME)* sk;
  36664. f = XFOPEN(cliCertDerFileExt, "rb");
  36665. AssertTrue((f != XBADFILE));
  36666. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36667. XFCLOSE(f);
  36668. for (j = 0; j < 2; ++j) {
  36669. bufPt = buf;
  36670. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36671. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36672. NID_subject_alt_name, NULL, NULL));
  36673. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  36674. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  36675. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  36676. switch (gn->type) {
  36677. case GEN_DNS:
  36678. fprintf(stderr, "found type GEN_DNS\n");
  36679. break;
  36680. case GEN_EMAIL:
  36681. fprintf(stderr, "found type GEN_EMAIL\n");
  36682. break;
  36683. case GEN_URI:
  36684. fprintf(stderr, "found type GEN_URI\n");
  36685. break;
  36686. }
  36687. }
  36688. X509_free(x509);
  36689. if (j == 0) {
  36690. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36691. }
  36692. else {
  36693. /*
  36694. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  36695. * it was supposed to. This is a regression test for that bug.
  36696. */
  36697. GENERAL_NAMES_free(sk);
  36698. }
  36699. }
  36700. res = TEST_RES_CHECK(1);
  36701. #endif
  36702. return res;
  36703. }
  36704. static int test_wolfSSL_GENERAL_NAME_print(void)
  36705. {
  36706. int res = TEST_SKIPPED;
  36707. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  36708. X509* x509;
  36709. GENERAL_NAME* gn;
  36710. unsigned char buf[4096];
  36711. const unsigned char* bufPt;
  36712. int bytes;
  36713. XFILE f;
  36714. STACK_OF(GENERAL_NAME)* sk;
  36715. BIO* out;
  36716. unsigned char outbuf[128];
  36717. X509_EXTENSION* ext;
  36718. AUTHORITY_INFO_ACCESS* aia;
  36719. ACCESS_DESCRIPTION* ad;
  36720. const unsigned char v4Addr[] = {192,168,53,1};
  36721. const unsigned char v6Addr[] =
  36722. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36723. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  36724. const unsigned char email[] =
  36725. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  36726. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  36727. const char* dnsStr = "DNS:example.com";
  36728. const char* uriStr = "URI:http://127.0.0.1:22220";
  36729. const char* v4addStr = "IP Address:192.168.53.1";
  36730. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  36731. const char* emailStr = "email:info@wolfssl.com";
  36732. const char* othrStr = "othername:<unsupported>";
  36733. const char* x400Str = "X400Name:<unsupported>";
  36734. const char* ediStr = "EdiPartyName:<unsupported>";
  36735. /* BIO to output */
  36736. AssertNotNull(out = BIO_new(BIO_s_mem()));
  36737. /* test for NULL param */
  36738. gn = NULL;
  36739. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  36740. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  36741. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  36742. /* test for GEN_DNS */
  36743. f = XFOPEN(cliCertDerFileExt, "rb");
  36744. AssertTrue((f != XBADFILE));
  36745. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36746. XFCLOSE(f);
  36747. bufPt = buf;
  36748. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36749. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36750. NID_subject_alt_name, NULL, NULL));
  36751. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  36752. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36753. XMEMSET(outbuf,0,sizeof(outbuf));
  36754. BIO_read(out, outbuf, sizeof(outbuf));
  36755. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  36756. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36757. X509_free(x509);
  36758. /* test for GEN_URI */
  36759. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  36760. AssertTrue((f != XBADFILE));
  36761. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36762. XFCLOSE(f);
  36763. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  36764. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36765. AssertNotNull(aia);
  36766. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  36767. gn = ad->location;
  36768. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36769. XMEMSET(outbuf,0,sizeof(outbuf));
  36770. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36771. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  36772. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  36773. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36774. AssertNotNull(aia);
  36775. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  36776. X509_free(x509);
  36777. /* test for GEN_IPADD */
  36778. /* ip v4 address */
  36779. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36780. gn->type = GEN_IPADD;
  36781. gn->d.iPAddress->length = sizeof(v4Addr);
  36782. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  36783. sizeof(v4Addr)), 1);
  36784. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36785. XMEMSET(outbuf,0,sizeof(outbuf));
  36786. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36787. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  36788. GENERAL_NAME_free(gn);
  36789. /* ip v6 address */
  36790. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36791. gn->type = GEN_IPADD;
  36792. gn->d.iPAddress->length = sizeof(v6Addr);
  36793. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  36794. sizeof(v6Addr)), 1);
  36795. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36796. XMEMSET(outbuf,0,sizeof(outbuf));
  36797. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36798. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  36799. GENERAL_NAME_free(gn);
  36800. /* test for GEN_EMAIL */
  36801. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36802. gn->type = GEN_EMAIL;
  36803. gn->d.rfc822Name->length = sizeof(email);
  36804. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  36805. sizeof(email)), 1);
  36806. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36807. XMEMSET(outbuf,0,sizeof(outbuf));
  36808. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36809. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  36810. GENERAL_NAME_free(gn);
  36811. /* test for GEN_OTHERNAME */
  36812. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36813. gn->type = GEN_OTHERNAME;
  36814. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36815. XMEMSET(outbuf,0,sizeof(outbuf));
  36816. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36817. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  36818. GENERAL_NAME_free(gn);
  36819. /* test for GEN_X400 */
  36820. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36821. gn->type = GEN_X400;
  36822. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36823. XMEMSET(outbuf,0,sizeof(outbuf));
  36824. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36825. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  36826. GENERAL_NAME_free(gn);
  36827. /* test for GEN_EDIPARTY */
  36828. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36829. gn->type = GEN_EDIPARTY;
  36830. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36831. XMEMSET(outbuf,0,sizeof(outbuf));
  36832. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36833. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  36834. GENERAL_NAME_free(gn);
  36835. BIO_free(out);
  36836. res = TEST_RES_CHECK(1);
  36837. #endif /* OPENSSL_ALL */
  36838. return res;
  36839. }
  36840. static int test_wolfSSL_sk_DIST_POINT(void)
  36841. {
  36842. int res = TEST_SKIPPED;
  36843. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36844. !defined(NO_RSA)
  36845. X509* x509;
  36846. unsigned char buf[4096];
  36847. const unsigned char* bufPt;
  36848. int bytes, i, j;
  36849. XFILE f;
  36850. DIST_POINT* dp;
  36851. DIST_POINT_NAME* dpn;
  36852. GENERAL_NAME* gn;
  36853. ASN1_IA5STRING* uri;
  36854. STACK_OF(DIST_POINT)* dps;
  36855. STACK_OF(GENERAL_NAME)* gns;
  36856. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  36857. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  36858. AssertTrue((f != XBADFILE));
  36859. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36860. XFCLOSE(f);
  36861. bufPt = buf;
  36862. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36863. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  36864. NID_crl_distribution_points, NULL, NULL));
  36865. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  36866. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  36867. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  36868. AssertNotNull(dpn = dp->distpoint);
  36869. /* this should be type 0, fullname */
  36870. AssertIntEQ(dpn->type, 0);
  36871. gns = dp->distpoint->name.fullname;
  36872. AssertNotNull(gns);
  36873. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  36874. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  36875. gn = sk_GENERAL_NAME_value(gns, j);
  36876. AssertIntEQ(gn->type, GEN_URI);
  36877. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  36878. AssertNotNull(uri->data);
  36879. AssertIntGT(uri->length, 0);
  36880. }
  36881. }
  36882. X509_free(x509);
  36883. CRL_DIST_POINTS_free(dps);
  36884. res = TEST_RES_CHECK(1);
  36885. #endif
  36886. return res;
  36887. }
  36888. static int test_wolfSSL_MD4(void)
  36889. {
  36890. int res = TEST_SKIPPED;
  36891. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36892. MD4_CTX md4;
  36893. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36894. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36895. "789012345678901234567890";
  36896. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36897. "\xcc\x05\x36";
  36898. int msgSz = (int)XSTRLEN(msg);
  36899. XMEMSET(out, 0, sizeof(out));
  36900. MD4_Init(&md4);
  36901. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36902. MD4_Final(out, &md4);
  36903. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36904. res = TEST_RES_CHECK(1);
  36905. #endif
  36906. return res;
  36907. }
  36908. static int test_wolfSSL_verify_mode(void)
  36909. {
  36910. int res = TEST_SKIPPED;
  36911. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  36912. WOLFSSL* ssl;
  36913. WOLFSSL_CTX* ctx;
  36914. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36915. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  36916. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  36917. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  36918. AssertNotNull(ssl = SSL_new(ctx));
  36919. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36920. SSL_free(ssl);
  36921. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  36922. AssertNotNull(ssl = SSL_new(ctx));
  36923. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36924. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36925. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36926. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  36927. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36928. SSL_free(ssl);
  36929. wolfSSL_CTX_set_verify(ctx,
  36930. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36931. AssertNotNull(ssl = SSL_new(ctx));
  36932. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36933. AssertIntEQ(SSL_get_verify_mode(ssl),
  36934. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36935. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  36936. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  36937. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36938. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36939. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36940. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36941. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36942. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36943. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  36944. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  36945. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  36946. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  36947. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  36948. #endif
  36949. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  36950. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36951. SSL_free(ssl);
  36952. SSL_CTX_free(ctx);
  36953. res = TEST_RES_CHECK(1);
  36954. #endif
  36955. return res;
  36956. }
  36957. static int test_wolfSSL_verify_depth(void)
  36958. {
  36959. int res = TEST_SKIPPED;
  36960. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36961. WOLFSSL* ssl;
  36962. WOLFSSL_CTX* ctx;
  36963. long depth;
  36964. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36965. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  36966. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  36967. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  36968. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  36969. AssertNotNull(ssl = SSL_new(ctx));
  36970. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  36971. SSL_free(ssl);
  36972. SSL_CTX_set_verify_depth(ctx, -1);
  36973. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  36974. SSL_CTX_set_verify_depth(ctx, 2);
  36975. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  36976. AssertNotNull(ssl = SSL_new(ctx));
  36977. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  36978. SSL_free(ssl);
  36979. SSL_CTX_free(ctx);
  36980. res = TEST_RES_CHECK(1);
  36981. #endif
  36982. return res;
  36983. }
  36984. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36985. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36986. * buffer of 64 bytes.
  36987. *
  36988. * returns the size of the digest buffer on success and a negative value on
  36989. * failure.
  36990. */
  36991. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  36992. {
  36993. HMAC_CTX ctx1;
  36994. HMAC_CTX ctx2;
  36995. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36996. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36997. unsigned char long_key[] =
  36998. "0123456789012345678901234567890123456789"
  36999. "0123456789012345678901234567890123456789"
  37000. "0123456789012345678901234567890123456789"
  37001. "0123456789012345678901234567890123456789";
  37002. unsigned char msg[] = "message to hash";
  37003. unsigned int digestSz = 64;
  37004. int keySz = sizeof(key);
  37005. int long_keySz = sizeof(long_key);
  37006. int msgSz = sizeof(msg);
  37007. unsigned char digest2[64];
  37008. unsigned int digestSz2 = 64;
  37009. HMAC_CTX_init(&ctx1);
  37010. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37011. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37012. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37013. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37014. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37015. HMAC_CTX_cleanup(&ctx1);
  37016. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37017. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  37018. HMAC_CTX_cleanup(&ctx2);
  37019. AssertIntEQ(digestSz, digestSz2);
  37020. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37021. /* test HMAC_Init with NULL key */
  37022. /* init after copy */
  37023. HMAC_CTX_init(&ctx1);
  37024. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37025. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37026. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37027. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37028. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37029. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37030. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37031. HMAC_CTX_cleanup(&ctx1);
  37032. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  37033. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37034. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37035. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37036. HMAC_CTX_cleanup(&ctx2);
  37037. AssertIntEQ(digestSz, digestSz2);
  37038. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37039. /* long key */
  37040. HMAC_CTX_init(&ctx1);
  37041. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  37042. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37043. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37044. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37045. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37046. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37047. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37048. HMAC_CTX_cleanup(&ctx1);
  37049. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  37050. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37051. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37052. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37053. HMAC_CTX_cleanup(&ctx2);
  37054. AssertIntEQ(digestSz, digestSz2);
  37055. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37056. /* init before copy */
  37057. HMAC_CTX_init(&ctx1);
  37058. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37059. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37060. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37061. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37062. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37063. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37064. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37065. HMAC_CTX_cleanup(&ctx1);
  37066. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37067. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37068. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37069. HMAC_CTX_cleanup(&ctx2);
  37070. AssertIntEQ(digestSz, digestSz2);
  37071. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37072. return digestSz;
  37073. }
  37074. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  37075. static int test_wolfSSL_HMAC_CTX(void)
  37076. {
  37077. int res = TEST_SKIPPED;
  37078. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  37079. unsigned char digest[64];
  37080. int digestSz;
  37081. #ifndef NO_SHA
  37082. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  37083. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  37084. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  37085. #endif /* !NO_SHA */
  37086. #ifdef WOLFSSL_SHA224
  37087. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  37088. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  37089. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  37090. "\x02\x0E", digest, digestSz), 0);
  37091. #endif /* WOLFSSL_SHA224 */
  37092. #ifndef NO_SHA256
  37093. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  37094. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  37095. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  37096. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  37097. #endif /* !NO_SHA256 */
  37098. #ifdef WOLFSSL_SHA384
  37099. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  37100. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  37101. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  37102. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  37103. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  37104. digest, digestSz), 0);
  37105. #endif /* WOLFSSL_SHA384 */
  37106. #ifdef WOLFSSL_SHA512
  37107. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  37108. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  37109. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  37110. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  37111. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  37112. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  37113. digest, digestSz), 0);
  37114. #endif /* WOLFSSL_SHA512 */
  37115. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  37116. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  37117. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  37118. "\xE4\x98\xDD", digest, digestSz), 0);
  37119. #endif /* !NO_MD5 */
  37120. res = TEST_RES_CHECK(1);
  37121. #endif
  37122. return res;
  37123. }
  37124. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  37125. static void sslMsgCb(int w, int version, int type, const void* buf,
  37126. size_t sz, SSL* ssl, void* arg)
  37127. {
  37128. int i;
  37129. unsigned char* pt = (unsigned char*)buf;
  37130. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  37131. (w)?"Writing":"Reading", (int)sz, version, type);
  37132. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  37133. fprintf(stderr, "\n");
  37134. (void)ssl;
  37135. (void)arg;
  37136. }
  37137. #endif /* OPENSSL_EXTRA */
  37138. static int test_wolfSSL_msg_callback(void)
  37139. {
  37140. int res = TEST_SKIPPED;
  37141. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  37142. WOLFSSL* ssl;
  37143. WOLFSSL_CTX* ctx;
  37144. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  37145. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  37146. SSL_FILETYPE_PEM));
  37147. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  37148. SSL_FILETYPE_PEM));
  37149. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  37150. SSL_SUCCESS);
  37151. AssertNotNull(ssl = SSL_new(ctx));
  37152. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  37153. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  37154. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  37155. SSL_free(ssl);
  37156. SSL_CTX_free(ctx);
  37157. res = TEST_RES_CHECK(1);
  37158. #endif
  37159. return res;
  37160. }
  37161. static int test_wolfSSL_SHA(void)
  37162. {
  37163. int res = TEST_SKIPPED;
  37164. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  37165. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  37166. (!defined(HAVE_FIPS) || \
  37167. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37168. {
  37169. const unsigned char in[] = "abc";
  37170. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  37171. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  37172. unsigned char out[WC_SHA_DIGEST_SIZE];
  37173. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37174. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  37175. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37176. /* SHA interface test */
  37177. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37178. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  37179. AssertNotNull(SHA(in, 0, out));
  37180. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  37181. AssertNotNull(SHA(NULL, 0, out));
  37182. AssertNotNull(SHA(NULL, 0, NULL));
  37183. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  37184. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37185. }
  37186. #endif
  37187. #if !defined(NO_SHA256)
  37188. {
  37189. const unsigned char in[] = "abc";
  37190. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37191. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37192. "\x15\xAD";
  37193. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37194. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37195. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37196. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37197. #else
  37198. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37199. #endif
  37200. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37201. }
  37202. #endif
  37203. #if defined(WOLFSSL_SHA384)
  37204. {
  37205. const unsigned char in[] = "abc";
  37206. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37207. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37208. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37209. "\xc8\x25\xa7";
  37210. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37211. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37212. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37213. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37214. #else
  37215. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37216. #endif
  37217. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37218. }
  37219. #endif
  37220. #if defined(WOLFSSL_SHA512)
  37221. {
  37222. const unsigned char in[] = "abc";
  37223. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37224. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37225. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37226. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37227. "\xa5\x4c\xa4\x9f";
  37228. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37229. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37230. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37231. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37232. #else
  37233. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37234. #endif
  37235. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37236. }
  37237. #endif
  37238. res = TEST_RES_CHECK(1);
  37239. #endif
  37240. return res;
  37241. }
  37242. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  37243. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  37244. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  37245. static void binary_dump(void *ptr, int size)
  37246. {
  37247. #ifdef WOLFSSL_EVP_PRINT
  37248. int i = 0;
  37249. unsigned char *p = (unsigned char *) ptr;
  37250. fprintf(stderr, "{");
  37251. while ((p != NULL) && (i < size)) {
  37252. if ((i % 8) == 0) {
  37253. fprintf(stderr, "\n");
  37254. fprintf(stderr, " ");
  37255. }
  37256. fprintf(stderr, "0x%02x, ", p[i]);
  37257. i++;
  37258. }
  37259. fprintf(stderr, "\n};\n");
  37260. #else
  37261. (void) ptr;
  37262. (void) size;
  37263. #endif
  37264. }
  37265. static int last_val = 0x0f;
  37266. static int check_result(unsigned char *data, int len)
  37267. {
  37268. int i;
  37269. for ( ; len; ) {
  37270. last_val = (last_val + 1) % 16;
  37271. for (i = 0; i < 16; len--, i++, data++)
  37272. if (*data != last_val) {
  37273. return -1;
  37274. }
  37275. }
  37276. return 0;
  37277. }
  37278. static int r_offset;
  37279. static int w_offset;
  37280. static void init_offset(void)
  37281. {
  37282. r_offset = 0;
  37283. w_offset = 0;
  37284. }
  37285. static void get_record(unsigned char *data, unsigned char *buf, int len)
  37286. {
  37287. XMEMCPY(buf, data+r_offset, len);
  37288. r_offset += len;
  37289. }
  37290. static void set_record(unsigned char *data, unsigned char *buf, int len)
  37291. {
  37292. XMEMCPY(data+w_offset, buf, len);
  37293. w_offset += len;
  37294. }
  37295. static void set_plain(unsigned char *plain, int rec)
  37296. {
  37297. int i, j;
  37298. unsigned char *p = plain;
  37299. #define BLOCKSZ 16
  37300. for (i=0; i<(rec/BLOCKSZ); i++) {
  37301. for (j=0; j<BLOCKSZ; j++)
  37302. *p++ = (i % 16);
  37303. }
  37304. }
  37305. #endif
  37306. static int test_wolfSSL_EVP_Cipher_extra(void)
  37307. {
  37308. int res = TEST_SKIPPED;
  37309. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  37310. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  37311. /* aes128-cbc, keylen=16, ivlen=16 */
  37312. byte aes128_cbc_key[] = {
  37313. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  37314. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  37315. };
  37316. byte aes128_cbc_iv[] = {
  37317. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  37318. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  37319. };
  37320. /* teset data size table */
  37321. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  37322. int test_drive2[] = {8, 3, 8, 512, 0};
  37323. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  37324. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  37325. int test_drive_len[100];
  37326. int ret = 0;
  37327. EVP_CIPHER_CTX *evp = NULL;
  37328. int ilen = 0;
  37329. int klen = 0;
  37330. int i, j;
  37331. const EVP_CIPHER *type;
  37332. byte *iv;
  37333. byte *key;
  37334. int ivlen;
  37335. int keylen;
  37336. #define RECORDS 16
  37337. #define BUFFSZ 512
  37338. byte plain [BUFFSZ * RECORDS];
  37339. byte cipher[BUFFSZ * RECORDS];
  37340. byte inb[BUFFSZ];
  37341. byte outb[BUFFSZ+16];
  37342. int outl, inl;
  37343. iv = aes128_cbc_iv;
  37344. ivlen = sizeof(aes128_cbc_iv);
  37345. key = aes128_cbc_key;
  37346. keylen = sizeof(aes128_cbc_key);
  37347. type = EVP_aes_128_cbc();
  37348. set_plain(plain, BUFFSZ * RECORDS);
  37349. SSL_library_init();
  37350. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  37351. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  37352. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  37353. klen = EVP_CIPHER_CTX_key_length(evp);
  37354. if (klen > 0 && keylen != klen) {
  37355. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  37356. }
  37357. ilen = EVP_CIPHER_CTX_iv_length(evp);
  37358. if (ilen > 0 && ivlen != ilen) {
  37359. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  37360. }
  37361. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37362. for (j = 0; j<RECORDS; j++)
  37363. {
  37364. inl = BUFFSZ;
  37365. get_record(plain, inb, inl);
  37366. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  37367. set_record(cipher, outb, outl);
  37368. }
  37369. for (i = 0; test_drive[i]; i++) {
  37370. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37371. init_offset();
  37372. test_drive_len[i] = 0;
  37373. for (j = 0; test_drive[i][j]; j++)
  37374. {
  37375. inl = test_drive[i][j];
  37376. test_drive_len[i] += inl;
  37377. get_record(plain, inb, inl);
  37378. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  37379. /* output to cipher buffer, so that following Dec test can detect
  37380. if any error */
  37381. set_record(cipher, outb, outl);
  37382. }
  37383. EVP_CipherFinal(evp, outb, &outl);
  37384. if (outl > 0)
  37385. set_record(cipher, outb, outl);
  37386. }
  37387. for (i = 0; test_drive[i]; i++) {
  37388. last_val = 0x0f;
  37389. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  37390. init_offset();
  37391. for (j = 0; test_drive[i][j]; j++) {
  37392. inl = test_drive[i][j];
  37393. get_record(cipher, inb, inl);
  37394. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  37395. binary_dump(outb, outl);
  37396. AssertIntEQ((ret = check_result(outb, outl)), 0);
  37397. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  37398. }
  37399. ret = EVP_CipherFinal(evp, outb, &outl);
  37400. binary_dump(outb, outl);
  37401. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  37402. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  37403. AssertTrue(ret);
  37404. }
  37405. EVP_CIPHER_CTX_free(evp);
  37406. /* Do an extra test to verify correct behavior with empty input. */
  37407. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  37408. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  37409. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  37410. klen = EVP_CIPHER_CTX_key_length(evp);
  37411. if (klen > 0 && keylen != klen) {
  37412. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  37413. }
  37414. ilen = EVP_CIPHER_CTX_iv_length(evp);
  37415. if (ilen > 0 && ivlen != ilen) {
  37416. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  37417. }
  37418. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37419. /* outl should be set to 0 after passing NULL, 0 for input args. */
  37420. outl = -1;
  37421. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  37422. AssertIntEQ(outl, 0);
  37423. EVP_CIPHER_CTX_free(evp);
  37424. res = TEST_RES_CHECK(1);
  37425. #endif /* test_EVP_Cipher */
  37426. return res;
  37427. }
  37428. static int test_wolfSSL_PEM_read_DHparams(void)
  37429. {
  37430. int res = TEST_SKIPPED;
  37431. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  37432. !defined(NO_FILESYSTEM)
  37433. DH* dh;
  37434. XFILE fp;
  37435. unsigned char derOut[300];
  37436. unsigned char* derOutBuf = derOut;
  37437. int derOutSz = 0;
  37438. unsigned char derExpected[300];
  37439. int derExpectedSz = 0;
  37440. XMEMSET(derOut, 0, sizeof(derOut));
  37441. XMEMSET(derExpected, 0, sizeof(derExpected));
  37442. /* open DH param file, read into DH struct */
  37443. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  37444. /* bad args */
  37445. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  37446. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  37447. /* good args */
  37448. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37449. XFCLOSE(fp);
  37450. /* read in certs/dh2048.der for comparison against exported params */
  37451. fp = XFOPEN("./certs/dh2048.der", "rb");
  37452. AssertTrue(fp != XBADFILE);
  37453. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  37454. XFCLOSE(fp);
  37455. /* export DH back to DER and compare */
  37456. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  37457. AssertIntEQ(derOutSz, derExpectedSz);
  37458. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  37459. DH_free(dh);
  37460. dh = NULL;
  37461. /* Test parsing with X9.42 header */
  37462. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  37463. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37464. XFCLOSE(fp);
  37465. DH_free(dh);
  37466. res = TEST_RES_CHECK(1);
  37467. #endif
  37468. return res;
  37469. }
  37470. static int test_wolfSSL_AES_ecb_encrypt(void)
  37471. {
  37472. int res = TEST_SKIPPED;
  37473. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  37474. AES_KEY aes;
  37475. const byte msg[] =
  37476. {
  37477. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37478. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37479. };
  37480. const byte verify[] =
  37481. {
  37482. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37483. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37484. };
  37485. const byte key[] =
  37486. {
  37487. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37488. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37489. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37490. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37491. };
  37492. byte out[AES_BLOCK_SIZE];
  37493. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37494. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37495. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37496. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37497. #ifdef HAVE_AES_DECRYPT
  37498. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37499. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37500. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37501. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37502. #endif
  37503. /* test bad arguments */
  37504. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37505. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37506. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37507. res = TEST_RES_CHECK(1);
  37508. #endif
  37509. return res;
  37510. }
  37511. static int test_wolfSSL_MD5(void)
  37512. {
  37513. int res = TEST_SKIPPED;
  37514. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37515. byte input1[] = "";
  37516. byte input2[] = "message digest";
  37517. byte hash[WC_MD5_DIGEST_SIZE];
  37518. unsigned char output1[] =
  37519. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37520. unsigned char output2[] =
  37521. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37522. WOLFSSL_MD5_CTX md5;
  37523. XMEMSET(&md5, 0, sizeof(md5));
  37524. /* Test cases for illegal parameters */
  37525. AssertIntEQ(MD5_Init(NULL), 0);
  37526. AssertIntEQ(MD5_Init(&md5), 1);
  37527. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  37528. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37529. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37530. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  37531. AssertIntEQ(MD5_Final(hash, NULL), 0);
  37532. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  37533. /* Init MD5 CTX */
  37534. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37535. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  37536. XSTRLEN((const char*)&input1)), 1);
  37537. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37538. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37539. /* Init MD5 CTX */
  37540. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37541. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37542. (int)XSTRLEN((const char*)input2)), 1);
  37543. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37544. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37545. #if !defined(NO_OLD_NAMES) && \
  37546. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37547. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37548. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37549. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  37550. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  37551. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  37552. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37553. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  37554. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37555. {
  37556. byte data[] = "Data to be hashed.";
  37557. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37558. AssertNotNull(MD5(data, sizeof(data), NULL));
  37559. AssertNotNull(MD5(data, sizeof(data), hash));
  37560. AssertNotNull(MD5(NULL, 0, hash));
  37561. AssertNull(MD5(NULL, sizeof(data), hash));
  37562. }
  37563. #endif
  37564. res = TEST_RES_CHECK(1);
  37565. #endif
  37566. return res;
  37567. }
  37568. static int test_wolfSSL_MD5_Transform(void)
  37569. {
  37570. int res = TEST_SKIPPED;
  37571. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37572. byte input1[] = "";
  37573. byte input2[] = "abc";
  37574. byte local[WC_MD5_BLOCK_SIZE];
  37575. word32 sLen = 0;
  37576. #ifdef BIG_ENDIAN_ORDER
  37577. unsigned char output1[] =
  37578. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37579. unsigned char output2[] =
  37580. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37581. #else
  37582. unsigned char output1[] =
  37583. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37584. unsigned char output2[] =
  37585. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37586. #endif
  37587. union {
  37588. wc_Md5 native;
  37589. MD5_CTX compat;
  37590. } md5;
  37591. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37592. XMEMSET(&local, 0, sizeof(local));
  37593. /* sanity check */
  37594. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  37595. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37596. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37597. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37598. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37599. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37600. /* Init MD5 CTX */
  37601. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37602. /* Do Transform*/
  37603. sLen = (word32)XSTRLEN((char*)input1);
  37604. XMEMCPY(local, input1, sLen);
  37605. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37606. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  37607. WC_MD5_DIGEST_SIZE), 0);
  37608. /* Init MD5 CTX */
  37609. AssertIntEQ(MD5_Init(&md5.compat), 1);
  37610. sLen = (word32)XSTRLEN((char*)input2);
  37611. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37612. XMEMCPY(local, input2, sLen);
  37613. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37614. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  37615. WC_MD5_DIGEST_SIZE), 0);
  37616. res = TEST_RES_CHECK(1);
  37617. #endif
  37618. return res;
  37619. }
  37620. static int test_wolfSSL_SHA224(void)
  37621. {
  37622. int res = TEST_SKIPPED;
  37623. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37624. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37625. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37626. unsigned char input[] =
  37627. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37628. unsigned char output[] =
  37629. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37630. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37631. size_t inLen;
  37632. byte hash[WC_SHA224_DIGEST_SIZE];
  37633. inLen = XSTRLEN((char*)input);
  37634. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37635. AssertNull(SHA224(NULL, inLen, hash));
  37636. AssertNotNull(SHA224(input, 0, hash));
  37637. AssertNotNull(SHA224(input, inLen, NULL));
  37638. AssertNotNull(SHA224(NULL, 0, hash));
  37639. AssertNotNull(SHA224(NULL, 0, NULL));
  37640. AssertNotNull(SHA224(input, inLen, hash));
  37641. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37642. res = TEST_RES_CHECK(1);
  37643. #endif
  37644. return res;
  37645. }
  37646. static int test_wolfSSL_SHA_Transform(void)
  37647. {
  37648. int res = TEST_SKIPPED;
  37649. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37650. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37651. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37652. byte input1[] = "";
  37653. byte input2[] = "abc";
  37654. byte local[WC_SHA_BLOCK_SIZE];
  37655. word32 sLen = 0;
  37656. #ifdef BIG_ENDIAN_ORDER
  37657. unsigned char output1[] =
  37658. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37659. "\x09\xf7\x3a\x93";
  37660. unsigned char output2[] =
  37661. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37662. "\x7c\x6e\x08\xb8";
  37663. #else
  37664. unsigned char output1[] =
  37665. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37666. "\x93\x3a\xf7\x09";
  37667. unsigned char output2[] =
  37668. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37669. "\xb8\x08\x6e\x7c";
  37670. #endif
  37671. union {
  37672. wc_Sha native;
  37673. SHA_CTX compat;
  37674. } sha;
  37675. union {
  37676. wc_Sha native;
  37677. SHA_CTX compat;
  37678. } sha1;
  37679. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37680. XMEMSET(&local, 0, sizeof(local));
  37681. /* sanity check */
  37682. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  37683. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37684. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37685. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  37686. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37687. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37688. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37689. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37690. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37691. /* Init SHA CTX */
  37692. AssertIntEQ(SHA_Init(&sha.compat), 1);
  37693. /* Do Transform*/
  37694. sLen = (word32)XSTRLEN((char*)input1);
  37695. XMEMCPY(local, input1, sLen);
  37696. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37697. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  37698. WC_SHA_DIGEST_SIZE), 0);
  37699. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37700. /* Init SHA CTX */
  37701. AssertIntEQ(SHA_Init(&sha.compat), 1);
  37702. sLen = (word32)XSTRLEN((char*)input2);
  37703. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37704. XMEMCPY(local, input2, sLen);
  37705. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37706. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  37707. WC_SHA_DIGEST_SIZE), 0);
  37708. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37709. /* SHA1 */
  37710. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37711. /* Init SHA CTX */
  37712. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  37713. /* Do Transform*/
  37714. sLen = (word32)XSTRLEN((char*)input1);
  37715. XMEMCPY(local, input1, sLen);
  37716. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37717. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  37718. WC_SHA_DIGEST_SIZE), 0);
  37719. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37720. /* Init SHA CTX */
  37721. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  37722. sLen = (word32)XSTRLEN((char*)input2);
  37723. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37724. XMEMCPY(local, input2, sLen);
  37725. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37726. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  37727. WC_SHA_DIGEST_SIZE), 0);
  37728. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37729. res = TEST_RES_CHECK(1);
  37730. #endif
  37731. #endif
  37732. return res;
  37733. }
  37734. static int test_wolfSSL_SHA256_Transform(void)
  37735. {
  37736. int res = TEST_SKIPPED;
  37737. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37738. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37739. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37740. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37741. !defined(WOLFSSL_KCAPI_HASH)
  37742. byte input1[] = "";
  37743. byte input2[] = "abc";
  37744. byte local[WC_SHA256_BLOCK_SIZE];
  37745. word32 sLen = 0;
  37746. #ifdef BIG_ENDIAN_ORDER
  37747. unsigned char output1[] =
  37748. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37749. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37750. unsigned char output2[] =
  37751. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37752. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37753. #else
  37754. unsigned char output1[] =
  37755. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37756. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37757. unsigned char output2[] =
  37758. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37759. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37760. #endif
  37761. union {
  37762. wc_Sha256 native;
  37763. SHA256_CTX compat;
  37764. } sha256;
  37765. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37766. XMEMSET(&local, 0, sizeof(local));
  37767. /* sanity check */
  37768. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  37769. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37770. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37771. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37772. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37773. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37774. /* Init SHA256 CTX */
  37775. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  37776. /* Do Transform*/
  37777. sLen = (word32)XSTRLEN((char*)input1);
  37778. XMEMCPY(local, input1, sLen);
  37779. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37780. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  37781. WC_SHA256_DIGEST_SIZE), 0);
  37782. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37783. /* Init SHA256 CTX */
  37784. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  37785. sLen = (word32)XSTRLEN((char*)input2);
  37786. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37787. XMEMCPY(local, input2, sLen);
  37788. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37789. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  37790. WC_SHA256_DIGEST_SIZE), 0);
  37791. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37792. res = TEST_RES_CHECK(1);
  37793. #endif
  37794. #endif
  37795. return res;
  37796. }
  37797. static int test_wolfSSL_SHA256(void)
  37798. {
  37799. int res = TEST_SKIPPED;
  37800. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37801. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37802. unsigned char input[] =
  37803. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37804. unsigned char output[] =
  37805. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37806. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37807. "\x06\xC1";
  37808. size_t inLen;
  37809. byte hash[WC_SHA256_DIGEST_SIZE];
  37810. inLen = XSTRLEN((char*)input);
  37811. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37812. AssertNotNull(SHA256(input, inLen, hash));
  37813. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37814. res = TEST_RES_CHECK(1);
  37815. #endif
  37816. return res;
  37817. }
  37818. static int test_wolfSSL_SHA512_Transform(void)
  37819. {
  37820. int res = TEST_SKIPPED;
  37821. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37822. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37823. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37824. !defined(WOLFSSL_KCAPI_HASH)
  37825. byte input1[] = "";
  37826. byte input2[] = "abc";
  37827. byte local[WC_SHA512_BLOCK_SIZE];
  37828. word32 sLen = 0;
  37829. #ifdef BIG_ENDIAN_ORDER
  37830. unsigned char output1[] =
  37831. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37832. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37833. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37834. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37835. unsigned char output2[] =
  37836. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37837. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37838. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37839. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37840. #else
  37841. unsigned char output1[] =
  37842. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37843. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37844. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37845. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37846. unsigned char output2[] =
  37847. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37848. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37849. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37850. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37851. #endif
  37852. union {
  37853. wc_Sha512 native;
  37854. SHA512_CTX compat;
  37855. } sha512;
  37856. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37857. XMEMSET(&local, 0, sizeof(local));
  37858. /* sanity check */
  37859. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  37860. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37861. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37862. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37863. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37864. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37865. /* Init SHA512 CTX */
  37866. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37867. /* Do Transform*/
  37868. sLen = (word32)XSTRLEN((char*)input1);
  37869. XMEMCPY(local, input1, sLen);
  37870. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37871. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  37872. WC_SHA512_DIGEST_SIZE), 0);
  37873. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37874. /* Init SHA512 CTX */
  37875. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  37876. sLen = (word32)XSTRLEN((char*)input2);
  37877. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37878. XMEMCPY(local, input2, sLen);
  37879. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37880. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  37881. WC_SHA512_DIGEST_SIZE), 0);
  37882. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37883. (void)input1;
  37884. res = TEST_RES_CHECK(1);
  37885. #endif
  37886. #endif
  37887. return res;
  37888. }
  37889. static int test_wolfSSL_X509_get_serialNumber(void)
  37890. {
  37891. int res = TEST_SKIPPED;
  37892. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37893. ASN1_INTEGER* a;
  37894. BIGNUM* bn;
  37895. X509* x509;
  37896. char *serialHex;
  37897. byte serial[3];
  37898. int serialSz;
  37899. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  37900. SSL_FILETYPE_PEM));
  37901. AssertNotNull(a = X509_get_serialNumber(x509));
  37902. /* check on value of ASN1 Integer */
  37903. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  37904. /* test setting serial number and then retrieving it */
  37905. AssertNotNull(a = ASN1_INTEGER_new());
  37906. ASN1_INTEGER_set(a, 3);
  37907. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37908. serialSz = sizeof(serial);
  37909. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37910. WOLFSSL_SUCCESS);
  37911. AssertIntEQ(serialSz, 1);
  37912. AssertIntEQ(serial[0], 3);
  37913. ASN1_INTEGER_free(a);
  37914. /* test setting serial number with 0's in it */
  37915. serial[0] = 0x01;
  37916. serial[1] = 0x00;
  37917. serial[2] = 0x02;
  37918. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  37919. a->data[0] = ASN_INTEGER;
  37920. a->data[1] = sizeof(serial);
  37921. XMEMCPY(&a->data[2], serial, sizeof(serial));
  37922. a->length = sizeof(serial) + 2;
  37923. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37924. XMEMSET(serial, 0, sizeof(serial));
  37925. serialSz = sizeof(serial);
  37926. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37927. WOLFSSL_SUCCESS);
  37928. AssertIntEQ(serialSz, 3);
  37929. AssertIntEQ(serial[0], 0x01);
  37930. AssertIntEQ(serial[1], 0x00);
  37931. AssertIntEQ(serial[2], 0x02);
  37932. ASN1_INTEGER_free(a);
  37933. X509_free(x509); /* free's a */
  37934. AssertNotNull(serialHex = BN_bn2hex(bn));
  37935. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  37936. AssertStrEQ(serialHex, "01");
  37937. #else
  37938. AssertStrEQ(serialHex, "1");
  37939. #endif
  37940. OPENSSL_free(serialHex);
  37941. AssertIntEQ(BN_get_word(bn), 1);
  37942. BN_free(bn);
  37943. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  37944. a = ASN1_INTEGER_new();
  37945. if (a) {
  37946. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  37947. DYNAMIC_TYPE_OPENSSL));
  37948. a->isDynamic = 1;
  37949. ASN1_INTEGER_free(a);
  37950. }
  37951. res = TEST_RES_CHECK(1);
  37952. #endif
  37953. return res;
  37954. }
  37955. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  37956. {
  37957. int res = TEST_SKIPPED;
  37958. #if defined(OPENSSL_EXTRA)
  37959. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  37960. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  37961. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  37962. res = TEST_RES_CHECK(1);
  37963. #endif
  37964. return res;
  37965. }
  37966. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  37967. {
  37968. int res = TEST_SKIPPED;
  37969. #if defined(OPENSSL_EXTRA)
  37970. #define MAX_HEXSTR_BUFSZ 9
  37971. #define NUM_CASES 5
  37972. struct Output {
  37973. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  37974. long ret;
  37975. };
  37976. int i;
  37977. int j;
  37978. const char* inputs[NUM_CASES] = {
  37979. "aabcd1357e",
  37980. "01:12:23:34:a5:b6:c7:d8:e9",
  37981. ":01:02",
  37982. "012",
  37983. ":ab:ac:d"
  37984. };
  37985. struct Output expectedOutputs[NUM_CASES] = {
  37986. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  37987. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  37988. {{0x01, 0x02}, 2},
  37989. {{0x00}, 0},
  37990. {{0x00}, 0}
  37991. };
  37992. long len = 0;
  37993. unsigned char* returnedBuf = NULL;
  37994. for (i = 0; i < NUM_CASES; ++i) {
  37995. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  37996. if (returnedBuf == NULL) {
  37997. AssertIntEQ(expectedOutputs[i].ret, 0);
  37998. continue;
  37999. }
  38000. AssertIntEQ(expectedOutputs[i].ret, len);
  38001. for (j = 0; j < len; ++j) {
  38002. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  38003. }
  38004. OPENSSL_free(returnedBuf);
  38005. }
  38006. res = TEST_RES_CHECK(1);
  38007. #endif
  38008. return res;
  38009. }
  38010. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  38011. {
  38012. int res = TEST_SKIPPED;
  38013. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  38014. #ifndef NO_BIO
  38015. ASN1_STRING* asn_str;
  38016. const char data[] = "Hello wolfSSL!";
  38017. ASN1_STRING* esc_str;
  38018. const char esc_data[] = "a+;<>";
  38019. BIO *bio;
  38020. unsigned long flags;
  38021. int p_len;
  38022. unsigned char rbuf[255];
  38023. /* setup */
  38024. XMEMSET(rbuf, 0, 255);
  38025. bio = BIO_new(BIO_s_mem());
  38026. BIO_set_write_buf_size(bio,255);
  38027. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  38028. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  38029. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  38030. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  38031. /* no flags */
  38032. XMEMSET(rbuf, 0, 255);
  38033. flags = 0;
  38034. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  38035. AssertIntEQ(p_len, 15);
  38036. BIO_read(bio, (void*)rbuf, 15);
  38037. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  38038. /* RFC2253 Escape */
  38039. XMEMSET(rbuf, 0, 255);
  38040. flags = ASN1_STRFLGS_ESC_2253;
  38041. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  38042. AssertIntEQ(p_len, 9);
  38043. BIO_read(bio, (void*)rbuf, 9);
  38044. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  38045. /* Show type */
  38046. XMEMSET(rbuf, 0, 255);
  38047. flags = ASN1_STRFLGS_SHOW_TYPE;
  38048. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  38049. AssertIntEQ(p_len, 28);
  38050. BIO_read(bio, (void*)rbuf, 28);
  38051. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  38052. /* Dump All */
  38053. XMEMSET(rbuf, 0, 255);
  38054. flags = ASN1_STRFLGS_DUMP_ALL;
  38055. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  38056. AssertIntEQ(p_len, 31);
  38057. BIO_read(bio, (void*)rbuf, 31);
  38058. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  38059. /* Dump Der */
  38060. XMEMSET(rbuf, 0, 255);
  38061. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  38062. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  38063. AssertIntEQ(p_len, 35);
  38064. BIO_read(bio, (void*)rbuf, 35);
  38065. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  38066. /* Dump All + Show type */
  38067. XMEMSET(rbuf, 0, 255);
  38068. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  38069. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  38070. AssertIntEQ(p_len, 44);
  38071. BIO_read(bio, (void*)rbuf, 44);
  38072. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  38073. BIO_free(bio);
  38074. ASN1_STRING_free(asn_str);
  38075. ASN1_STRING_free(esc_str);
  38076. res = TEST_RES_CHECK(1);
  38077. #endif /* !NO_BIO */
  38078. #endif
  38079. return res;
  38080. }
  38081. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  38082. {
  38083. int res = TEST_SKIPPED;
  38084. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  38085. WOLFSSL_ASN1_TIME *t;
  38086. WOLFSSL_ASN1_TIME *out;
  38087. WOLFSSL_ASN1_TIME *gtime;
  38088. int tlen = 0;
  38089. unsigned char *data;
  38090. /* UTC Time test */
  38091. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  38092. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  38093. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  38094. t->type = ASN_UTC_TIME;
  38095. t->length = ASN_UTC_TIME_SIZE;
  38096. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  38097. tlen = wolfSSL_ASN1_TIME_get_length(t);
  38098. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  38099. data = wolfSSL_ASN1_TIME_get_data(t);
  38100. AssertStrEQ((char*)data, "050727123456Z");
  38101. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  38102. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  38103. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  38104. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  38105. /* Generalized Time test */
  38106. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  38107. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  38108. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  38109. t->type = ASN_GENERALIZED_TIME;
  38110. t->length = ASN_GENERALIZED_TIME_SIZE;
  38111. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  38112. tlen = wolfSSL_ASN1_TIME_get_length(t);
  38113. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  38114. data = wolfSSL_ASN1_TIME_get_data(t);
  38115. AssertStrEQ((char*)data, "20050727123456Z");
  38116. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  38117. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  38118. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  38119. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  38120. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38121. /* Null parameter test */
  38122. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  38123. gtime = NULL;
  38124. out = NULL;
  38125. t->type = ASN_UTC_TIME;
  38126. t->length = ASN_UTC_TIME_SIZE;
  38127. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  38128. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  38129. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  38130. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  38131. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  38132. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38133. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38134. res = TEST_RES_CHECK(1);
  38135. #endif
  38136. return res;
  38137. }
  38138. static int test_wolfSSL_X509_CA_num(void)
  38139. {
  38140. int res = TEST_SKIPPED;
  38141. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38142. defined(HAVE_ECC) && !defined(NO_RSA)
  38143. WOLFSSL_X509_STORE *store;
  38144. WOLFSSL_X509 *x509_1, *x509_2;
  38145. int ca_num = 0;
  38146. store = wolfSSL_X509_STORE_new();
  38147. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  38148. wolfSSL_X509_STORE_add_cert(store, x509_1);
  38149. ca_num = wolfSSL_X509_CA_num(store);
  38150. AssertIntEQ(ca_num, 1);
  38151. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  38152. wolfSSL_X509_STORE_add_cert(store, x509_2);
  38153. ca_num = wolfSSL_X509_CA_num(store);
  38154. AssertIntEQ(ca_num, 2);
  38155. wolfSSL_X509_free(x509_1);
  38156. wolfSSL_X509_free(x509_2);
  38157. wolfSSL_X509_STORE_free(store);
  38158. res = TEST_RES_CHECK(1);
  38159. #endif
  38160. return res;
  38161. }
  38162. static int test_wolfSSL_X509_check_ca(void)
  38163. {
  38164. int res = TEST_SKIPPED;
  38165. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  38166. WOLFSSL_X509 *x509;
  38167. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  38168. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  38169. wolfSSL_X509_free(x509);
  38170. res = TEST_RES_CHECK(1);
  38171. #endif
  38172. return res;
  38173. }
  38174. static int test_wolfSSL_X509_check_ip_asc(void)
  38175. {
  38176. int res = TEST_SKIPPED;
  38177. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  38178. WOLFSSL_X509 *x509;
  38179. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  38180. #if 0
  38181. /* TODO: add cert gen for testing positive case */
  38182. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  38183. #endif
  38184. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  38185. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  38186. wolfSSL_X509_free(x509);
  38187. res = TEST_RES_CHECK(1);
  38188. #endif
  38189. return res;
  38190. }
  38191. static int test_wolfSSL_make_cert(void)
  38192. {
  38193. int res = TEST_SKIPPED;
  38194. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  38195. int ret;
  38196. Cert cert;
  38197. CertName name;
  38198. RsaKey key;
  38199. WC_RNG rng;
  38200. byte der[FOURK_BUF];
  38201. word32 idx;
  38202. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  38203. #ifdef OPENSSL_EXTRA
  38204. const unsigned char* pt;
  38205. int certSz;
  38206. X509* x509;
  38207. X509_NAME* x509name;
  38208. X509_NAME_ENTRY* entry;
  38209. ASN1_STRING* entryValue;
  38210. #endif
  38211. XMEMSET(&name, 0, sizeof(CertName));
  38212. /* set up cert name */
  38213. XMEMCPY(name.country, "US", sizeof("US"));
  38214. name.countryEnc = CTC_PRINTABLE;
  38215. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  38216. name.stateEnc = CTC_UTF8;
  38217. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  38218. name.localityEnc = CTC_UTF8;
  38219. XMEMCPY(name.sur, "Test", sizeof("Test"));
  38220. name.surEnc = CTC_UTF8;
  38221. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  38222. name.orgEnc = CTC_UTF8;
  38223. XMEMCPY(name.unit, "Development", sizeof("Development"));
  38224. name.unitEnc = CTC_UTF8;
  38225. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  38226. name.commonNameEnc = CTC_UTF8;
  38227. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  38228. name.serialDevEnc = CTC_PRINTABLE;
  38229. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  38230. name.userIdEnc = CTC_PRINTABLE;
  38231. #ifdef WOLFSSL_MULTI_ATTRIB
  38232. #if CTC_MAX_ATTRIB > 2
  38233. {
  38234. NameAttrib* n;
  38235. n = &name.name[0];
  38236. n->id = ASN_DOMAIN_COMPONENT;
  38237. n->type = CTC_UTF8;
  38238. n->sz = sizeof("com");
  38239. XMEMCPY(n->value, "com", sizeof("com"));
  38240. n = &name.name[1];
  38241. n->id = ASN_DOMAIN_COMPONENT;
  38242. n->type = CTC_UTF8;
  38243. n->sz = sizeof("wolfssl");
  38244. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  38245. }
  38246. #endif
  38247. #endif /* WOLFSSL_MULTI_ATTRIB */
  38248. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  38249. #ifndef HAVE_FIPS
  38250. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  38251. #else
  38252. AssertIntEQ(wc_InitRng(&rng), 0);
  38253. #endif
  38254. /* load test RSA key */
  38255. idx = 0;
  38256. #if defined(USE_CERT_BUFFERS_1024)
  38257. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  38258. sizeof_server_key_der_1024), 0);
  38259. #elif defined(USE_CERT_BUFFERS_2048)
  38260. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  38261. sizeof_server_key_der_2048), 0);
  38262. #else
  38263. /* error case, no RSA key loaded, happens later */
  38264. (void)idx;
  38265. #endif
  38266. XMEMSET(&cert, 0 , sizeof(Cert));
  38267. AssertIntEQ(wc_InitCert(&cert), 0);
  38268. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  38269. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  38270. cert.serialSz = (int)sizeof(mySerial);
  38271. cert.isCA = 1;
  38272. #ifndef NO_SHA256
  38273. cert.sigType = CTC_SHA256wRSA;
  38274. #else
  38275. cert.sigType = CTC_SHAwRSA;
  38276. #endif
  38277. /* add SKID from the Public Key */
  38278. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  38279. /* add AKID from the Public Key */
  38280. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  38281. ret = 0;
  38282. do {
  38283. #if defined(WOLFSSL_ASYNC_CRYPT)
  38284. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  38285. #endif
  38286. if (ret >= 0) {
  38287. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  38288. }
  38289. } while (ret == WC_PENDING_E);
  38290. AssertIntGT(ret, 0);
  38291. #ifdef OPENSSL_EXTRA
  38292. /* der holds a certificate with DC's now check X509 parsing of it */
  38293. certSz = ret;
  38294. pt = der;
  38295. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  38296. AssertNotNull(x509name = X509_get_subject_name(x509));
  38297. #ifdef WOLFSSL_MULTI_ATTRIB
  38298. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38299. -1)), 5);
  38300. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38301. idx)), 6);
  38302. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38303. idx)), -1);
  38304. #endif /* WOLFSSL_MULTI_ATTRIB */
  38305. /* compare DN at index 0 */
  38306. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  38307. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38308. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  38309. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  38310. #ifdef WOLFSSL_MULTI_ATTRIB
  38311. /* get first and second DC and compare result */
  38312. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38313. -1)), 5);
  38314. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  38315. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38316. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  38317. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38318. idx)), 6);
  38319. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  38320. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38321. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  38322. #endif /* WOLFSSL_MULTI_ATTRIB */
  38323. /* try invalid index locations for regression test and sanity check */
  38324. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  38325. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  38326. X509_free(x509);
  38327. #endif /* OPENSSL_EXTRA */
  38328. wc_FreeRsaKey(&key);
  38329. wc_FreeRng(&rng);
  38330. res = TEST_RES_CHECK(1);
  38331. #endif
  38332. return res;
  38333. }
  38334. static int test_wolfSSL_X509_get_version(void)
  38335. {
  38336. int res = TEST_SKIPPED;
  38337. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38338. WOLFSSL_X509 *x509;
  38339. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  38340. AssertNotNull(x509);
  38341. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  38342. wolfSSL_X509_free(x509);
  38343. res = TEST_RES_CHECK(1);
  38344. #endif
  38345. return res;
  38346. }
  38347. static int test_wolfSSL_DES_ncbc(void)
  38348. {
  38349. int res = TEST_SKIPPED;
  38350. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38351. const_DES_cblock myDes;
  38352. DES_cblock iv = {1};
  38353. DES_key_schedule key = {0};
  38354. unsigned char msg[] = "hello wolfssl";
  38355. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38356. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38357. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38358. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38359. /* partial block test */
  38360. DES_set_key(&key, &myDes);
  38361. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38362. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38363. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38364. DES_set_key(&key, &myDes);
  38365. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38366. *((byte*)&iv) = 1;
  38367. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38368. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  38369. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38370. /* full block test */
  38371. DES_set_key(&key, &myDes);
  38372. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38373. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38374. *((byte*)&iv) = 1;
  38375. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38376. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38377. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38378. DES_set_key(&key, &myDes);
  38379. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38380. *((byte*)&iv) = 1;
  38381. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38382. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  38383. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38384. res = TEST_RES_CHECK(1);
  38385. #endif
  38386. return res;
  38387. }
  38388. static int test_wolfSSL_AES_cbc_encrypt(void)
  38389. {
  38390. int res = TEST_SKIPPED;
  38391. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  38392. AES_KEY aes;
  38393. AES_KEY* aesN = NULL;
  38394. size_t len = 0;
  38395. size_t lenB = 0;
  38396. int keySz0 = 0;
  38397. int keySzN = -1;
  38398. byte out[AES_BLOCK_SIZE] = {0};
  38399. byte* outN = NULL;
  38400. /* Test vectors retrieved from:
  38401. * <begin URL>
  38402. * https://csrc.nist.gov/
  38403. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38404. * documents/aes/KAT_AES.zip
  38405. * </end URL>
  38406. */
  38407. const byte* pt128N = NULL;
  38408. byte* key128N = NULL;
  38409. byte* iv128N = NULL;
  38410. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38411. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38412. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38413. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38414. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38415. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38416. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38417. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38418. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38419. len = sizeof(pt128);
  38420. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38421. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38422. AssertIntNE(XMEMCMP(b, g, h), i)
  38423. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38424. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38425. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38426. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38427. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38428. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38429. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38430. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38431. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38432. /* Stressing wolfSSL_AES_set_encrypt_key */
  38433. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38434. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38435. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38436. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38437. /* Stressing wolfSSL_AES_set_decrypt_key */
  38438. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38439. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38440. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38441. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38442. #ifdef WOLFSSL_AES_128
  38443. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38444. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38445. RESET_IV(iv128tmp, iv128);
  38446. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38447. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38448. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38449. wc_AesFree((Aes*)&aes);
  38450. #ifdef HAVE_AES_DECRYPT
  38451. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38452. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38453. RESET_IV(iv128tmp, iv128);
  38454. len = sizeof(ct128);
  38455. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38456. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38457. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38458. wc_AesFree((Aes*)&aes);
  38459. #endif
  38460. #endif /* WOLFSSL_AES_128 */
  38461. #ifdef WOLFSSL_AES_192
  38462. {
  38463. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38464. * Appendix F.2.3 */
  38465. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38466. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38467. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38468. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38469. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38470. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38471. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38472. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38473. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38474. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38475. len = sizeof(pt192);
  38476. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38477. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38478. RESET_IV(iv192tmp, iv192);
  38479. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38480. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38481. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38482. wc_AesFree((Aes*)&aes);
  38483. #ifdef HAVE_AES_DECRYPT
  38484. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38485. len = sizeof(ct192);
  38486. RESET_IV(iv192tmp, iv192);
  38487. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38488. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38489. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38490. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38491. wc_AesFree((Aes*)&aes);
  38492. #endif
  38493. }
  38494. #endif /* WOLFSSL_AES_192 */
  38495. #ifdef WOLFSSL_AES_256
  38496. {
  38497. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38498. * Appendix F.2.5 */
  38499. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38500. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38501. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38502. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38503. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38504. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38505. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38506. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38507. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38508. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38509. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38510. len = sizeof(pt256);
  38511. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38512. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38513. RESET_IV(iv256tmp, iv256);
  38514. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38515. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38516. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38517. wc_AesFree((Aes*)&aes);
  38518. #ifdef HAVE_AES_DECRYPT
  38519. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38520. len = sizeof(ct256);
  38521. RESET_IV(iv256tmp, iv256);
  38522. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38523. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38524. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  38525. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  38526. wc_AesFree((Aes*)&aes);
  38527. #endif
  38528. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38529. {
  38530. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  38531. byte wrapPlain[sizeof(key256)] = { 0 };
  38532. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  38533. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  38534. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38535. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38536. 15), WOLFSSL_FAILURE);
  38537. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38538. sizeof(key256)), sizeof(wrapCipher));
  38539. wc_AesFree((Aes*)&aes);
  38540. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  38541. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38542. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38543. 23), WOLFSSL_FAILURE);
  38544. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38545. sizeof(wrapCipher)), sizeof(wrapPlain));
  38546. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38547. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  38548. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  38549. wc_AesFree((Aes*)&aes);
  38550. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  38551. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38552. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  38553. sizeof(key256)), sizeof(wrapCipher));
  38554. wc_AesFree((Aes*)&aes);
  38555. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  38556. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38557. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  38558. sizeof(wrapCipher)), sizeof(wrapPlain));
  38559. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38560. wc_AesFree((Aes*)&aes);
  38561. }
  38562. #endif /* HAVE_AES_KEYWRAP */
  38563. }
  38564. #endif /* WOLFSSL_AES_256 */
  38565. res = TEST_RES_CHECK(1);
  38566. #endif
  38567. return res;
  38568. }
  38569. static int test_wolfSSL_CRYPTO_cts128(void)
  38570. {
  38571. int res = TEST_SKIPPED;
  38572. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  38573. && defined(HAVE_CTS)
  38574. byte tmp[64]; /* Largest vector size */
  38575. /* Test vectors taken form RFC3962 Appendix B */
  38576. const testVector vects[] = {
  38577. {
  38578. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38579. "\x20",
  38580. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  38581. "\x97",
  38582. 17, 17
  38583. },
  38584. {
  38585. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38586. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  38587. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  38588. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  38589. 31, 31
  38590. },
  38591. {
  38592. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38593. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38594. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38595. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38596. 32, 32
  38597. },
  38598. {
  38599. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38600. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38601. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38602. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38603. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38604. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38605. 47, 47
  38606. },
  38607. {
  38608. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38609. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38610. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38611. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38612. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38613. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38614. 48, 48
  38615. },
  38616. {
  38617. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38618. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38619. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38620. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38621. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38622. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38623. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38624. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38625. 64, 64
  38626. }
  38627. };
  38628. byte keyBytes[AES_128_KEY_SIZE] = {
  38629. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38630. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38631. };
  38632. size_t i;
  38633. XMEMSET(tmp, 0, sizeof(tmp));
  38634. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38635. AES_KEY encKey;
  38636. AES_KEY decKey;
  38637. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38638. XMEMSET(iv, 0, sizeof(iv));
  38639. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  38640. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  38641. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38642. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38643. vects[i].outLen);
  38644. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38645. XMEMSET(iv, 0, sizeof(iv));
  38646. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38647. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38648. vects[i].inLen);
  38649. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38650. }
  38651. res = TEST_RES_CHECK(1);
  38652. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38653. return res;
  38654. }
  38655. #if defined(OPENSSL_ALL)
  38656. #if !defined(NO_ASN)
  38657. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  38658. {
  38659. int res = TEST_SKIPPED;
  38660. #if !defined(NO_RSA)
  38661. WOLFSSL_X509* x509;
  38662. WOLFSSL_X509_NAME* subject;
  38663. WOLFSSL_X509_NAME_ENTRY* e;
  38664. WOLFSSL_ASN1_STRING* a;
  38665. FILE* file;
  38666. int idx = 0;
  38667. char targetOutput[16] = "www.wolfssl.com";
  38668. unsigned char* actual_output;
  38669. int len = 0;
  38670. int result = 0;
  38671. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  38672. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  38673. fclose(file);
  38674. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  38675. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  38676. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  38677. NID_commonName, -1)), 5);
  38678. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  38679. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  38680. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  38681. result = strncmp((const char*)actual_output, targetOutput, len);
  38682. AssertIntEQ(result, 0);
  38683. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  38684. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  38685. WOLFSSL_FATAL_ERROR);
  38686. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  38687. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  38688. WOLFSSL_FATAL_ERROR);
  38689. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  38690. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  38691. WOLFSSL_FATAL_ERROR);
  38692. wolfSSL_X509_free(x509);
  38693. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38694. res = TEST_RES_CHECK(1);
  38695. #endif
  38696. return res;
  38697. }
  38698. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  38699. {
  38700. ASN1_STRING* asn1str_test;
  38701. ASN1_STRING* asn1str_answer;
  38702. /* Each character is encoded using 4 bytes */
  38703. char input[] = {
  38704. 0, 0, 0, 'T',
  38705. 0, 0, 0, 'e',
  38706. 0, 0, 0, 's',
  38707. 0, 0, 0, 't',
  38708. };
  38709. char output[] = "Test";
  38710. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  38711. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  38712. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  38713. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  38714. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  38715. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  38716. ASN1_STRING_free(asn1str_test);
  38717. ASN1_STRING_free(asn1str_answer);
  38718. return TEST_RES_CHECK(1);
  38719. }
  38720. #endif /* !defined(NO_ASN) */
  38721. static int test_wolfSSL_sk_CIPHER_description(void)
  38722. {
  38723. int res = TEST_SKIPPED;
  38724. #if !defined(NO_RSA)
  38725. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38726. int i,j,k;
  38727. int numCiphers = 0;
  38728. const SSL_METHOD *method = NULL;
  38729. const SSL_CIPHER *cipher = NULL;
  38730. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38731. SSL_CTX *ctx = NULL;
  38732. SSL *ssl = NULL;
  38733. char buf[256];
  38734. char test_str[9] = "0000000";
  38735. const char badStr[] = "unknown";
  38736. const char certPath[] = "./certs/client-cert.pem";
  38737. XMEMSET(buf, 0, sizeof(buf));
  38738. AssertNotNull(method = TLSv1_2_client_method());
  38739. AssertNotNull(ctx = SSL_CTX_new(method));
  38740. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38741. SSL_CTX_set_verify_depth(ctx, 4);
  38742. SSL_CTX_set_options(ctx, flags);
  38743. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38744. WOLFSSL_SUCCESS);
  38745. AssertNotNull(ssl = SSL_new(ctx));
  38746. /* SSL_get_ciphers returns a stack of all configured ciphers
  38747. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  38748. */
  38749. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38750. /* loop through the amount of supportedCiphers */
  38751. numCiphers = sk_num(supportedCiphers);
  38752. for (i = 0; i < numCiphers; ++i) {
  38753. /* sk_value increments "sk->data.cipher->cipherOffset".
  38754. * wolfSSL_sk_CIPHER_description sets the description for
  38755. * the cipher based on the provided offset.
  38756. */
  38757. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  38758. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  38759. }
  38760. /* Search cipher description string for "unknown" descriptor */
  38761. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  38762. k = 0;
  38763. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  38764. test_str[k] = badStr[k];
  38765. j++;
  38766. k++;
  38767. }
  38768. }
  38769. /* Fail if test_str == badStr == "unknown" */
  38770. AssertStrNE(test_str,badStr);
  38771. }
  38772. SSL_free(ssl);
  38773. SSL_CTX_free(ctx);
  38774. res = TEST_RES_CHECK(1);
  38775. #endif
  38776. return res;
  38777. }
  38778. static int test_wolfSSL_get_ciphers_compat(void)
  38779. {
  38780. int res = TEST_SKIPPED;
  38781. #if !defined(NO_RSA)
  38782. const SSL_METHOD *method = NULL;
  38783. const char certPath[] = "./certs/client-cert.pem";
  38784. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38785. SSL_CTX *ctx = NULL;
  38786. WOLFSSL *ssl = NULL;
  38787. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38788. method = SSLv23_client_method();
  38789. AssertNotNull(method);
  38790. ctx = SSL_CTX_new(method);
  38791. AssertNotNull(ctx);
  38792. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38793. SSL_CTX_set_verify_depth(ctx, 4);
  38794. SSL_CTX_set_options(ctx, flags);
  38795. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38796. WOLFSSL_SUCCESS);
  38797. AssertNotNull(ssl = SSL_new(ctx));
  38798. /* Test Bad NULL input */
  38799. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  38800. /* Test for Good input */
  38801. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38802. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  38803. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  38804. SSL_free(ssl);
  38805. SSL_CTX_free(ctx);
  38806. res = TEST_RES_CHECK(1);
  38807. #endif
  38808. return res;
  38809. }
  38810. static int test_wolfSSL_X509_PUBKEY_get(void)
  38811. {
  38812. WOLFSSL_X509_PUBKEY pubkey;
  38813. WOLFSSL_X509_PUBKEY* key;
  38814. WOLFSSL_EVP_PKEY evpkey ;
  38815. WOLFSSL_EVP_PKEY* evpPkey;
  38816. WOLFSSL_EVP_PKEY* retEvpPkey;
  38817. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  38818. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  38819. key = &pubkey;
  38820. evpPkey = &evpkey;
  38821. evpPkey->type = WOLFSSL_SUCCESS;
  38822. key->pkey = evpPkey;
  38823. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38824. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  38825. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  38826. key->pkey = NULL;
  38827. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38828. return TEST_RES_CHECK(retEvpPkey == NULL);
  38829. }
  38830. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  38831. {
  38832. int res = TEST_SKIPPED;
  38833. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  38834. DSA *dsa = NULL;
  38835. DSA *setDsa = NULL;
  38836. EVP_PKEY *pkey = NULL;
  38837. EVP_PKEY *set1Pkey = NULL;
  38838. SHA_CTX sha;
  38839. byte signature[DSA_SIG_SIZE];
  38840. byte hash[WC_SHA_DIGEST_SIZE];
  38841. word32 bytes;
  38842. int answer;
  38843. #ifdef USE_CERT_BUFFERS_1024
  38844. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  38845. int dsaKeySz = sizeof_dsa_key_der_1024;
  38846. byte tmp[ONEK_BUF];
  38847. XMEMSET(tmp, 0, sizeof(tmp));
  38848. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38849. bytes = dsaKeySz;
  38850. #elif defined(USE_CERT_BUFFERS_2048)
  38851. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  38852. int dsaKeySz = sizeof_dsa_key_der_2048;
  38853. byte tmp[TWOK_BUF];
  38854. XMEMSET(tmp, 0, sizeof(tmp));
  38855. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38856. bytes = dsaKeySz;
  38857. #else
  38858. byte tmp[TWOK_BUF];
  38859. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  38860. int dsaKeySz;
  38861. XMEMSET(tmp, 0, sizeof(tmp));
  38862. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  38863. if (fp == XBADFILE) {
  38864. return WOLFSSL_BAD_FILE;
  38865. }
  38866. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  38867. XFCLOSE(fp);
  38868. #endif /* END USE_CERT_BUFFERS_1024 */
  38869. /* Create hash to later Sign and Verify */
  38870. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  38871. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  38872. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  38873. /* Initialize pkey with der format dsa key */
  38874. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  38875. (long)dsaKeySz));
  38876. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  38877. /* Should Fail: NULL argument */
  38878. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  38879. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  38880. /* Should Pass: Initialized pkey argument */
  38881. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  38882. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  38883. #ifdef USE_CERT_BUFFERS_1024
  38884. AssertIntEQ(DSA_bits(dsa), 1024);
  38885. #else
  38886. AssertIntEQ(DSA_bits(dsa), 2048);
  38887. #endif
  38888. /* Sign */
  38889. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  38890. /* Verify. */
  38891. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  38892. WOLFSSL_SUCCESS);
  38893. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  38894. /* Should Fail: set1Pkey not initialized */
  38895. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38896. /* Initialize set1Pkey */
  38897. set1Pkey = EVP_PKEY_new();
  38898. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  38899. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  38900. WOLFSSL_SUCCESS);
  38901. /* Should Pass: set dsa into set1Pkey */
  38902. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38903. DSA_free(dsa);
  38904. DSA_free(setDsa);
  38905. EVP_PKEY_free(pkey);
  38906. EVP_PKEY_free(set1Pkey);
  38907. res = TEST_RES_CHECK(1);
  38908. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  38909. return res;
  38910. } /* END test_EVP_PKEY_set1_get1_DSA */
  38911. static int test_wolfSSL_DSA_SIG(void)
  38912. {
  38913. int res = TEST_SKIPPED;
  38914. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  38915. !defined(HAVE_FIPS)
  38916. DSA *dsa = NULL;
  38917. DSA *dsa2 = NULL;
  38918. DSA_SIG *sig = NULL;
  38919. const BIGNUM *p = NULL;
  38920. const BIGNUM *q = NULL;
  38921. const BIGNUM *g = NULL;
  38922. const BIGNUM *pub = NULL;
  38923. const BIGNUM *priv = NULL;
  38924. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  38925. AssertNotNull(dsa = DSA_generate_parameters(2048,
  38926. NULL, 0, NULL, NULL, NULL, NULL));
  38927. DSA_free(dsa);
  38928. AssertNotNull(dsa = DSA_new());
  38929. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  38930. NULL, 0, NULL, NULL, NULL), 1);
  38931. AssertIntEQ(DSA_generate_key(dsa), 1);
  38932. DSA_get0_pqg(dsa, &p, &q, &g);
  38933. DSA_get0_key(dsa, &pub, &priv);
  38934. AssertNotNull(p = BN_dup(p));
  38935. AssertNotNull(q = BN_dup(q));
  38936. AssertNotNull(g = BN_dup(g));
  38937. AssertNotNull(pub = BN_dup(pub));
  38938. AssertNotNull(priv = BN_dup(priv));
  38939. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  38940. AssertNotNull(dsa2 = DSA_new());
  38941. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  38942. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  38943. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  38944. DSA_free(dsa);
  38945. DSA_free(dsa2);
  38946. DSA_SIG_free(sig);
  38947. res = TEST_RES_CHECK(1);
  38948. #endif
  38949. return res;
  38950. }
  38951. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  38952. {
  38953. int res = TEST_SKIPPED;
  38954. #ifdef HAVE_ECC
  38955. WOLFSSL_EC_KEY *ecKey = NULL;
  38956. WOLFSSL_EC_KEY *ecGet1 = NULL;
  38957. EVP_PKEY *pkey = NULL;
  38958. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38959. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38960. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  38961. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  38962. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  38963. /* Should fail since ecKey is empty */
  38964. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  38965. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38966. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  38967. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  38968. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  38969. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  38970. wolfSSL_EC_KEY_free(ecKey);
  38971. wolfSSL_EC_KEY_free(ecGet1);
  38972. EVP_PKEY_free(pkey);
  38973. res = TEST_RES_CHECK(1);
  38974. #endif /* HAVE_ECC */
  38975. return res;
  38976. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  38977. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  38978. {
  38979. int res = TEST_SKIPPED;
  38980. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  38981. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38982. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38983. DH *dh = NULL;
  38984. DH *setDh = NULL;
  38985. EVP_PKEY *pkey = NULL;
  38986. FILE* f = NULL;
  38987. unsigned char buf[4096];
  38988. const unsigned char* pt = buf;
  38989. const char* dh2048 = "./certs/dh2048.der";
  38990. long len = 0;
  38991. int code = -1;
  38992. XMEMSET(buf, 0, sizeof(buf));
  38993. f = XFOPEN(dh2048, "rb");
  38994. AssertTrue(f != XBADFILE);
  38995. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38996. XFCLOSE(f);
  38997. /* Load dh2048.der into DH with internal format */
  38998. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38999. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  39000. AssertIntEQ(code, 0);
  39001. code = -1;
  39002. pkey = wolfSSL_EVP_PKEY_new();
  39003. /* Set DH into PKEY */
  39004. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  39005. /* Get DH from PKEY */
  39006. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  39007. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  39008. AssertIntEQ(code, 0);
  39009. EVP_PKEY_free(pkey);
  39010. DH_free(setDh);
  39011. DH_free(dh);
  39012. res = TEST_RES_CHECK(1);
  39013. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  39014. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  39015. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  39016. return res;
  39017. } /* END test_EVP_PKEY_set1_get1_DH */
  39018. static int test_wolfSSL_CTX_ctrl(void)
  39019. {
  39020. int res = TEST_SKIPPED;
  39021. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39022. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  39023. char caFile[] = "./certs/client-ca.pem";
  39024. char clientFile[] = "./certs/client-cert.pem";
  39025. SSL_CTX* ctx;
  39026. X509* x509 = NULL;
  39027. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  39028. byte buf[6000];
  39029. char file[] = "./certs/dsaparams.pem";
  39030. XFILE f;
  39031. int bytes;
  39032. BIO* bio;
  39033. DSA* dsa;
  39034. DH* dh;
  39035. #endif
  39036. #ifdef HAVE_ECC
  39037. WOLFSSL_EC_KEY* ecKey;
  39038. #endif
  39039. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  39040. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  39041. AssertNotNull(x509);
  39042. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  39043. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  39044. AssertNotNull(x509);
  39045. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  39046. /* Initialize DH */
  39047. f = XFOPEN(file, "rb");
  39048. AssertTrue((f != XBADFILE));
  39049. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  39050. XFCLOSE(f);
  39051. bio = BIO_new_mem_buf((void*)buf, bytes);
  39052. AssertNotNull(bio);
  39053. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  39054. AssertNotNull(dsa);
  39055. dh = wolfSSL_DSA_dup_DH(dsa);
  39056. AssertNotNull(dh);
  39057. #endif
  39058. #ifdef HAVE_ECC
  39059. /* Initialize WOLFSSL_EC_KEY */
  39060. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  39061. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  39062. #endif
  39063. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  39064. /* additional test of getting EVP_PKEY key size from X509
  39065. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  39066. * allowed with user RSA */
  39067. {
  39068. EVP_PKEY* pkey;
  39069. #if defined(HAVE_ECC)
  39070. X509* ecX509;
  39071. #endif /* HAVE_ECC */
  39072. AssertNotNull(pkey = X509_get_pubkey(x509));
  39073. /* current RSA key is 2048 bit (256 bytes) */
  39074. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  39075. EVP_PKEY_free(pkey);
  39076. #if defined(HAVE_ECC)
  39077. #if defined(USE_CERT_BUFFERS_256)
  39078. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  39079. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  39080. SSL_FILETYPE_ASN1));
  39081. #else
  39082. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  39083. cliEccCertFile, SSL_FILETYPE_PEM));
  39084. #endif
  39085. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  39086. /* current ECC key is 256 bit (32 bytes) */
  39087. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  39088. X509_free(ecX509);
  39089. EVP_PKEY_free(pkey);
  39090. #endif /* HAVE_ECC */
  39091. }
  39092. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  39093. /* Tests should fail with passed in NULL pointer */
  39094. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  39095. SSL_FAILURE);
  39096. #if !defined(NO_DH) && !defined(NO_DSA)
  39097. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  39098. SSL_FAILURE);
  39099. #endif
  39100. #ifdef HAVE_ECC
  39101. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  39102. SSL_FAILURE);
  39103. #endif
  39104. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  39105. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  39106. */
  39107. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  39108. SSL_SUCCESS);
  39109. /* Test with SSL_CTRL_OPTIONS
  39110. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  39111. */
  39112. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  39113. == SSL_OP_NO_TLSv1);
  39114. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  39115. /* Test with SSL_CTRL_SET_TMP_DH
  39116. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  39117. */
  39118. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  39119. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  39120. SSL_SUCCESS);
  39121. #endif
  39122. /* Test with SSL_CTRL_SET_TMP_ECDH
  39123. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  39124. */
  39125. #ifdef HAVE_ECC
  39126. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  39127. SSL_SUCCESS);
  39128. #endif
  39129. #ifdef WOLFSSL_ENCRYPTED_KEYS
  39130. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  39131. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  39132. #endif
  39133. /* Test for min/max proto */
  39134. #ifndef WOLFSSL_NO_TLS12
  39135. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  39136. 0, NULL), SSL_SUCCESS);
  39137. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  39138. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  39139. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  39140. #endif
  39141. #ifdef WOLFSSL_TLS13
  39142. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  39143. 0, NULL), SSL_SUCCESS);
  39144. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  39145. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  39146. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  39147. #ifndef WOLFSSL_NO_TLS12
  39148. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  39149. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  39150. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  39151. #endif
  39152. #endif
  39153. /* Cleanup and Pass */
  39154. #if !defined(NO_DH) && !defined(NO_DSA)
  39155. #ifndef NO_BIO
  39156. BIO_free(bio);
  39157. DSA_free(dsa);
  39158. DH_free(dh);
  39159. #endif
  39160. #endif
  39161. #ifdef HAVE_ECC
  39162. wolfSSL_EC_KEY_free(ecKey);
  39163. #endif
  39164. SSL_CTX_free(ctx);
  39165. res = TEST_RES_CHECK(1);
  39166. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39167. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  39168. return res;
  39169. }
  39170. static int test_wolfSSL_EVP_PKEY_assign(void)
  39171. {
  39172. int res = TEST_SKIPPED;
  39173. int type;
  39174. WOLFSSL_EVP_PKEY* pkey;
  39175. #ifndef NO_RSA
  39176. WOLFSSL_RSA* rsa;
  39177. #endif
  39178. #ifndef NO_DSA
  39179. WOLFSSL_DSA* dsa;
  39180. #endif
  39181. #ifdef HAVE_ECC
  39182. WOLFSSL_EC_KEY* ecKey;
  39183. #endif
  39184. (void)pkey;
  39185. #ifndef NO_RSA
  39186. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39187. type = EVP_PKEY_RSA;
  39188. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39189. AssertNotNull(rsa = wolfSSL_RSA_new());
  39190. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  39191. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  39192. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  39193. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  39194. wolfSSL_EVP_PKEY_free(pkey);
  39195. res = TEST_RES_CHECK(1);
  39196. }
  39197. #endif /* NO_RSA */
  39198. #ifndef NO_DSA
  39199. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39200. type = EVP_PKEY_DSA;
  39201. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39202. AssertNotNull(dsa = wolfSSL_DSA_new());
  39203. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  39204. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  39205. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  39206. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  39207. wolfSSL_EVP_PKEY_free(pkey);
  39208. res = TEST_RES_CHECK(1);
  39209. }
  39210. #endif /* NO_DSA */
  39211. #ifdef HAVE_ECC
  39212. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39213. type = EVP_PKEY_EC;
  39214. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39215. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  39216. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  39217. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  39218. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  39219. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  39220. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  39221. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  39222. wolfSSL_EVP_PKEY_free(pkey);
  39223. res = TEST_RES_CHECK(1);
  39224. }
  39225. #endif /* HAVE_ECC */
  39226. (void)type;
  39227. return res;
  39228. }
  39229. static int test_wolfSSL_EVP_PKEY_base_id(void)
  39230. {
  39231. WOLFSSL_EVP_PKEY* pkey;
  39232. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39233. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  39234. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  39235. EVP_PKEY_free(pkey);
  39236. return TEST_RES_CHECK(1);
  39237. }
  39238. static int test_wolfSSL_EVP_PKEY_id(void)
  39239. {
  39240. WOLFSSL_EVP_PKEY* pkey;
  39241. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39242. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  39243. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  39244. EVP_PKEY_free(pkey);
  39245. return TEST_RES_CHECK(1);
  39246. }
  39247. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  39248. {
  39249. int res = TEST_SKIPPED;
  39250. #if defined(OPENSSL_ALL) && \
  39251. !defined(NO_ECC_SECP) && \
  39252. /* This last bit is taken from ecc.c. It is the condition that
  39253. * defines ECC256 */ \
  39254. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  39255. ECC_MIN_KEY_SZ <= 256)
  39256. EVP_PKEY_CTX* ctx;
  39257. EVP_PKEY* pkey = NULL;
  39258. /* Test error conditions. */
  39259. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  39260. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  39261. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  39262. #ifndef NO_RSA
  39263. EVP_PKEY_CTX_free(ctx);
  39264. /* Parameter generation for RSA not supported yet. */
  39265. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  39266. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  39267. #endif
  39268. #ifdef HAVE_ECC
  39269. EVP_PKEY_CTX_free(ctx);
  39270. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  39271. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  39272. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  39273. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  39274. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  39275. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  39276. WOLFSSL_SUCCESS);
  39277. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39278. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39279. #endif
  39280. EVP_PKEY_CTX_free(ctx);
  39281. EVP_PKEY_free(pkey);
  39282. res = TEST_RES_CHECK(1);
  39283. #endif
  39284. return res;
  39285. }
  39286. static int test_wolfSSL_EVP_PKEY_keygen(void)
  39287. {
  39288. WOLFSSL_EVP_PKEY* pkey = NULL;
  39289. EVP_PKEY_CTX* ctx = NULL;
  39290. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39291. WOLFSSL_EVP_PKEY* params = NULL;
  39292. DH* dh = NULL;
  39293. const BIGNUM* pubkey = NULL;
  39294. const BIGNUM* privkey = NULL;
  39295. ASN1_INTEGER* asn1int = NULL;
  39296. unsigned int length = 0;
  39297. byte* derBuffer = NULL;
  39298. #endif
  39299. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39300. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39301. /* Bad cases */
  39302. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  39303. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  39304. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  39305. /* Good case */
  39306. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  39307. EVP_PKEY_CTX_free(ctx);
  39308. EVP_PKEY_free(pkey);
  39309. pkey = NULL;
  39310. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39311. /* Test DH keygen */
  39312. {
  39313. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  39314. AssertNotNull(dh = DH_get_2048_256());
  39315. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39316. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  39317. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39318. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39319. DH_free(dh);
  39320. EVP_PKEY_CTX_free(ctx);
  39321. EVP_PKEY_free(params);
  39322. /* try exporting generated key to DER, to verify */
  39323. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  39324. DH_get0_key(dh, &pubkey, &privkey);
  39325. AssertNotNull(pubkey);
  39326. AssertNotNull(privkey);
  39327. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  39328. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  39329. ASN1_INTEGER_free(asn1int);
  39330. DH_free(dh);
  39331. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39332. EVP_PKEY_free(pkey);
  39333. }
  39334. #endif
  39335. return TEST_RES_CHECK(1);
  39336. }
  39337. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  39338. {
  39339. WOLFSSL_EVP_PKEY* pkey;
  39340. EVP_PKEY_CTX *ctx;
  39341. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39342. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39343. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39344. EVP_PKEY_CTX_free(ctx);
  39345. EVP_PKEY_free(pkey);
  39346. return TEST_RES_CHECK(1);
  39347. }
  39348. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  39349. {
  39350. int res = TEST_SKIPPED;
  39351. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  39352. WOLFSSL_EVP_PKEY* pkey;
  39353. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39354. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  39355. EVP_PKEY_free(pkey);
  39356. res = TEST_RES_CHECK(1);
  39357. #endif
  39358. return res;
  39359. }
  39360. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  39361. {
  39362. int res = TEST_SKIPPED;
  39363. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  39364. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  39365. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  39366. !defined(NO_FILESYSTEM)
  39367. WOLFSSL_EVP_PKEY* params = NULL;
  39368. WOLFSSL_EVP_PKEY* copy = NULL;
  39369. DH* dh = NULL;
  39370. BIGNUM* p1;
  39371. BIGNUM* g1;
  39372. BIGNUM* q1;
  39373. BIGNUM* p2;
  39374. BIGNUM* g2;
  39375. BIGNUM* q2;
  39376. /* create DH with DH_get_2048_256 params */
  39377. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  39378. AssertNotNull(dh = DH_get_2048_256());
  39379. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39380. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  39381. (const BIGNUM**)&q1,
  39382. (const BIGNUM**)&g1);
  39383. DH_free(dh);
  39384. /* create DH with random generated DH params */
  39385. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  39386. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  39387. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  39388. DH_free(dh);
  39389. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  39390. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  39391. AssertNotNull(dh->p);
  39392. AssertNotNull(dh->g);
  39393. AssertNotNull(dh->q);
  39394. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  39395. (const BIGNUM**)&q2,
  39396. (const BIGNUM**)&g2);
  39397. AssertIntEQ(BN_cmp(p1, p2), 0);
  39398. AssertIntEQ(BN_cmp(q1, q2), 0);
  39399. AssertIntEQ(BN_cmp(g1, g2), 0);
  39400. DH_free(dh);
  39401. EVP_PKEY_free(copy);
  39402. EVP_PKEY_free(params);
  39403. res = TEST_RES_CHECK(1);
  39404. #endif
  39405. return res;
  39406. }
  39407. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  39408. {
  39409. WOLFSSL_EVP_PKEY* pkey;
  39410. EVP_PKEY_CTX *ctx;
  39411. int bits = 2048;
  39412. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39413. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39414. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  39415. WOLFSSL_SUCCESS);
  39416. EVP_PKEY_CTX_free(ctx);
  39417. EVP_PKEY_free(pkey);
  39418. return TEST_RES_CHECK(1);
  39419. }
  39420. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  39421. {
  39422. /* This is large enough to be used for all key sizes */
  39423. byte key[AES_256_KEY_SIZE] = {0};
  39424. byte iv[AES_BLOCK_SIZE] = {0};
  39425. int i, enumlen;
  39426. EVP_CIPHER_CTX *ctx;
  39427. const EVP_CIPHER *init;
  39428. int enumArray[] = {
  39429. #ifdef HAVE_AES_CBC
  39430. NID_aes_128_cbc,
  39431. #endif
  39432. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39433. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39434. #ifdef HAVE_AESGCM
  39435. NID_aes_128_gcm,
  39436. #endif
  39437. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39438. #ifdef WOLFSSL_AES_COUNTER
  39439. NID_aes_128_ctr,
  39440. #endif
  39441. #ifndef NO_DES3
  39442. NID_des_cbc,
  39443. NID_des_ede3_cbc,
  39444. #endif
  39445. };
  39446. int iv_lengths[] = {
  39447. #ifdef HAVE_AES_CBC
  39448. AES_BLOCK_SIZE,
  39449. #endif
  39450. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39451. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39452. #ifdef HAVE_AESGCM
  39453. GCM_NONCE_MID_SZ,
  39454. #endif
  39455. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39456. #ifdef WOLFSSL_AES_COUNTER
  39457. AES_BLOCK_SIZE,
  39458. #endif
  39459. #ifndef NO_DES3
  39460. DES_BLOCK_SIZE,
  39461. DES_BLOCK_SIZE,
  39462. #endif
  39463. };
  39464. enumlen = (sizeof(enumArray)/sizeof(int));
  39465. for (i = 0; i < enumlen; i++) {
  39466. ctx = EVP_CIPHER_CTX_new();
  39467. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  39468. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39469. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39470. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  39471. EVP_CIPHER_CTX_free(ctx);
  39472. }
  39473. return TEST_RES_CHECK(1);
  39474. }
  39475. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  39476. {
  39477. int res = TEST_SKIPPED;
  39478. #if !defined(NO_DES3)
  39479. byte key[AES_256_KEY_SIZE] = {0};
  39480. byte iv[AES_BLOCK_SIZE] = {0};
  39481. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39482. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39483. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39484. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39485. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  39486. EVP_CIPHER_CTX_free(ctx);
  39487. res = TEST_RES_CHECK(1);
  39488. #endif
  39489. return res;
  39490. }
  39491. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  39492. {
  39493. int res = TEST_SKIPPED;
  39494. #if !defined(NO_DES3)
  39495. byte key[AES_256_KEY_SIZE] = {0};
  39496. byte iv[AES_BLOCK_SIZE] = {0};
  39497. int keylen;
  39498. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39499. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39500. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39501. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39502. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39503. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  39504. WOLFSSL_SUCCESS);
  39505. EVP_CIPHER_CTX_free(ctx);
  39506. res = TEST_RES_CHECK(1);
  39507. #endif
  39508. return res;
  39509. }
  39510. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  39511. {
  39512. int res = TEST_SKIPPED;
  39513. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  39514. byte key[DES3_KEY_SIZE] = {0};
  39515. byte iv[DES_BLOCK_SIZE] = {0};
  39516. int ivLen, keyLen;
  39517. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39518. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39519. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39520. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39521. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  39522. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39523. /* Bad cases */
  39524. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  39525. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  39526. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  39527. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  39528. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  39529. /* Good case */
  39530. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  39531. EVP_CIPHER_CTX_free(ctx);
  39532. res = TEST_RES_CHECK(1);
  39533. #endif
  39534. return res;
  39535. }
  39536. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  39537. {
  39538. WOLFSSL_ENGINE* e = NULL;
  39539. int id = 0;
  39540. EVP_PKEY_CTX *ctx;
  39541. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  39542. EVP_PKEY_CTX_free(ctx);
  39543. return TEST_RES_CHECK(1);
  39544. }
  39545. static int test_wolfSSL_EVP_rc4(void)
  39546. {
  39547. int res = TEST_SKIPPED;
  39548. #if !defined(NO_RC4)
  39549. res = TEST_RES_CHECK(wolfSSL_EVP_rc4() != NULL);
  39550. #endif
  39551. return res;
  39552. }
  39553. static int test_wolfSSL_EVP_enc_null(void)
  39554. {
  39555. return TEST_RES_CHECK(wolfSSL_EVP_enc_null() != NULL);
  39556. }
  39557. static int test_wolfSSL_EVP_rc2_cbc(void)
  39558. {
  39559. int res = TEST_SKIPPED;
  39560. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  39561. res = TEST_RES_CHECK(wolfSSL_EVP_rc2_cbc() == NULL);
  39562. #endif
  39563. return res;
  39564. }
  39565. static int test_wolfSSL_EVP_mdc2(void)
  39566. {
  39567. int res = TEST_SKIPPED;
  39568. #if !defined(NO_WOLFSSL_STUB)
  39569. res = TEST_RES_CHECK(wolfSSL_EVP_mdc2() == NULL);
  39570. #endif
  39571. return res;
  39572. }
  39573. static int test_wolfSSL_EVP_md4(void)
  39574. {
  39575. int res = TEST_SKIPPED;
  39576. #if !defined(NO_MD4)
  39577. res = TEST_RES_CHECK(wolfSSL_EVP_md4() != NULL);
  39578. #endif
  39579. return res;
  39580. }
  39581. static int test_wolfSSL_EVP_aes_256_gcm(void)
  39582. {
  39583. int res = TEST_SKIPPED;
  39584. #ifdef HAVE_AESGCM
  39585. res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_gcm() != NULL);
  39586. #endif
  39587. return res;
  39588. }
  39589. static int test_wolfSSL_EVP_aes_192_gcm(void)
  39590. {
  39591. int res = TEST_SKIPPED;
  39592. #ifdef HAVE_AESGCM
  39593. res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_gcm() != NULL);
  39594. #endif
  39595. return res;
  39596. }
  39597. static int test_wolfSSL_EVP_aes_256_ccm(void)
  39598. {
  39599. int res = TEST_SKIPPED;
  39600. #ifdef HAVE_AESCCM
  39601. res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_ccm() != NULL);
  39602. #endif
  39603. return res;
  39604. }
  39605. static int test_wolfSSL_EVP_aes_192_ccm(void)
  39606. {
  39607. int res = TEST_SKIPPED;
  39608. #ifdef HAVE_AESCCM
  39609. res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_ccm() != NULL);
  39610. #endif
  39611. return res;
  39612. }
  39613. static int test_wolfSSL_EVP_aes_128_ccm(void)
  39614. {
  39615. int res = TEST_SKIPPED;
  39616. #ifdef HAVE_AESCCM
  39617. res = TEST_RES_CHECK(wolfSSL_EVP_aes_128_ccm() != NULL);
  39618. #endif
  39619. return res;
  39620. }
  39621. static int test_wolfSSL_EVP_ripemd160(void)
  39622. {
  39623. int res = TEST_SKIPPED;
  39624. #if !defined(NO_WOLFSSL_STUB)
  39625. res = TEST_RES_CHECK(wolfSSL_EVP_ripemd160() == NULL);
  39626. #endif
  39627. return res;
  39628. }
  39629. static int test_wolfSSL_EVP_get_digestbynid(void)
  39630. {
  39631. #ifndef NO_MD5
  39632. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  39633. #endif
  39634. #ifndef NO_SHA
  39635. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  39636. #endif
  39637. #ifndef NO_SHA256
  39638. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  39639. #endif
  39640. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  39641. return TEST_RES_CHECK(1);
  39642. }
  39643. static int test_wolfSSL_EVP_MD_nid(void)
  39644. {
  39645. #ifndef NO_MD5
  39646. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  39647. #endif
  39648. #ifndef NO_SHA
  39649. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  39650. #endif
  39651. #ifndef NO_SHA256
  39652. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  39653. #endif
  39654. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  39655. return TEST_RES_CHECK(1);
  39656. }
  39657. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  39658. {
  39659. int res = TEST_SKIPPED;
  39660. #if defined(HAVE_ECC)
  39661. WOLFSSL_EVP_PKEY* pkey;
  39662. AssertNotNull(pkey = EVP_PKEY_new());
  39663. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  39664. EVP_PKEY_free(pkey);
  39665. res = TEST_RES_CHECK(1);
  39666. #endif
  39667. return res;
  39668. }
  39669. static int test_wolfSSL_EVP_X_STATE(void)
  39670. {
  39671. int res = TEST_SKIPPED;
  39672. #if !defined(NO_DES3) && !defined(NO_RC4)
  39673. byte key[DES3_KEY_SIZE] = {0};
  39674. byte iv[DES_IV_SIZE] = {0};
  39675. EVP_CIPHER_CTX *ctx;
  39676. const EVP_CIPHER *init;
  39677. /* Bad test cases */
  39678. ctx = EVP_CIPHER_CTX_new();
  39679. init = EVP_des_ede3_cbc();
  39680. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39681. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39682. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  39683. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  39684. EVP_CIPHER_CTX_free(ctx);
  39685. /* Good test case */
  39686. ctx = EVP_CIPHER_CTX_new();
  39687. init = wolfSSL_EVP_rc4();
  39688. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39689. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39690. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  39691. EVP_CIPHER_CTX_free(ctx);
  39692. res = TEST_RES_CHECK(1);
  39693. #endif
  39694. return res;
  39695. }
  39696. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  39697. {
  39698. int res = TEST_SKIPPED;
  39699. #if !defined(NO_DES3) && !defined(NO_RC4)
  39700. byte key[DES3_KEY_SIZE] = {0};
  39701. byte iv[DES_IV_SIZE] = {0};
  39702. EVP_CIPHER_CTX *ctx;
  39703. const EVP_CIPHER *init;
  39704. /* Bad test cases */
  39705. ctx = EVP_CIPHER_CTX_new();
  39706. init = EVP_des_ede3_cbc();
  39707. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39708. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39709. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  39710. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  39711. EVP_CIPHER_CTX_free(ctx);
  39712. /* Good test case */
  39713. ctx = EVP_CIPHER_CTX_new();
  39714. init = wolfSSL_EVP_rc4();
  39715. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39716. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39717. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  39718. EVP_CIPHER_CTX_free(ctx);
  39719. res = TEST_RES_CHECK(1);
  39720. #endif
  39721. return res;
  39722. }
  39723. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  39724. {
  39725. int res = TEST_SKIPPED;
  39726. #ifdef HAVE_AES_CBC
  39727. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39728. #ifdef WOLFSSL_AES_128
  39729. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  39730. #endif
  39731. #ifdef WOLFSSL_AES_192
  39732. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  39733. #endif
  39734. #ifdef WOLFSSL_AES_256
  39735. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  39736. #endif
  39737. res = TEST_RES_CHECK(1);
  39738. }
  39739. #endif
  39740. #ifdef HAVE_AESGCM
  39741. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39742. #ifdef WOLFSSL_AES_128
  39743. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  39744. #endif
  39745. #ifdef WOLFSSL_AES_192
  39746. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  39747. #endif
  39748. #ifdef WOLFSSL_AES_256
  39749. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  39750. #endif
  39751. res = TEST_RES_CHECK(1);
  39752. }
  39753. #endif
  39754. #ifdef HAVE_AESCCM
  39755. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39756. #ifdef WOLFSSL_AES_128
  39757. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  39758. #endif
  39759. #ifdef WOLFSSL_AES_192
  39760. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  39761. #endif
  39762. #ifdef WOLFSSL_AES_256
  39763. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  39764. #endif
  39765. res = TEST_RES_CHECK(1);
  39766. }
  39767. #endif
  39768. #ifdef WOLFSSL_AES_COUNTER
  39769. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39770. #ifdef WOLFSSL_AES_128
  39771. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  39772. #endif
  39773. #ifdef WOLFSSL_AES_192
  39774. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  39775. #endif
  39776. #ifdef WOLFSSL_AES_256
  39777. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  39778. #endif
  39779. res = TEST_RES_CHECK(1);
  39780. }
  39781. #endif
  39782. #ifdef HAVE_AES_ECB
  39783. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39784. #ifdef WOLFSSL_AES_128
  39785. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  39786. #endif
  39787. #ifdef WOLFSSL_AES_192
  39788. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  39789. #endif
  39790. #ifdef WOLFSSL_AES_256
  39791. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  39792. #endif
  39793. res = TEST_RES_CHECK(1);
  39794. }
  39795. #endif
  39796. #ifdef WOLFSSL_AES_OFB
  39797. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39798. #ifdef WOLFSSL_AES_128
  39799. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  39800. #endif
  39801. #ifdef WOLFSSL_AES_192
  39802. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  39803. #endif
  39804. #ifdef WOLFSSL_AES_256
  39805. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  39806. #endif
  39807. res = TEST_RES_CHECK(1);
  39808. }
  39809. #endif
  39810. #ifndef NO_RC4
  39811. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39812. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  39813. res = TEST_RES_CHECK(1);
  39814. }
  39815. #endif
  39816. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39817. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39818. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  39819. res = TEST_RES_CHECK(1);
  39820. }
  39821. #endif
  39822. return res;
  39823. }
  39824. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  39825. {
  39826. int i, enumlen;
  39827. int enumArray[] = {
  39828. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39829. #ifdef WOLFSSL_AES_128
  39830. NID_aes_128_cbc,
  39831. #endif
  39832. #ifdef WOLFSSL_AES_192
  39833. NID_aes_192_cbc,
  39834. #endif
  39835. #ifdef WOLFSSL_AES_256
  39836. NID_aes_256_cbc,
  39837. #endif
  39838. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39839. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39840. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39841. #ifdef HAVE_AESGCM
  39842. #ifdef WOLFSSL_AES_128
  39843. NID_aes_128_gcm,
  39844. #endif
  39845. #ifdef WOLFSSL_AES_192
  39846. NID_aes_192_gcm,
  39847. #endif
  39848. #ifdef WOLFSSL_AES_256
  39849. NID_aes_256_gcm,
  39850. #endif
  39851. #endif /* HAVE_AESGCM */
  39852. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39853. #ifdef WOLFSSL_AES_COUNTER
  39854. #ifdef WOLFSSL_AES_128
  39855. NID_aes_128_ctr,
  39856. #endif
  39857. #ifdef WOLFSSL_AES_192
  39858. NID_aes_192_ctr,
  39859. #endif
  39860. #ifdef WOLFSSL_AES_256
  39861. NID_aes_256_ctr,
  39862. #endif
  39863. #endif
  39864. #ifndef NO_DES3
  39865. NID_des_cbc,
  39866. NID_des_ede3_cbc,
  39867. #endif
  39868. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39869. NID_chacha20_poly1305,
  39870. #endif
  39871. };
  39872. int iv_lengths[] = {
  39873. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39874. #ifdef WOLFSSL_AES_128
  39875. AES_BLOCK_SIZE,
  39876. #endif
  39877. #ifdef WOLFSSL_AES_192
  39878. AES_BLOCK_SIZE,
  39879. #endif
  39880. #ifdef WOLFSSL_AES_256
  39881. AES_BLOCK_SIZE,
  39882. #endif
  39883. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39884. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39885. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39886. #ifdef HAVE_AESGCM
  39887. #ifdef WOLFSSL_AES_128
  39888. GCM_NONCE_MID_SZ,
  39889. #endif
  39890. #ifdef WOLFSSL_AES_192
  39891. GCM_NONCE_MID_SZ,
  39892. #endif
  39893. #ifdef WOLFSSL_AES_256
  39894. GCM_NONCE_MID_SZ,
  39895. #endif
  39896. #endif /* HAVE_AESGCM */
  39897. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39898. #ifdef WOLFSSL_AES_COUNTER
  39899. #ifdef WOLFSSL_AES_128
  39900. AES_BLOCK_SIZE,
  39901. #endif
  39902. #ifdef WOLFSSL_AES_192
  39903. AES_BLOCK_SIZE,
  39904. #endif
  39905. #ifdef WOLFSSL_AES_256
  39906. AES_BLOCK_SIZE,
  39907. #endif
  39908. #endif
  39909. #ifndef NO_DES3
  39910. DES_BLOCK_SIZE,
  39911. DES_BLOCK_SIZE,
  39912. #endif
  39913. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39914. CHACHA20_POLY1305_AEAD_IV_SIZE,
  39915. #endif
  39916. };
  39917. enumlen = (sizeof(enumArray)/sizeof(int));
  39918. for (i = 0; i < enumlen; i++) {
  39919. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  39920. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  39921. }
  39922. return TEST_RES_CHECK(1);
  39923. }
  39924. static int test_wolfSSL_EVP_SignInit_ex(void)
  39925. {
  39926. WOLFSSL_EVP_MD_CTX mdCtx;
  39927. WOLFSSL_ENGINE* e = 0;
  39928. const EVP_MD* md;
  39929. md = "SHA256";
  39930. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39931. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  39932. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39933. return TEST_RES_CHECK(1);
  39934. }
  39935. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  39936. {
  39937. int res = TEST_SKIPPED;
  39938. #if !defined(NO_SHA256)
  39939. WOLFSSL_EVP_MD_CTX mdCtx;
  39940. unsigned int s = 0;
  39941. unsigned char md[WC_SHA256_DIGEST_SIZE];
  39942. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  39943. /* Bad Case */
  39944. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39945. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39946. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  39947. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39948. #else
  39949. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39950. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  39951. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39952. #endif
  39953. /* Good Case */
  39954. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39955. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  39956. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  39957. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39958. res = TEST_RES_CHECK(1);
  39959. #endif
  39960. return res;
  39961. }
  39962. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  39963. {
  39964. int res = TEST_SKIPPED;
  39965. #if !defined(NO_DH) && \
  39966. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39967. FILE* f = NULL;
  39968. unsigned char buf[4096];
  39969. const unsigned char* pt = buf;
  39970. const char* params1 = "./certs/dh2048.der";
  39971. long len = 0;
  39972. WOLFSSL_DH* dh = NULL;
  39973. WOLFSSL_EVP_PKEY* pkey;
  39974. XMEMSET(buf, 0, sizeof(buf));
  39975. f = XFOPEN(params1, "rb");
  39976. AssertTrue(f != XBADFILE);
  39977. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39978. XFCLOSE(f);
  39979. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  39980. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  39981. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39982. /* Bad cases */
  39983. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  39984. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  39985. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  39986. /* Good case */
  39987. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  39988. EVP_PKEY_free(pkey);
  39989. res = TEST_RES_CHECK(1);
  39990. #endif
  39991. return res;
  39992. }
  39993. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  39994. {
  39995. int res = TEST_SKIPPED;
  39996. #if defined(OPENSSL_EXTRA)
  39997. EVP_PKEY* pkey;
  39998. EVP_PKEY_CTX* ctx;
  39999. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40000. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40001. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  40002. /* void */
  40003. EVP_PKEY_CTX_free(ctx);
  40004. AssertTrue(1);
  40005. #else
  40006. /* int */
  40007. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  40008. #endif
  40009. EVP_PKEY_free(pkey);
  40010. res = TEST_RES_CHECK(1);
  40011. #endif
  40012. return res;
  40013. }
  40014. static int test_wolfSSL_EVP_PKEY_param_check(void)
  40015. {
  40016. int res = TEST_SKIPPED;
  40017. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  40018. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  40019. DH *dh = NULL;
  40020. DH *setDh = NULL;
  40021. EVP_PKEY *pkey = NULL;
  40022. EVP_PKEY_CTX* ctx = NULL;
  40023. FILE* f = NULL;
  40024. unsigned char buf[512];
  40025. const unsigned char* pt = buf;
  40026. const char* dh2048 = "./certs/dh2048.der";
  40027. long len = 0;
  40028. int code = -1;
  40029. XMEMSET(buf, 0, sizeof(buf));
  40030. f = XFOPEN(dh2048, "rb");
  40031. AssertTrue(f != XBADFILE);
  40032. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  40033. XFCLOSE(f);
  40034. /* Load dh2048.der into DH with internal format */
  40035. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  40036. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  40037. AssertIntEQ(code, 0);
  40038. code = -1;
  40039. pkey = wolfSSL_EVP_PKEY_new();
  40040. /* Set DH into PKEY */
  40041. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  40042. /* create ctx from pkey */
  40043. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40044. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  40045. /* */
  40046. /* TO DO invlaid case */
  40047. /* */
  40048. EVP_PKEY_CTX_free(ctx);
  40049. EVP_PKEY_free(pkey);
  40050. DH_free(setDh);
  40051. DH_free(dh);
  40052. res = TEST_RES_CHECK(1);
  40053. #endif
  40054. #endif
  40055. return res;
  40056. }
  40057. static int test_wolfSSL_EVP_BytesToKey(void)
  40058. {
  40059. int res = TEST_SKIPPED;
  40060. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  40061. byte key[AES_BLOCK_SIZE] = {0};
  40062. byte iv[AES_BLOCK_SIZE] = {0};
  40063. int sz = 5;
  40064. int count = 0;
  40065. const EVP_MD* md = "SHA256";
  40066. const EVP_CIPHER *type;
  40067. const unsigned char *salt = (unsigned char *)"salt1234";
  40068. const byte data[] = {
  40069. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  40070. 0x72,0x6c,0x64
  40071. };
  40072. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  40073. /* Bad cases */
  40074. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  40075. 0);
  40076. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  40077. 16);
  40078. md = "2";
  40079. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  40080. WOLFSSL_FAILURE);
  40081. /* Good case */
  40082. md = "SHA256";
  40083. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  40084. 16);
  40085. res = TEST_RES_CHECK(1);
  40086. #endif
  40087. return res;
  40088. }
  40089. static int test_evp_cipher_aes_gcm(void)
  40090. {
  40091. int res = TEST_SKIPPED;
  40092. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  40093. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  40094. (HAVE_FIPS_VERSION >= 2)))
  40095. /*
  40096. * This test checks data at various points in the encrypt/decrypt process
  40097. * against known values produced using the same test with OpenSSL. This
  40098. * interop testing is critical for verifying the correctness of our
  40099. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  40100. * a flow supported by OpenSSL that uses the control command
  40101. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  40102. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  40103. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  40104. * wolfSSL OpenSSH clients because there was a bug in this flow that
  40105. * happened to "cancel out" if both sides of the connection had the bug.
  40106. */
  40107. enum {
  40108. NUM_ENCRYPTIONS = 3,
  40109. AAD_SIZE = 4
  40110. };
  40111. byte plainText1[] = {
  40112. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  40113. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  40114. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  40115. };
  40116. byte plainText2[] = {
  40117. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  40118. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  40119. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  40120. };
  40121. byte plainText3[] = {
  40122. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  40123. 0x5b, 0x57, 0x10
  40124. };
  40125. byte* plainTexts[NUM_ENCRYPTIONS] = {
  40126. plainText1,
  40127. plainText2,
  40128. plainText3
  40129. };
  40130. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  40131. sizeof(plainText1),
  40132. sizeof(plainText2),
  40133. sizeof(plainText3)
  40134. };
  40135. byte aad1[AAD_SIZE] = {
  40136. 0x00, 0x00, 0x00, 0x01
  40137. };
  40138. byte aad2[AAD_SIZE] = {
  40139. 0x00, 0x00, 0x00, 0x10
  40140. };
  40141. byte aad3[AAD_SIZE] = {
  40142. 0x00, 0x00, 0x01, 0x00
  40143. };
  40144. byte* aads[NUM_ENCRYPTIONS] = {
  40145. aad1,
  40146. aad2,
  40147. aad3
  40148. };
  40149. const byte iv[GCM_NONCE_MID_SZ] = {
  40150. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  40151. };
  40152. byte currentIv[GCM_NONCE_MID_SZ];
  40153. const byte key[] = {
  40154. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  40155. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  40156. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  40157. };
  40158. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  40159. {
  40160. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  40161. 0xEF
  40162. },
  40163. {
  40164. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  40165. 0xF0
  40166. },
  40167. {
  40168. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  40169. 0xF1
  40170. }
  40171. };
  40172. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  40173. {
  40174. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  40175. 0x3D, 0x32, 0x18, 0x34, 0xA9
  40176. },
  40177. {
  40178. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  40179. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  40180. },
  40181. {
  40182. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  40183. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  40184. }
  40185. };
  40186. const byte expCipherText1[] = {
  40187. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  40188. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  40189. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  40190. };
  40191. const byte expCipherText2[] = {
  40192. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  40193. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  40194. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  40195. };
  40196. const byte expCipherText3[] = {
  40197. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  40198. 0x80, 0x5E, 0x6B,
  40199. };
  40200. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  40201. expCipherText1,
  40202. expCipherText2,
  40203. expCipherText3
  40204. };
  40205. byte* cipherText;
  40206. byte* calcPlainText;
  40207. byte tag[AES_BLOCK_SIZE];
  40208. EVP_CIPHER_CTX* encCtx = NULL;
  40209. EVP_CIPHER_CTX* decCtx = NULL;
  40210. int i, j, outl;
  40211. /****************************************************/
  40212. for (i = 0; i < 3; ++i) {
  40213. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  40214. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  40215. /* First iteration, set key before IV. */
  40216. if (i == 0) {
  40217. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  40218. SSL_SUCCESS);
  40219. /*
  40220. * The call to EVP_CipherInit below (with NULL key) should clear the
  40221. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  40222. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  40223. * behavior.
  40224. */
  40225. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40226. (void*)iv), SSL_SUCCESS);
  40227. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  40228. SSL_SUCCESS);
  40229. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40230. currentIv), SSL_FAILURE);
  40231. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  40232. SSL_SUCCESS);
  40233. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  40234. SSL_SUCCESS);
  40235. }
  40236. /* Second iteration, IV before key. */
  40237. else {
  40238. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  40239. SSL_SUCCESS);
  40240. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  40241. SSL_SUCCESS);
  40242. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  40243. SSL_SUCCESS);
  40244. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  40245. SSL_SUCCESS);
  40246. }
  40247. /*
  40248. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  40249. * been issued first.
  40250. */
  40251. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40252. currentIv), SSL_FAILURE);
  40253. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40254. (void*)iv), SSL_SUCCESS);
  40255. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40256. (void*)iv), SSL_SUCCESS);
  40257. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  40258. /*************** Encrypt ***************/
  40259. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40260. currentIv), SSL_SUCCESS);
  40261. /* Check current IV against expected. */
  40262. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  40263. /* Add AAD. */
  40264. if (i == 2) {
  40265. /* Test streaming API. */
  40266. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  40267. AAD_SIZE), SSL_SUCCESS);
  40268. }
  40269. else {
  40270. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  40271. AAD_SIZE);
  40272. }
  40273. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  40274. DYNAMIC_TYPE_TMP_BUFFER));
  40275. /* Encrypt plaintext. */
  40276. if (i == 2) {
  40277. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  40278. plainTexts[j], plainTextSzs[j]),
  40279. SSL_SUCCESS);
  40280. }
  40281. else {
  40282. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  40283. plainTextSzs[j]), plainTextSzs[j]);
  40284. }
  40285. if (i == 2) {
  40286. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  40287. SSL_SUCCESS);
  40288. }
  40289. else {
  40290. /*
  40291. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  40292. * akin to calling EVP_CipherFinal.
  40293. */
  40294. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  40295. }
  40296. /* Check ciphertext against expected. */
  40297. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  40298. 0);
  40299. /* Get and check tag against expected. */
  40300. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  40301. sizeof(tag), tag), SSL_SUCCESS);
  40302. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  40303. /*************** Decrypt ***************/
  40304. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40305. currentIv), SSL_SUCCESS);
  40306. /* Check current IV against expected. */
  40307. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  40308. /* Add AAD. */
  40309. if (i == 2) {
  40310. /* Test streaming API. */
  40311. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  40312. AAD_SIZE), SSL_SUCCESS);
  40313. }
  40314. else {
  40315. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  40316. AAD_SIZE);
  40317. }
  40318. /* Set expected tag. */
  40319. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  40320. sizeof(tag), tag), SSL_SUCCESS);
  40321. /* Decrypt ciphertext. */
  40322. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  40323. DYNAMIC_TYPE_TMP_BUFFER));
  40324. if (i == 2) {
  40325. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  40326. cipherText, plainTextSzs[j]),
  40327. SSL_SUCCESS);
  40328. }
  40329. else {
  40330. /* This first EVP_Cipher call will check the tag, too. */
  40331. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  40332. plainTextSzs[j]), plainTextSzs[j]);
  40333. }
  40334. if (i == 2) {
  40335. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  40336. SSL_SUCCESS);
  40337. }
  40338. else {
  40339. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  40340. }
  40341. /* Check plaintext against expected. */
  40342. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  40343. 0);
  40344. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40345. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40346. }
  40347. EVP_CIPHER_CTX_free(encCtx);
  40348. EVP_CIPHER_CTX_free(decCtx);
  40349. }
  40350. res = TEST_RES_CHECK(1);
  40351. #endif
  40352. return res;
  40353. }
  40354. static int test_wolfSSL_OBJ_ln(void)
  40355. {
  40356. const int nid_set[] = {
  40357. NID_commonName,
  40358. NID_serialNumber,
  40359. NID_countryName,
  40360. NID_localityName,
  40361. NID_stateOrProvinceName,
  40362. NID_organizationName,
  40363. NID_organizationalUnitName,
  40364. NID_domainComponent,
  40365. NID_businessCategory,
  40366. NID_jurisdictionCountryName,
  40367. NID_jurisdictionStateOrProvinceName,
  40368. NID_emailAddress
  40369. };
  40370. const char* ln_set[] = {
  40371. "commonName",
  40372. "serialNumber",
  40373. "countryName",
  40374. "localityName",
  40375. "stateOrProvinceName",
  40376. "organizationName",
  40377. "organizationalUnitName",
  40378. "domainComponent",
  40379. "businessCategory",
  40380. "jurisdictionCountryName",
  40381. "jurisdictionStateOrProvinceName",
  40382. "emailAddress",
  40383. };
  40384. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  40385. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  40386. #ifdef HAVE_ECC
  40387. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40388. {
  40389. EC_builtin_curve r[27];
  40390. size_t nCurves = sizeof(r) / sizeof(r[0]);
  40391. nCurves = EC_get_builtin_curves(r,nCurves);
  40392. for (i = 0; i < nCurves; i++) {
  40393. /* skip ECC_CURVE_INVALID */
  40394. if (r[i].nid != ECC_CURVE_INVALID) {
  40395. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  40396. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  40397. }
  40398. }
  40399. }
  40400. #endif
  40401. #endif
  40402. for (i = 0; i < maxIdx; i++) {
  40403. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  40404. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  40405. }
  40406. return TEST_RES_CHECK(1);
  40407. }
  40408. static int test_wolfSSL_OBJ_sn(void)
  40409. {
  40410. int i = 0, maxIdx = 7;
  40411. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  40412. NID_stateOrProvinceName,NID_organizationName,
  40413. NID_organizationalUnitName,NID_emailAddress};
  40414. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  40415. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  40416. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  40417. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  40418. WOLFSSL_EMAIL_ADDR};
  40419. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  40420. for (i = 0; i < maxIdx; i++) {
  40421. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  40422. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  40423. }
  40424. return TEST_RES_CHECK(1);
  40425. }
  40426. #if !defined(NO_BIO)
  40427. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  40428. {
  40429. return lh_strhash(s[3]);
  40430. }
  40431. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  40432. {
  40433. return XSTRCMP(a[3], b[3]);
  40434. }
  40435. #endif
  40436. static int test_wolfSSL_TXT_DB(void)
  40437. {
  40438. int res = TEST_SKIPPED;
  40439. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40440. BIO *bio;
  40441. TXT_DB *db = NULL;
  40442. const int columns = 6;
  40443. const char *fields[6] = {
  40444. "V",
  40445. "320926161116Z",
  40446. "",
  40447. "12BD",
  40448. "unknown",
  40449. "/CN=rsa doe",
  40450. };
  40451. char** fields_copy;
  40452. /* Test read */
  40453. AssertNotNull(bio = BIO_new(BIO_s_file()));
  40454. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  40455. AssertNotNull(db = TXT_DB_read(bio, columns));
  40456. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  40457. DYNAMIC_TYPE_OPENSSL));
  40458. XMEMCPY(fields_copy, fields, sizeof(fields));
  40459. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  40460. BIO_free(bio);
  40461. /* Test write */
  40462. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  40463. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  40464. BIO_free(bio);
  40465. /* Test index */
  40466. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  40467. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  40468. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40469. fields[3] = "12DA";
  40470. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40471. fields[3] = "FFFF";
  40472. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40473. fields[3] = "";
  40474. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40475. TXT_DB_free(db);
  40476. res = TEST_RES_CHECK(1);
  40477. #endif
  40478. return res;
  40479. }
  40480. static int test_wolfSSL_NCONF(void)
  40481. {
  40482. int res = TEST_SKIPPED;
  40483. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40484. const char* confFile = "./tests/NCONF_test.cnf";
  40485. CONF* conf = NULL;
  40486. long eline = 0;
  40487. long num = 0;
  40488. AssertNotNull(conf = NCONF_new(NULL));
  40489. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  40490. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  40491. AssertIntEQ(num, 1234);
  40492. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  40493. AssertIntEQ(num, 4321);
  40494. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  40495. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  40496. "./test-dir/file1");
  40497. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  40498. "./test-dir/file1:./test-dir/file2:./section1:file2");
  40499. NCONF_free(conf);
  40500. res = TEST_RES_CHECK(1);
  40501. #endif
  40502. return res;
  40503. }
  40504. #endif /* OPENSSL_ALL */
  40505. static int test_wolfSSL_X509V3_EXT_get(void) {
  40506. int res = TEST_SKIPPED;
  40507. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40508. FILE* f;
  40509. int numOfExt =0;
  40510. int extNid = 0;
  40511. int i = 0;
  40512. WOLFSSL_X509* x509;
  40513. WOLFSSL_X509_EXTENSION* ext;
  40514. const WOLFSSL_v3_ext_method* method;
  40515. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40516. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40517. fclose(f);
  40518. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  40519. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40520. for (i = 0; i < numOfExt; i++) {
  40521. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40522. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  40523. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40524. AssertIntEQ(method->ext_nid, extNid);
  40525. }
  40526. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  40527. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  40528. wolfSSL_X509_free(x509);
  40529. res = TEST_RES_CHECK(1);
  40530. #endif
  40531. return res;
  40532. }
  40533. static int test_wolfSSL_X509V3_EXT_nconf(void)
  40534. {
  40535. int res = TEST_SKIPPED;
  40536. #ifdef OPENSSL_ALL
  40537. const char *ext_names[] = {
  40538. "subjectKeyIdentifier",
  40539. "authorityKeyIdentifier",
  40540. "subjectAltName",
  40541. "keyUsage",
  40542. };
  40543. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  40544. int ext_nids[] = {
  40545. NID_subject_key_identifier,
  40546. NID_authority_key_identifier,
  40547. NID_subject_alt_name,
  40548. NID_key_usage,
  40549. };
  40550. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  40551. const char *ext_values[] = {
  40552. "hash",
  40553. "hash",
  40554. "DNS:example.com, IP:127.0.0.1",
  40555. "digitalSignature,keyEncipherment,dataEncipherment",
  40556. };
  40557. size_t i;
  40558. X509_EXTENSION* ext;
  40559. X509* x509 = X509_new();
  40560. for (i = 0; i < ext_names_count; i++) {
  40561. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  40562. AssertNotNull(ext);
  40563. X509_EXTENSION_free(ext);
  40564. }
  40565. for (i = 0; i < ext_nids_count; i++) {
  40566. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  40567. AssertNotNull(ext);
  40568. X509_EXTENSION_free(ext);
  40569. }
  40570. /* Test adding extension to X509 */
  40571. for (i = 0; i < ext_nids_count; i++) {
  40572. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  40573. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  40574. X509_EXTENSION_free(ext);
  40575. }
  40576. X509_free(x509);
  40577. res = TEST_RES_CHECK(1);
  40578. #endif
  40579. return res;
  40580. }
  40581. static int test_wolfSSL_X509V3_EXT(void) {
  40582. int res = TEST_SKIPPED;
  40583. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40584. FILE* f;
  40585. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  40586. char* str;
  40587. unsigned char* data;
  40588. const WOLFSSL_v3_ext_method* method;
  40589. WOLFSSL_X509* x509;
  40590. WOLFSSL_X509_EXTENSION* ext;
  40591. WOLFSSL_X509_EXTENSION* ext2;
  40592. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  40593. WOLFSSL_ASN1_STRING* asn1str;
  40594. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  40595. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  40596. WOLFSSL_BASIC_CONSTRAINTS* bc;
  40597. WOLFSSL_ACCESS_DESCRIPTION* ad;
  40598. WOLFSSL_GENERAL_NAME* gn;
  40599. /* Check NULL argument */
  40600. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  40601. /* Using OCSP cert with X509V3 extensions */
  40602. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  40603. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40604. fclose(f);
  40605. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40606. /* Basic Constraints */
  40607. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40608. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40609. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  40610. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  40611. AssertIntEQ(bc->ca, 1);
  40612. AssertNull(bc->pathlen);
  40613. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  40614. i++;
  40615. /* Subject Key Identifier */
  40616. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40617. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40618. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  40619. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40620. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  40621. asn1str));
  40622. X509_EXTENSION_free(ext2);
  40623. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40624. AssertNotNull(method->i2s);
  40625. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  40626. wolfSSL_ASN1_STRING_free(asn1str);
  40627. actual = strcmp(str,
  40628. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40629. AssertIntEQ(actual, 0);
  40630. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40631. i++;
  40632. /* Authority Key Identifier */
  40633. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40634. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40635. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  40636. AssertNotNull(aKeyId =
  40637. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  40638. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40639. AssertNotNull(asn1str = aKeyId->keyid);
  40640. AssertNotNull(str =
  40641. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  40642. actual = strcmp(str,
  40643. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40644. AssertIntEQ(actual, 0);
  40645. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40646. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  40647. i++;
  40648. /* Key Usage */
  40649. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40650. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40651. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  40652. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40653. #if defined(WOLFSSL_QT)
  40654. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  40655. #else
  40656. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  40657. #endif
  40658. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  40659. #ifdef BIG_ENDIAN_ORDER
  40660. actual = data[1];
  40661. #else
  40662. actual = data[0];
  40663. #endif
  40664. AssertIntEQ(actual, expected);
  40665. wolfSSL_ASN1_STRING_free(asn1str);
  40666. #if 1
  40667. i++;
  40668. /* Authority Info Access */
  40669. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40670. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40671. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  40672. AssertNotNull(aia =
  40673. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  40674. #if defined(WOLFSSL_QT)
  40675. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40676. #else
  40677. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40678. #endif
  40679. /* URI entry is an ACCESS_DESCRIPTION type */
  40680. #if defined(WOLFSSL_QT)
  40681. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  40682. #else
  40683. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  40684. #endif
  40685. AssertNotNull(adObj = ad->method);
  40686. /* Make sure nid is OCSP */
  40687. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  40688. /* GENERAL_NAME stores URI as an ASN1_STRING */
  40689. AssertNotNull(gn = ad->location);
  40690. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  40691. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  40692. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  40693. #if defined(WOLFSSL_QT)
  40694. str = (char*)ASN1_STRING_get0_data(asn1str);
  40695. #else
  40696. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  40697. #endif
  40698. actual = strcmp(str, "http://127.0.0.1:22220");
  40699. AssertIntEQ(actual, 0);
  40700. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40701. #else
  40702. (void) aia; (void) ad; (void) adObj; (void) gn;
  40703. #endif
  40704. wolfSSL_X509_free(x509);
  40705. res = TEST_RES_CHECK(1);
  40706. #endif
  40707. return res;
  40708. }
  40709. static int test_wolfSSL_X509_get_extension_flags(void)
  40710. {
  40711. int res = TEST_SKIPPED;
  40712. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40713. XFILE f;
  40714. X509* x509;
  40715. unsigned int extFlags;
  40716. unsigned int keyUsageFlags;
  40717. unsigned int extKeyUsageFlags;
  40718. /* client-int-cert.pem has the following extension flags. */
  40719. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  40720. /* and the following key usage flags. */
  40721. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40722. | KU_NON_REPUDIATION
  40723. | KU_KEY_ENCIPHERMENT;
  40724. /* and the following extended key usage flags. */
  40725. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  40726. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  40727. AssertTrue(f != XBADFILE);
  40728. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40729. XFCLOSE(f);
  40730. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  40731. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40732. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  40733. X509_free(x509);
  40734. /* client-cert-ext.pem has the following extension flags. */
  40735. extFlags = EXFLAG_KUSAGE;
  40736. /* and the following key usage flags. */
  40737. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40738. | KU_KEY_CERT_SIGN
  40739. | KU_CRL_SIGN;
  40740. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  40741. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40742. XFCLOSE(f);
  40743. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  40744. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40745. X509_free(x509);
  40746. res = TEST_RES_CHECK(1);
  40747. #endif /* OPENSSL_ALL */
  40748. return res;
  40749. }
  40750. static int test_wolfSSL_X509_get_ext(void)
  40751. {
  40752. int res = TEST_SKIPPED;
  40753. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40754. int ret = 0;
  40755. FILE* f;
  40756. WOLFSSL_X509* x509;
  40757. WOLFSSL_X509_EXTENSION* foundExtension;
  40758. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40759. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40760. fclose(f);
  40761. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  40762. /* wolfSSL_X509_get_ext() valid input */
  40763. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  40764. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  40765. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  40766. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  40767. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  40768. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  40769. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  40770. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  40771. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  40772. wolfSSL_X509_free(x509);
  40773. res = TEST_RES_CHECK(1);
  40774. #endif
  40775. return res;
  40776. }
  40777. static int test_wolfSSL_X509_get_ext_by_NID(void)
  40778. {
  40779. int res = TEST_SKIPPED;
  40780. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40781. int rc;
  40782. FILE* f;
  40783. WOLFSSL_X509* x509;
  40784. ASN1_OBJECT* obj = NULL;
  40785. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40786. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40787. fclose(f);
  40788. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40789. AssertIntGE(rc, 0);
  40790. /* Start search from last location (should fail) */
  40791. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  40792. AssertIntGE(rc, -1);
  40793. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  40794. AssertIntGE(rc, -1);
  40795. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  40796. AssertIntEQ(rc, -1);
  40797. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  40798. AssertIntEQ(rc, -1);
  40799. /* NID_ext_key_usage, check also its nid and oid */
  40800. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  40801. AssertIntGT(rc, -1);
  40802. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  40803. AssertIntEQ(obj->nid, NID_ext_key_usage);
  40804. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  40805. wolfSSL_X509_free(x509);
  40806. res = TEST_RES_CHECK(1);
  40807. #endif
  40808. return res;
  40809. }
  40810. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  40811. {
  40812. int res = TEST_SKIPPED;
  40813. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40814. int rc;
  40815. XFILE f;
  40816. WOLFSSL_X509* x509;
  40817. WOLFSSL_X509_EXTENSION* ext;
  40818. WOLFSSL_ASN1_STRING* sanString;
  40819. byte* sanDer;
  40820. const byte expectedDer[] = {
  40821. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  40822. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  40823. f = XFOPEN("./certs/server-cert.pem", "rb");
  40824. AssertTrue(f != XBADFILE);
  40825. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40826. fclose(f);
  40827. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  40828. AssertIntNE(rc, -1);
  40829. AssertNotNull(ext = X509_get_ext(x509, rc));
  40830. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  40831. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  40832. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  40833. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  40834. X509_free(x509);
  40835. res = TEST_RES_CHECK(1);
  40836. #endif
  40837. return res;
  40838. }
  40839. static int test_wolfSSL_X509_EXTENSION_new(void)
  40840. {
  40841. int res = TEST_SKIPPED;
  40842. #if defined (OPENSSL_ALL)
  40843. WOLFSSL_X509_EXTENSION* ext;
  40844. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  40845. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  40846. ext->obj->nid = WOLFSSL_SUCCESS;
  40847. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  40848. wolfSSL_X509_EXTENSION_free(ext);
  40849. res = TEST_RES_CHECK(1);
  40850. #endif
  40851. return res;
  40852. }
  40853. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  40854. {
  40855. int res = TEST_SKIPPED;
  40856. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40857. WOLFSSL_X509* x509;
  40858. WOLFSSL_X509_EXTENSION* ext;
  40859. WOLFSSL_ASN1_OBJECT* o;
  40860. FILE* file;
  40861. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40862. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40863. fclose(file);
  40864. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  40865. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40866. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  40867. AssertIntEQ(o->nid, 128);
  40868. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  40869. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  40870. wolfSSL_X509_free(x509);
  40871. res = TEST_RES_CHECK(1);
  40872. #endif
  40873. return res;
  40874. }
  40875. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  40876. {
  40877. int res = TEST_SKIPPED;
  40878. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40879. WOLFSSL_X509* x509;
  40880. WOLFSSL_X509_EXTENSION* ext;
  40881. WOLFSSL_ASN1_STRING* str;
  40882. FILE* file;
  40883. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40884. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40885. fclose(file);
  40886. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40887. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  40888. wolfSSL_X509_free(x509);
  40889. res = TEST_RES_CHECK(1);
  40890. #endif
  40891. return res;
  40892. }
  40893. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  40894. {
  40895. int res = TEST_SKIPPED;
  40896. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40897. WOLFSSL_X509* x509;
  40898. WOLFSSL_X509_EXTENSION* ext;
  40899. FILE* file;
  40900. int crit;
  40901. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40902. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40903. fclose(file);
  40904. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40905. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  40906. AssertIntEQ(crit, 0);
  40907. wolfSSL_X509_free(x509);
  40908. res = TEST_RES_CHECK(1);
  40909. #endif
  40910. return res;
  40911. }
  40912. static int test_wolfSSL_X509V3_EXT_print(void)
  40913. {
  40914. int res = TEST_SKIPPED;
  40915. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  40916. !defined(NO_RSA)
  40917. {
  40918. FILE* f;
  40919. WOLFSSL_X509* x509;
  40920. X509_EXTENSION * ext = NULL;
  40921. int loc;
  40922. BIO *bio = NULL;
  40923. AssertNotNull(f = fopen(svrCertFile, "rb"));
  40924. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40925. fclose(f);
  40926. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  40927. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40928. AssertIntGT(loc, -1);
  40929. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40930. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40931. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  40932. AssertIntGT(loc, -1);
  40933. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40934. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40935. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  40936. AssertIntGT(loc, -1);
  40937. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40938. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40939. wolfSSL_BIO_free(bio);
  40940. wolfSSL_X509_free(x509);
  40941. }
  40942. {
  40943. X509 *x509;
  40944. BIO *bio;
  40945. X509_EXTENSION *ext;
  40946. unsigned int i;
  40947. unsigned int idx;
  40948. /* Some NIDs to test with */
  40949. int nids[] = {
  40950. /* NID_key_usage, currently X509_get_ext returns this as a bit
  40951. * string, which messes up X509V3_EXT_print */
  40952. /* NID_ext_key_usage, */
  40953. NID_subject_alt_name,
  40954. };
  40955. int* n;
  40956. AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  40957. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  40958. WOLFSSL_FILETYPE_PEM));
  40959. fprintf(stderr, "\nPrinting extension values:\n");
  40960. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  40961. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  40962. * update the index */
  40963. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  40964. AssertNotNull(ext = X509_get_ext(x509, idx));
  40965. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  40966. fprintf(stderr, "\n");
  40967. }
  40968. BIO_free(bio);
  40969. X509_free(x509);
  40970. }
  40971. res = TEST_RES_CHECK(1);
  40972. #endif
  40973. return res;
  40974. }
  40975. static int test_wolfSSL_X509_cmp(void)
  40976. {
  40977. int res = TEST_SKIPPED;
  40978. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40979. FILE* file1;
  40980. FILE* file2;
  40981. WOLFSSL_X509* cert1;
  40982. WOLFSSL_X509* cert2;
  40983. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  40984. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  40985. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  40986. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  40987. fclose(file1);
  40988. fclose(file2);
  40989. /* wolfSSL_X509_cmp() testing matching certs */
  40990. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  40991. /* wolfSSL_X509_cmp() testing mismatched certs */
  40992. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  40993. /* wolfSSL_X509_cmp() testing NULL, valid args */
  40994. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  40995. /* wolfSSL_X509_cmp() testing valid, NULL args */
  40996. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  40997. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  40998. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  40999. wolfSSL_X509_free(cert1);
  41000. wolfSSL_X509_free(cert2);
  41001. res = TEST_RES_CHECK(1);
  41002. #endif
  41003. return res;
  41004. }
  41005. static int test_wolfSSL_PKEY_up_ref(void)
  41006. {
  41007. int res = TEST_SKIPPED;
  41008. #if defined(OPENSSL_ALL)
  41009. EVP_PKEY* pkey;
  41010. pkey = EVP_PKEY_new();
  41011. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  41012. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  41013. EVP_PKEY_free(pkey);
  41014. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  41015. EVP_PKEY_free(pkey);
  41016. EVP_PKEY_free(pkey);
  41017. res = TEST_RES_CHECK(1);
  41018. #endif
  41019. return res;
  41020. }
  41021. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  41022. {
  41023. int res = TEST_SKIPPED;
  41024. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  41025. EVP_PKEY* pkey;
  41026. const unsigned char* p;
  41027. unsigned char *der = NULL, *tmp = NULL;
  41028. int derLen;
  41029. p = client_keypub_der_2048;
  41030. /* Check that key can be successfully decoded. */
  41031. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  41032. sizeof_client_keypub_der_2048));
  41033. /* Check that key can be successfully encoded. */
  41034. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  41035. /* Ensure that the encoded version matches the original. */
  41036. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  41037. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  41038. /* Do same test except with pre-allocated buffer to ensure the der pointer
  41039. * is advanced. */
  41040. tmp = der;
  41041. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  41042. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  41043. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  41044. AssertTrue(der + derLen == tmp);
  41045. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  41046. EVP_PKEY_free(pkey);
  41047. res = TEST_RES_CHECK(1);
  41048. #endif
  41049. return res;
  41050. }
  41051. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  41052. {
  41053. int res = TEST_SKIPPED;
  41054. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  41055. !defined(NO_ASN) && !defined(NO_PWDBASED)
  41056. EVP_PKEY* pkey;
  41057. const unsigned char* p;
  41058. unsigned char *der = NULL, *tmp = NULL;
  41059. int derLen;
  41060. unsigned char pub_buf[65];
  41061. const int pub_len = 65;
  41062. BN_CTX * ctx;
  41063. EC_GROUP * curve;
  41064. EC_KEY * ephemeral_key;
  41065. const EC_POINT * h;
  41066. /* Generate an x963 key pair and get public part into pub_buf */
  41067. AssertNotNull(ctx = BN_CTX_new());
  41068. AssertNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  41069. AssertNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  41070. NID_X9_62_prime256v1));
  41071. AssertIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  41072. AssertNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  41073. AssertIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  41074. POINT_CONVERSION_UNCOMPRESSED,
  41075. pub_buf, pub_len, ctx));
  41076. /* Prepare the EVP_PKEY */
  41077. AssertNotNull(pkey = EVP_PKEY_new());
  41078. p = pub_buf;
  41079. /* Check that key can be successfully decoded. */
  41080. AssertNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  41081. pub_len));
  41082. /* Check that key can be successfully encoded. */
  41083. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  41084. /* Ensure that the encoded version matches the original. */
  41085. AssertIntEQ(derLen, pub_len);
  41086. AssertIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  41087. /* Do same test except with pre-allocated buffer to ensure the der pointer
  41088. * is advanced. */
  41089. tmp = der;
  41090. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  41091. AssertIntEQ(derLen, pub_len);
  41092. AssertIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  41093. AssertTrue(der + derLen == tmp);
  41094. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  41095. EVP_PKEY_free(pkey);
  41096. EC_KEY_free(ephemeral_key);
  41097. EC_GROUP_free(curve);
  41098. res = TEST_RES_CHECK(1);
  41099. #endif
  41100. return res;
  41101. }
  41102. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  41103. {
  41104. int res = TEST_SKIPPED;
  41105. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  41106. DSA* dsa;
  41107. char file[] = "./certs/dsaparams.der";
  41108. XFILE f;
  41109. int derInLen;
  41110. byte* derIn;
  41111. int derOutLen;
  41112. byte* derOut = NULL;
  41113. f = XFOPEN(file, "rb");
  41114. AssertTrue(f != XBADFILE);
  41115. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  41116. derInLen = (int)XFTELL(f);
  41117. XREWIND(f);
  41118. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  41119. DYNAMIC_TYPE_TMP_BUFFER));
  41120. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  41121. XFCLOSE(f);
  41122. /* Check that params can be successfully decoded. */
  41123. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  41124. /* Check that params can be successfully encoded. */
  41125. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  41126. /* Ensure that the encoded version matches the original. */
  41127. AssertIntEQ(derInLen, derOutLen);
  41128. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  41129. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41130. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  41131. DSA_free(dsa);
  41132. res = TEST_RES_CHECK(1);
  41133. #endif
  41134. return res;
  41135. }
  41136. static int test_wolfSSL_i2d_PrivateKey(void)
  41137. {
  41138. int res = TEST_SKIPPED;
  41139. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  41140. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  41141. {
  41142. EVP_PKEY* pkey;
  41143. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  41144. unsigned char buf[FOURK_BUF];
  41145. unsigned char* pt = NULL;
  41146. int bufSz;
  41147. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  41148. (long)sizeof_server_key_der_2048));
  41149. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  41150. pt = buf;
  41151. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  41152. AssertIntNE((pt - buf), 0);
  41153. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  41154. EVP_PKEY_free(pkey);
  41155. }
  41156. #endif
  41157. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41158. {
  41159. EVP_PKEY* pkey;
  41160. const unsigned char* client_key =
  41161. (const unsigned char*)ecc_clikey_der_256;
  41162. unsigned char buf[FOURK_BUF];
  41163. unsigned char* pt = NULL;
  41164. int bufSz;
  41165. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  41166. sizeof_ecc_clikey_der_256)));
  41167. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  41168. pt = buf;
  41169. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  41170. AssertIntNE((pt - buf), 0);
  41171. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  41172. EVP_PKEY_free(pkey);
  41173. }
  41174. #endif
  41175. res = TEST_RES_CHECK(1);
  41176. #endif
  41177. return res;
  41178. }
  41179. static int test_wolfSSL_OCSP_id_get0_info(void)
  41180. {
  41181. int res = TEST_SKIPPED;
  41182. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  41183. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41184. X509* cert;
  41185. X509* issuer;
  41186. OCSP_CERTID* id;
  41187. OCSP_CERTID* id2;
  41188. ASN1_STRING* name = NULL;
  41189. ASN1_OBJECT* pmd = NULL;
  41190. ASN1_STRING* keyHash = NULL;
  41191. ASN1_INTEGER* serial = NULL;
  41192. ASN1_INTEGER* x509Int;
  41193. AssertNotNull(cert =
  41194. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  41195. AssertNotNull(issuer =
  41196. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  41197. id = OCSP_cert_to_id(NULL, cert, issuer);
  41198. AssertNotNull(id);
  41199. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  41200. AssertNotNull(id2);
  41201. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  41202. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  41203. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  41204. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  41205. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  41206. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  41207. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  41208. AssertNotNull(serial);
  41209. /* compare serial number to one in cert, should be equal */
  41210. x509Int = X509_get_serialNumber(cert);
  41211. AssertNotNull(x509Int);
  41212. AssertIntEQ(x509Int->length, serial->length);
  41213. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  41214. /* test OCSP_id_cmp */
  41215. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  41216. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  41217. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  41218. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  41219. id->issuerHash[0] = ~id->issuerHash[0];
  41220. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  41221. OCSP_CERTID_free(id);
  41222. OCSP_CERTID_free(id2);
  41223. X509_free(cert); /* free's x509Int */
  41224. X509_free(issuer);
  41225. res = TEST_RES_CHECK(1);
  41226. #endif
  41227. return res;
  41228. }
  41229. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  41230. {
  41231. int res = TEST_SKIPPED;
  41232. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  41233. WOLFSSL_OCSP_CERTID certId;
  41234. byte* targetBuffer;
  41235. byte* beginTargetBuffer;
  41236. /* OCSP CertID bytes taken from PCAP */
  41237. byte rawCertId[] = {
  41238. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  41239. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  41240. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  41241. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  41242. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  41243. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  41244. 0xcf, 0xbc, 0x91
  41245. };
  41246. int ret, i;
  41247. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  41248. certId.rawCertId = rawCertId;
  41249. certId.rawCertIdSize = sizeof(rawCertId);
  41250. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41251. beginTargetBuffer = targetBuffer;
  41252. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  41253. /* If target buffer is not null, function increments targetBuffer to point
  41254. just past the end of the encoded data. */
  41255. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  41256. /* Function returns the size of the encoded data. */
  41257. AssertIntEQ(ret, sizeof(rawCertId));
  41258. for (i = 0; i < ret; ++i)
  41259. {
  41260. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  41261. }
  41262. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41263. targetBuffer = NULL;
  41264. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  41265. /* If target buffer is null, function allocates memory for a buffer and
  41266. copies the encoded data into it. targetBuffer then points to the start of
  41267. this newly allocate buffer. */
  41268. AssertIntEQ(ret, sizeof(rawCertId));
  41269. for (i = 0; i < ret; ++i)
  41270. {
  41271. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  41272. }
  41273. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  41274. res = TEST_RES_CHECK(1);
  41275. #endif
  41276. return res;
  41277. }
  41278. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  41279. {
  41280. int res = TEST_SKIPPED;
  41281. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  41282. WOLFSSL_OCSP_CERTID* certId;
  41283. WOLFSSL_OCSP_CERTID* certIdBad;
  41284. const unsigned char* rawCertIdPtr;
  41285. const unsigned char rawCertId[] = {
  41286. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  41287. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  41288. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  41289. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  41290. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  41291. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  41292. 0xcf, 0xbc, 0x91
  41293. };
  41294. rawCertIdPtr = &rawCertId[0];
  41295. /* If the cert ID is NULL the function should allocate it and copy the
  41296. * data to it. */
  41297. certId = NULL;
  41298. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  41299. AssertNotNull(certId);
  41300. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  41301. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  41302. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  41303. /* If the cert ID is not NULL the function will just copy the data to it. */
  41304. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  41305. DYNAMIC_TYPE_TMP_BUFFER);
  41306. AssertNotNull(certId);
  41307. XMEMSET(certId, 0, sizeof(*certId));
  41308. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  41309. rawCertIdPtr = &rawCertId[0];
  41310. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  41311. AssertNotNull(certId);
  41312. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  41313. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  41314. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41315. /* The below tests should fail when passed bad parameters. NULL should
  41316. * always be returned. */
  41317. certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr, sizeof(rawCertId));
  41318. AssertNull(certIdBad);
  41319. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL, sizeof(rawCertId));
  41320. AssertNull(certIdBad);
  41321. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0);
  41322. AssertNull(certIdBad);
  41323. res = TEST_RES_CHECK(1);
  41324. #endif
  41325. return res;
  41326. }
  41327. static int test_wolfSSL_OCSP_id_cmp(void)
  41328. {
  41329. int res = TEST_SKIPPED;
  41330. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41331. OCSP_CERTID id1;
  41332. OCSP_CERTID id2;
  41333. XMEMSET(&id1, 0, sizeof(id1));
  41334. XMEMSET(&id2, 0, sizeof(id2));
  41335. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  41336. res = TEST_RES_CHECK(1);
  41337. #endif
  41338. return res;
  41339. }
  41340. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  41341. {
  41342. int res = TEST_SKIPPED;
  41343. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41344. WOLFSSL_OCSP_SINGLERESP single;
  41345. const WOLFSSL_OCSP_CERTID* certId;
  41346. XMEMSET(&single, 0, sizeof(single));
  41347. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  41348. AssertPtrEq(&single, certId);
  41349. res = TEST_RES_CHECK(1);
  41350. #endif
  41351. return res;
  41352. }
  41353. static int test_wolfSSL_OCSP_single_get0_status(void)
  41354. {
  41355. int res = TEST_SKIPPED;
  41356. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41357. WOLFSSL_OCSP_SINGLERESP single;
  41358. CertStatus certStatus;
  41359. WOLFSSL_ASN1_TIME* thisDate;
  41360. WOLFSSL_ASN1_TIME* nextDate;
  41361. int ret, i;
  41362. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41363. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  41364. /* Fill the date fields with some dummy data. */
  41365. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  41366. certStatus.thisDateParsed.data[i] = i;
  41367. certStatus.nextDateParsed.data[i] = i;
  41368. }
  41369. certStatus.status = CERT_GOOD;
  41370. single.status = &certStatus;
  41371. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  41372. &nextDate);
  41373. AssertIntEQ(ret, CERT_GOOD);
  41374. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  41375. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  41376. res = TEST_RES_CHECK(1);
  41377. #endif
  41378. return res;
  41379. }
  41380. static int test_wolfSSL_OCSP_resp_count(void)
  41381. {
  41382. int res = TEST_SKIPPED;
  41383. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41384. WOLFSSL_OCSP_BASICRESP basicResp;
  41385. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41386. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41387. int count;
  41388. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41389. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41390. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41391. count = wolfSSL_OCSP_resp_count(&basicResp);
  41392. AssertIntEQ(count, 0);
  41393. basicResp.single = &singleRespOne;
  41394. count = wolfSSL_OCSP_resp_count(&basicResp);
  41395. AssertIntEQ(count, 1);
  41396. singleRespOne.next = &singleRespTwo;
  41397. count = wolfSSL_OCSP_resp_count(&basicResp);
  41398. AssertIntEQ(count, 2);
  41399. res = TEST_RES_CHECK(1);
  41400. #endif
  41401. return res;
  41402. }
  41403. static int test_wolfSSL_OCSP_resp_get0(void)
  41404. {
  41405. int res = TEST_SKIPPED;
  41406. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41407. WOLFSSL_OCSP_BASICRESP basicResp;
  41408. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41409. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41410. WOLFSSL_OCSP_SINGLERESP* ret;
  41411. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41412. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41413. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41414. basicResp.single = &singleRespOne;
  41415. singleRespOne.next = &singleRespTwo;
  41416. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  41417. AssertPtrEq(ret, &singleRespOne);
  41418. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  41419. AssertPtrEq(ret, &singleRespTwo);
  41420. res = TEST_RES_CHECK(1);
  41421. #endif
  41422. return res;
  41423. }
  41424. static int test_wolfSSL_EVP_PKEY_derive(void)
  41425. {
  41426. int res = TEST_SKIPPED;
  41427. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41428. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  41429. EVP_PKEY_CTX *ctx;
  41430. unsigned char *skey;
  41431. size_t skeylen;
  41432. EVP_PKEY *pkey, *peerkey;
  41433. const unsigned char* key;
  41434. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  41435. /* DH */
  41436. key = dh_key_der_2048;
  41437. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41438. sizeof_dh_key_der_2048)));
  41439. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  41440. key = dh_key_der_2048;
  41441. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41442. sizeof_dh_key_der_2048)));
  41443. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  41444. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41445. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41446. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41447. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41448. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  41449. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41450. EVP_PKEY_CTX_free(ctx);
  41451. EVP_PKEY_free(peerkey);
  41452. EVP_PKEY_free(pkey);
  41453. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41454. #endif
  41455. #ifdef HAVE_ECC
  41456. /* ECDH */
  41457. key = ecc_clikey_der_256;
  41458. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  41459. sizeof_ecc_clikey_der_256)));
  41460. key = ecc_clikeypub_der_256;
  41461. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  41462. sizeof_ecc_clikeypub_der_256)));
  41463. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41464. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41465. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41466. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41467. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  41468. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41469. EVP_PKEY_CTX_free(ctx);
  41470. EVP_PKEY_free(peerkey);
  41471. EVP_PKEY_free(pkey);
  41472. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41473. #endif /* HAVE_ECC */
  41474. res = TEST_RES_CHECK(1);
  41475. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  41476. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41477. return res;
  41478. }
  41479. static int test_wolfSSL_EVP_PBE_scrypt(void)
  41480. {
  41481. int res = TEST_SKIPPED;
  41482. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  41483. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  41484. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  41485. int ret;
  41486. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  41487. int pwdlen = sizeof(pwd);
  41488. const byte salt[] = {'N','a','C','l'};
  41489. int saltlen = sizeof(salt);
  41490. byte key[80];
  41491. word64 numOvr32 = (word64)INT32_MAX + 1;
  41492. /* expected derived key for N:16, r:1, p:1 */
  41493. const byte expectedKey[] = {
  41494. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  41495. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  41496. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  41497. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  41498. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  41499. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  41500. 0xE7, 0xE9, 0xC0, 0x9A};
  41501. /* N r p mx key keylen */
  41502. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  41503. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  41504. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  41505. AssertIntEQ(ret, 0); /* N must be power of 2 */
  41506. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  41507. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  41508. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  41509. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  41510. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  41511. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  41512. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  41513. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  41514. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  41515. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  41516. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  41517. AssertIntEQ(ret, 1); /* should succeed */
  41518. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  41519. key, 64);
  41520. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  41521. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  41522. key, 64);
  41523. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  41524. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  41525. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  41526. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  41527. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  41528. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  41529. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  41530. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  41531. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  41532. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  41533. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  41534. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  41535. AssertIntEQ(ret, 1);
  41536. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  41537. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  41538. res = TEST_RES_CHECK(1);
  41539. #endif /* !NO_PWDBASED && !NO_SHA256 */
  41540. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  41541. return res;
  41542. }
  41543. static int test_no_op_functions(void)
  41544. {
  41545. int res = TEST_SKIPPED;
  41546. #if defined(OPENSSL_EXTRA)
  41547. /* this makes sure wolfSSL can compile and run these no-op functions */
  41548. SSL_load_error_strings();
  41549. ENGINE_load_builtin_engines();
  41550. OpenSSL_add_all_ciphers();
  41551. AssertIntEQ(CRYPTO_malloc_init(), 0);
  41552. res = TEST_RES_CHECK(1);
  41553. #endif
  41554. return res;
  41555. }
  41556. static int test_wolfSSL_CRYPTO_memcmp(void)
  41557. {
  41558. int res = TEST_SKIPPED;
  41559. #ifdef OPENSSL_EXTRA
  41560. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41561. "implementation of TLS/SSL for embedded devices to the cloud.";
  41562. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41563. "implementation of TLS/SSL for embedded devices to the cloud.";
  41564. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41565. "implementation of TLS/SSL for embedded devices to the cloud!";
  41566. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  41567. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  41568. res = TEST_RES_CHECK(1);
  41569. #endif
  41570. return res;
  41571. }
  41572. /*----------------------------------------------------------------------------*
  41573. | wolfCrypt ASN
  41574. *----------------------------------------------------------------------------*/
  41575. static int test_wc_CreateEncryptedPKCS8Key(void)
  41576. {
  41577. int res = TEST_SKIPPED;
  41578. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  41579. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  41580. WC_RNG rng;
  41581. byte* encKey = NULL;
  41582. word32 encKeySz = 0;
  41583. word32 decKeySz = 0;
  41584. const char password[] = "Lorem ipsum dolor sit amet";
  41585. word32 passwordSz = (word32)XSTRLEN(password);
  41586. word32 tradIdx = 0;
  41587. AssertIntEQ(wc_InitRng(&rng), 0);
  41588. /* Call with NULL for out buffer to get necessary length. */
  41589. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41590. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  41591. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41592. LENGTH_ONLY_E);
  41593. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  41594. DYNAMIC_TYPE_TMP_BUFFER));
  41595. /* Call with the allocated out buffer. */
  41596. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41597. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  41598. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41599. 0);
  41600. /* Decrypt the encrypted PKCS8 key we just made. */
  41601. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  41602. passwordSz)), 0);
  41603. /* encKey now holds the decrypted key (decrypted in place). */
  41604. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  41605. /* Check that the decrypted key matches the key prior to encryption. */
  41606. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  41607. sizeof_server_key_der_2048), 0);
  41608. if (encKey != NULL)
  41609. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41610. wc_FreeRng(&rng);
  41611. res = TEST_RES_CHECK(1);
  41612. #endif
  41613. return res;
  41614. }
  41615. static int test_wc_GetPkcs8TraditionalOffset(void)
  41616. {
  41617. int res = TEST_SKIPPED;
  41618. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  41619. int length, derSz;
  41620. word32 inOutIdx;
  41621. const char* path = "./certs/server-keyPkcs8.der";
  41622. XFILE file;
  41623. byte der[2048];
  41624. file = XFOPEN(path, "rb");
  41625. AssertTrue(file != XBADFILE);
  41626. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  41627. XFCLOSE(file);
  41628. /* valid case */
  41629. inOutIdx = 0;
  41630. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41631. AssertIntGT(length, 0);
  41632. /* inOutIdx > sz */
  41633. inOutIdx = 4000;
  41634. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41635. AssertIntEQ(length, BAD_FUNC_ARG);
  41636. /* null input */
  41637. inOutIdx = 0;
  41638. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  41639. AssertIntEQ(length, BAD_FUNC_ARG);
  41640. /* invalid input, fill buffer with 1's */
  41641. XMEMSET(der, 1, sizeof(der));
  41642. inOutIdx = 0;
  41643. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41644. AssertIntEQ(length, ASN_PARSE_E);
  41645. res = TEST_RES_CHECK(1);
  41646. #endif /* NO_ASN */
  41647. return res;
  41648. }
  41649. static int test_wc_SetSubjectRaw(void)
  41650. {
  41651. int res = TEST_SKIPPED;
  41652. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41653. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41654. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41655. WOLFSSL_X509* x509;
  41656. int peerCertSz;
  41657. const byte* peerCertBuf;
  41658. Cert forgedCert;
  41659. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41660. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41661. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41662. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  41663. wolfSSL_FreeX509(x509);
  41664. res = TEST_RES_CHECK(1);
  41665. #endif
  41666. return res;
  41667. }
  41668. static int test_wc_GetSubjectRaw(void)
  41669. {
  41670. int res = TEST_SKIPPED;
  41671. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41672. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  41673. Cert cert;
  41674. byte *subjectRaw;
  41675. AssertIntEQ(0, wc_InitCert(&cert));
  41676. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  41677. res = TEST_RES_CHECK(1);
  41678. #endif
  41679. return res;
  41680. }
  41681. static int test_wc_SetIssuerRaw(void)
  41682. {
  41683. int res = TEST_SKIPPED;
  41684. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41685. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41686. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41687. WOLFSSL_X509* x509;
  41688. int peerCertSz;
  41689. const byte* peerCertBuf;
  41690. Cert forgedCert;
  41691. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41692. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41693. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41694. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  41695. wolfSSL_FreeX509(x509);
  41696. res = TEST_RES_CHECK(1);
  41697. #endif
  41698. return res;
  41699. }
  41700. static int test_wc_SetIssueBuffer(void)
  41701. {
  41702. int res = TEST_SKIPPED;
  41703. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41704. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41705. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41706. WOLFSSL_X509* x509;
  41707. int peerCertSz;
  41708. const byte* peerCertBuf;
  41709. Cert forgedCert;
  41710. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41711. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41712. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41713. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  41714. wolfSSL_FreeX509(x509);
  41715. res = TEST_RES_CHECK(1);
  41716. #endif
  41717. return res;
  41718. }
  41719. /*
  41720. * Testing wc_SetSubjectKeyId
  41721. */
  41722. static int test_wc_SetSubjectKeyId(void)
  41723. {
  41724. int res = TEST_SKIPPED;
  41725. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41726. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41727. Cert cert;
  41728. const char* file = "certs/ecc-client-keyPub.pem";
  41729. AssertIntEQ(0, wc_InitCert(&cert));
  41730. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  41731. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  41732. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  41733. res = TEST_RES_CHECK(1);
  41734. #endif
  41735. return res;
  41736. } /* END test_wc_SetSubjectKeyId */
  41737. /*
  41738. * Testing wc_SetSubject
  41739. */
  41740. static int test_wc_SetSubject(void)
  41741. {
  41742. int res = TEST_SKIPPED;
  41743. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41744. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41745. Cert cert;
  41746. const char* file = "./certs/ca-ecc-cert.pem";
  41747. AssertIntEQ(0, wc_InitCert(&cert));
  41748. AssertIntEQ(0, wc_SetSubject(&cert, file));
  41749. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  41750. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  41751. res = TEST_RES_CHECK(1);
  41752. #endif
  41753. return res;
  41754. } /* END test_wc_SetSubject */
  41755. static int test_CheckCertSignature(void)
  41756. {
  41757. int res = TEST_SKIPPED;
  41758. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  41759. WOLFSSL_CERT_MANAGER* cm = NULL;
  41760. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41761. FILE* fp;
  41762. byte cert[4096];
  41763. int certSz;
  41764. #endif
  41765. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  41766. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41767. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  41768. #ifndef NO_RSA
  41769. #ifdef USE_CERT_BUFFERS_1024
  41770. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  41771. sizeof_server_cert_der_1024, NULL, cm));
  41772. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41773. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  41774. WOLFSSL_FILETYPE_ASN1));
  41775. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  41776. sizeof_server_cert_der_1024, NULL, cm));
  41777. #elif defined(USE_CERT_BUFFERS_2048)
  41778. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  41779. sizeof_server_cert_der_2048, NULL, cm));
  41780. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41781. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  41782. WOLFSSL_FILETYPE_ASN1));
  41783. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  41784. sizeof_server_cert_der_2048, NULL, cm));
  41785. #endif
  41786. #endif
  41787. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41788. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  41789. sizeof_serv_ecc_der_256, NULL, cm));
  41790. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41791. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  41792. WOLFSSL_FILETYPE_ASN1));
  41793. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  41794. NULL, cm));
  41795. #endif
  41796. #if !defined(NO_FILESYSTEM)
  41797. wolfSSL_CertManagerFree(cm);
  41798. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41799. #ifndef NO_RSA
  41800. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  41801. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41802. XFCLOSE(fp);
  41803. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41804. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41805. "./certs/ca-cert.pem", NULL));
  41806. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41807. #endif
  41808. #ifdef HAVE_ECC
  41809. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  41810. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41811. XFCLOSE(fp);
  41812. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41813. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41814. "./certs/ca-ecc-cert.pem", NULL));
  41815. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41816. #endif
  41817. #endif
  41818. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41819. (void)fp;
  41820. (void)cert;
  41821. (void)certSz;
  41822. #endif
  41823. wolfSSL_CertManagerFree(cm);
  41824. res = TEST_RES_CHECK(1);
  41825. #endif
  41826. return res;
  41827. }
  41828. static int test_wc_ParseCert(void)
  41829. {
  41830. int res = TEST_SKIPPED;
  41831. #if !defined(NO_CERTS) && !defined(NO_RSA)
  41832. DecodedCert decodedCert;
  41833. const byte* rawCert = client_cert_der_2048;
  41834. const int rawCertSize = sizeof_client_cert_der_2048;
  41835. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  41836. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41837. #ifndef IGNORE_NAME_CONSTRAINTS
  41838. /* check that the subjects emailAddress was not put in the alt name list */
  41839. AssertNotNull(decodedCert.subjectEmail);
  41840. AssertNull(decodedCert.altEmailNames);
  41841. #endif
  41842. wc_FreeDecodedCert(&decodedCert);
  41843. res = TEST_RES_CHECK(1);
  41844. #endif
  41845. return res;
  41846. }
  41847. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  41848. * the API safely errors out on badly-formed ASN input.
  41849. * NOTE: Test not compatible with released FIPS implementations!
  41850. */
  41851. static int test_wc_ParseCert_Error(void)
  41852. {
  41853. int res = TEST_SKIPPED;
  41854. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  41855. (!defined(HAVE_FIPS) || \
  41856. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  41857. DecodedCert decodedCert;
  41858. /* Certificate data */
  41859. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  41860. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  41861. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  41862. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  41863. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  41864. /* Test data */
  41865. const struct testStruct {
  41866. const byte* c;
  41867. const int cSz;
  41868. const int expRet;
  41869. } t[] = {
  41870. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  41871. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  41872. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  41873. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  41874. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  41875. };
  41876. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  41877. for (int i = 0; i < tSz; i++) {
  41878. WOLFSSL_MSG_EX("i == %d", i);
  41879. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  41880. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  41881. wc_FreeDecodedCert(&decodedCert);
  41882. }
  41883. res = TEST_RES_CHECK(1);
  41884. #endif
  41885. return res;
  41886. }
  41887. static int test_MakeCertWithPathLen(void)
  41888. {
  41889. int res = TEST_SKIPPED;
  41890. #if defined(WOLFSSL_CERT_REQ) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  41891. const byte expectedPathLen = 7;
  41892. Cert cert;
  41893. DecodedCert decodedCert;
  41894. byte der[FOURK_BUF];
  41895. int derSize = 0;
  41896. WC_RNG rng;
  41897. ecc_key key;
  41898. AssertIntEQ(wc_InitRng(&rng), 0);
  41899. AssertIntEQ(wc_ecc_init(&key), 0);
  41900. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  41901. AssertIntEQ(wc_InitCert(&cert), 0);
  41902. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  41903. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  41904. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  41905. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  41906. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  41907. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  41908. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  41909. cert.selfSigned = 1;
  41910. cert.isCA = 1;
  41911. cert.pathLen = expectedPathLen;
  41912. cert.pathLenSet = 1;
  41913. cert.sigType = CTC_SHA256wECDSA;
  41914. #ifdef WOLFSSL_CERT_EXT
  41915. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  41916. #endif
  41917. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  41918. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  41919. &key, &rng);
  41920. AssertIntGE(derSize, 0);
  41921. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  41922. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41923. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  41924. wc_FreeDecodedCert(&decodedCert);
  41925. AssertIntEQ(wc_ecc_free(&key), 0);
  41926. AssertIntEQ(wc_FreeRng(&rng), 0);
  41927. res = TEST_RES_CHECK(1);
  41928. #endif
  41929. return res;
  41930. }
  41931. /*----------------------------------------------------------------------------*
  41932. | wolfCrypt ECC
  41933. *----------------------------------------------------------------------------*/
  41934. static int test_wc_ecc_get_curve_size_from_name(void)
  41935. {
  41936. int res = TEST_SKIPPED;
  41937. #ifdef HAVE_ECC
  41938. int ret;
  41939. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41940. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  41941. AssertIntEQ(ret, 32);
  41942. #endif
  41943. /* invalid case */
  41944. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  41945. AssertIntEQ(ret, -1);
  41946. /* NULL input */
  41947. ret = wc_ecc_get_curve_size_from_name(NULL);
  41948. AssertIntEQ(ret, BAD_FUNC_ARG);
  41949. res = TEST_RES_CHECK(1);
  41950. #endif /* HAVE_ECC */
  41951. return res;
  41952. }
  41953. static int test_wc_ecc_get_curve_id_from_name(void)
  41954. {
  41955. int res = TEST_SKIPPED;
  41956. #ifdef HAVE_ECC
  41957. int id;
  41958. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41959. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41960. AssertIntEQ(id, ECC_SECP256R1);
  41961. #endif
  41962. /* invalid case */
  41963. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  41964. AssertIntEQ(id, -1);
  41965. /* NULL input */
  41966. id = wc_ecc_get_curve_id_from_name(NULL);
  41967. AssertIntEQ(id, BAD_FUNC_ARG);
  41968. res = TEST_RES_CHECK(1);
  41969. #endif /* HAVE_ECC */
  41970. return res;
  41971. }
  41972. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  41973. !defined(HAVE_SELFTEST) && \
  41974. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  41975. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  41976. {
  41977. int id;
  41978. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41979. int curve_id;
  41980. ecc_key* key;
  41981. const ecc_set_type* params;
  41982. int ret;
  41983. #endif
  41984. WOLFSSL_EC_KEY *ecKey = NULL;
  41985. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41986. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41987. AssertIntEQ(id, ECC_SECP256R1);
  41988. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  41989. AssertNotNull(ecKey);
  41990. ret = EC_KEY_generate_key(ecKey);
  41991. if (ret == 0) {
  41992. /* normal test */
  41993. key = (ecc_key*)ecKey->internal;
  41994. params = key->dp;
  41995. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  41996. AssertIntEQ(curve_id, id);
  41997. }
  41998. #endif
  41999. /* invalid case, NULL input*/
  42000. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  42001. AssertIntEQ(id, BAD_FUNC_ARG);
  42002. wolfSSL_EC_KEY_free(ecKey);
  42003. return TEST_RES_CHECK(1);
  42004. }
  42005. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  42006. static int test_wc_ecc_get_curve_id_from_params(void)
  42007. {
  42008. int res = TEST_SKIPPED;
  42009. #ifdef HAVE_ECC
  42010. int id;
  42011. const byte prime[] =
  42012. {
  42013. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  42014. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42015. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  42016. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  42017. };
  42018. const byte primeInvalid[] =
  42019. {
  42020. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  42021. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42022. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  42023. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  42024. };
  42025. const byte Af[] =
  42026. {
  42027. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  42028. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42029. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  42030. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  42031. };
  42032. const byte Bf[] =
  42033. {
  42034. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  42035. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  42036. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  42037. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  42038. };
  42039. const byte order[] =
  42040. {
  42041. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  42042. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  42043. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  42044. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  42045. };
  42046. const byte Gx[] =
  42047. {
  42048. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  42049. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  42050. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  42051. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  42052. };
  42053. const byte Gy[] =
  42054. {
  42055. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  42056. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  42057. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  42058. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  42059. };
  42060. int cofactor = 1;
  42061. int fieldSize = 256;
  42062. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  42063. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  42064. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  42065. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  42066. AssertIntEQ(id, ECC_SECP256R1);
  42067. #endif
  42068. /* invalid case, fieldSize = 0 */
  42069. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  42070. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  42071. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  42072. AssertIntEQ(id, ECC_CURVE_INVALID);
  42073. /* invalid case, NULL prime */
  42074. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  42075. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  42076. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  42077. AssertIntEQ(id, BAD_FUNC_ARG);
  42078. /* invalid case, invalid prime */
  42079. id = wc_ecc_get_curve_id_from_params(fieldSize,
  42080. primeInvalid, sizeof(primeInvalid),
  42081. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  42082. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  42083. AssertIntEQ(id, ECC_CURVE_INVALID);
  42084. res = TEST_RES_CHECK(1);
  42085. #endif
  42086. return res;
  42087. }
  42088. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  42089. {
  42090. int res = TEST_SKIPPED;
  42091. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42092. !defined(HAVE_FAST_RSA)
  42093. WOLFSSL_RSA* rsa = NULL;
  42094. WOLFSSL_EVP_PKEY* pkey = NULL;
  42095. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  42096. const char* in = "What is easy to do is easy not to do.";
  42097. size_t inlen = XSTRLEN(in);
  42098. size_t outEncLen = 0;
  42099. byte* outEnc = NULL;
  42100. byte* outDec = NULL;
  42101. size_t outDecLen = 0;
  42102. size_t rsaKeySz = 2048/8; /* Bytes */
  42103. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  42104. byte* inTmp = NULL;
  42105. byte* outEncTmp = NULL;
  42106. byte* outDecTmp = NULL;
  42107. #endif
  42108. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42109. XMEMSET(outEnc, 0, rsaKeySz);
  42110. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42111. XMEMSET(outDec, 0, rsaKeySz);
  42112. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  42113. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42114. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42115. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42116. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  42117. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42118. WOLFSSL_SUCCESS);
  42119. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  42120. since ctx uses it.*/
  42121. AssertIntEQ(pkey->ref.count, 2);
  42122. EVP_PKEY_free(pkey);
  42123. AssertIntEQ(pkey->ref.count, 1);
  42124. /* Encrypt data */
  42125. /* Check that we can get the required output buffer length by passing in a
  42126. * NULL output buffer. */
  42127. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  42128. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  42129. AssertIntEQ(rsaKeySz, outEncLen);
  42130. /* Now do the actual encryption. */
  42131. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  42132. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  42133. /* Decrypt data */
  42134. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  42135. /* Check that we can get the required output buffer length by passing in a
  42136. * NULL output buffer. */
  42137. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  42138. WOLFSSL_SUCCESS);
  42139. AssertIntEQ(rsaKeySz, outDecLen);
  42140. /* Now do the actual decryption. */
  42141. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  42142. WOLFSSL_SUCCESS);
  42143. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  42144. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  42145. /* The input length must be the same size as the RSA key.*/
  42146. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42147. XMEMSET(inTmp, 9, rsaKeySz);
  42148. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42149. XMEMSET(outEncTmp, 0, rsaKeySz);
  42150. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42151. XMEMSET(outDecTmp, 0, rsaKeySz);
  42152. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  42153. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  42154. WOLFSSL_SUCCESS);
  42155. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  42156. WOLFSSL_SUCCESS);
  42157. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  42158. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  42159. WOLFSSL_SUCCESS);
  42160. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  42161. #endif
  42162. EVP_PKEY_CTX_free(ctx);
  42163. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42164. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42165. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  42166. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42167. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42168. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42169. #endif
  42170. res = TEST_RES_CHECK(1);
  42171. #endif
  42172. return res;
  42173. }
  42174. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  42175. {
  42176. int res = TEST_SKIPPED;
  42177. #if defined(OPENSSL_EXTRA)
  42178. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42179. WOLFSSL_DSA* dsa = NULL;
  42180. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42181. WOLFSSL_EVP_PKEY* pkey = NULL;
  42182. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  42183. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  42184. const char* in = "What is easy to do is easy not to do.";
  42185. size_t inlen = XSTRLEN(in);
  42186. byte hash[SHA256_DIGEST_LENGTH] = {0};
  42187. byte zero[SHA256_DIGEST_LENGTH] = {0};
  42188. SHA256_CTX c;
  42189. byte* sig = NULL;
  42190. byte* sigVerify = NULL;
  42191. size_t siglen;
  42192. size_t siglenOnlyLen;
  42193. size_t keySz = 2048/8; /* Bytes */
  42194. int i;
  42195. int encs[3] = {0};
  42196. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42197. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42198. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42199. encs[0] = EVP_PKEY_RSA;
  42200. #endif
  42201. #endif
  42202. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42203. encs[1] = EVP_PKEY_DSA;
  42204. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42205. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42206. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42207. encs[2] = EVP_PKEY_EC;
  42208. #endif
  42209. #endif
  42210. AssertNotNull(sig =
  42211. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42212. AssertNotNull(sigVerify =
  42213. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42214. for (i = 0; i < 3; i++) {
  42215. if (encs[i] == 0)
  42216. continue;
  42217. siglen = keySz;
  42218. XMEMSET(sig, 0, keySz);
  42219. XMEMSET(sigVerify, 0, keySz);
  42220. /* Generate hash */
  42221. SHA256_Init(&c);
  42222. SHA256_Update(&c, in, inlen);
  42223. SHA256_Final(hash, &c);
  42224. #ifdef WOLFSSL_SMALL_STACK_CACHE
  42225. /* workaround for small stack cache case */
  42226. wc_Sha256Free((wc_Sha256*)&c);
  42227. #endif
  42228. /* Generate key */
  42229. AssertNotNull(pkey = EVP_PKEY_new());
  42230. switch (encs[i]) {
  42231. case EVP_PKEY_RSA:
  42232. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42233. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42234. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42235. {
  42236. WOLFSSL_RSA* rsa = NULL;
  42237. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  42238. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42239. }
  42240. #endif
  42241. #endif
  42242. break;
  42243. case EVP_PKEY_DSA:
  42244. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42245. AssertNotNull(dsa = DSA_new());
  42246. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  42247. NULL, 0, NULL, NULL, NULL), 1);
  42248. AssertIntEQ(DSA_generate_key(dsa), 1);
  42249. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  42250. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42251. break;
  42252. case EVP_PKEY_EC:
  42253. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42254. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42255. {
  42256. WOLFSSL_EC_KEY* ecKey = NULL;
  42257. AssertNotNull(ecKey = EC_KEY_new());
  42258. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  42259. AssertIntEQ(
  42260. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42261. }
  42262. #endif
  42263. #endif
  42264. break;
  42265. }
  42266. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42267. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42268. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42269. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42270. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42271. if (encs[i] == EVP_PKEY_RSA)
  42272. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42273. WOLFSSL_SUCCESS);
  42274. #endif
  42275. #endif
  42276. /* Check returning only length */
  42277. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  42278. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  42279. AssertIntGT(siglenOnlyLen, 0);
  42280. /* Sign data */
  42281. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  42282. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  42283. AssertIntGE(siglenOnlyLen, siglen);
  42284. /* Verify signature */
  42285. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  42286. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42287. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42288. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42289. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42290. if (encs[i] == EVP_PKEY_RSA)
  42291. AssertIntEQ(
  42292. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  42293. WOLFSSL_SUCCESS);
  42294. #endif
  42295. #endif
  42296. AssertIntEQ(EVP_PKEY_verify(
  42297. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  42298. WOLFSSL_SUCCESS);
  42299. AssertIntEQ(EVP_PKEY_verify(
  42300. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  42301. WOLFSSL_FAILURE);
  42302. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42303. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42304. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42305. if (encs[i] == EVP_PKEY_RSA) {
  42306. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  42307. /* Try RSA sign/verify with no padding. */
  42308. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42309. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  42310. WOLFSSL_SUCCESS);
  42311. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  42312. siglen), WOLFSSL_SUCCESS);
  42313. AssertIntGE(siglenOnlyLen, siglen);
  42314. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42315. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42316. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  42317. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  42318. siglen), WOLFSSL_SUCCESS);
  42319. #endif
  42320. /* Wrong padding schemes. */
  42321. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42322. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  42323. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  42324. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  42325. siglen), WOLFSSL_SUCCESS);
  42326. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42327. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42328. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  42329. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  42330. siglen), WOLFSSL_SUCCESS);
  42331. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42332. WOLFSSL_SUCCESS);
  42333. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42334. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  42335. }
  42336. #endif
  42337. #endif
  42338. /* error cases */
  42339. siglen = keySz; /* Reset because sig size may vary slightly */
  42340. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  42341. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42342. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  42343. WOLFSSL_SUCCESS);
  42344. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  42345. WOLFSSL_SUCCESS);
  42346. EVP_PKEY_free(pkey);
  42347. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42348. DSA_free(dsa);
  42349. dsa = NULL;
  42350. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42351. EVP_PKEY_CTX_free(ctx_verify);
  42352. EVP_PKEY_CTX_free(ctx);
  42353. }
  42354. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42355. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42356. res = TEST_RES_CHECK(1);
  42357. #endif /* OPENSSL_EXTRA */
  42358. return res;
  42359. }
  42360. static int test_EVP_PKEY_rsa(void)
  42361. {
  42362. int res = TEST_SKIPPED;
  42363. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  42364. WOLFSSL_RSA* rsa;
  42365. WOLFSSL_EVP_PKEY* pkey;
  42366. AssertNotNull(rsa = wolfSSL_RSA_new());
  42367. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42368. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  42369. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  42370. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42371. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  42372. wolfSSL_EVP_PKEY_free(pkey);
  42373. res = TEST_RES_CHECK(1);
  42374. #endif
  42375. return res;
  42376. }
  42377. static int test_EVP_PKEY_ec(void)
  42378. {
  42379. int res = TEST_SKIPPED;
  42380. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42381. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42382. WOLFSSL_EC_KEY* ecKey;
  42383. WOLFSSL_EVP_PKEY* pkey;
  42384. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  42385. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42386. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42387. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42388. /* Should fail since ecKey is empty */
  42389. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42390. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42391. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42392. wolfSSL_EVP_PKEY_free(pkey);
  42393. res = TEST_RES_CHECK(1);
  42394. #endif
  42395. #endif
  42396. return res;
  42397. }
  42398. static int test_EVP_PKEY_cmp(void)
  42399. {
  42400. int res = TEST_SKIPPED;
  42401. #if defined(OPENSSL_EXTRA)
  42402. EVP_PKEY *a, *b;
  42403. const unsigned char *in;
  42404. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  42405. in = client_key_der_2048;
  42406. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42407. &in, (long)sizeof_client_key_der_2048));
  42408. in = client_key_der_2048;
  42409. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42410. &in, (long)sizeof_client_key_der_2048));
  42411. /* Test success case RSA */
  42412. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42413. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  42414. #else
  42415. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42416. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42417. EVP_PKEY_free(b);
  42418. EVP_PKEY_free(a);
  42419. #endif
  42420. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42421. in = ecc_clikey_der_256;
  42422. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42423. &in, (long)sizeof_ecc_clikey_der_256));
  42424. in = ecc_clikey_der_256;
  42425. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42426. &in, (long)sizeof_ecc_clikey_der_256));
  42427. /* Test success case ECC */
  42428. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42429. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  42430. #else
  42431. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42432. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42433. EVP_PKEY_free(b);
  42434. EVP_PKEY_free(a);
  42435. #endif
  42436. /* Test failure cases */
  42437. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  42438. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42439. in = client_key_der_2048;
  42440. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42441. &in, (long)sizeof_client_key_der_2048));
  42442. in = ecc_clikey_der_256;
  42443. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42444. &in, (long)sizeof_ecc_clikey_der_256));
  42445. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42446. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  42447. #else
  42448. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  42449. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42450. EVP_PKEY_free(b);
  42451. EVP_PKEY_free(a);
  42452. #endif
  42453. /* invalid or empty failure cases */
  42454. a = EVP_PKEY_new();
  42455. b = EVP_PKEY_new();
  42456. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42457. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  42458. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  42459. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  42460. #ifdef NO_RSA
  42461. /* Type check will fail since RSA is the default EVP key type */
  42462. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  42463. #else
  42464. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42465. #endif
  42466. #else
  42467. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  42468. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  42469. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  42470. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  42471. #endif
  42472. EVP_PKEY_free(b);
  42473. EVP_PKEY_free(a);
  42474. (void)in;
  42475. res = TEST_RES_CHECK(1);
  42476. #endif
  42477. return res;
  42478. }
  42479. static int test_ERR_load_crypto_strings(void)
  42480. {
  42481. int res = TEST_SKIPPED;
  42482. #if defined(OPENSSL_ALL)
  42483. ERR_load_crypto_strings();
  42484. res = TEST_RES_CHECK(1);
  42485. #endif
  42486. return res;
  42487. }
  42488. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42489. static void free_x509(X509* x)
  42490. {
  42491. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  42492. }
  42493. #endif
  42494. static int test_sk_X509(void)
  42495. {
  42496. int res = TEST_SKIPPED;
  42497. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42498. {
  42499. STACK_OF(X509)* s;
  42500. AssertNotNull(s = sk_X509_new_null());
  42501. AssertIntEQ(sk_X509_num(s), 0);
  42502. sk_X509_pop_free(s, NULL);
  42503. AssertNotNull(s = sk_X509_new_null());
  42504. AssertIntEQ(sk_X509_num(s), 0);
  42505. sk_X509_pop_free(s, NULL);
  42506. AssertNotNull(s = sk_X509_new_null());
  42507. sk_X509_push(s, (X509*)1);
  42508. AssertIntEQ(sk_X509_num(s), 1);
  42509. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  42510. sk_X509_push(s, (X509*)2);
  42511. AssertIntEQ(sk_X509_num(s), 2);
  42512. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  42513. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  42514. sk_X509_push(s, (X509*)2);
  42515. sk_X509_pop_free(s, free_x509);
  42516. }
  42517. {
  42518. /* Push a list of 10 X509s onto stack, then verify that
  42519. * value(), push(), shift(), and pop() behave as expected. */
  42520. STACK_OF(X509)* s;
  42521. X509* xList[10];
  42522. int i = 0;
  42523. const int len = (sizeof(xList) / sizeof(xList[0]));
  42524. for (i = 0; i < len; ++i)
  42525. AssertNotNull(xList[i] = X509_new());
  42526. /* test push, pop, and free */
  42527. AssertNotNull(s = sk_X509_new_null());
  42528. for (i = 0; i < len; ++i) {
  42529. sk_X509_push(s, xList[i]);
  42530. AssertIntEQ(sk_X509_num(s), i + 1);
  42531. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42532. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42533. }
  42534. /* pop returns and removes last pushed on stack, which is index 0
  42535. * in sk_x509_value */
  42536. for (i = 0; i < len; ++i) {
  42537. X509 * x = sk_X509_value(s, 0);
  42538. X509 * y = sk_X509_pop(s);
  42539. X509 * z = xList[len - 1 - i];
  42540. AssertIntEQ((x == y), 1);
  42541. AssertIntEQ((x == z), 1);
  42542. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  42543. }
  42544. sk_free(s);
  42545. /* test push, shift, and free */
  42546. AssertNotNull(s = sk_X509_new_null());
  42547. for (i = 0; i < len; ++i) {
  42548. sk_X509_push(s, xList[i]);
  42549. AssertIntEQ(sk_X509_num(s), i + 1);
  42550. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42551. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42552. }
  42553. /* shift returns and removes first pushed on stack, which is index i
  42554. * in sk_x509_value() */
  42555. for (i = 0; i < len; ++i) {
  42556. X509 * x = sk_X509_value(s, len - 1 - i);
  42557. X509 * y = sk_X509_shift(s);
  42558. X509 * z = xList[i];
  42559. AssertIntEQ((x == y), 1);
  42560. AssertIntEQ((x == z), 1);
  42561. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  42562. }
  42563. sk_free(s);
  42564. for (i = 0; i < len; ++i)
  42565. X509_free(xList[i]);
  42566. }
  42567. res = TEST_RES_CHECK(1);
  42568. #endif
  42569. return res;
  42570. }
  42571. static int test_sk_X509_CRL(void)
  42572. {
  42573. int res = TEST_SKIPPED;
  42574. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  42575. X509_CRL* crl;
  42576. XFILE fp;
  42577. STACK_OF(X509_CRL)* s;
  42578. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  42579. AssertTrue((fp != XBADFILE));
  42580. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  42581. XFCLOSE(fp);
  42582. AssertNotNull(s = sk_X509_CRL_new());
  42583. AssertIntEQ(sk_X509_CRL_num(s), 0);
  42584. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  42585. AssertIntEQ(sk_X509_CRL_num(s), 1);
  42586. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  42587. sk_X509_CRL_free(s);
  42588. res = TEST_RES_CHECK(1);
  42589. #endif
  42590. return res;
  42591. }
  42592. static int test_X509_get_signature_nid(void)
  42593. {
  42594. int res = TEST_SKIPPED;
  42595. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42596. X509* x509;
  42597. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  42598. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42599. SSL_FILETYPE_PEM));
  42600. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  42601. X509_free(x509);
  42602. res = TEST_RES_CHECK(1);
  42603. #endif
  42604. return res;
  42605. }
  42606. static int test_X509_REQ(void)
  42607. {
  42608. int res = TEST_SKIPPED;
  42609. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  42610. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  42611. X509_NAME* name;
  42612. #ifndef NO_RSA
  42613. X509_NAME* subject;
  42614. #endif
  42615. #if !defined(NO_RSA) || defined(HAVE_ECC)
  42616. X509_REQ* req;
  42617. EVP_PKEY* priv;
  42618. EVP_PKEY* pub;
  42619. unsigned char* der = NULL;
  42620. int len;
  42621. #endif
  42622. #ifndef NO_RSA
  42623. EVP_MD_CTX *mctx = NULL;
  42624. EVP_PKEY_CTX *pkctx = NULL;
  42625. #ifdef USE_CERT_BUFFERS_1024
  42626. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  42627. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  42628. #elif defined(USE_CERT_BUFFERS_2048)
  42629. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  42630. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  42631. #endif
  42632. #endif
  42633. #ifdef HAVE_ECC
  42634. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  42635. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  42636. #endif
  42637. AssertNotNull(name = X509_NAME_new());
  42638. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  42639. (byte*)"wolfssl.com", 11, 0, 1),
  42640. WOLFSSL_SUCCESS);
  42641. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  42642. (byte*)"support@wolfssl.com", 19, -1,
  42643. 1), WOLFSSL_SUCCESS);
  42644. #ifndef NO_RSA
  42645. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  42646. (long)sizeof_client_key_der_2048));
  42647. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  42648. (long)sizeof_client_keypub_der_2048));
  42649. AssertNotNull(req = X509_REQ_new());
  42650. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  42651. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  42652. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42653. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  42654. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  42655. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42656. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  42657. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  42658. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  42659. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42660. len = i2d_X509_REQ(req, &der);
  42661. DEBUG_WRITE_DER(der, len, "req.der");
  42662. #ifdef USE_CERT_BUFFERS_1024
  42663. AssertIntEQ(len, 381);
  42664. #else
  42665. AssertIntEQ(len, 643);
  42666. #endif
  42667. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42668. der = NULL;
  42669. mctx = EVP_MD_CTX_new();
  42670. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  42671. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  42672. EVP_MD_CTX_free(mctx);
  42673. X509_REQ_free(NULL);
  42674. X509_REQ_free(req);
  42675. /* Test getting the subject from a newly created X509_REQ */
  42676. AssertNotNull(req = X509_REQ_new());
  42677. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  42678. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  42679. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  42680. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  42681. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  42682. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  42683. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  42684. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  42685. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  42686. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  42687. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  42688. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  42689. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  42690. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42691. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42692. len = i2d_X509_REQ(req, &der);
  42693. DEBUG_WRITE_DER(der, len, "req2.der");
  42694. #ifdef USE_CERT_BUFFERS_1024
  42695. AssertIntEQ(len, 435);
  42696. #else
  42697. AssertIntEQ(len, 696);
  42698. #endif
  42699. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42700. der = NULL;
  42701. EVP_PKEY_free(pub);
  42702. EVP_PKEY_free(priv);
  42703. X509_REQ_free(req);
  42704. #endif
  42705. #ifdef HAVE_ECC
  42706. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  42707. sizeof_ecc_clikey_der_256));
  42708. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  42709. sizeof_ecc_clikeypub_der_256));
  42710. AssertNotNull(req = X509_REQ_new());
  42711. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42712. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42713. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42714. /* Signature is random and may be shorter or longer. */
  42715. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  42716. AssertIntLE(len, 253);
  42717. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42718. X509_REQ_free(req);
  42719. EVP_PKEY_free(pub);
  42720. EVP_PKEY_free(priv);
  42721. #ifdef FP_ECC
  42722. wc_ecc_fp_free();
  42723. #endif
  42724. #endif /* HAVE_ECC */
  42725. X509_NAME_free(name);
  42726. res = TEST_RES_CHECK(1);
  42727. #endif
  42728. return res;
  42729. }
  42730. static int test_wolfssl_PKCS7(void)
  42731. {
  42732. int res = TEST_SKIPPED;
  42733. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42734. !defined(NO_RSA)
  42735. PKCS7* pkcs7;
  42736. byte data[FOURK_BUF];
  42737. word32 len = sizeof(data);
  42738. const byte* p = data;
  42739. byte content[] = "Test data to encode.";
  42740. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42741. BIO* bio;
  42742. byte key[sizeof(client_key_der_2048)];
  42743. word32 keySz = (word32)sizeof(key);
  42744. byte* out = NULL;
  42745. #endif
  42746. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  42747. (word32)sizeof(content),
  42748. 0, 0, 0, RSA_TYPE)), 0);
  42749. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  42750. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  42751. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42752. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  42753. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  42754. PKCS7_free(pkcs7);
  42755. /* fail case, without PKCS7_NOVERIFY */
  42756. p = data;
  42757. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42758. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42759. 0), WOLFSSL_FAILURE);
  42760. PKCS7_free(pkcs7);
  42761. /* success case, with PKCS7_NOVERIFY */
  42762. p = data;
  42763. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42764. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42765. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  42766. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42767. /* test i2d */
  42768. XMEMCPY(key, client_key_der_2048, keySz);
  42769. pkcs7->privateKey = key;
  42770. pkcs7->privateKeySz = (word32)sizeof(key);
  42771. pkcs7->encryptOID = RSAk;
  42772. #ifdef NO_SHA
  42773. pkcs7->hashOID = SHA256h;
  42774. #else
  42775. pkcs7->hashOID = SHAh;
  42776. #endif
  42777. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  42778. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  42779. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  42780. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42781. BIO_free(bio);
  42782. #endif
  42783. PKCS7_free(NULL);
  42784. PKCS7_free(pkcs7);
  42785. res = TEST_RES_CHECK(1);
  42786. #endif
  42787. return res;
  42788. }
  42789. static int test_wolfSSL_PKCS7_sign(void)
  42790. {
  42791. int res = TEST_SKIPPED;
  42792. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42793. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42794. PKCS7* p7 = NULL;
  42795. PKCS7* p7Ver = NULL;
  42796. byte* out = NULL;
  42797. byte* tmpPtr = NULL;
  42798. int outLen = 0;
  42799. int flags = 0;
  42800. byte data[] = "Test data to encode.";
  42801. const char* cert = "./certs/server-cert.pem";
  42802. const char* key = "./certs/server-key.pem";
  42803. const char* ca = "./certs/ca-cert.pem";
  42804. WOLFSSL_BIO* certBio = NULL;
  42805. WOLFSSL_BIO* keyBio = NULL;
  42806. WOLFSSL_BIO* caBio = NULL;
  42807. WOLFSSL_BIO* inBio = NULL;
  42808. X509* signCert = NULL;
  42809. EVP_PKEY* signKey = NULL;
  42810. X509* caCert = NULL;
  42811. X509_STORE* store = NULL;
  42812. /* read signer cert/key into BIO */
  42813. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42814. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42815. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42816. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42817. /* read CA cert into store (for verify) */
  42818. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42819. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42820. AssertNotNull(store = X509_STORE_new());
  42821. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42822. /* data to be signed into BIO */
  42823. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42824. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42825. /* PKCS7_sign, bad args: signer NULL */
  42826. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  42827. /* PKCS7_sign, bad args: signer key NULL */
  42828. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  42829. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  42830. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  42831. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  42832. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  42833. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  42834. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  42835. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  42836. {
  42837. flags = PKCS7_BINARY;
  42838. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42839. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42840. /* verify with d2i_PKCS7 */
  42841. tmpPtr = out;
  42842. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42843. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42844. PKCS7_free(p7Ver);
  42845. /* verify with wc_PKCS7_VerifySignedData */
  42846. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42847. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  42848. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42849. /* compare the signer found to expected signer */
  42850. AssertIntNE(p7Ver->verifyCertSz, 0);
  42851. tmpPtr = NULL;
  42852. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  42853. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  42854. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42855. tmpPtr = NULL;
  42856. wc_PKCS7_Free(p7Ver);
  42857. AssertNotNull(out);
  42858. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42859. out = NULL;
  42860. PKCS7_free(p7);
  42861. }
  42862. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  42863. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  42864. {
  42865. /* re-populate input BIO, may have been consumed */
  42866. BIO_free(inBio);
  42867. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42868. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42869. flags = PKCS7_BINARY | PKCS7_STREAM;
  42870. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42871. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42872. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42873. /* PKCS7_final, bad args: PKCS7 null */
  42874. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  42875. /* PKCS7_final, bad args: PKCS7 null */
  42876. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  42877. tmpPtr = out;
  42878. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42879. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42880. PKCS7_free(p7Ver);
  42881. AssertNotNull(out);
  42882. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42883. out = NULL;
  42884. PKCS7_free(p7);
  42885. }
  42886. /* TEST SUCCESS: Detached, not streaming, not MIME */
  42887. {
  42888. /* re-populate input BIO, may have been consumed */
  42889. BIO_free(inBio);
  42890. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42891. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42892. flags = PKCS7_BINARY | PKCS7_DETACHED;
  42893. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42894. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42895. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42896. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42897. p7Ver->content = data;
  42898. p7Ver->contentSz = sizeof(data);
  42899. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42900. wc_PKCS7_Free(p7Ver);
  42901. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  42902. * yet support detached content */
  42903. tmpPtr = out;
  42904. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42905. PKCS7_free(p7Ver);
  42906. AssertNotNull(out);
  42907. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42908. out = NULL;
  42909. PKCS7_free(p7);
  42910. }
  42911. /* TEST SUCCESS: Detached, streaming, not MIME */
  42912. {
  42913. /* re-populate input BIO, may have been consumed */
  42914. BIO_free(inBio);
  42915. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42916. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42917. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  42918. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42919. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42920. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42921. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42922. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42923. p7Ver->content = data;
  42924. p7Ver->contentSz = sizeof(data);
  42925. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42926. wc_PKCS7_Free(p7Ver);
  42927. AssertNotNull(out);
  42928. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42929. PKCS7_free(p7);
  42930. }
  42931. X509_STORE_free(store);
  42932. X509_free(caCert);
  42933. X509_free(signCert);
  42934. EVP_PKEY_free(signKey);
  42935. BIO_free(inBio);
  42936. BIO_free(keyBio);
  42937. BIO_free(certBio);
  42938. BIO_free(caBio);
  42939. res = TEST_RES_CHECK(1);
  42940. #endif
  42941. return res;
  42942. }
  42943. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  42944. {
  42945. int res = TEST_SKIPPED;
  42946. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  42947. PKCS7_SIGNED* pkcs7;
  42948. pkcs7 = PKCS7_SIGNED_new();
  42949. AssertNotNull(pkcs7);
  42950. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  42951. PKCS7_SIGNED_free(pkcs7);
  42952. res = TEST_RES_CHECK(1);
  42953. #endif
  42954. return res;
  42955. }
  42956. #ifndef NO_BIO
  42957. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  42958. {
  42959. int res = TEST_SKIPPED;
  42960. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  42961. PKCS7* pkcs7 = NULL;
  42962. BIO* bio = NULL;
  42963. const byte* cert_buf = NULL;
  42964. int ret = 0;
  42965. WC_RNG rng;
  42966. const byte data[] = { /* Hello World */
  42967. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42968. 0x72,0x6c,0x64
  42969. };
  42970. #ifndef NO_RSA
  42971. #if defined(USE_CERT_BUFFERS_2048)
  42972. byte key[sizeof(client_key_der_2048)];
  42973. byte cert[sizeof(client_cert_der_2048)];
  42974. word32 keySz = (word32)sizeof(key);
  42975. word32 certSz = (word32)sizeof(cert);
  42976. XMEMSET(key, 0, keySz);
  42977. XMEMSET(cert, 0, certSz);
  42978. XMEMCPY(key, client_key_der_2048, keySz);
  42979. XMEMCPY(cert, client_cert_der_2048, certSz);
  42980. #elif defined(USE_CERT_BUFFERS_1024)
  42981. byte key[sizeof_client_key_der_1024];
  42982. byte cert[sizeof(sizeof_client_cert_der_1024)];
  42983. word32 keySz = (word32)sizeof(key);
  42984. word32 certSz = (word32)sizeof(cert);
  42985. XMEMSET(key, 0, keySz);
  42986. XMEMSET(cert, 0, certSz);
  42987. XMEMCPY(key, client_key_der_1024, keySz);
  42988. XMEMCPY(cert, client_cert_der_1024, certSz);
  42989. #else
  42990. unsigned char cert[ONEK_BUF];
  42991. unsigned char key[ONEK_BUF];
  42992. XFILE fp;
  42993. int certSz;
  42994. int keySz;
  42995. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  42996. AssertTrue((fp != XBADFILE));
  42997. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  42998. XFCLOSE(fp);
  42999. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  43000. AssertTrue(fp != XBADFILE);
  43001. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  43002. XFCLOSE(fp);
  43003. #endif
  43004. #elif defined(HAVE_ECC)
  43005. #if defined(USE_CERT_BUFFERS_256)
  43006. unsigned char cert[sizeof(cliecc_cert_der_256)];
  43007. unsigned char key[sizeof(ecc_clikey_der_256)];
  43008. int certSz = (int)sizeof(cert);
  43009. int keySz = (int)sizeof(key);
  43010. XMEMSET(cert, 0, certSz);
  43011. XMEMSET(key, 0, keySz);
  43012. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  43013. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  43014. #else
  43015. unsigned char cert[ONEK_BUF];
  43016. unsigned char key[ONEK_BUF];
  43017. XFILE fp;
  43018. int certSz, keySz;
  43019. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  43020. AssertTrue(fp != XBADFILE);
  43021. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  43022. XFCLOSE(fp);
  43023. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  43024. AssertTrue(fp != XBADFILE);
  43025. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  43026. XFCLOSE(fp);
  43027. #endif
  43028. #else
  43029. #error PKCS7 requires ECC or RSA
  43030. #endif
  43031. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  43032. /* initialize with DER encoded cert */
  43033. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  43034. /* init rng */
  43035. AssertIntEQ(wc_InitRng(&rng), 0);
  43036. pkcs7->rng = &rng;
  43037. pkcs7->content = (byte*)data; /* not used for ex */
  43038. pkcs7->contentSz = (word32)sizeof(data);
  43039. pkcs7->contentOID = SIGNED_DATA;
  43040. pkcs7->privateKey = key;
  43041. pkcs7->privateKeySz = (word32)sizeof(key);
  43042. pkcs7->encryptOID = RSAk;
  43043. #ifdef NO_SHA
  43044. pkcs7->hashOID = SHA256h;
  43045. #else
  43046. pkcs7->hashOID = SHAh;
  43047. #endif
  43048. pkcs7->signedAttribs = NULL;
  43049. pkcs7->signedAttribsSz = 0;
  43050. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  43051. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  43052. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  43053. /* Read PKCS#7 PEM from BIO */
  43054. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  43055. AssertIntGE(ret, 0);
  43056. BIO_free(bio);
  43057. wc_PKCS7_Free(pkcs7);
  43058. wc_FreeRng(&rng);
  43059. res = TEST_RES_CHECK(1);
  43060. #endif
  43061. return res;
  43062. }
  43063. #ifdef HAVE_SMIME
  43064. static int test_wolfSSL_SMIME_read_PKCS7(void)
  43065. {
  43066. int res = TEST_SKIPPED;
  43067. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  43068. !defined(NO_RSA)
  43069. PKCS7* pkcs7 = NULL;
  43070. BIO* bio = NULL;
  43071. BIO* bcont = NULL;
  43072. BIO* out = NULL;
  43073. const byte* outBuf = NULL;
  43074. int outBufLen = 0;
  43075. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  43076. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  43077. /* smime-test.p7s */
  43078. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  43079. AssertNotNull(bio);
  43080. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43081. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43082. AssertNotNull(pkcs7);
  43083. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43084. PKCS7_NOVERIFY), SSL_SUCCESS);
  43085. XFCLOSE(smimeTestFile);
  43086. if (bcont) BIO_free(bcont);
  43087. wolfSSL_PKCS7_free(pkcs7);
  43088. /* smime-test-multipart.p7s */
  43089. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  43090. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43091. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43092. AssertNotNull(pkcs7);
  43093. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43094. PKCS7_NOVERIFY), SSL_SUCCESS);
  43095. XFCLOSE(smimeTestFile);
  43096. if (bcont) BIO_free(bcont);
  43097. wolfSSL_PKCS7_free(pkcs7);
  43098. /* smime-test-multipart-badsig.p7s */
  43099. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  43100. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43101. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43102. AssertNull(pkcs7);
  43103. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43104. PKCS7_NOVERIFY), SSL_FAILURE);
  43105. XFCLOSE(smimeTestFile);
  43106. if (bcont) BIO_free(bcont);
  43107. wolfSSL_PKCS7_free(pkcs7);
  43108. /* smime-test-canon.p7s */
  43109. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  43110. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43111. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43112. AssertNotNull(pkcs7);
  43113. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43114. PKCS7_NOVERIFY), SSL_SUCCESS);
  43115. XFCLOSE(smimeTestFile);
  43116. if (bcont) BIO_free(bcont);
  43117. wolfSSL_PKCS7_free(pkcs7);
  43118. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  43119. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  43120. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43121. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43122. AssertNotNull(pkcs7);
  43123. out = wolfSSL_BIO_new(BIO_s_mem());
  43124. AssertNotNull(out);
  43125. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  43126. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  43127. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  43128. /* Content-Type should not show up at beginning of output buffer */
  43129. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  43130. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  43131. BIO_free(out);
  43132. BIO_free(bio);
  43133. if (bcont) BIO_free(bcont);
  43134. wolfSSL_PKCS7_free(pkcs7);
  43135. res = TEST_RES_CHECK(1);
  43136. #endif
  43137. return res;
  43138. }
  43139. static int test_wolfSSL_SMIME_write_PKCS7(void)
  43140. {
  43141. int res = TEST_SKIPPED;
  43142. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  43143. PKCS7* p7 = NULL;
  43144. PKCS7* p7Ver = NULL;
  43145. int flags = 0;
  43146. byte data[] = "Test data to encode.";
  43147. const char* cert = "./certs/server-cert.pem";
  43148. const char* key = "./certs/server-key.pem";
  43149. const char* ca = "./certs/ca-cert.pem";
  43150. WOLFSSL_BIO* certBio = NULL;
  43151. WOLFSSL_BIO* keyBio = NULL;
  43152. WOLFSSL_BIO* caBio = NULL;
  43153. WOLFSSL_BIO* inBio = NULL;
  43154. WOLFSSL_BIO* outBio = NULL;
  43155. WOLFSSL_BIO* content = NULL;
  43156. X509* signCert = NULL;
  43157. EVP_PKEY* signKey = NULL;
  43158. X509* caCert = NULL;
  43159. X509_STORE* store = NULL;
  43160. /* read signer cert/key into BIO */
  43161. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  43162. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  43163. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  43164. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  43165. /* read CA cert into store (for verify) */
  43166. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  43167. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  43168. AssertNotNull(store = X509_STORE_new());
  43169. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  43170. /* generate and verify SMIME: not detached */
  43171. {
  43172. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  43173. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43174. flags = PKCS7_STREAM;
  43175. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43176. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  43177. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43178. /* bad arg: out NULL */
  43179. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  43180. /* bad arg: pkcs7 NULL */
  43181. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  43182. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43183. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  43184. BIO_free(content);
  43185. BIO_free(inBio);
  43186. BIO_free(outBio);
  43187. PKCS7_free(p7Ver);
  43188. PKCS7_free(p7);
  43189. }
  43190. /* generate and verify SMIME: not detached, add Content-Type */
  43191. {
  43192. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  43193. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43194. flags = PKCS7_STREAM | PKCS7_TEXT;
  43195. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43196. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  43197. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43198. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43199. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  43200. BIO_free(content);
  43201. BIO_free(inBio);
  43202. BIO_free(outBio);
  43203. PKCS7_free(p7Ver);
  43204. PKCS7_free(p7);
  43205. }
  43206. /* generate and verify SMIME: detached */
  43207. {
  43208. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  43209. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43210. flags = PKCS7_DETACHED | PKCS7_STREAM;
  43211. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43212. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  43213. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43214. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43215. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  43216. BIO_free(content);
  43217. BIO_free(inBio);
  43218. BIO_free(outBio);
  43219. PKCS7_free(p7Ver);
  43220. PKCS7_free(p7);
  43221. }
  43222. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  43223. {
  43224. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  43225. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43226. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  43227. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43228. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  43229. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43230. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43231. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  43232. BIO_free(content);
  43233. BIO_free(inBio);
  43234. BIO_free(outBio);
  43235. PKCS7_free(p7Ver);
  43236. PKCS7_free(p7);
  43237. }
  43238. X509_STORE_free(store);
  43239. X509_free(caCert);
  43240. X509_free(signCert);
  43241. EVP_PKEY_free(signKey);
  43242. BIO_free(keyBio);
  43243. BIO_free(certBio);
  43244. BIO_free(caBio);
  43245. res = TEST_RES_CHECK(1);
  43246. #endif
  43247. return res;
  43248. }
  43249. #endif /* HAVE_SMIME */
  43250. #endif /* !NO_BIO */
  43251. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  43252. * returns 0) */
  43253. static int test_X509_STORE_No_SSL_CTX(void)
  43254. {
  43255. int res = TEST_SKIPPED;
  43256. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  43257. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  43258. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  43259. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  43260. !defined(NO_RSA)
  43261. X509_STORE * store;
  43262. X509_STORE_CTX * storeCtx;
  43263. X509_CRL * crl;
  43264. X509 * ca;
  43265. X509 * cert;
  43266. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  43267. const char srvCert[] = "./certs/server-cert.pem";
  43268. const char caCert[] = "./certs/ca-cert.pem";
  43269. const char caDir[] = "./certs/crl/hash_pem";
  43270. XFILE fp;
  43271. X509_LOOKUP * lookup;
  43272. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  43273. /* Set up store with CA */
  43274. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43275. SSL_FILETYPE_PEM)));
  43276. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43277. /* Add CRL lookup directory to store
  43278. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  43279. * of crl.pem */
  43280. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  43281. X509_LOOKUP_hash_dir())));
  43282. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  43283. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  43284. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43285. SSL_SUCCESS);
  43286. /* Add CRL to store NOT containing the verified certificate, which
  43287. * forces use of the CRL lookup directory */
  43288. fp = XFOPEN(cliCrlPem, "rb");
  43289. AssertTrue((fp != XBADFILE));
  43290. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43291. NULL, NULL));
  43292. XFCLOSE(fp);
  43293. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43294. /* Create verification context outside of an SSL session */
  43295. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  43296. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43297. SSL_FILETYPE_PEM)));
  43298. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43299. /* Perform verification, which should NOT indicate CRL missing due to the
  43300. * store CM's X509 store pointer being NULL */
  43301. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  43302. X509_CRL_free(crl);
  43303. X509_STORE_free(store);
  43304. X509_STORE_CTX_free(storeCtx);
  43305. X509_free(cert);
  43306. X509_free(ca);
  43307. res = TEST_RES_CHECK(1);
  43308. #endif
  43309. return res;
  43310. }
  43311. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  43312. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  43313. static int test_X509_LOOKUP_add_dir(void)
  43314. {
  43315. int res = TEST_SKIPPED;
  43316. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  43317. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  43318. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  43319. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  43320. !defined(NO_RSA)
  43321. X509_STORE * store;
  43322. X509_STORE_CTX * storeCtx;
  43323. X509_CRL * crl;
  43324. X509 * ca;
  43325. X509 * cert;
  43326. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  43327. const char srvCert[] = "./certs/server-cert.pem";
  43328. const char caCert[] = "./certs/ca-cert.pem";
  43329. const char caDir[] = "./certs/crl/hash_der";
  43330. XFILE fp;
  43331. X509_LOOKUP * lookup;
  43332. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  43333. /* Set up store with CA */
  43334. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43335. SSL_FILETYPE_PEM)));
  43336. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43337. /* Add CRL lookup directory to store.
  43338. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  43339. * of crl.der */
  43340. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  43341. X509_LOOKUP_hash_dir())));
  43342. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  43343. SSL_SUCCESS);
  43344. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43345. SSL_SUCCESS);
  43346. /* Add CRL to store NOT containing the verified certificate, which
  43347. * forces use of the CRL lookup directory */
  43348. fp = XFOPEN(cliCrlPem, "rb");
  43349. AssertTrue((fp != XBADFILE));
  43350. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43351. NULL, NULL));
  43352. XFCLOSE(fp);
  43353. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43354. /* Create verification context outside of an SSL session */
  43355. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  43356. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43357. SSL_FILETYPE_PEM)));
  43358. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43359. /* Perform verification, which should NOT return CRL missing */
  43360. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  43361. X509_CRL_free(crl);
  43362. X509_STORE_free(store);
  43363. X509_STORE_CTX_free(storeCtx);
  43364. X509_free(cert);
  43365. X509_free(ca);
  43366. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  43367. * We should get CRL_MISSING at the end, because the lookup
  43368. * dir has only ASN1 CRLs. */
  43369. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  43370. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43371. SSL_FILETYPE_PEM)));
  43372. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43373. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  43374. X509_LOOKUP_hash_dir())));
  43375. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  43376. SSL_SUCCESS);
  43377. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43378. SSL_SUCCESS);
  43379. fp = XFOPEN(cliCrlPem, "rb");
  43380. AssertTrue((fp != XBADFILE));
  43381. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43382. NULL, NULL));
  43383. XFCLOSE(fp);
  43384. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43385. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  43386. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43387. SSL_FILETYPE_PEM)));
  43388. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43389. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  43390. * in dir containing only ASN1/DER. */
  43391. AssertIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  43392. X509_CRL_free(crl);
  43393. X509_STORE_free(store);
  43394. X509_STORE_CTX_free(storeCtx);
  43395. X509_free(cert);
  43396. X509_free(ca);
  43397. res = TEST_RES_CHECK(1);
  43398. #endif
  43399. return res;
  43400. }
  43401. /*----------------------------------------------------------------------------*
  43402. | Certificate Failure Checks
  43403. *----------------------------------------------------------------------------*/
  43404. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  43405. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  43406. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  43407. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  43408. int type)
  43409. {
  43410. int ret;
  43411. WOLFSSL_CERT_MANAGER* cm = NULL;
  43412. switch (type) {
  43413. case TESTING_RSA:
  43414. #ifdef NO_RSA
  43415. fprintf(stderr, "RSA disabled, skipping test\n");
  43416. return ASN_SIG_CONFIRM_E;
  43417. #else
  43418. break;
  43419. #endif
  43420. case TESTING_ECC:
  43421. #ifndef HAVE_ECC
  43422. fprintf(stderr, "ECC disabled, skipping test\n");
  43423. return ASN_SIG_CONFIRM_E;
  43424. #else
  43425. break;
  43426. #endif
  43427. default:
  43428. fprintf(stderr, "Bad function argument\n");
  43429. return BAD_FUNC_ARG;
  43430. }
  43431. cm = wolfSSL_CertManagerNew();
  43432. if (cm == NULL) {
  43433. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  43434. return -1;
  43435. }
  43436. #ifndef NO_FILESYSTEM
  43437. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  43438. if (ret != WOLFSSL_SUCCESS) {
  43439. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  43440. wolfSSL_CertManagerFree(cm);
  43441. return ret;
  43442. }
  43443. #else
  43444. (void)ca;
  43445. #endif
  43446. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  43447. /* Let AssertIntEQ handle return code */
  43448. wolfSSL_CertManagerFree(cm);
  43449. return ret;
  43450. }
  43451. #if !defined(NO_FILESYSTEM)
  43452. static int test_RsaSigFailure_cm(void)
  43453. {
  43454. int ret = 0;
  43455. const char* ca_cert = "./certs/ca-cert.pem";
  43456. const char* server_cert = "./certs/server-cert.der";
  43457. byte* cert_buf = NULL;
  43458. size_t cert_sz = 0;
  43459. ret = load_file(server_cert, &cert_buf, &cert_sz);
  43460. if (ret == 0) {
  43461. /* corrupt DER - invert last byte, which is signature */
  43462. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43463. /* test bad cert */
  43464. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  43465. }
  43466. if (cert_buf)
  43467. free(cert_buf);
  43468. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43469. if (ret == WOLFSSL_FATAL_ERROR) {
  43470. ret = 0;
  43471. }
  43472. #else
  43473. if (ret == ASN_SIG_CONFIRM_E) {
  43474. ret = 0;
  43475. }
  43476. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  43477. return TEST_RES_CHECK(ret == 0);
  43478. }
  43479. static int test_EccSigFailure_cm(void)
  43480. {
  43481. int ret = 0;
  43482. /* self-signed ECC cert, so use server cert as CA */
  43483. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  43484. const char* server_cert = "./certs/server-ecc.der";
  43485. byte* cert_buf = NULL;
  43486. size_t cert_sz = 0;
  43487. ret = load_file(server_cert, &cert_buf, &cert_sz);
  43488. if (ret == 0) {
  43489. /* corrupt DER - invert last byte, which is signature */
  43490. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43491. /* test bad cert */
  43492. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  43493. }
  43494. if (cert_buf)
  43495. free(cert_buf);
  43496. #ifdef FP_ECC
  43497. wc_ecc_fp_free();
  43498. #endif
  43499. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43500. if (ret == WOLFSSL_FATAL_ERROR) {
  43501. ret = 0;
  43502. }
  43503. #else
  43504. if (ret == ASN_SIG_CONFIRM_E) {
  43505. ret = 0;
  43506. }
  43507. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  43508. return TEST_RES_CHECK(ret == 0);
  43509. }
  43510. #endif /* !NO_FILESYSTEM */
  43511. #endif /* NO_CERTS */
  43512. #ifdef WOLFSSL_TLS13
  43513. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  43514. #ifdef WC_SHA384_DIGEST_SIZE
  43515. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  43516. #else
  43517. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  43518. #endif
  43519. #endif
  43520. #ifdef WOLFSSL_EARLY_DATA
  43521. static const char earlyData[] = "Early Data";
  43522. static char earlyDataBuffer[1];
  43523. #endif
  43524. static int test_tls13_apis(void)
  43525. {
  43526. int ret = 0;
  43527. #ifndef WOLFSSL_NO_TLS12
  43528. #ifndef NO_WOLFSSL_CLIENT
  43529. WOLFSSL_CTX* clientTls12Ctx;
  43530. WOLFSSL* clientTls12Ssl;
  43531. #endif
  43532. #ifndef NO_WOLFSSL_SERVER
  43533. WOLFSSL_CTX* serverTls12Ctx;
  43534. WOLFSSL* serverTls12Ssl;
  43535. #endif
  43536. #endif
  43537. #ifndef NO_WOLFSSL_CLIENT
  43538. WOLFSSL_CTX* clientCtx;
  43539. WOLFSSL* clientSsl;
  43540. #endif
  43541. #ifndef NO_WOLFSSL_SERVER
  43542. WOLFSSL_CTX* serverCtx;
  43543. WOLFSSL* serverSsl;
  43544. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43545. const char* ourCert = svrCertFile;
  43546. const char* ourKey = svrKeyFile;
  43547. #endif
  43548. #endif
  43549. int required;
  43550. #ifdef WOLFSSL_EARLY_DATA
  43551. int outSz;
  43552. #endif
  43553. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  43554. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  43555. #ifdef HAVE_PQC
  43556. WOLFSSL_KYBER_LEVEL1
  43557. #else
  43558. WOLFSSL_ECC_SECP256R1
  43559. #endif
  43560. };
  43561. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  43562. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  43563. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  43564. int numGroups = 2;
  43565. #endif
  43566. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  43567. char groupList[] =
  43568. #ifndef NO_ECC_SECP
  43569. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  43570. "P-521:"
  43571. #endif
  43572. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  43573. "P-384:"
  43574. #endif
  43575. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  43576. "P-256"
  43577. #ifdef HAVE_PQC
  43578. ":P256_KYBER_LEVEL1"
  43579. #endif
  43580. #endif
  43581. #ifdef HAVE_PQC
  43582. ":KYBER_LEVEL1"
  43583. #endif
  43584. "";
  43585. #endif /* !defined(NO_ECC_SECP) */
  43586. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  43587. (void)ret;
  43588. #ifndef WOLFSSL_NO_TLS12
  43589. #ifndef NO_WOLFSSL_CLIENT
  43590. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  43591. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  43592. #endif
  43593. #ifndef NO_WOLFSSL_SERVER
  43594. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  43595. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43596. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  43597. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  43598. #endif
  43599. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  43600. #endif
  43601. #endif
  43602. #ifndef NO_WOLFSSL_CLIENT
  43603. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  43604. clientSsl = wolfSSL_new(clientCtx);
  43605. #endif
  43606. #ifndef NO_WOLFSSL_SERVER
  43607. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  43608. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43609. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  43610. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  43611. #endif
  43612. serverSsl = wolfSSL_new(serverCtx);
  43613. #endif
  43614. #ifdef WOLFSSL_SEND_HRR_COOKIE
  43615. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  43616. #ifndef NO_WOLFSSL_CLIENT
  43617. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  43618. #endif
  43619. #ifndef NO_WOLFSSL_SERVER
  43620. #ifndef WOLFSSL_NO_TLS12
  43621. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  43622. #endif
  43623. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  43624. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  43625. WOLFSSL_SUCCESS);
  43626. #endif
  43627. #endif
  43628. #ifdef HAVE_SUPPORTED_CURVES
  43629. #ifdef HAVE_ECC
  43630. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  43631. #ifndef NO_WOLFSSL_SERVER
  43632. do {
  43633. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  43634. #ifdef WOLFSSL_ASYNC_CRYPT
  43635. if (ret == WC_PENDING_E)
  43636. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  43637. #endif
  43638. } while (ret == WC_PENDING_E);
  43639. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43640. #endif
  43641. #ifndef NO_WOLFSSL_CLIENT
  43642. #ifndef WOLFSSL_NO_TLS12
  43643. do {
  43644. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  43645. #ifdef WOLFSSL_ASYNC_CRYPT
  43646. if (ret == WC_PENDING_E)
  43647. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  43648. #endif
  43649. } while (ret == WC_PENDING_E);
  43650. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43651. #endif
  43652. do {
  43653. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  43654. #ifdef WOLFSSL_ASYNC_CRYPT
  43655. if (ret == WC_PENDING_E)
  43656. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  43657. #endif
  43658. } while (ret == WC_PENDING_E);
  43659. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43660. #endif
  43661. #elif defined(HAVE_CURVE25519)
  43662. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  43663. #ifndef NO_WOLFSSL_SERVER
  43664. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  43665. WOLFSSL_SUCCESS);
  43666. #endif
  43667. #ifndef NO_WOLFSSL_CLIENT
  43668. #ifndef WOLFSSL_NO_TLS12
  43669. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  43670. WOLFSSL_SUCCESS);
  43671. #endif
  43672. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  43673. WOLFSSL_SUCCESS);
  43674. #endif
  43675. #elif defined(HAVE_CURVE448)
  43676. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  43677. #ifndef NO_WOLFSSL_SERVER
  43678. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  43679. WOLFSSL_SUCCESS);
  43680. #endif
  43681. #ifndef NO_WOLFSSL_CLIENT
  43682. #ifndef WOLFSSL_NO_TLS12
  43683. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  43684. WOLFSSL_SUCCESS);
  43685. #endif
  43686. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  43687. WOLFSSL_SUCCESS);
  43688. #endif
  43689. #else
  43690. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  43691. #ifndef NO_WOLFSSL_CLIENT
  43692. #ifndef WOLFSSL_NO_TLS12
  43693. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  43694. NOT_COMPILED_IN);
  43695. #endif
  43696. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  43697. NOT_COMPILED_IN);
  43698. #endif
  43699. #endif
  43700. #if defined(HAVE_PQC)
  43701. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  43702. #ifndef NO_WOLFSSL_SERVER
  43703. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  43704. WOLFSSL_SUCCESS);
  43705. #endif
  43706. #ifndef NO_WOLFSSL_CLIENT
  43707. #ifndef WOLFSSL_NO_TLS12
  43708. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  43709. BAD_FUNC_ARG);
  43710. #endif
  43711. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  43712. WOLFSSL_SUCCESS);
  43713. #endif
  43714. #endif
  43715. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  43716. #ifndef NO_WOLFSSL_SERVER
  43717. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  43718. #endif
  43719. #ifndef NO_WOLFSSL_CLIENT
  43720. #ifndef WOLFSSL_NO_TLS12
  43721. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  43722. #endif
  43723. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  43724. #endif
  43725. #endif /* HAVE_SUPPORTED_CURVES */
  43726. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43727. #ifndef NO_WOLFSSL_CLIENT
  43728. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  43729. #endif
  43730. #ifndef NO_WOLFSSL_SERVER
  43731. #ifndef WOLFSSL_NO_TLS12
  43732. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  43733. #endif
  43734. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  43735. #endif
  43736. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43737. #ifndef NO_WOLFSSL_CLIENT
  43738. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  43739. #endif
  43740. #ifndef NO_WOLFSSL_SERVER
  43741. #ifndef WOLFSSL_NO_TLS12
  43742. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  43743. #endif
  43744. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  43745. #endif
  43746. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43747. #ifndef NO_WOLFSSL_CLIENT
  43748. #ifndef WOLFSSL_NO_TLS12
  43749. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  43750. #endif
  43751. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  43752. #endif
  43753. #ifndef NO_WOLFSSL_SERVER
  43754. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  43755. #endif
  43756. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43757. #ifndef NO_WOLFSSL_CLIENT
  43758. #ifndef WOLFSSL_NO_TLS12
  43759. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  43760. #endif
  43761. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  43762. #endif
  43763. #ifndef NO_WOLFSSL_SERVER
  43764. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  43765. #endif
  43766. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  43767. #ifndef NO_WOLFSSL_CLIENT
  43768. #ifndef WOLFSSL_NO_TLS12
  43769. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  43770. #endif
  43771. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  43772. #endif
  43773. #ifndef NO_WOLFSSL_SERVER
  43774. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  43775. #endif
  43776. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  43777. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  43778. #ifndef NO_WOLFSSL_CLIENT
  43779. #ifndef WOLFSSL_NO_TLS12
  43780. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  43781. BAD_FUNC_ARG);
  43782. #endif
  43783. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  43784. #endif
  43785. #ifndef NO_WOLFSSL_SERVER
  43786. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  43787. #endif
  43788. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  43789. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43790. #ifndef NO_WOLFSSL_SERVER
  43791. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  43792. #endif
  43793. #ifndef NO_WOLFSSL_CLIENT
  43794. #ifndef WOLFSSL_NO_TLS12
  43795. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  43796. BAD_FUNC_ARG);
  43797. #endif
  43798. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  43799. #endif
  43800. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43801. #ifndef NO_WOLFSSL_SERVER
  43802. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  43803. #endif
  43804. #ifndef NO_WOLFSSL_CLIENT
  43805. #ifndef WOLFSSL_NO_TLS12
  43806. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  43807. BAD_FUNC_ARG);
  43808. #endif
  43809. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  43810. #endif
  43811. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  43812. #ifndef NO_WOLFSSL_CLIENT
  43813. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  43814. #endif
  43815. #ifndef NO_WOLFSSL_SERVER
  43816. #ifndef WOLFSSL_NO_TLS12
  43817. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  43818. BAD_FUNC_ARG);
  43819. #endif
  43820. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  43821. #endif
  43822. #endif
  43823. #ifdef HAVE_ECC
  43824. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  43825. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  43826. #ifndef NO_WOLFSSL_SERVER
  43827. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  43828. #endif
  43829. #ifndef NO_WOLFSSL_CLIENT
  43830. #ifndef WOLFSSL_NO_TLS12
  43831. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  43832. #endif
  43833. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  43834. #endif
  43835. #endif
  43836. #ifdef HAVE_SUPPORTED_CURVES
  43837. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43838. #ifndef NO_WOLFSSL_CLIENT
  43839. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  43840. #endif
  43841. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43842. #ifndef NO_WOLFSSL_CLIENT
  43843. #ifndef WOLFSSL_NO_TLS12
  43844. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  43845. BAD_FUNC_ARG);
  43846. #endif
  43847. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  43848. WOLFSSL_MAX_GROUP_COUNT + 1),
  43849. BAD_FUNC_ARG);
  43850. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  43851. WOLFSSL_SUCCESS);
  43852. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  43853. BAD_FUNC_ARG);
  43854. #endif
  43855. #ifndef NO_WOLFSSL_SERVER
  43856. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  43857. WOLFSSL_SUCCESS);
  43858. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  43859. BAD_FUNC_ARG);
  43860. #endif
  43861. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43862. #ifndef NO_WOLFSSL_CLIENT
  43863. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  43864. #endif
  43865. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43866. #ifndef NO_WOLFSSL_CLIENT
  43867. #ifndef WOLFSSL_NO_TLS12
  43868. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  43869. BAD_FUNC_ARG);
  43870. #endif
  43871. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  43872. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  43873. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  43874. WOLFSSL_SUCCESS);
  43875. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  43876. BAD_FUNC_ARG);
  43877. #endif
  43878. #ifndef NO_WOLFSSL_SERVER
  43879. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  43880. WOLFSSL_SUCCESS);
  43881. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  43882. BAD_FUNC_ARG);
  43883. #endif
  43884. #ifdef OPENSSL_EXTRA
  43885. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43886. #ifndef NO_WOLFSSL_CLIENT
  43887. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  43888. #endif
  43889. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43890. #ifndef NO_WOLFSSL_CLIENT
  43891. #ifndef WOLFSSL_NO_TLS12
  43892. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  43893. WOLFSSL_FAILURE);
  43894. #endif
  43895. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  43896. WOLFSSL_SUCCESS);
  43897. #endif
  43898. #ifndef NO_WOLFSSL_SERVER
  43899. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  43900. WOLFSSL_SUCCESS);
  43901. #endif
  43902. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43903. #ifndef NO_WOLFSSL_CLIENT
  43904. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  43905. #endif
  43906. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43907. #ifndef NO_WOLFSSL_CLIENT
  43908. #ifndef WOLFSSL_NO_TLS12
  43909. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  43910. WOLFSSL_FAILURE);
  43911. #endif
  43912. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  43913. WOLFSSL_SUCCESS);
  43914. #endif
  43915. #ifndef NO_WOLFSSL_SERVER
  43916. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  43917. WOLFSSL_SUCCESS);
  43918. #endif
  43919. #endif /* OPENSSL_EXTRA */
  43920. #endif /* HAVE_SUPPORTED_CURVES */
  43921. #endif /* HAVE_ECC */
  43922. #ifdef WOLFSSL_EARLY_DATA
  43923. #ifndef OPENSSL_EXTRA
  43924. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43925. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43926. #else
  43927. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43928. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43929. #endif
  43930. #ifndef NO_WOLFSSL_CLIENT
  43931. #ifndef OPENSSL_EXTRA
  43932. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43933. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43934. #else
  43935. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43936. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43937. #endif
  43938. #endif
  43939. #ifndef NO_WOLFSSL_SERVER
  43940. #ifndef WOLFSSL_NO_TLS12
  43941. #ifndef OPENSSL_EXTRA
  43942. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43943. BAD_FUNC_ARG);
  43944. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43945. #else
  43946. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43947. BAD_FUNC_ARG);
  43948. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43949. #endif
  43950. #endif
  43951. #ifndef OPENSSL_EXTRA
  43952. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  43953. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), WOLFSSL_SUCCESS);
  43954. #else
  43955. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  43956. #endif
  43957. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  43958. #else
  43959. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  43960. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  43961. #endif
  43962. #endif
  43963. #ifndef OPENSSL_EXTRA
  43964. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43965. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  43966. #else
  43967. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43968. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  43969. #endif
  43970. #ifndef NO_WOLFSSL_CLIENT
  43971. #ifndef OPENSSL_EXTRA
  43972. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  43973. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  43974. #else
  43975. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  43976. #endif
  43977. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  43978. #else
  43979. AssertIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  43980. AssertIntEQ(SSL_get_max_early_data(clientSsl), 17);
  43981. #endif
  43982. #endif
  43983. #ifndef NO_WOLFSSL_SERVER
  43984. #ifndef WOLFSSL_NO_TLS12
  43985. #ifndef OPENSSL_EXTRA
  43986. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  43987. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  43988. #else
  43989. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  43990. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  43991. #endif
  43992. #endif
  43993. #ifndef OPENSSL_EXTRA
  43994. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  43995. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  43996. #else
  43997. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  43998. #endif
  43999. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  44000. #else
  44001. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  44002. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  44003. #endif
  44004. #endif
  44005. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  44006. &outSz), BAD_FUNC_ARG);
  44007. #ifndef NO_WOLFSSL_CLIENT
  44008. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  44009. &outSz), BAD_FUNC_ARG);
  44010. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  44011. BAD_FUNC_ARG);
  44012. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  44013. sizeof(earlyData), NULL),
  44014. BAD_FUNC_ARG);
  44015. #endif
  44016. #ifndef NO_WOLFSSL_SERVER
  44017. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  44018. sizeof(earlyData), &outSz),
  44019. SIDE_ERROR);
  44020. #endif
  44021. #ifndef NO_WOLFSSL_CLIENT
  44022. #ifndef WOLFSSL_NO_TLS12
  44023. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  44024. sizeof(earlyData), &outSz),
  44025. BAD_FUNC_ARG);
  44026. #endif
  44027. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  44028. sizeof(earlyData), &outSz),
  44029. WOLFSSL_FATAL_ERROR);
  44030. #endif
  44031. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  44032. sizeof(earlyDataBuffer), &outSz),
  44033. BAD_FUNC_ARG);
  44034. #ifndef NO_WOLFSSL_SERVER
  44035. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  44036. sizeof(earlyDataBuffer), &outSz),
  44037. BAD_FUNC_ARG);
  44038. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  44039. BAD_FUNC_ARG);
  44040. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  44041. sizeof(earlyDataBuffer), NULL),
  44042. BAD_FUNC_ARG);
  44043. #endif
  44044. #ifndef NO_WOLFSSL_CLIENT
  44045. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  44046. sizeof(earlyDataBuffer), &outSz),
  44047. SIDE_ERROR);
  44048. #endif
  44049. #ifndef NO_WOLFSSL_SERVER
  44050. #ifndef WOLFSSL_NO_TLS12
  44051. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  44052. sizeof(earlyDataBuffer), &outSz),
  44053. BAD_FUNC_ARG);
  44054. #endif
  44055. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  44056. sizeof(earlyDataBuffer), &outSz),
  44057. WOLFSSL_FATAL_ERROR);
  44058. #endif
  44059. #endif
  44060. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  44061. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  44062. #endif
  44063. #ifndef NO_WOLFSSL_SERVER
  44064. wolfSSL_free(serverSsl);
  44065. wolfSSL_CTX_free(serverCtx);
  44066. #endif
  44067. #ifndef NO_WOLFSSL_CLIENT
  44068. wolfSSL_free(clientSsl);
  44069. wolfSSL_CTX_free(clientCtx);
  44070. #endif
  44071. #ifndef WOLFSSL_NO_TLS12
  44072. #ifndef NO_WOLFSSL_SERVER
  44073. wolfSSL_free(serverTls12Ssl);
  44074. wolfSSL_CTX_free(serverTls12Ctx);
  44075. #endif
  44076. #ifndef NO_WOLFSSL_CLIENT
  44077. wolfSSL_free(clientTls12Ssl);
  44078. wolfSSL_CTX_free(clientTls12Ctx);
  44079. #endif
  44080. #endif
  44081. return TEST_RES_CHECK(1);
  44082. }
  44083. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  44084. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  44085. defined(BUILD_TLS_AES_256_GCM_SHA384)
  44086. /* Called when writing. */
  44087. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  44088. {
  44089. (void)ssl;
  44090. (void)buf;
  44091. (void)sz;
  44092. (void)ctx;
  44093. /* Force error return from wolfSSL_accept_TLSv13(). */
  44094. return WANT_WRITE;
  44095. }
  44096. /* Called when reading. */
  44097. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  44098. {
  44099. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  44100. int len = (int)msg->length;
  44101. (void)ssl;
  44102. (void)sz;
  44103. /* Pass back as much of message as will fit in buffer. */
  44104. if (len > sz)
  44105. len = sz;
  44106. XMEMCPY(buf, msg->buffer, len);
  44107. /* Move over returned data. */
  44108. msg->buffer += len;
  44109. msg->length -= len;
  44110. /* Amount actually copied. */
  44111. return len;
  44112. }
  44113. #endif
  44114. static int test_tls13_cipher_suites(void)
  44115. {
  44116. int res = TEST_SKIPPED;
  44117. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  44118. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  44119. defined(BUILD_TLS_AES_256_GCM_SHA384)
  44120. WOLFSSL_CTX* ctx;
  44121. WOLFSSL *ssl;
  44122. int i;
  44123. byte clientHello[] = {
  44124. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  44125. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  44126. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  44127. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  44128. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  44129. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  44130. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  44131. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  44132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  44134. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  44135. 0x13, 0x01,
  44136. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  44137. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  44138. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  44139. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  44140. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  44141. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  44142. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  44143. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  44144. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  44145. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  44146. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  44147. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  44148. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  44149. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  44150. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  44151. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  44152. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  44153. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  44154. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  44155. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  44156. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  44157. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  44158. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  44159. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  44160. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  44161. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  44162. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  44163. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  44164. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  44165. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  44166. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  44167. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  44168. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  44169. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  44170. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  44171. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44173. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  44174. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  44175. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  44176. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  44177. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  44178. };
  44179. WOLFSSL_BUFFER_INFO msg;
  44180. /* Offset into ClientHello message data of first cipher suite. */
  44181. const int csOff = 78;
  44182. /* Server cipher list. */
  44183. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  44184. /* Suite list with duplicates. */
  44185. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  44186. "TLS13-AES128-GCM-SHA256:"
  44187. "TLS13-AES256-GCM-SHA384:"
  44188. "TLS13-AES256-GCM-SHA384:"
  44189. "TLS13-AES128-GCM-SHA256";
  44190. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  44191. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  44192. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  44193. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  44194. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  44195. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  44196. #endif
  44197. /* Set up wolfSSL context. */
  44198. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  44199. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  44200. WOLFSSL_FILETYPE_PEM));
  44201. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  44202. WOLFSSL_FILETYPE_PEM));
  44203. /* Read from 'msg'. */
  44204. wolfSSL_SetIORecv(ctx, CsRecv);
  44205. /* No where to send to - dummy sender. */
  44206. wolfSSL_SetIOSend(ctx, CsSend);
  44207. /* Test cipher suite list with many copies of a cipher suite. */
  44208. AssertNotNull(ssl = wolfSSL_new(ctx));
  44209. msg.buffer = clientHello;
  44210. msg.length = (unsigned int)sizeof(clientHello);
  44211. wolfSSL_SetIOReadCtx(ssl, &msg);
  44212. /* Force server to have as many occurrences of same cipher suite as
  44213. * possible. */
  44214. {
  44215. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  44216. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  44217. for (i = 0; i < suites->suiteSz; i += 2) {
  44218. suites->suites[i + 0] = TLS13_BYTE;
  44219. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  44220. }
  44221. }
  44222. /* Test multiple occurrences of same cipher suite. */
  44223. wolfSSL_accept_TLSv13(ssl);
  44224. wolfSSL_free(ssl);
  44225. /* Set client order opposite to server order:
  44226. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  44227. clientHello[csOff + 0] = TLS13_BYTE;
  44228. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  44229. clientHello[csOff + 2] = TLS13_BYTE;
  44230. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  44231. /* Test server order negotiation. */
  44232. AssertNotNull(ssl = wolfSSL_new(ctx));
  44233. msg.buffer = clientHello;
  44234. msg.length = (unsigned int)sizeof(clientHello);
  44235. wolfSSL_SetIOReadCtx(ssl, &msg);
  44236. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  44237. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  44238. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  44239. wolfSSL_accept_TLSv13(ssl);
  44240. /* Check refined order - server order. */
  44241. AssertIntEQ(ssl->suites->suiteSz, 4);
  44242. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  44243. AssertIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  44244. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  44245. AssertIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  44246. wolfSSL_free(ssl);
  44247. /* Test client order negotiation. */
  44248. AssertNotNull(ssl = wolfSSL_new(ctx));
  44249. msg.buffer = clientHello;
  44250. msg.length = (unsigned int)sizeof(clientHello);
  44251. wolfSSL_SetIOReadCtx(ssl, &msg);
  44252. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  44253. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  44254. AssertIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  44255. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  44256. wolfSSL_accept_TLSv13(ssl);
  44257. /* Check refined order - client order. */
  44258. AssertIntEQ(ssl->suites->suiteSz, 4);
  44259. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  44260. AssertIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  44261. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  44262. AssertIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  44263. wolfSSL_free(ssl);
  44264. /* Check duplicate detection is working. */
  44265. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  44266. AssertIntEQ(ctx->suites->suiteSz, 4);
  44267. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  44268. AssertIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  44269. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  44270. AssertIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  44271. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  44272. AssertIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  44273. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  44274. AssertIntEQ(ctx->suites->suiteSz, 4);
  44275. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  44276. AssertIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  44277. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  44278. AssertIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  44279. #endif
  44280. wolfSSL_CTX_free(ctx);
  44281. res = TEST_RES_CHECK(1);
  44282. #endif
  44283. return res;
  44284. }
  44285. #endif
  44286. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  44287. !defined(NO_OLD_TLS))
  44288. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  44289. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  44290. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  44291. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  44292. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  44293. const unsigned char* priv, unsigned int privSz,
  44294. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  44295. unsigned char* out, unsigned int* outlen,
  44296. void* ctx)
  44297. {
  44298. int result;
  44299. /* Test fail when context associated with WOLFSSL is NULL */
  44300. if (ctx == NULL) {
  44301. return -1;
  44302. }
  44303. (void)ssl;
  44304. /* return 0 on success */
  44305. PRIVATE_KEY_UNLOCK();
  44306. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  44307. PRIVATE_KEY_LOCK();
  44308. return result;
  44309. }
  44310. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  44311. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  44312. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  44313. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  44314. WOLFSSL_SUCCESS);
  44315. #endif
  44316. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44317. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  44318. WOLFSSL_SUCCESS);
  44319. #endif
  44320. }
  44321. static void test_dh_ssl_setup(WOLFSSL* ssl)
  44322. {
  44323. static int dh_test_ctx = 1;
  44324. int ret;
  44325. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  44326. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  44327. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  44328. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  44329. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  44330. }
  44331. }
  44332. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  44333. {
  44334. int ret;
  44335. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  44336. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  44337. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  44338. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  44339. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  44340. }
  44341. }
  44342. #endif
  44343. static int test_DhCallbacks(void)
  44344. {
  44345. int res = TEST_SKIPPED;
  44346. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  44347. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  44348. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  44349. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  44350. WOLFSSL_CTX *ctx;
  44351. WOLFSSL *ssl;
  44352. tcp_ready ready;
  44353. func_args server_args;
  44354. func_args client_args;
  44355. THREAD_TYPE serverThread;
  44356. callback_functions func_cb_client;
  44357. callback_functions func_cb_server;
  44358. int test;
  44359. #ifndef NO_WOLFSSL_CLIENT
  44360. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44361. #else
  44362. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  44363. #endif
  44364. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  44365. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  44366. /* load client ca cert */
  44367. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  44368. WOLFSSL_SUCCESS);
  44369. /* test with NULL arguments */
  44370. wolfSSL_SetDhAgreeCtx(NULL, &test);
  44371. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  44372. /* test success case */
  44373. test = 1;
  44374. AssertNotNull(ssl = wolfSSL_new(ctx));
  44375. wolfSSL_SetDhAgreeCtx(ssl, &test);
  44376. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  44377. wolfSSL_free(ssl);
  44378. wolfSSL_CTX_free(ctx);
  44379. /* test a connection where callback is used */
  44380. #ifdef WOLFSSL_TIRTOS
  44381. fdOpenSession(Task_self());
  44382. #endif
  44383. XMEMSET(&server_args, 0, sizeof(func_args));
  44384. XMEMSET(&client_args, 0, sizeof(func_args));
  44385. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  44386. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  44387. StartTCP();
  44388. InitTcpReady(&ready);
  44389. #if defined(USE_WINDOWS_API)
  44390. /* use RNG to get random port if using windows */
  44391. ready.port = GetRandomPort();
  44392. #endif
  44393. server_args.signal = &ready;
  44394. client_args.signal = &ready;
  44395. server_args.return_code = TEST_FAIL;
  44396. client_args.return_code = TEST_FAIL;
  44397. /* set callbacks to use DH functions */
  44398. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44399. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  44400. #ifndef WOLFSSL_NO_TLS12
  44401. func_cb_client.method = wolfTLSv1_2_client_method;
  44402. #else
  44403. func_cb_client.method = wolfTLSv1_3_client_method;
  44404. #endif
  44405. client_args.callbacks = &func_cb_client;
  44406. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44407. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  44408. #ifndef WOLFSSL_NO_TLS12
  44409. func_cb_server.method = wolfTLSv1_2_server_method;
  44410. #else
  44411. func_cb_server.method = wolfTLSv1_3_server_method;
  44412. #endif
  44413. server_args.callbacks = &func_cb_server;
  44414. start_thread(test_server_nofail, &server_args, &serverThread);
  44415. wait_tcp_ready(&server_args);
  44416. test_client_nofail(&client_args, NULL);
  44417. join_thread(serverThread);
  44418. AssertTrue(client_args.return_code);
  44419. AssertTrue(server_args.return_code);
  44420. FreeTcpReady(&ready);
  44421. #ifdef WOLFSSL_TIRTOS
  44422. fdOpenSession(Task_self());
  44423. #endif
  44424. /* now set user ctx to not be 1 so that the callback returns fail case */
  44425. #ifdef WOLFSSL_TIRTOS
  44426. fdOpenSession(Task_self());
  44427. #endif
  44428. XMEMSET(&server_args, 0, sizeof(func_args));
  44429. XMEMSET(&client_args, 0, sizeof(func_args));
  44430. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  44431. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  44432. StartTCP();
  44433. InitTcpReady(&ready);
  44434. #if defined(USE_WINDOWS_API)
  44435. /* use RNG to get random port if using windows */
  44436. ready.port = GetRandomPort();
  44437. #endif
  44438. server_args.signal = &ready;
  44439. client_args.signal = &ready;
  44440. server_args.return_code = TEST_FAIL;
  44441. client_args.return_code = TEST_FAIL;
  44442. /* set callbacks to use DH functions */
  44443. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44444. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  44445. #ifndef WOLFSSL_NO_TLS12
  44446. func_cb_client.method = wolfTLSv1_2_client_method;
  44447. #else
  44448. func_cb_client.method = wolfTLSv1_3_client_method;
  44449. #endif
  44450. client_args.callbacks = &func_cb_client;
  44451. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44452. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  44453. #ifndef WOLFSSL_NO_TLS12
  44454. func_cb_server.method = wolfTLSv1_2_server_method;
  44455. #else
  44456. func_cb_server.method = wolfTLSv1_3_server_method;
  44457. #endif
  44458. server_args.callbacks = &func_cb_server;
  44459. start_thread(test_server_nofail, &server_args, &serverThread);
  44460. wait_tcp_ready(&server_args);
  44461. test_client_nofail(&client_args, NULL);
  44462. join_thread(serverThread);
  44463. AssertIntEQ(client_args.return_code, TEST_FAIL);
  44464. AssertIntEQ(server_args.return_code, TEST_FAIL);
  44465. FreeTcpReady(&ready);
  44466. #ifdef WOLFSSL_TIRTOS
  44467. fdOpenSession(Task_self());
  44468. #endif
  44469. res = TEST_RES_CHECK(1);
  44470. #endif
  44471. return res;
  44472. }
  44473. #endif /* HAVE_PK_CALLBACKS */
  44474. #ifdef HAVE_HASHDRBG
  44475. #ifdef TEST_RESEED_INTERVAL
  44476. static int test_wc_RNG_GenerateBlock_Reseed(void)
  44477. {
  44478. int i, ret;
  44479. WC_RNG rng;
  44480. byte key[32];
  44481. ret = wc_InitRng(&rng);
  44482. if (ret == 0) {
  44483. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  44484. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  44485. if (ret != 0) {
  44486. break;
  44487. }
  44488. }
  44489. }
  44490. wc_FreeRng(&rng);
  44491. return TEST_RES_CHECK(ret == 0);
  44492. }
  44493. #endif /* TEST_RESEED_INTERVAL */
  44494. static int test_wc_RNG_GenerateBlock(void)
  44495. {
  44496. int i, ret;
  44497. WC_RNG rng;
  44498. byte key[32];
  44499. ret = wc_InitRng(&rng);
  44500. if (ret == 0) {
  44501. for (i = 0; i < 10; i++) {
  44502. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  44503. if (ret != 0) {
  44504. break;
  44505. }
  44506. }
  44507. }
  44508. wc_FreeRng(&rng);
  44509. (void)rng; /* for WC_NO_RNG case */
  44510. (void)key;
  44511. return TEST_RES_CHECK(ret == 0);
  44512. }
  44513. #endif
  44514. /*
  44515. * Testing get_rand_digit
  44516. */
  44517. static int test_get_rand_digit(void)
  44518. {
  44519. int res = TEST_SKIPPED;
  44520. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  44521. int ret = 0;
  44522. WC_RNG rng;
  44523. mp_digit d;
  44524. ret = wc_InitRng(&rng);
  44525. if (ret == 0) {
  44526. ret = get_rand_digit(&rng, &d);
  44527. }
  44528. if (ret == 0) {
  44529. ret = get_rand_digit(NULL, NULL);
  44530. if (ret == BAD_FUNC_ARG) {
  44531. ret = 0;
  44532. }
  44533. }
  44534. if (ret == 0) {
  44535. ret = get_rand_digit(NULL, &d);
  44536. if (ret == BAD_FUNC_ARG) {
  44537. ret = 0;
  44538. }
  44539. }
  44540. if (ret == 0) {
  44541. ret = get_rand_digit(&rng, NULL);
  44542. if (ret == BAD_FUNC_ARG) {
  44543. ret = 0;
  44544. }
  44545. }
  44546. if (ret == 0) {
  44547. ret = wc_FreeRng(&rng);
  44548. }
  44549. res = TEST_RES_CHECK(ret == 0);
  44550. #endif
  44551. return res;
  44552. }/* End test_get_rand_digit*/
  44553. /*
  44554. * Testing get_digit_count
  44555. */
  44556. static int test_get_digit_count(void)
  44557. {
  44558. int res = TEST_SKIPPED;
  44559. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  44560. int ret = 0;
  44561. mp_int a;
  44562. if (mp_init(&a) != MP_OKAY) {
  44563. ret = -1;
  44564. }
  44565. if (ret == 0) {
  44566. ret = get_digit_count(NULL);
  44567. }
  44568. if (ret == 0) {
  44569. ret = get_digit_count(&a);
  44570. }
  44571. mp_clear(&a);
  44572. res = TEST_RES_CHECK(ret == 0);
  44573. #endif
  44574. return res;
  44575. }/* End test_get_digit_count*/
  44576. /*
  44577. * Testing mp_cond_copy
  44578. */
  44579. static int test_mp_cond_copy(void)
  44580. {
  44581. int res = TEST_SKIPPED;
  44582. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  44583. defined(WOLFSSL_PUBLIC_MP)
  44584. int ret = 0;
  44585. mp_int a;
  44586. mp_int b;
  44587. int copy = 0;
  44588. if (mp_init(&a) != MP_OKAY) {
  44589. ret = -1;
  44590. }
  44591. if (ret == 0) {
  44592. if (mp_init(&b) != MP_OKAY) {
  44593. ret = -1;
  44594. }
  44595. }
  44596. if (ret == 0) {
  44597. ret = mp_cond_copy(NULL, copy, NULL);
  44598. if (ret == BAD_FUNC_ARG) {
  44599. ret = 0;
  44600. }
  44601. }
  44602. if (ret == 0) {
  44603. ret = mp_cond_copy(NULL, copy, &b);
  44604. if (ret == BAD_FUNC_ARG) {
  44605. ret = 0;
  44606. }
  44607. }
  44608. if (ret == 0) {
  44609. ret = mp_cond_copy(&a, copy, NULL);
  44610. if (ret == BAD_FUNC_ARG) {
  44611. ret = 0;
  44612. }
  44613. }
  44614. if (ret == 0) {
  44615. ret = mp_cond_copy(&a, copy, &b);
  44616. }
  44617. mp_clear(&a);
  44618. mp_clear(&b);
  44619. res = TEST_RES_CHECK(ret == 0);
  44620. #endif
  44621. return res;
  44622. }/* End test_mp_cond_copy*/
  44623. /*
  44624. * Testing mp_rand
  44625. */
  44626. static int test_mp_rand(void)
  44627. {
  44628. int res = TEST_SKIPPED;
  44629. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  44630. int ret = 0;
  44631. mp_int a;
  44632. int digits = 1;
  44633. WC_RNG rng;
  44634. if (mp_init(&a) != MP_OKAY) {
  44635. ret = -1;
  44636. }
  44637. if (ret == 0) {
  44638. ret = wc_InitRng(&rng);
  44639. }
  44640. if (ret == 0) {
  44641. ret = mp_rand(&a, digits, NULL);
  44642. if (ret == MISSING_RNG_E) {
  44643. ret = 0;
  44644. }
  44645. }
  44646. if (ret == 0) {
  44647. ret = mp_rand(NULL, digits, &rng);
  44648. if (ret == BAD_FUNC_ARG) {
  44649. ret = 0;
  44650. }
  44651. }
  44652. if (ret == 0) {
  44653. ret = mp_rand(&a, 0, &rng);
  44654. if (ret == BAD_FUNC_ARG) {
  44655. ret = 0;
  44656. }
  44657. }
  44658. if (ret == 0) {
  44659. ret = mp_rand(&a, digits, &rng);
  44660. }
  44661. mp_clear(&a);
  44662. wc_FreeRng(&rng);
  44663. res = TEST_RES_CHECK(ret == 0);
  44664. #endif
  44665. return res;
  44666. }/* End test_mp_rand*/
  44667. /*
  44668. * Testing get_digit
  44669. */
  44670. static int test_get_digit(void)
  44671. {
  44672. int res = TEST_SKIPPED;
  44673. #if defined(WOLFSSL_PUBLIC_MP)
  44674. int ret = 0;
  44675. mp_int a;
  44676. int n = 0;
  44677. if (mp_init(&a) != MP_OKAY) {
  44678. ret = -1;
  44679. }
  44680. if (ret == 0) {
  44681. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  44682. ret = -1;
  44683. }
  44684. }
  44685. if (ret == 0) {
  44686. if (get_digit(NULL, n) == 0) { /* Should hit this */
  44687. ret = 0;
  44688. }
  44689. }
  44690. if (ret == 0) {
  44691. if (get_digit(&a, n) != 0) { /* Should not hit this */
  44692. ret = -1;
  44693. }
  44694. }
  44695. if (ret == 0) {
  44696. if (get_digit(&a, n) == 0) { /* Should hit this */
  44697. ret = 0;
  44698. }
  44699. }
  44700. mp_clear(&a);
  44701. res = TEST_RES_CHECK(ret == 0);
  44702. #endif
  44703. return res;
  44704. }/* End test_get_digit*/
  44705. /*
  44706. * Testing wc_export_int
  44707. */
  44708. static int test_wc_export_int(void)
  44709. {
  44710. int res = TEST_SKIPPED;
  44711. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  44712. defined(WOLFSSL_PUBLIC_MP)
  44713. int ret = 0;
  44714. mp_int mp;
  44715. byte buf[32];
  44716. word32 keySz = (word32)sizeof(buf);
  44717. word32 len = (word32)sizeof(buf);
  44718. if (mp_init(&mp) != MP_OKAY) {
  44719. ret = -1;
  44720. }
  44721. if (ret == 0) {
  44722. ret = mp_set(&mp, 1234);
  44723. }
  44724. if (ret == 0) {
  44725. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44726. if (ret == BAD_FUNC_ARG) {
  44727. ret = 0;
  44728. }
  44729. }
  44730. if (ret == 0) {
  44731. len = sizeof(buf)-1;
  44732. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44733. if (ret == BUFFER_E) {
  44734. ret = 0;
  44735. }
  44736. }
  44737. if (ret == 0) {
  44738. len = sizeof(buf);
  44739. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44740. }
  44741. if (ret == 0) {
  44742. len = 4; /* test input too small */
  44743. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  44744. if (ret == BUFFER_E) {
  44745. ret = 0;
  44746. }
  44747. }
  44748. if (ret == 0) {
  44749. len = sizeof(buf);
  44750. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  44751. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  44752. if (ret == 0 && len != 5) {
  44753. ret = BAD_FUNC_ARG;
  44754. }
  44755. }
  44756. mp_clear(&mp);
  44757. res = TEST_RES_CHECK(ret == 0);
  44758. #endif
  44759. return res;
  44760. }/* End test_wc_export_int*/
  44761. static int test_wc_InitRngNonce(void)
  44762. {
  44763. int res = TEST_SKIPPED;
  44764. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44765. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  44766. int ret;
  44767. WC_RNG rng;
  44768. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44769. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44770. word32 nonceSz = sizeof(nonce);
  44771. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  44772. wc_FreeRng(&rng);
  44773. res = TEST_RES_CHECK(ret == 0);
  44774. #endif
  44775. return res;
  44776. }/* End test_wc_InitRngNonce*/
  44777. /*
  44778. * Testing wc_InitRngNonce_ex
  44779. */
  44780. static int test_wc_InitRngNonce_ex(void)
  44781. {
  44782. int res = TEST_SKIPPED;
  44783. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44784. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  44785. int ret;
  44786. WC_RNG rng;
  44787. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44788. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44789. word32 nonceSz = sizeof(nonce);
  44790. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
  44791. wc_FreeRng(&rng);
  44792. res = TEST_RES_CHECK(ret == 0);
  44793. #endif
  44794. return res;
  44795. }/*End test_wc_InitRngNonce_ex*/
  44796. static int test_wolfSSL_X509_CRL(void)
  44797. {
  44798. int res = TEST_SKIPPED;
  44799. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  44800. X509_CRL *crl;
  44801. char pem[][100] = {
  44802. "./certs/crl/crl.pem",
  44803. "./certs/crl/crl2.pem",
  44804. "./certs/crl/caEccCrl.pem",
  44805. "./certs/crl/eccCliCRL.pem",
  44806. "./certs/crl/eccSrvCRL.pem",
  44807. ""
  44808. };
  44809. #ifndef NO_BIO
  44810. BIO *bio;
  44811. #endif
  44812. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44813. char der[][100] = {
  44814. "./certs/crl/crl.der",
  44815. "./certs/crl/crl2.der",
  44816. ""};
  44817. #endif
  44818. XFILE fp;
  44819. int i;
  44820. for (i = 0; pem[i][0] != '\0'; i++)
  44821. {
  44822. fp = XFOPEN(pem[i], "rb");
  44823. AssertTrue((fp != XBADFILE));
  44824. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  44825. AssertNotNull(crl);
  44826. X509_CRL_free(crl);
  44827. XFCLOSE(fp);
  44828. fp = XFOPEN(pem[i], "rb");
  44829. AssertTrue((fp != XBADFILE));
  44830. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  44831. AssertNotNull(crl);
  44832. X509_CRL_free(crl);
  44833. XFCLOSE(fp);
  44834. }
  44835. #ifndef NO_BIO
  44836. for (i = 0; pem[i][0] != '\0'; i++)
  44837. {
  44838. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  44839. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  44840. X509_CRL_free(crl);
  44841. BIO_free(bio);
  44842. }
  44843. #endif
  44844. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44845. for (i = 0; der[i][0] != '\0'; i++) {
  44846. fp = XFOPEN(der[i], "rb");
  44847. AssertTrue((fp != XBADFILE));
  44848. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  44849. AssertNotNull(crl);
  44850. X509_CRL_free(crl);
  44851. XFCLOSE(fp);
  44852. fp = XFOPEN(der[i], "rb");
  44853. AssertTrue((fp != XBADFILE));
  44854. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  44855. AssertNotNull(crl);
  44856. X509_CRL_free(crl);
  44857. XFCLOSE(fp);
  44858. }
  44859. #endif
  44860. res = TEST_RES_CHECK(1);
  44861. #endif
  44862. return res;
  44863. }
  44864. static int test_wolfSSL_X509_load_crl_file(void)
  44865. {
  44866. int res = TEST_SKIPPED;
  44867. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44868. !defined(NO_RSA) && !defined(NO_BIO)
  44869. int i;
  44870. char pem[][100] = {
  44871. "./certs/crl/crl.pem",
  44872. "./certs/crl/crl2.pem",
  44873. "./certs/crl/caEccCrl.pem",
  44874. "./certs/crl/eccCliCRL.pem",
  44875. "./certs/crl/eccSrvCRL.pem",
  44876. ""
  44877. };
  44878. char der[][100] = {
  44879. "./certs/crl/crl.der",
  44880. "./certs/crl/crl2.der",
  44881. ""
  44882. };
  44883. WOLFSSL_X509_STORE* store;
  44884. WOLFSSL_X509_LOOKUP* lookup;
  44885. AssertNotNull(store = wolfSSL_X509_STORE_new());
  44886. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44887. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44888. X509_FILETYPE_PEM), 1);
  44889. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44890. X509_FILETYPE_PEM), 1);
  44891. if (store) {
  44892. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44893. WOLFSSL_FILETYPE_PEM), 1);
  44894. /* since store hasn't yet known the revoked cert*/
  44895. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44896. WOLFSSL_FILETYPE_PEM), 1);
  44897. }
  44898. for (i = 0; pem[i][0] != '\0'; i++)
  44899. {
  44900. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  44901. }
  44902. if (store) {
  44903. /* since store knows crl list */
  44904. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44905. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  44906. }
  44907. /* once feeing store */
  44908. X509_STORE_free(store);
  44909. store = NULL;
  44910. AssertNotNull(store = wolfSSL_X509_STORE_new());
  44911. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44912. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44913. X509_FILETYPE_PEM), 1);
  44914. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44915. X509_FILETYPE_PEM), 1);
  44916. if (store) {
  44917. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44918. WOLFSSL_FILETYPE_PEM), 1);
  44919. /* since store hasn't yet known the revoked cert*/
  44920. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44921. WOLFSSL_FILETYPE_PEM), 1);
  44922. }
  44923. for (i = 0; der[i][0] != '\0'; i++)
  44924. {
  44925. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  44926. }
  44927. if (store) {
  44928. /* since store knows crl list */
  44929. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44930. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  44931. }
  44932. /* test for incorrect parameter */
  44933. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  44934. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  44935. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  44936. X509_STORE_free(store);
  44937. store = NULL;
  44938. res = TEST_RES_CHECK(1);
  44939. #endif
  44940. return res;
  44941. }
  44942. static int test_wolfSSL_d2i_X509_REQ(void)
  44943. {
  44944. int res = TEST_SKIPPED;
  44945. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  44946. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  44947. !defined(WOLFSSL_SP_MATH)
  44948. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  44949. * generated by libest
  44950. * ./certs/csr.attr.der contains sample attributes
  44951. * ./certs/csr.ext.der contains sample extensions */
  44952. const char* csrFile = "./certs/csr.signed.der";
  44953. const char* csrPopFile = "./certs/csr.attr.der";
  44954. const char* csrExtFile = "./certs/csr.ext.der";
  44955. /* ./certs/csr.dsa.pem is generated using
  44956. * openssl req -newkey dsa:certs/dsaparams.pem \
  44957. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  44958. * -outform PEM
  44959. * with the passphrase "wolfSSL"
  44960. */
  44961. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44962. const char* csrDsaFile = "./certs/csr.dsa.pem";
  44963. XFILE f;
  44964. #endif
  44965. BIO* bio = NULL;
  44966. X509* req = NULL;
  44967. EVP_PKEY *pub_key = NULL;
  44968. {
  44969. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  44970. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44971. /*
  44972. * Extract the public key from the CSR
  44973. */
  44974. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44975. /*
  44976. * Verify the signature in the CSR
  44977. */
  44978. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44979. X509_free(req);
  44980. BIO_free(bio);
  44981. EVP_PKEY_free(pub_key);
  44982. }
  44983. {
  44984. #ifdef OPENSSL_ALL
  44985. X509_ATTRIBUTE* attr;
  44986. ASN1_TYPE *at;
  44987. #endif
  44988. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  44989. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44990. /*
  44991. * Extract the public key from the CSR
  44992. */
  44993. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44994. /*
  44995. * Verify the signature in the CSR
  44996. */
  44997. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44998. #ifdef OPENSSL_ALL
  44999. /*
  45000. * Obtain the challenge password from the CSR
  45001. */
  45002. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  45003. 1);
  45004. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  45005. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  45006. AssertNotNull(at->value.asn1_string);
  45007. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  45008. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  45009. #endif
  45010. X509_free(req);
  45011. BIO_free(bio);
  45012. EVP_PKEY_free(pub_key);
  45013. }
  45014. {
  45015. #ifdef OPENSSL_ALL
  45016. X509_ATTRIBUTE* attr;
  45017. ASN1_TYPE *at;
  45018. STACK_OF(X509_EXTENSION) *exts = NULL;
  45019. #endif
  45020. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  45021. /* This CSR contains an Extension Request attribute so
  45022. * we test extension parsing in a CSR attribute here. */
  45023. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  45024. /*
  45025. * Extract the public key from the CSR
  45026. */
  45027. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  45028. /*
  45029. * Verify the signature in the CSR
  45030. */
  45031. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  45032. #ifdef OPENSSL_ALL
  45033. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  45034. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  45035. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  45036. /*
  45037. * Obtain the challenge password from the CSR
  45038. */
  45039. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  45040. 0);
  45041. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  45042. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  45043. AssertNotNull(at->value.asn1_string);
  45044. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  45045. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  45046. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  45047. #endif
  45048. X509_free(req);
  45049. BIO_free(bio);
  45050. EVP_PKEY_free(pub_key);
  45051. }
  45052. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  45053. {
  45054. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  45055. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  45056. /*
  45057. * Extract the public key from the CSR
  45058. */
  45059. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  45060. /*
  45061. * Verify the signature in the CSR
  45062. */
  45063. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  45064. X509_free(req);
  45065. BIO_free(bio);
  45066. /* Run the same test, but with a file pointer instead of a BIO.
  45067. * (PEM_read_X509_REQ)*/
  45068. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  45069. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  45070. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  45071. X509_free(req);
  45072. EVP_PKEY_free(pub_key);
  45073. }
  45074. res = TEST_RES_CHECK(1);
  45075. #endif /* !NO_DSA && !HAVE_SELFTEST */
  45076. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  45077. return res;
  45078. }
  45079. static int test_wolfSSL_PEM_read_X509(void)
  45080. {
  45081. int res = TEST_SKIPPED;
  45082. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  45083. !defined(NO_RSA)
  45084. X509 *x509 = NULL;
  45085. XFILE fp;
  45086. fp = XFOPEN(svrCertFile, "rb");
  45087. AssertTrue((fp != XBADFILE));
  45088. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  45089. X509_free(x509);
  45090. XFCLOSE(fp);
  45091. res = TEST_RES_CHECK(1);
  45092. #endif
  45093. return res;
  45094. }
  45095. static int test_wolfSSL_PEM_read(void)
  45096. {
  45097. int res = TEST_SKIPPED;
  45098. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  45099. const char* filename = "./certs/server-keyEnc.pem";
  45100. XFILE fp;
  45101. char* name = NULL;
  45102. char* header = NULL;
  45103. byte* data = NULL;
  45104. long len;
  45105. EVP_CIPHER_INFO cipher;
  45106. WOLFSSL_BIO* bio;
  45107. byte* fileData;
  45108. size_t fileDataSz;
  45109. byte* out;
  45110. fp = XFOPEN(filename, "rb");
  45111. AssertTrue((fp != XBADFILE));
  45112. /* Fail cases. */
  45113. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  45114. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  45115. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  45116. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  45117. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  45118. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  45119. AssertIntGT(XSTRLEN(header), 0);
  45120. AssertIntGT(len, 0);
  45121. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  45122. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  45123. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  45124. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  45125. DYNAMIC_TYPE_TMP_BUFFER));
  45126. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  45127. XFCLOSE(fp);
  45128. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45129. /* Fail cases. */
  45130. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  45131. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  45132. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  45133. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  45134. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  45135. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  45136. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  45137. /* Fail cases. */
  45138. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  45139. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  45140. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  45141. #ifndef NO_DES3
  45142. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  45143. #endif
  45144. /* Fail cases. */
  45145. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  45146. (void*)"yassl123"), WOLFSSL_FAILURE);
  45147. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  45148. (void*)"yassl123"), WOLFSSL_FAILURE);
  45149. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  45150. (void*)"yassl123"), WOLFSSL_FAILURE);
  45151. #if !defined(NO_DES3) && !defined(NO_MD5)
  45152. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  45153. (void*)"yassl123"), WOLFSSL_SUCCESS);
  45154. #endif
  45155. BIO_free(bio);
  45156. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45157. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45158. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45159. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45160. name = NULL;
  45161. header = NULL;
  45162. data = NULL;
  45163. fp = XFOPEN(svrKeyFile, "rb");
  45164. AssertTrue((fp != XBADFILE));
  45165. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  45166. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  45167. AssertIntEQ(XSTRLEN(header), 0);
  45168. AssertIntGT(len, 0);
  45169. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  45170. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  45171. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  45172. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  45173. DYNAMIC_TYPE_TMP_BUFFER));
  45174. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  45175. XFCLOSE(fp);
  45176. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45177. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  45178. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  45179. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  45180. BIO_free(bio);
  45181. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45182. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45183. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45184. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45185. res = TEST_RES_CHECK(1);
  45186. #endif
  45187. return res;
  45188. }
  45189. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  45190. {
  45191. int res = TEST_SKIPPED;
  45192. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45193. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45194. const byte iv[12] = { 0 };
  45195. const byte key[16] = { 0 };
  45196. const byte cleartext[16] = { 0 };
  45197. const byte aad[] = {
  45198. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  45199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  45200. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  45201. 0x4f
  45202. };
  45203. byte out1Part[16];
  45204. byte outTag1Part[16];
  45205. byte out2Part[16];
  45206. byte outTag2Part[16];
  45207. byte decryptBuf[16];
  45208. int len;
  45209. int tlen;
  45210. EVP_CIPHER_CTX* ctx = NULL;
  45211. /* ENCRYPT */
  45212. /* Send AAD and data in 1 part */
  45213. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  45214. tlen = 0;
  45215. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45216. 1);
  45217. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45218. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  45219. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  45220. sizeof(cleartext)), 1);
  45221. tlen += len;
  45222. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  45223. tlen += len;
  45224. AssertIntEQ(tlen, sizeof(cleartext));
  45225. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  45226. outTag1Part), 1);
  45227. EVP_CIPHER_CTX_free(ctx);
  45228. /* DECRYPT */
  45229. /* Send AAD and data in 1 part */
  45230. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  45231. tlen = 0;
  45232. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45233. 1);
  45234. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45235. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  45236. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  45237. sizeof(cleartext)), 1);
  45238. tlen += len;
  45239. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  45240. outTag1Part), 1);
  45241. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  45242. tlen += len;
  45243. AssertIntEQ(tlen, sizeof(cleartext));
  45244. EVP_CIPHER_CTX_free(ctx);
  45245. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  45246. /* ENCRYPT */
  45247. /* Send AAD and data in 2 parts */
  45248. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  45249. tlen = 0;
  45250. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45251. 1);
  45252. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45253. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  45254. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  45255. 1);
  45256. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  45257. tlen += len;
  45258. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  45259. sizeof(cleartext) - 1), 1);
  45260. tlen += len;
  45261. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  45262. tlen += len;
  45263. AssertIntEQ(tlen, sizeof(cleartext));
  45264. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  45265. outTag2Part), 1);
  45266. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  45267. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  45268. EVP_CIPHER_CTX_free(ctx);
  45269. /* DECRYPT */
  45270. /* Send AAD and data in 2 parts */
  45271. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  45272. tlen = 0;
  45273. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45274. 1);
  45275. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45276. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  45277. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  45278. 1);
  45279. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  45280. tlen += len;
  45281. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  45282. sizeof(cleartext) - 1), 1);
  45283. tlen += len;
  45284. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  45285. outTag1Part), 1);
  45286. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  45287. tlen += len;
  45288. AssertIntEQ(tlen, sizeof(cleartext));
  45289. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  45290. /* Test AAD re-use */
  45291. EVP_CIPHER_CTX_free(ctx);
  45292. res = TEST_RES_CHECK(1);
  45293. #endif
  45294. return res;
  45295. }
  45296. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  45297. {
  45298. int res = TEST_SKIPPED;
  45299. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45300. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45301. /* Zero length plain text */
  45302. byte key[] = {
  45303. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45304. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45305. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45306. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45307. }; /* align */
  45308. byte iv[] = {
  45309. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45310. }; /* align */
  45311. byte plaintxt[1];
  45312. int ivSz = 12;
  45313. int plaintxtSz = 0;
  45314. unsigned char tag[16];
  45315. unsigned char tag_kat[] =
  45316. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  45317. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  45318. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45319. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45320. int ciphertxtSz = 0;
  45321. int decryptedtxtSz = 0;
  45322. int len = 0;
  45323. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  45324. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  45325. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  45326. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45327. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  45328. plaintxtSz));
  45329. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  45330. ciphertxtSz += len;
  45331. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  45332. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  45333. AssertIntEQ(0, ciphertxtSz);
  45334. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  45335. EVP_CIPHER_CTX_init(de);
  45336. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  45337. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45338. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  45339. decryptedtxtSz = len;
  45340. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  45341. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  45342. decryptedtxtSz += len;
  45343. AssertIntEQ(0, decryptedtxtSz);
  45344. EVP_CIPHER_CTX_free(en);
  45345. EVP_CIPHER_CTX_free(de);
  45346. res = TEST_RES_CHECK(1);
  45347. #endif
  45348. return res;
  45349. }
  45350. static int test_wolfssl_EVP_aes_gcm(void)
  45351. {
  45352. int res = TEST_SKIPPED;
  45353. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45354. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45355. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45356. byte *key = (byte*)"01234567890123456789012345678901";
  45357. /* A 128 bit IV */
  45358. byte *iv = (byte*)"0123456789012345";
  45359. int ivSz = AES_BLOCK_SIZE;
  45360. /* Message to be encrypted */
  45361. byte *plaintxt = (byte*)"for things to change you have to change";
  45362. /* Additional non-confidential data */
  45363. byte *aad = (byte*)"Don't spend major time on minor things.";
  45364. unsigned char tag[AES_BLOCK_SIZE] = {0};
  45365. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  45366. int aadSz = (int)XSTRLEN((char*)aad);
  45367. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45368. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45369. int ciphertxtSz = 0;
  45370. int decryptedtxtSz = 0;
  45371. int len = 0;
  45372. int i = 0;
  45373. EVP_CIPHER_CTX en[2];
  45374. EVP_CIPHER_CTX de[2];
  45375. for (i = 0; i < 2; i++) {
  45376. EVP_CIPHER_CTX_init(&en[i]);
  45377. if (i == 0) {
  45378. /* Default uses 96-bits IV length */
  45379. #ifdef WOLFSSL_AES_128
  45380. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  45381. #elif defined(WOLFSSL_AES_192)
  45382. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  45383. #elif defined(WOLFSSL_AES_256)
  45384. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  45385. #endif
  45386. }
  45387. else {
  45388. #ifdef WOLFSSL_AES_128
  45389. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  45390. #elif defined(WOLFSSL_AES_192)
  45391. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  45392. #elif defined(WOLFSSL_AES_256)
  45393. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  45394. #endif
  45395. /* non-default must to set the IV length first */
  45396. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45397. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45398. }
  45399. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45400. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  45401. ciphertxtSz = len;
  45402. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45403. ciphertxtSz += len;
  45404. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  45405. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  45406. EVP_CIPHER_CTX_init(&de[i]);
  45407. if (i == 0) {
  45408. /* Default uses 96-bits IV length */
  45409. #ifdef WOLFSSL_AES_128
  45410. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  45411. #elif defined(WOLFSSL_AES_192)
  45412. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  45413. #elif defined(WOLFSSL_AES_256)
  45414. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  45415. #endif
  45416. }
  45417. else {
  45418. #ifdef WOLFSSL_AES_128
  45419. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  45420. #elif defined(WOLFSSL_AES_192)
  45421. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  45422. #elif defined(WOLFSSL_AES_256)
  45423. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  45424. #endif
  45425. /* non-default must to set the IV length first */
  45426. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45427. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45428. }
  45429. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45430. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45431. decryptedtxtSz = len;
  45432. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  45433. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45434. decryptedtxtSz += len;
  45435. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  45436. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45437. /* modify tag*/
  45438. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45439. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45440. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  45441. /* fail due to wrong tag */
  45442. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45443. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45444. AssertIntEQ(0, len);
  45445. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  45446. }
  45447. res = TEST_RES_CHECK(1);
  45448. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  45449. return res;
  45450. }
  45451. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  45452. {
  45453. int res = TEST_SKIPPED;
  45454. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  45455. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45456. /* Zero length plain text */
  45457. byte key[] = {
  45458. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45459. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45460. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45461. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45462. }; /* align */
  45463. byte iv[] = {
  45464. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45465. }; /* align */
  45466. byte plaintxt[1];
  45467. int ivSz = 12;
  45468. int plaintxtSz = 0;
  45469. unsigned char tag[16];
  45470. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45471. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45472. int ciphertxtSz = 0;
  45473. int decryptedtxtSz = 0;
  45474. int len = 0;
  45475. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  45476. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  45477. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  45478. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  45479. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  45480. plaintxtSz));
  45481. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  45482. ciphertxtSz += len;
  45483. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  45484. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  45485. AssertIntEQ(0, ciphertxtSz);
  45486. EVP_CIPHER_CTX_init(de);
  45487. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  45488. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  45489. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  45490. decryptedtxtSz = len;
  45491. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  45492. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  45493. decryptedtxtSz += len;
  45494. AssertIntEQ(0, decryptedtxtSz);
  45495. EVP_CIPHER_CTX_free(en);
  45496. EVP_CIPHER_CTX_free(de);
  45497. res = TEST_RES_CHECK(1);
  45498. #endif
  45499. return res;
  45500. }
  45501. static int test_wolfssl_EVP_aes_ccm(void)
  45502. {
  45503. int res = TEST_SKIPPED;
  45504. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  45505. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45506. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45507. byte *key = (byte*)"01234567890123456789012345678901";
  45508. /* A 128 bit IV */
  45509. byte *iv = (byte*)"0123456789012";
  45510. int ivSz = (int)XSTRLEN((char*)iv);
  45511. /* Message to be encrypted */
  45512. byte *plaintxt = (byte*)"for things to change you have to change";
  45513. /* Additional non-confidential data */
  45514. byte *aad = (byte*)"Don't spend major time on minor things.";
  45515. unsigned char tag[AES_BLOCK_SIZE] = {0};
  45516. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  45517. int aadSz = (int)XSTRLEN((char*)aad);
  45518. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45519. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45520. int ciphertxtSz = 0;
  45521. int decryptedtxtSz = 0;
  45522. int len = 0;
  45523. int i = 0;
  45524. EVP_CIPHER_CTX en[2];
  45525. EVP_CIPHER_CTX de[2];
  45526. for (i = 0; i < 2; i++) {
  45527. EVP_CIPHER_CTX_init(&en[i]);
  45528. if (i == 0) {
  45529. /* Default uses 96-bits IV length */
  45530. #ifdef WOLFSSL_AES_128
  45531. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  45532. EVP_aes_128_ccm(), NULL, key, iv));
  45533. #elif defined(WOLFSSL_AES_192)
  45534. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  45535. EVP_aes_192_ccm(), NULL, key, iv));
  45536. #elif defined(WOLFSSL_AES_256)
  45537. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  45538. EVP_aes_256_ccm(), NULL, key, iv));
  45539. #endif
  45540. }
  45541. else {
  45542. #ifdef WOLFSSL_AES_128
  45543. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  45544. EVP_aes_128_ccm(), NULL, NULL, NULL));
  45545. #elif defined(WOLFSSL_AES_192)
  45546. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  45547. EVP_aes_192_ccm(), NULL, NULL, NULL));
  45548. #elif defined(WOLFSSL_AES_256)
  45549. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  45550. EVP_aes_256_ccm(), NULL, NULL, NULL));
  45551. #endif
  45552. /* non-default must to set the IV length first */
  45553. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
  45554. EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  45555. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  45556. NULL, NULL, key, iv));
  45557. }
  45558. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45559. AssertIntEQ(1, EVP_EncryptUpdate(&en[i],
  45560. ciphertxt, &len, plaintxt, plaintxtSz));
  45561. ciphertxtSz = len;
  45562. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45563. ciphertxtSz += len;
  45564. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
  45565. EVP_CTRL_CCM_GET_TAG, AES_BLOCK_SIZE, tag));
  45566. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  45567. EVP_CIPHER_CTX_init(&de[i]);
  45568. if (i == 0) {
  45569. /* Default uses 96-bits IV length */
  45570. #ifdef WOLFSSL_AES_128
  45571. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  45572. EVP_aes_128_ccm(), NULL, key, iv));
  45573. #elif defined(WOLFSSL_AES_192)
  45574. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  45575. EVP_aes_192_ccm(), NULL, key, iv));
  45576. #elif defined(WOLFSSL_AES_256)
  45577. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  45578. EVP_aes_256_ccm(), NULL, key, iv));
  45579. #endif
  45580. }
  45581. else {
  45582. #ifdef WOLFSSL_AES_128
  45583. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  45584. EVP_aes_128_ccm(), NULL, NULL, NULL));
  45585. #elif defined(WOLFSSL_AES_192)
  45586. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  45587. EVP_aes_192_ccm(), NULL, NULL, NULL));
  45588. #elif defined(WOLFSSL_AES_256)
  45589. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  45590. EVP_aes_256_ccm(), NULL, NULL, NULL));
  45591. #endif
  45592. /* non-default must to set the IV length first */
  45593. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  45594. EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  45595. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45596. }
  45597. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45598. AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
  45599. decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45600. decryptedtxtSz = len;
  45601. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  45602. EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
  45603. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i],
  45604. decryptedtxt, &len));
  45605. decryptedtxtSz += len;
  45606. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  45607. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45608. /* modify tag*/
  45609. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45610. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45611. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  45612. EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
  45613. /* fail due to wrong tag */
  45614. AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
  45615. decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45616. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45617. AssertIntEQ(0, len);
  45618. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  45619. }
  45620. res = TEST_RES_CHECK(1);
  45621. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  45622. return res;
  45623. }
  45624. static int test_wolfssl_EVP_chacha20_poly1305(void)
  45625. {
  45626. int res = TEST_SKIPPED;
  45627. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45628. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  45629. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  45630. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45631. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  45632. byte cipherText[sizeof(plainText)];
  45633. byte decryptedText[sizeof(plainText)];
  45634. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  45635. EVP_CIPHER_CTX* ctx;
  45636. int outSz;
  45637. /* Encrypt. */
  45638. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45639. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45640. NULL), WOLFSSL_SUCCESS);
  45641. /* Invalid IV length. */
  45642. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45643. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  45644. /* Valid IV length. */
  45645. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45646. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45647. /* Invalid tag length. */
  45648. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45649. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  45650. /* Valid tag length. */
  45651. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45652. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  45653. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45654. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45655. WOLFSSL_SUCCESS);
  45656. AssertIntEQ(outSz, sizeof(aad));
  45657. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45658. sizeof(plainText)), WOLFSSL_SUCCESS);
  45659. AssertIntEQ(outSz, sizeof(plainText));
  45660. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45661. AssertIntEQ(outSz, 0);
  45662. /* Invalid tag length. */
  45663. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45664. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  45665. /* Valid tag length. */
  45666. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45667. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45668. EVP_CIPHER_CTX_free(ctx);
  45669. /* Decrypt. */
  45670. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45671. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45672. NULL), WOLFSSL_SUCCESS);
  45673. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45674. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45675. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45676. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45677. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45678. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45679. WOLFSSL_SUCCESS);
  45680. AssertIntEQ(outSz, sizeof(aad));
  45681. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45682. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45683. AssertIntEQ(outSz, sizeof(cipherText));
  45684. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45685. WOLFSSL_SUCCESS);
  45686. AssertIntEQ(outSz, 0);
  45687. EVP_CIPHER_CTX_free(ctx);
  45688. /* Test partial Inits. CipherInit() allow setting of key and iv
  45689. * in separate calls. */
  45690. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45691. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  45692. key, NULL, 1), WOLFSSL_SUCCESS);
  45693. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45694. WOLFSSL_SUCCESS);
  45695. AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  45696. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  45697. AssertIntEQ(outSz, sizeof(aad));
  45698. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45699. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45700. AssertIntEQ(outSz, sizeof(cipherText));
  45701. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45702. WOLFSSL_SUCCESS);
  45703. AssertIntEQ(outSz, 0);
  45704. EVP_CIPHER_CTX_free(ctx);
  45705. res = TEST_RES_CHECK(1);
  45706. #endif
  45707. return res;
  45708. }
  45709. static int test_wolfssl_EVP_chacha20(void)
  45710. {
  45711. int res = TEST_SKIPPED;
  45712. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  45713. byte key[CHACHA_MAX_KEY_SZ];
  45714. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  45715. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45716. byte cipherText[sizeof(plainText)];
  45717. byte decryptedText[sizeof(plainText)];
  45718. EVP_CIPHER_CTX* ctx;
  45719. int outSz;
  45720. /* Encrypt. */
  45721. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45722. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45723. NULL), WOLFSSL_SUCCESS);
  45724. /* Any tag length must fail - not an AEAD cipher. */
  45725. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45726. 16, NULL), WOLFSSL_FAILURE);
  45727. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45728. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45729. sizeof(plainText)), WOLFSSL_SUCCESS);
  45730. AssertIntEQ(outSz, sizeof(plainText));
  45731. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45732. AssertIntEQ(outSz, 0);
  45733. EVP_CIPHER_CTX_free(ctx);
  45734. /* Decrypt. */
  45735. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45736. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45737. NULL), WOLFSSL_SUCCESS);
  45738. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45739. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45740. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45741. AssertIntEQ(outSz, sizeof(cipherText));
  45742. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45743. WOLFSSL_SUCCESS);
  45744. AssertIntEQ(outSz, 0);
  45745. EVP_CIPHER_CTX_free(ctx);
  45746. /* Test partial Inits. CipherInit() allow setting of key and iv
  45747. * in separate calls. */
  45748. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45749. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  45750. key, NULL, 1), WOLFSSL_SUCCESS);
  45751. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45752. WOLFSSL_SUCCESS);
  45753. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45754. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45755. AssertIntEQ(outSz, sizeof(cipherText));
  45756. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45757. WOLFSSL_SUCCESS);
  45758. AssertIntEQ(outSz, 0);
  45759. EVP_CIPHER_CTX_free(ctx);
  45760. res = TEST_RES_CHECK(1);
  45761. #endif
  45762. return res;
  45763. }
  45764. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  45765. {
  45766. int res = TEST_SKIPPED;
  45767. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  45768. EVP_PKEY_CTX* ctx;
  45769. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  45770. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  45771. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  45772. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  45773. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  45774. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  45775. byte outKey[34];
  45776. size_t outKeySz = sizeof(outKey);
  45777. /* These expected outputs were gathered by running the same test below using
  45778. * OpenSSL. */
  45779. const byte extractAndExpand[] = {
  45780. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  45781. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  45782. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  45783. };
  45784. const byte extractOnly[] = {
  45785. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  45786. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  45787. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  45788. };
  45789. const byte expandOnly[] = {
  45790. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  45791. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  45792. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  45793. };
  45794. const byte extractAndExpandAddInfo[] = {
  45795. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  45796. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  45797. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  45798. };
  45799. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  45800. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  45801. /* NULL ctx. */
  45802. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45803. /* NULL md. */
  45804. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  45805. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  45806. /* NULL ctx. */
  45807. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  45808. WOLFSSL_FAILURE);
  45809. /* NULL salt is ok. */
  45810. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  45811. WOLFSSL_SUCCESS);
  45812. /* Salt length <= 0. */
  45813. /* Length 0 salt is ok. */
  45814. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  45815. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  45816. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  45817. WOLFSSL_SUCCESS);
  45818. /* NULL ctx. */
  45819. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  45820. WOLFSSL_FAILURE);
  45821. /* NULL key. */
  45822. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  45823. WOLFSSL_FAILURE);
  45824. /* Key length <= 0 */
  45825. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  45826. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  45827. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  45828. WOLFSSL_SUCCESS);
  45829. /* NULL ctx. */
  45830. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  45831. WOLFSSL_FAILURE);
  45832. /* NULL info is ok. */
  45833. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  45834. WOLFSSL_SUCCESS);
  45835. /* Info length <= 0 */
  45836. /* Length 0 info is ok. */
  45837. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  45838. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  45839. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  45840. WOLFSSL_SUCCESS);
  45841. /* NULL ctx. */
  45842. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  45843. WOLFSSL_FAILURE);
  45844. /* Extract and expand (default). */
  45845. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45846. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  45847. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  45848. /* Extract only. */
  45849. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  45850. WOLFSSL_SUCCESS);
  45851. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45852. AssertIntEQ(outKeySz, sizeof(extractOnly));
  45853. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  45854. outKeySz = sizeof(outKey);
  45855. /* Expand only. */
  45856. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  45857. WOLFSSL_SUCCESS);
  45858. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45859. AssertIntEQ(outKeySz, sizeof(expandOnly));
  45860. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  45861. outKeySz = sizeof(outKey);
  45862. /* Extract and expand with appended additional info. */
  45863. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  45864. WOLFSSL_SUCCESS);
  45865. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  45866. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  45867. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45868. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  45869. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  45870. EVP_PKEY_CTX_free(ctx);
  45871. res = TEST_RES_CHECK(1);
  45872. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  45873. return res;
  45874. }
  45875. #ifndef NO_BIO
  45876. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  45877. {
  45878. int res = TEST_SKIPPED;
  45879. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45880. BIO* bio;
  45881. X509_INFO* info;
  45882. STACK_OF(X509_INFO)* sk;
  45883. char* subject;
  45884. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  45885. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  45886. AssertNotNull(bio = BIO_new(BIO_s_file()));
  45887. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  45888. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  45889. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  45890. /* using dereference to maintain testing for Apache port*/
  45891. AssertNotNull(info = sk_X509_INFO_pop(sk));
  45892. AssertNotNull(subject =
  45893. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  45894. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  45895. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  45896. X509_INFO_free(info);
  45897. AssertNotNull(info = sk_X509_INFO_pop(sk));
  45898. AssertNotNull(subject =
  45899. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  45900. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  45901. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  45902. X509_INFO_free(info);
  45903. AssertNull(info = sk_X509_INFO_pop(sk));
  45904. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  45905. BIO_free(bio);
  45906. res = TEST_RES_CHECK(1);
  45907. #endif
  45908. return res;
  45909. }
  45910. #endif /* !NO_BIO */
  45911. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  45912. {
  45913. int res = TEST_SKIPPED;
  45914. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45915. X509 *x509;
  45916. X509_NAME* name;
  45917. int idx = 0;
  45918. X509_NAME_ENTRY *ne;
  45919. ASN1_OBJECT *object = NULL;
  45920. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  45921. AssertNotNull(x509);
  45922. name = X509_get_subject_name(x509);
  45923. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  45924. AssertIntGE(idx, 0);
  45925. ne = X509_NAME_get_entry(name, idx);
  45926. AssertNotNull(ne);
  45927. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  45928. X509_free(x509);
  45929. res = TEST_RES_CHECK(1);
  45930. #endif
  45931. return res;
  45932. }
  45933. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  45934. {
  45935. int res = TEST_SKIPPED;
  45936. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  45937. ASN1_INTEGER *a;
  45938. long val;
  45939. int ret;
  45940. a = ASN1_INTEGER_new();
  45941. val = 0;
  45942. ret = ASN1_INTEGER_set(NULL, val);
  45943. AssertIntEQ(ret, 0);
  45944. ASN1_INTEGER_free(a);
  45945. /* 0 */
  45946. a = ASN1_INTEGER_new();
  45947. val = 0;
  45948. ret = ASN1_INTEGER_set(a, val);
  45949. AssertIntEQ(ret, 1);
  45950. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45951. ASN1_INTEGER_free(a);
  45952. /* 40 */
  45953. a = ASN1_INTEGER_new();
  45954. val = 40;
  45955. ret = ASN1_INTEGER_set(a, val);
  45956. AssertIntEQ(ret, 1);
  45957. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45958. ASN1_INTEGER_free(a);
  45959. /* -40 */
  45960. a = ASN1_INTEGER_new();
  45961. val = -40;
  45962. ret = ASN1_INTEGER_set(a, val);
  45963. AssertIntEQ(ret, 1);
  45964. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45965. ASN1_INTEGER_free(a);
  45966. /* 128 */
  45967. a = ASN1_INTEGER_new();
  45968. val = 128;
  45969. ret = ASN1_INTEGER_set(a, val);
  45970. AssertIntEQ(ret, 1);
  45971. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45972. ASN1_INTEGER_free(a);
  45973. /* -128 */
  45974. a = ASN1_INTEGER_new();
  45975. val = -128;
  45976. ret = ASN1_INTEGER_set(a, val);
  45977. AssertIntEQ(ret, 1);
  45978. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45979. ASN1_INTEGER_free(a);
  45980. /* 200 */
  45981. a = ASN1_INTEGER_new();
  45982. val = 200;
  45983. ret = ASN1_INTEGER_set(a, val);
  45984. AssertIntEQ(ret, 1);
  45985. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45986. ASN1_INTEGER_free(a);
  45987. /* int max (2147483647) */
  45988. a = ASN1_INTEGER_new();
  45989. val = 2147483647;
  45990. ret = ASN1_INTEGER_set(a, val);
  45991. AssertIntEQ(ret, 1);
  45992. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45993. ASN1_INTEGER_free(a);
  45994. /* int min (-2147483648) */
  45995. a = ASN1_INTEGER_new();
  45996. val = -2147483647 - 1;
  45997. ret = ASN1_INTEGER_set(a, val);
  45998. AssertIntEQ(ret, 1);
  45999. AssertIntEQ(ASN1_INTEGER_get(a), val);
  46000. ASN1_INTEGER_free(a);
  46001. res = TEST_RES_CHECK(1);
  46002. #endif
  46003. return res;
  46004. }
  46005. #if defined(OPENSSL_EXTRA)
  46006. typedef struct ASN1IntTestVector {
  46007. const byte* der;
  46008. const size_t derSz;
  46009. const long value;
  46010. } ASN1IntTestVector;
  46011. #endif
  46012. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  46013. {
  46014. int res = TEST_SKIPPED;
  46015. #if defined(OPENSSL_EXTRA)
  46016. size_t i;
  46017. WOLFSSL_ASN1_INTEGER* a = NULL;
  46018. WOLFSSL_ASN1_INTEGER* b = NULL;
  46019. WOLFSSL_ASN1_INTEGER* c = NULL;
  46020. const byte* p = NULL;
  46021. byte* reEncoded = NULL;
  46022. int reEncodedSz;
  46023. static const byte zeroDer[] = {
  46024. 0x02, 0x01, 0x00
  46025. };
  46026. static const byte oneDer[] = {
  46027. 0x02, 0x01, 0x01
  46028. };
  46029. static const byte negativeDer[] = {
  46030. 0x02, 0x03, 0xC1, 0x16, 0x0D
  46031. };
  46032. static const byte positiveDer[] = {
  46033. 0x02, 0x03, 0x01, 0x00, 0x01
  46034. };
  46035. static const byte primeDer[] = {
  46036. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  46037. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  46038. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  46039. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  46040. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  46041. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  46042. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  46043. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  46044. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  46045. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  46046. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  46047. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  46048. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  46049. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  46050. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  46051. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  46052. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  46053. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  46054. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  46055. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  46056. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  46057. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  46058. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  46059. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  46060. };
  46061. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  46062. static const ASN1IntTestVector testVectors[] = {
  46063. {zeroDer, sizeof(zeroDer), 0},
  46064. {oneDer, sizeof(oneDer), 1},
  46065. {negativeDer, sizeof(negativeDer), -4123123},
  46066. {positiveDer, sizeof(positiveDer), 65537},
  46067. {primeDer, sizeof(primeDer), 0}
  46068. };
  46069. static const size_t NUM_TEST_VECTORS = sizeof(testVectors)/sizeof(testVectors[0]);
  46070. /* Check d2i error conditions */
  46071. /* NULL pointer to input. */
  46072. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  46073. AssertNull(b);
  46074. /* NULL input. */
  46075. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  46076. AssertNull(b);
  46077. /* 0 length. */
  46078. p = testVectors[0].der;
  46079. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  46080. AssertNull(b);
  46081. /* Negative length. */
  46082. p = testVectors[0].der;
  46083. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  46084. AssertNull(b);
  46085. /* Garbage DER input. */
  46086. p = garbageDer;
  46087. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  46088. AssertNull(b);
  46089. {
  46090. /* Check i2d error conditions */
  46091. /* NULL input. */
  46092. byte* p2 = NULL;
  46093. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  46094. /* 0 length input data buffer (a->length == 0). */
  46095. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  46096. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  46097. a->data = NULL;
  46098. /* NULL input data buffer. */
  46099. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  46100. /* Reset a->data. */
  46101. a->data = a->intData;
  46102. /* Set a to valid value. */
  46103. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  46104. /* NULL output buffer. */
  46105. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  46106. wolfSSL_ASN1_INTEGER_free(a);
  46107. }
  46108. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  46109. p = testVectors[i].der;
  46110. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  46111. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  46112. if (testVectors[i].derSz <= sizeof(long)) {
  46113. c = wolfSSL_ASN1_INTEGER_new();
  46114. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  46115. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  46116. wolfSSL_ASN1_INTEGER_free(c);
  46117. }
  46118. /* Convert to DER without a pre-allocated output buffer. */
  46119. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  46120. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  46121. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  46122. /* Convert to DER with a pre-allocated output buffer. In this case, the
  46123. * output buffer pointer should be incremented just past the end of the
  46124. * encoded data. */
  46125. p = reEncoded;
  46126. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  46127. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  46128. AssertPtrEq(p, reEncoded - reEncodedSz);
  46129. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  46130. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  46131. reEncoded = NULL;
  46132. wolfSSL_ASN1_INTEGER_free(a);
  46133. }
  46134. res = TEST_RES_CHECK(1);
  46135. #endif /* OPENSSL_EXTRA */
  46136. return res;
  46137. }
  46138. static int test_wolfSSL_X509_STORE_get1_certs(void)
  46139. {
  46140. int res = TEST_SKIPPED;
  46141. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  46142. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46143. X509_STORE_CTX *storeCtx;
  46144. X509_STORE *store;
  46145. X509 *caX509;
  46146. X509 *svrX509;
  46147. X509_NAME *subject;
  46148. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  46149. AssertNotNull(caX509 =
  46150. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  46151. AssertNotNull((svrX509 =
  46152. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  46153. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  46154. AssertNotNull(store = X509_STORE_new());
  46155. AssertNotNull(subject = X509_get_subject_name(caX509));
  46156. /* Errors */
  46157. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  46158. AssertNull(X509_STORE_get1_certs(NULL, subject));
  46159. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  46160. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  46161. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  46162. /* Should find the cert */
  46163. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  46164. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  46165. sk_X509_pop_free(certs, NULL);
  46166. /* Should not find the cert */
  46167. AssertNotNull(subject = X509_get_subject_name(svrX509));
  46168. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  46169. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  46170. sk_X509_pop_free(certs, NULL);
  46171. X509_STORE_free(store);
  46172. X509_STORE_CTX_free(storeCtx);
  46173. X509_free(svrX509);
  46174. X509_free(caX509);
  46175. res = TEST_RES_CHECK(1);
  46176. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  46177. return res;
  46178. }
  46179. /* Testing code used in dpp.c in hostap */
  46180. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46181. typedef struct {
  46182. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  46183. * as an OID identifying the curve */
  46184. X509_ALGOR *alg;
  46185. /* Compressed format public key per ANSI X9.63 */
  46186. ASN1_BIT_STRING *pub_key;
  46187. } DPP_BOOTSTRAPPING_KEY;
  46188. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  46189. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  46190. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  46191. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  46192. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  46193. #endif
  46194. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  46195. {
  46196. int res = TEST_SKIPPED;
  46197. /* Testing code used in dpp.c in hostap */
  46198. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46199. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46200. EC_KEY *eckey;
  46201. EVP_PKEY *key;
  46202. size_t len;
  46203. unsigned char *der = NULL;
  46204. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  46205. const unsigned char *in = ecc_clikey_der_256;
  46206. const EC_GROUP *group;
  46207. const EC_POINT *point;
  46208. int nid;
  46209. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  46210. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  46211. (long)sizeof_ecc_clikey_der_256));
  46212. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  46213. AssertNotNull(group = EC_KEY_get0_group(eckey));
  46214. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  46215. nid = EC_GROUP_get_curve_name(group);
  46216. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  46217. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  46218. AssertIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  46219. #ifdef HAVE_COMP_KEY
  46220. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  46221. NULL, 0, NULL)), 0);
  46222. #else
  46223. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  46224. NULL, 0, NULL)), 0);
  46225. #endif
  46226. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  46227. #ifdef HAVE_COMP_KEY
  46228. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  46229. der, len-1, NULL), 0);
  46230. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  46231. der, len, NULL), len);
  46232. #else
  46233. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  46234. der, len-1, NULL), 0);
  46235. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  46236. der, len, NULL), len);
  46237. #endif
  46238. bootstrap->pub_key->data = der;
  46239. bootstrap->pub_key->length = (int)len;
  46240. /* Not actually used */
  46241. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  46242. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  46243. der = NULL;
  46244. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  46245. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  46246. EVP_PKEY_free(key);
  46247. EC_KEY_free(eckey);
  46248. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  46249. res = TEST_RES_CHECK(1);
  46250. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  46251. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  46252. return res;
  46253. }
  46254. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  46255. {
  46256. int res = TEST_SKIPPED;
  46257. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  46258. ASN1_INTEGER *a;
  46259. unsigned char *pp,*tpp;
  46260. int ret;
  46261. a = wolfSSL_ASN1_INTEGER_new();
  46262. /* 40 */
  46263. a->intData[0] = ASN_INTEGER;
  46264. a->intData[1] = 1;
  46265. a->intData[2] = 40;
  46266. ret = i2c_ASN1_INTEGER(a, NULL);
  46267. AssertIntEQ(ret, 1);
  46268. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  46269. DYNAMIC_TYPE_TMP_BUFFER));
  46270. tpp = pp;
  46271. XMEMSET(pp, 0, ret + 1);
  46272. i2c_ASN1_INTEGER(a, &pp);
  46273. pp--;
  46274. AssertIntEQ(*pp, 40);
  46275. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46276. /* 128 */
  46277. a->intData[0] = ASN_INTEGER;
  46278. a->intData[1] = 1;
  46279. a->intData[2] = 128;
  46280. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  46281. AssertIntEQ(ret, 2);
  46282. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  46283. DYNAMIC_TYPE_TMP_BUFFER));
  46284. tpp = pp;
  46285. XMEMSET(pp, 0, ret + 1);
  46286. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  46287. pp--;
  46288. AssertIntEQ(*(pp--), 128);
  46289. AssertIntEQ(*pp, 0);
  46290. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46291. /* -40 */
  46292. a->intData[0] = ASN_INTEGER;
  46293. a->intData[1] = 1;
  46294. a->intData[2] = 40;
  46295. a->negative = 1;
  46296. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  46297. AssertIntEQ(ret, 1);
  46298. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  46299. DYNAMIC_TYPE_TMP_BUFFER));
  46300. tpp = pp;
  46301. XMEMSET(pp, 0, ret + 1);
  46302. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  46303. pp--;
  46304. AssertIntEQ(*pp, 216);
  46305. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46306. /* -128 */
  46307. a->intData[0] = ASN_INTEGER;
  46308. a->intData[1] = 1;
  46309. a->intData[2] = 128;
  46310. a->negative = 1;
  46311. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  46312. AssertIntEQ(ret, 1);
  46313. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  46314. DYNAMIC_TYPE_TMP_BUFFER));
  46315. tpp = pp;
  46316. XMEMSET(pp, 0, ret + 1);
  46317. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  46318. pp--;
  46319. AssertIntEQ(*pp, 128);
  46320. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46321. /* -200 */
  46322. a->intData[0] = ASN_INTEGER;
  46323. a->intData[1] = 1;
  46324. a->intData[2] = 200;
  46325. a->negative = 1;
  46326. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  46327. AssertIntEQ(ret, 2);
  46328. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  46329. DYNAMIC_TYPE_TMP_BUFFER));
  46330. tpp = pp;
  46331. XMEMSET(pp, 0, ret + 1);
  46332. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  46333. pp--;
  46334. AssertIntEQ(*(pp--), 56);
  46335. AssertIntEQ(*pp, 255);
  46336. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46337. wolfSSL_ASN1_INTEGER_free(a);
  46338. res = TEST_RES_CHECK(1);
  46339. #endif /* OPENSSL_EXTRA && !NO_ASN */
  46340. return res;
  46341. }
  46342. #ifndef NO_INLINE
  46343. #define WOLFSSL_MISC_INCLUDED
  46344. #include <wolfcrypt/src/misc.c>
  46345. #else
  46346. #include <wolfssl/wolfcrypt/misc.h>
  46347. #endif
  46348. static int test_ForceZero(void)
  46349. {
  46350. unsigned char data[32];
  46351. unsigned int i, j, len;
  46352. /* Test case with 0 length */
  46353. ForceZero(data, 0);
  46354. /* Test ForceZero */
  46355. for (i = 0; i < sizeof(data); i++) {
  46356. for (len = 1; len < sizeof(data) - i; len++) {
  46357. for (j = 0; j < sizeof(data); j++)
  46358. data[j] = j + 1;
  46359. ForceZero(data + i, len);
  46360. for (j = 0; j < sizeof(data); j++) {
  46361. if (j < i || j >= i + len) {
  46362. if (data[j] == 0x00)
  46363. return -10200;
  46364. }
  46365. else if (data[j] != 0x00)
  46366. return -10201;
  46367. }
  46368. }
  46369. }
  46370. return TEST_RES_CHECK(1);
  46371. }
  46372. #ifndef NO_BIO
  46373. static int test_wolfSSL_X509_print(void)
  46374. {
  46375. int res = TEST_SKIPPED;
  46376. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46377. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  46378. X509 *x509;
  46379. BIO *bio;
  46380. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  46381. const X509_ALGOR *cert_sig_alg;
  46382. #endif
  46383. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  46384. AssertNotNull(x509);
  46385. /* print to memory */
  46386. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  46387. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  46388. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  46389. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  46390. /* Will print IP address subject alt name. */
  46391. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  46392. #else
  46393. /* Will print IP address subject alt name. */
  46394. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  46395. #endif
  46396. #elif defined(NO_ASN_TIME)
  46397. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  46398. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  46399. #else
  46400. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  46401. #endif
  46402. BIO_free(bio);
  46403. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46404. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  46405. /* Print signature */
  46406. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  46407. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  46408. #endif
  46409. /* print to stderr */
  46410. #if !defined(NO_WOLFSSL_DIR)
  46411. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  46412. #endif
  46413. /* print again */
  46414. AssertIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  46415. X509_free(x509);
  46416. BIO_free(bio);
  46417. res = TEST_RES_CHECK(1);
  46418. #endif
  46419. return res;
  46420. }
  46421. static int test_wolfSSL_X509_CRL_print(void)
  46422. {
  46423. int res = TEST_SKIPPED;
  46424. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  46425. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  46426. X509_CRL* crl;
  46427. BIO *bio;
  46428. XFILE fp;
  46429. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  46430. AssertTrue((fp != XBADFILE));
  46431. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46432. NULL, NULL));
  46433. XFCLOSE(fp);
  46434. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  46435. AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  46436. X509_CRL_free(crl);
  46437. BIO_free(bio);
  46438. res = TEST_RES_CHECK(1);
  46439. #endif
  46440. return res;
  46441. }
  46442. static int test_wolfSSL_BIO_get_len(void)
  46443. {
  46444. int res = TEST_SKIPPED;
  46445. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  46446. BIO *bio = NULL;
  46447. const char txt[] = "Some example text to push to the BIO.";
  46448. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  46449. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  46450. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  46451. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  46452. BIO_free(bio);
  46453. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46454. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  46455. BIO_free(bio);
  46456. res = TEST_RES_CHECK(1);
  46457. #endif
  46458. return res;
  46459. }
  46460. static int test_wolfSSL_ASN1_STRING_print(void)
  46461. {
  46462. int res = TEST_SKIPPED;
  46463. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  46464. ASN1_STRING* asnStr = NULL;
  46465. const char HELLO_DATA[]= \
  46466. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  46467. #define MAX_UNPRINTABLE_CHAR 32
  46468. #define MAX_BUF 255
  46469. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  46470. unsigned char expected[sizeof(unprintableData)+1];
  46471. unsigned char rbuf[MAX_BUF];
  46472. BIO *bio;
  46473. int p_len, i;
  46474. /* setup */
  46475. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  46476. unprintableData[i] = HELLO_DATA[i];
  46477. expected[i] = HELLO_DATA[i];
  46478. }
  46479. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  46480. unprintableData[sizeof(HELLO_DATA)+i] = i;
  46481. if (i == (int)'\n' || i == (int)'\r')
  46482. expected[sizeof(HELLO_DATA)+i] = i;
  46483. else
  46484. expected[sizeof(HELLO_DATA)+i] = '.';
  46485. }
  46486. unprintableData[sizeof(unprintableData)-1] = '\0';
  46487. expected[sizeof(expected)-1] = '\0';
  46488. XMEMSET(rbuf, 0, MAX_BUF);
  46489. bio = BIO_new(BIO_s_mem());
  46490. BIO_set_write_buf_size(bio, MAX_BUF);
  46491. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  46492. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  46493. (int)sizeof(unprintableData));
  46494. /* test */
  46495. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  46496. AssertIntEQ(p_len, 46);
  46497. BIO_read(bio, (void*)rbuf, 46);
  46498. AssertStrEQ((char*)rbuf, (const char*)expected);
  46499. BIO_free(bio);
  46500. ASN1_STRING_free(asnStr);
  46501. res = TEST_RES_CHECK(1);
  46502. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  46503. return res;
  46504. }
  46505. #endif /* !NO_BIO */
  46506. static int test_wolfSSL_ASN1_get_object(void)
  46507. {
  46508. int res = TEST_SKIPPED;
  46509. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46510. const unsigned char* derBuf = cliecc_cert_der_256;
  46511. int len = sizeof_cliecc_cert_der_256;
  46512. long asnLen = 0;
  46513. int tag = 0, cls = 0;
  46514. ASN1_OBJECT *a;
  46515. /* Read a couple TLV triplets and make sure they match the expected values */
  46516. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  46517. AssertIntEQ(asnLen, 862);
  46518. AssertIntEQ(tag, 0x10);
  46519. AssertIntEQ(cls, 0);
  46520. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46521. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46522. AssertIntEQ(asnLen, 772);
  46523. AssertIntEQ(tag, 0x10);
  46524. AssertIntEQ(cls, 0);
  46525. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46526. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46527. AssertIntEQ(asnLen, 3);
  46528. AssertIntEQ(tag, 0);
  46529. AssertIntEQ(cls, 0x80);
  46530. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46531. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46532. AssertIntEQ(asnLen, 1);
  46533. AssertIntEQ(tag, 0x2);
  46534. AssertIntEQ(cls, 0);
  46535. derBuf += asnLen;
  46536. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46537. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46538. AssertIntEQ(asnLen, 20);
  46539. AssertIntEQ(tag, 0x2);
  46540. AssertIntEQ(cls, 0);
  46541. derBuf += asnLen;
  46542. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46543. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46544. AssertIntEQ(asnLen, 10);
  46545. AssertIntEQ(tag, 0x10);
  46546. AssertIntEQ(cls, 0);
  46547. /* Read an ASN OBJECT */
  46548. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  46549. ASN1_OBJECT_free(a);
  46550. res = TEST_RES_CHECK(1);
  46551. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  46552. return res;
  46553. }
  46554. static int test_wolfSSL_RSA(void)
  46555. {
  46556. int res = TEST_SKIPPED;
  46557. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  46558. defined(WOLFSSL_KEY_GEN)
  46559. RSA* rsa;
  46560. const BIGNUM *n;
  46561. const BIGNUM *e;
  46562. const BIGNUM *d;
  46563. const BIGNUM *p;
  46564. const BIGNUM *q;
  46565. const BIGNUM *dmp1;
  46566. const BIGNUM *dmq1;
  46567. const BIGNUM *iqmp;
  46568. AssertNotNull(rsa = RSA_new());
  46569. AssertIntEQ(RSA_size(NULL), 0);
  46570. AssertIntEQ(RSA_size(rsa), 0);
  46571. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  46572. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  46573. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  46574. #ifdef WOLFSSL_RSA_KEY_CHECK
  46575. AssertIntEQ(RSA_check_key(rsa), 0);
  46576. #endif
  46577. RSA_free(rsa);
  46578. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46579. AssertIntEQ(RSA_size(rsa), 256);
  46580. #ifdef WOLFSSL_RSA_KEY_CHECK
  46581. AssertIntEQ(RSA_check_key(NULL), 0);
  46582. AssertIntEQ(RSA_check_key(rsa), 1);
  46583. #endif
  46584. /* sanity check */
  46585. AssertIntEQ(RSA_bits(NULL), 0);
  46586. /* key */
  46587. AssertIntEQ(RSA_bits(rsa), 2048);
  46588. RSA_get0_key(rsa, &n, &e, &d);
  46589. AssertPtrEq(rsa->n, n);
  46590. AssertPtrEq(rsa->e, e);
  46591. AssertPtrEq(rsa->d, d);
  46592. AssertNotNull(n = BN_new());
  46593. AssertNotNull(e = BN_new());
  46594. AssertNotNull(d = BN_new());
  46595. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  46596. AssertPtrEq(rsa->n, n);
  46597. AssertPtrEq(rsa->e, e);
  46598. AssertPtrEq(rsa->d, d);
  46599. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  46600. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  46601. /* crt_params */
  46602. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  46603. AssertPtrEq(rsa->dmp1, dmp1);
  46604. AssertPtrEq(rsa->dmq1, dmq1);
  46605. AssertPtrEq(rsa->iqmp, iqmp);
  46606. AssertNotNull(dmp1 = BN_new());
  46607. AssertNotNull(dmq1 = BN_new());
  46608. AssertNotNull(iqmp = BN_new());
  46609. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46610. (BIGNUM*)iqmp), 1);
  46611. AssertPtrEq(rsa->dmp1, dmp1);
  46612. AssertPtrEq(rsa->dmq1, dmq1);
  46613. AssertPtrEq(rsa->iqmp, iqmp);
  46614. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  46615. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46616. (BIGNUM*)iqmp), 0);
  46617. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  46618. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  46619. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  46620. AssertNull(dmp1);
  46621. AssertNull(dmq1);
  46622. AssertNull(iqmp);
  46623. /* factors */
  46624. RSA_get0_factors(rsa, NULL, NULL);
  46625. RSA_get0_factors(rsa, &p, &q);
  46626. AssertPtrEq(rsa->p, p);
  46627. AssertPtrEq(rsa->q, q);
  46628. AssertNotNull(p = BN_new());
  46629. AssertNotNull(q = BN_new());
  46630. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  46631. AssertPtrEq(rsa->p, p);
  46632. AssertPtrEq(rsa->q, q);
  46633. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  46634. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  46635. RSA_get0_factors(NULL, NULL, NULL);
  46636. RSA_get0_factors(NULL, &p, &q);
  46637. AssertNull(p);
  46638. AssertNull(q);
  46639. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  46640. AssertIntEQ(RSA_bits(rsa), 21);
  46641. RSA_free(rsa);
  46642. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  46643. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  46644. AssertIntEQ(RSA_size(rsa), 384);
  46645. AssertIntEQ(RSA_bits(rsa), 3072);
  46646. RSA_free(rsa);
  46647. #endif
  46648. /* remove for now with odd key size until adjusting rsa key size check with
  46649. wc_MakeRsaKey()
  46650. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  46651. RSA_free(rsa);
  46652. */
  46653. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  46654. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  46655. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  46656. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  46657. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  46658. {
  46659. byte buff[FOURK_BUF];
  46660. byte der[FOURK_BUF];
  46661. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  46662. XFILE f;
  46663. int bytes;
  46664. /* test loading encrypted RSA private pem w/o password */
  46665. f = XFOPEN(PrivKeyPemFile, "rb");
  46666. AssertTrue((f != XBADFILE));
  46667. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  46668. XFCLOSE(f);
  46669. XMEMSET(der, 0, sizeof(der));
  46670. /* test that error value is returned with no password */
  46671. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  46672. }
  46673. #endif
  46674. res = TEST_RES_CHECK(1);
  46675. #endif
  46676. return res;
  46677. }
  46678. static int test_wolfSSL_RSA_DER(void)
  46679. {
  46680. int res = TEST_SKIPPED;
  46681. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46682. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  46683. RSA *rsa;
  46684. int i;
  46685. const unsigned char *buff = NULL;
  46686. unsigned char *newBuff = NULL;
  46687. struct tbl_s
  46688. {
  46689. const unsigned char *der;
  46690. int sz;
  46691. } tbl[] = {
  46692. #ifdef USE_CERT_BUFFERS_1024
  46693. {client_key_der_1024, sizeof_client_key_der_1024},
  46694. {server_key_der_1024, sizeof_server_key_der_1024},
  46695. #endif
  46696. #ifdef USE_CERT_BUFFERS_2048
  46697. {client_key_der_2048, sizeof_client_key_der_2048},
  46698. {server_key_der_2048, sizeof_server_key_der_2048},
  46699. #endif
  46700. {NULL, 0}
  46701. };
  46702. /* Public Key DER */
  46703. struct tbl_s pub[] = {
  46704. #ifdef USE_CERT_BUFFERS_1024
  46705. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  46706. #endif
  46707. #ifdef USE_CERT_BUFFERS_2048
  46708. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  46709. #endif
  46710. {NULL, 0}
  46711. };
  46712. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  46713. buff = pub[0].der;
  46714. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  46715. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  46716. buff = tbl[0].der;
  46717. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  46718. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  46719. rsa = RSA_new();
  46720. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  46721. RSA_free(rsa);
  46722. for (i = 0; tbl[i].der != NULL; i++)
  46723. {
  46724. /* Passing in pointer results in pointer moving. */
  46725. buff = tbl[i].der;
  46726. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  46727. AssertNotNull(rsa);
  46728. RSA_free(rsa);
  46729. }
  46730. for (i = 0; tbl[i].der != NULL; i++)
  46731. {
  46732. /* Passing in pointer results in pointer moving. */
  46733. buff = tbl[i].der;
  46734. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  46735. AssertNotNull(rsa);
  46736. RSA_free(rsa);
  46737. }
  46738. for (i = 0; pub[i].der != NULL; i++)
  46739. {
  46740. buff = pub[i].der;
  46741. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  46742. AssertNotNull(rsa);
  46743. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  46744. newBuff = NULL;
  46745. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  46746. AssertNotNull(newBuff);
  46747. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  46748. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46749. RSA_free(rsa);
  46750. }
  46751. res = TEST_RES_CHECK(1);
  46752. #endif
  46753. return res;
  46754. }
  46755. static int test_wolfSSL_RSA_print(void)
  46756. {
  46757. int res = TEST_SKIPPED;
  46758. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46759. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46760. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  46761. BIO *bio;
  46762. WOLFSSL_RSA* rsa = NULL;
  46763. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46764. AssertNotNull(rsa = RSA_new());
  46765. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  46766. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  46767. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  46768. AssertIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  46769. /* Some very large number of indent spaces. */
  46770. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  46771. /* RSA is empty. */
  46772. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  46773. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  46774. RSA_free(rsa);
  46775. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46776. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  46777. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  46778. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  46779. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  46780. AssertIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  46781. AssertIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  46782. BIO_free(bio);
  46783. RSA_free(rsa);
  46784. res = TEST_RES_CHECK(1);
  46785. #endif
  46786. return res;
  46787. }
  46788. #ifndef NO_RSA
  46789. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  46790. {
  46791. int res = TEST_SKIPPED;
  46792. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46793. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46794. RSA *rsa;
  46795. const unsigned char *derBuf = client_key_der_2048;
  46796. unsigned char em[256] = {0}; /* len = 2048/8 */
  46797. /* Random data simulating a hash */
  46798. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  46799. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46800. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46801. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46802. };
  46803. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46804. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  46805. RSA_PSS_SALTLEN_DIGEST), 0);
  46806. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  46807. RSA_PSS_SALTLEN_DIGEST), 0);
  46808. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  46809. RSA_PSS_SALTLEN_DIGEST), 0);
  46810. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  46811. RSA_PSS_SALTLEN_DIGEST), 0);
  46812. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  46813. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  46814. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46815. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  46816. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46817. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  46818. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46819. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  46820. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46821. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46822. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46823. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  46824. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46825. RSA_PSS_SALTLEN_DIGEST), 1);
  46826. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46827. RSA_PSS_SALTLEN_DIGEST), 1);
  46828. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46829. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46830. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46831. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46832. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46833. RSA_PSS_SALTLEN_MAX), 1);
  46834. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46835. RSA_PSS_SALTLEN_MAX), 1);
  46836. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  46837. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  46838. RSA_free(rsa);
  46839. res = TEST_RES_CHECK(1);
  46840. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  46841. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46842. return res;
  46843. }
  46844. #endif
  46845. static int test_wolfSSL_RSA_sign_sha3(void)
  46846. {
  46847. int res = TEST_SKIPPED;
  46848. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  46849. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46850. RSA *rsa;
  46851. const unsigned char *derBuf = client_key_der_2048;
  46852. unsigned char sigRet[256] = {0};
  46853. unsigned int sigLen = sizeof(sigRet);
  46854. /* Random data simulating a hash */
  46855. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  46856. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46857. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46858. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46859. };
  46860. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46861. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  46862. &sigLen, rsa), 1);
  46863. RSA_free(rsa);
  46864. res = TEST_RES_CHECK(1);
  46865. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46866. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  46867. return res;
  46868. }
  46869. static int test_wolfSSL_RSA_get0_key(void)
  46870. {
  46871. int res = TEST_SKIPPED;
  46872. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  46873. RSA *rsa = NULL;
  46874. const BIGNUM* n = NULL;
  46875. const BIGNUM* e = NULL;
  46876. const BIGNUM* d = NULL;
  46877. const unsigned char* der;
  46878. int derSz;
  46879. #ifdef USE_CERT_BUFFERS_1024
  46880. der = client_key_der_1024;
  46881. derSz = sizeof_client_key_der_1024;
  46882. #elif defined(USE_CERT_BUFFERS_2048)
  46883. der = client_key_der_2048;
  46884. derSz = sizeof_client_key_der_2048;
  46885. #else
  46886. der = NULL;
  46887. derSz = 0;
  46888. #endif
  46889. if (der != NULL) {
  46890. RSA_get0_key(NULL, NULL, NULL, NULL);
  46891. RSA_get0_key(rsa, NULL, NULL, NULL);
  46892. RSA_get0_key(NULL, &n, &e, &d);
  46893. AssertNull(n);
  46894. AssertNull(e);
  46895. AssertNull(d);
  46896. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  46897. AssertNotNull(rsa);
  46898. RSA_get0_key(rsa, NULL, NULL, NULL);
  46899. RSA_get0_key(rsa, &n, NULL, NULL);
  46900. AssertNotNull(n);
  46901. RSA_get0_key(rsa, NULL, &e, NULL);
  46902. AssertNotNull(e);
  46903. RSA_get0_key(rsa, NULL, NULL, &d);
  46904. AssertNotNull(d);
  46905. RSA_get0_key(rsa, &n, &e, &d);
  46906. AssertNotNull(n);
  46907. AssertNotNull(e);
  46908. AssertNotNull(d);
  46909. RSA_free(rsa);
  46910. }
  46911. res = TEST_RES_CHECK(1);
  46912. #endif
  46913. return res;
  46914. }
  46915. static int test_wolfSSL_RSA_meth(void)
  46916. {
  46917. int res = TEST_SKIPPED;
  46918. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46919. RSA *rsa;
  46920. RSA_METHOD *rsa_meth;
  46921. #ifdef WOLFSSL_KEY_GEN
  46922. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46923. RSA_free(rsa);
  46924. #else
  46925. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46926. #endif
  46927. AssertNotNull(RSA_get_default_method());
  46928. wolfSSL_RSA_meth_free(NULL);
  46929. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  46930. AssertNotNull(rsa_meth =
  46931. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  46932. #ifndef NO_WOLFSSL_STUB
  46933. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  46934. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  46935. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  46936. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  46937. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  46938. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  46939. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  46940. #endif
  46941. AssertIntEQ(RSA_flags(NULL), 0);
  46942. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  46943. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  46944. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  46945. AssertNotNull(rsa = RSA_new());
  46946. /* No method set. */
  46947. AssertIntEQ(RSA_flags(rsa), 0);
  46948. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46949. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46950. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46951. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  46952. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  46953. AssertNull(RSA_get_method(NULL));
  46954. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  46955. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  46956. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46957. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46958. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  46959. RSA_METHOD_FLAG_NO_CHECK);
  46960. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46961. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46962. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  46963. /* rsa_meth is freed here */
  46964. RSA_free(rsa);
  46965. res = TEST_RES_CHECK(1);
  46966. #endif
  46967. return res;
  46968. }
  46969. static int test_wolfSSL_RSA_verify(void)
  46970. {
  46971. int res = TEST_SKIPPED;
  46972. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  46973. !defined(NO_FILESYSTEM)
  46974. #ifndef NO_BIO
  46975. XFILE fp;
  46976. RSA *pKey, *pubKey;
  46977. X509 *cert;
  46978. const char *text = "Hello wolfSSL !";
  46979. unsigned char hash[SHA256_DIGEST_LENGTH];
  46980. unsigned char signature[2048/8];
  46981. unsigned int signatureLength;
  46982. byte *buf;
  46983. BIO *bio;
  46984. SHA256_CTX c;
  46985. EVP_PKEY *evpPkey, *evpPubkey;
  46986. size_t sz;
  46987. /* generate hash */
  46988. SHA256_Init(&c);
  46989. SHA256_Update(&c, text, strlen(text));
  46990. SHA256_Final(hash, &c);
  46991. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46992. /* workaround for small stack cache case */
  46993. wc_Sha256Free((wc_Sha256*)&c);
  46994. #endif
  46995. /* read privete key file */
  46996. fp = XFOPEN(svrKeyFile, "rb");
  46997. AssertTrue((fp != XBADFILE));
  46998. AssertIntGE(XFSEEK(fp, 0, XSEEK_END), 0);
  46999. sz = XFTELL(fp);
  47000. XREWIND(fp);
  47001. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  47002. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  47003. XFCLOSE(fp);
  47004. /* read private key and sign hash data */
  47005. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  47006. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  47007. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  47008. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  47009. signature, &signatureLength, pKey), SSL_SUCCESS);
  47010. /* read public key and verify signed data */
  47011. fp = XFOPEN(svrCertFile,"rb");
  47012. AssertTrue((fp != XBADFILE));
  47013. cert = PEM_read_X509(fp, 0, 0, 0 );
  47014. XFCLOSE(fp);
  47015. evpPubkey = X509_get_pubkey(cert);
  47016. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  47017. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  47018. signatureLength, pubKey), SSL_SUCCESS);
  47019. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  47020. signatureLength, NULL), SSL_FAILURE);
  47021. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  47022. signatureLength, pubKey), SSL_FAILURE);
  47023. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  47024. signatureLength, pubKey), SSL_FAILURE);
  47025. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  47026. signatureLength, NULL), SSL_FAILURE);
  47027. RSA_free(pKey);
  47028. EVP_PKEY_free(evpPkey);
  47029. RSA_free(pubKey);
  47030. EVP_PKEY_free(evpPubkey);
  47031. X509_free(cert);
  47032. BIO_free(bio);
  47033. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  47034. res = TEST_RES_CHECK(1);
  47035. #endif
  47036. #endif
  47037. return res;
  47038. }
  47039. static int test_wolfSSL_RSA_sign(void)
  47040. {
  47041. int res = TEST_SKIPPED;
  47042. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47043. RSA *rsa;
  47044. unsigned char hash[SHA256_DIGEST_LENGTH];
  47045. #ifdef USE_CERT_BUFFERS_1024
  47046. const unsigned char* privDer = client_key_der_1024;
  47047. size_t privDerSz = sizeof_client_key_der_1024;
  47048. const unsigned char* pubDer = client_keypub_der_1024;
  47049. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47050. unsigned char signature[1024/8];
  47051. #else
  47052. const unsigned char* privDer = client_key_der_2048;
  47053. size_t privDerSz = sizeof_client_key_der_2048;
  47054. const unsigned char* pubDer = client_keypub_der_2048;
  47055. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47056. unsigned char signature[2048/8];
  47057. #endif
  47058. unsigned int signatureLen;
  47059. const unsigned char* der;
  47060. XMEMSET(hash, 0, sizeof(hash));
  47061. der = privDer;
  47062. rsa = NULL;
  47063. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47064. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  47065. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  47066. &signatureLen, rsa), 0);
  47067. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  47068. &signatureLen, rsa), 0);
  47069. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  47070. &signatureLen, rsa), 0);
  47071. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47072. NULL, rsa), 0);
  47073. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47074. &signatureLen, NULL), 0);
  47075. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47076. &signatureLen, rsa), 1);
  47077. RSA_free(rsa);
  47078. der = pubDer;
  47079. rsa = NULL;
  47080. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47081. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  47082. signatureLen, rsa), 1);
  47083. RSA_free(rsa);
  47084. res = TEST_RES_CHECK(1);
  47085. #endif
  47086. return res;
  47087. }
  47088. static int test_wolfSSL_RSA_sign_ex(void)
  47089. {
  47090. int res = TEST_SKIPPED;
  47091. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47092. RSA *rsa;
  47093. unsigned char hash[SHA256_DIGEST_LENGTH];
  47094. #ifdef USE_CERT_BUFFERS_1024
  47095. const unsigned char* privDer = client_key_der_1024;
  47096. size_t privDerSz = sizeof_client_key_der_1024;
  47097. const unsigned char* pubDer = client_keypub_der_1024;
  47098. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47099. unsigned char signature[1024/8];
  47100. #else
  47101. const unsigned char* privDer = client_key_der_2048;
  47102. size_t privDerSz = sizeof_client_key_der_2048;
  47103. const unsigned char* pubDer = client_keypub_der_2048;
  47104. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47105. unsigned char signature[2048/8];
  47106. #endif
  47107. unsigned int signatureLen;
  47108. const unsigned char* der;
  47109. unsigned char encodedHash[51];
  47110. unsigned int encodedHashLen;
  47111. const unsigned char expEncHash[] = {
  47112. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  47113. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  47114. 0x00, 0x04, 0x20,
  47115. /* Hash data */
  47116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47118. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47120. };
  47121. XMEMSET(hash, 0, sizeof(hash));
  47122. AssertNotNull(rsa = wolfSSL_RSA_new());
  47123. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47124. &signatureLen, rsa, 1), 0);
  47125. wolfSSL_RSA_free(rsa);
  47126. der = privDer;
  47127. rsa = NULL;
  47128. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47129. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  47130. -1), 0);
  47131. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  47132. signature, &signatureLen, rsa, 1), 0);
  47133. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  47134. &signatureLen, rsa, 1), 0);
  47135. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  47136. &signatureLen, rsa, 1), 0);
  47137. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47138. NULL, rsa, 1), 0);
  47139. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47140. &signatureLen, NULL, 1), 0);
  47141. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47142. &signatureLen, rsa, -1), 0);
  47143. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  47144. &signatureLen, rsa, 0), 0);
  47145. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  47146. &signatureLen, rsa, 0), 0);
  47147. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47148. NULL, rsa, 0), 0);
  47149. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47150. &signatureLen, rsa, 1), 1);
  47151. /* Test returning encoded hash. */
  47152. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  47153. &encodedHashLen, rsa, 0), 1);
  47154. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  47155. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  47156. RSA_free(rsa);
  47157. der = pubDer;
  47158. rsa = NULL;
  47159. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47160. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  47161. signatureLen, rsa), 1);
  47162. RSA_free(rsa);
  47163. res = TEST_RES_CHECK(1);
  47164. #endif
  47165. return res;
  47166. }
  47167. static int test_wolfSSL_RSA_public_decrypt(void)
  47168. {
  47169. int res = TEST_SKIPPED;
  47170. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47171. RSA *rsa;
  47172. unsigned char msg[SHA256_DIGEST_LENGTH];
  47173. #ifdef USE_CERT_BUFFERS_1024
  47174. const unsigned char* pubDer = client_keypub_der_1024;
  47175. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47176. unsigned char decMsg[1024/8];
  47177. const unsigned char encMsg[] = {
  47178. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  47179. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  47180. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  47181. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  47182. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  47183. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  47184. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  47185. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  47186. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  47187. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  47188. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  47189. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  47190. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  47191. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  47192. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  47193. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  47194. };
  47195. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47196. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47197. defined(WC_RSA_NO_PADDING)
  47198. const unsigned char encMsgNoPad[] = {
  47199. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  47200. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  47201. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  47202. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  47203. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  47204. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  47205. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  47206. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  47207. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  47208. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  47209. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  47210. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  47211. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  47212. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  47213. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  47214. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  47215. };
  47216. #endif
  47217. #else
  47218. const unsigned char* pubDer = client_keypub_der_2048;
  47219. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47220. unsigned char decMsg[2048/8];
  47221. const unsigned char encMsg[] = {
  47222. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  47223. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  47224. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  47225. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  47226. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  47227. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  47228. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  47229. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  47230. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  47231. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  47232. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  47233. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  47234. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  47235. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  47236. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  47237. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  47238. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  47239. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  47240. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  47241. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  47242. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  47243. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  47244. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  47245. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  47246. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  47247. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  47248. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  47249. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  47250. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  47251. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  47252. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  47253. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  47254. };
  47255. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47256. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47257. defined(WC_RSA_NO_PADDING)
  47258. const unsigned char encMsgNoPad[] = {
  47259. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  47260. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  47261. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  47262. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  47263. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  47264. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  47265. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  47266. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  47267. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  47268. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  47269. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  47270. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  47271. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  47272. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  47273. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  47274. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  47275. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  47276. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  47277. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  47278. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  47279. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  47280. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  47281. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  47282. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  47283. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  47284. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  47285. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  47286. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  47287. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  47288. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  47289. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  47290. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  47291. };
  47292. #endif
  47293. #endif
  47294. const unsigned char* der;
  47295. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47296. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47297. defined(WC_RSA_NO_PADDING)
  47298. int i;
  47299. #endif
  47300. XMEMSET(msg, 0, sizeof(msg));
  47301. der = pubDer;
  47302. rsa = NULL;
  47303. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47304. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  47305. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  47306. RSA_PKCS1_PADDING), -1);
  47307. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  47308. RSA_PKCS1_PADDING), -1);
  47309. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  47310. RSA_PKCS1_PADDING), -1);
  47311. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  47312. RSA_PKCS1_PADDING), -1);
  47313. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  47314. RSA_PKCS1_PSS_PADDING), -1);
  47315. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  47316. RSA_PKCS1_PADDING), 32);
  47317. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  47318. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47319. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47320. defined(WC_RSA_NO_PADDING)
  47321. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  47322. rsa, RSA_NO_PADDING), sizeof(decMsg));
  47323. /* Zeros before actual data. */
  47324. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  47325. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  47326. }
  47327. /* Check actual data. */
  47328. XMEMSET(msg, 0x01, sizeof(msg));
  47329. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  47330. #endif
  47331. RSA_free(rsa);
  47332. res = TEST_RES_CHECK(1);
  47333. #endif
  47334. return res;
  47335. }
  47336. static int test_wolfSSL_RSA_private_encrypt(void)
  47337. {
  47338. int res = TEST_SKIPPED;
  47339. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47340. RSA *rsa;
  47341. unsigned char msg[SHA256_DIGEST_LENGTH];
  47342. #ifdef USE_CERT_BUFFERS_1024
  47343. const unsigned char* privDer = client_key_der_1024;
  47344. size_t privDerSz = sizeof_client_key_der_1024;
  47345. unsigned char encMsg[1024/8];
  47346. const unsigned char expEncMsg[] = {
  47347. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  47348. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  47349. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  47350. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  47351. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  47352. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  47353. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  47354. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  47355. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  47356. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  47357. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  47358. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  47359. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  47360. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  47361. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  47362. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  47363. };
  47364. #ifdef WC_RSA_NO_PADDING
  47365. const unsigned char expEncMsgNoPad[] = {
  47366. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  47367. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  47368. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  47369. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  47370. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  47371. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  47372. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  47373. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  47374. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  47375. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  47376. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  47377. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  47378. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  47379. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  47380. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  47381. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  47382. };
  47383. #endif
  47384. #else
  47385. const unsigned char* privDer = client_key_der_2048;
  47386. size_t privDerSz = sizeof_client_key_der_2048;
  47387. unsigned char encMsg[2048/8];
  47388. const unsigned char expEncMsg[] = {
  47389. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  47390. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  47391. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  47392. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  47393. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  47394. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  47395. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  47396. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  47397. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  47398. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  47399. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  47400. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  47401. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  47402. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  47403. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  47404. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  47405. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  47406. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  47407. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  47408. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  47409. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  47410. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  47411. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  47412. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  47413. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  47414. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  47415. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  47416. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  47417. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  47418. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  47419. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  47420. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  47421. };
  47422. #ifdef WC_RSA_NO_PADDING
  47423. const unsigned char expEncMsgNoPad[] = {
  47424. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  47425. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  47426. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  47427. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  47428. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  47429. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  47430. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  47431. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  47432. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  47433. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  47434. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  47435. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  47436. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  47437. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  47438. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  47439. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  47440. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  47441. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  47442. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  47443. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  47444. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  47445. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  47446. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  47447. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  47448. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  47449. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  47450. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  47451. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  47452. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  47453. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  47454. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  47455. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  47456. };
  47457. #endif
  47458. #endif
  47459. const unsigned char* der;
  47460. XMEMSET(msg, 0x00, sizeof(msg));
  47461. der = privDer;
  47462. rsa = NULL;
  47463. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47464. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  47465. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  47466. -1);
  47467. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  47468. RSA_PKCS1_PADDING), -1);
  47469. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  47470. RSA_PKCS1_PADDING), -1);
  47471. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  47472. RSA_PKCS1_PADDING), -1);
  47473. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47474. RSA_PKCS1_PSS_PADDING), -1);
  47475. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47476. RSA_PKCS1_PADDING), sizeof(encMsg));
  47477. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  47478. #ifdef WC_RSA_NO_PADDING
  47479. /* Non-zero message. */
  47480. XMEMSET(msg, 0x01, sizeof(msg));
  47481. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47482. RSA_NO_PADDING), sizeof(encMsg));
  47483. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  47484. #endif
  47485. RSA_free(rsa);
  47486. res = TEST_RES_CHECK(1);
  47487. #endif
  47488. return res;
  47489. }
  47490. static int test_wolfSSL_RSA_public_encrypt(void)
  47491. {
  47492. int res = TEST_SKIPPED;
  47493. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47494. RSA* rsa;
  47495. const unsigned char msg[2048/8] = { 0 };
  47496. unsigned char encMsg[2048/8];
  47497. AssertNotNull(rsa = RSA_new());
  47498. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  47499. RSA_PKCS1_PADDING), -1);
  47500. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  47501. RSA_PKCS1_PADDING), -1);
  47502. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  47503. RSA_PKCS1_PADDING), -1);
  47504. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  47505. RSA_PKCS1_PADDING), -1);
  47506. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  47507. RSA_PKCS1_PSS_PADDING), -1);
  47508. /* Empty RSA key. */
  47509. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  47510. RSA_PKCS1_PADDING), -1);
  47511. RSA_free(rsa);
  47512. res = TEST_RES_CHECK(1);
  47513. #endif
  47514. return res;
  47515. }
  47516. static int test_wolfSSL_RSA_private_decrypt(void)
  47517. {
  47518. int res = TEST_SKIPPED;
  47519. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47520. RSA* rsa;
  47521. unsigned char msg[2048/8];
  47522. const unsigned char encMsg[2048/8] = { 0 };
  47523. AssertNotNull(rsa = RSA_new());
  47524. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  47525. RSA_PKCS1_PADDING), -1);
  47526. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  47527. RSA_PKCS1_PADDING), -1);
  47528. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  47529. RSA_PKCS1_PADDING), -1);
  47530. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  47531. RSA_PKCS1_PADDING), -1);
  47532. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  47533. RSA_PKCS1_PSS_PADDING), -1);
  47534. /* Empty RSA key. */
  47535. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  47536. RSA_PKCS1_PADDING), -1);
  47537. RSA_free(rsa);
  47538. res = TEST_RES_CHECK(1);
  47539. #endif
  47540. return res;
  47541. }
  47542. static int test_wolfSSL_RSA_GenAdd(void)
  47543. {
  47544. int res = TEST_SKIPPED;
  47545. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47546. RSA *rsa;
  47547. #ifdef USE_CERT_BUFFERS_1024
  47548. const unsigned char* privDer = client_key_der_1024;
  47549. size_t privDerSz = sizeof_client_key_der_1024;
  47550. const unsigned char* pubDer = client_keypub_der_1024;
  47551. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47552. #else
  47553. const unsigned char* privDer = client_key_der_2048;
  47554. size_t privDerSz = sizeof_client_key_der_2048;
  47555. const unsigned char* pubDer = client_keypub_der_2048;
  47556. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47557. #endif
  47558. const unsigned char* der;
  47559. der = privDer;
  47560. rsa = NULL;
  47561. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47562. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  47563. #ifndef RSA_LOW_MEM
  47564. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  47565. #else
  47566. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  47567. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47568. #endif
  47569. RSA_free(rsa);
  47570. der = pubDer;
  47571. rsa = NULL;
  47572. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47573. /* Need private values. */
  47574. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47575. RSA_free(rsa);
  47576. res = TEST_RES_CHECK(1);
  47577. #endif
  47578. return res;
  47579. }
  47580. static int test_wolfSSL_RSA_blinding_on(void)
  47581. {
  47582. int res = TEST_SKIPPED;
  47583. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  47584. RSA *rsa;
  47585. WOLFSSL_BN_CTX *bnCtx;
  47586. #ifdef USE_CERT_BUFFERS_1024
  47587. const unsigned char* privDer = client_key_der_1024;
  47588. size_t privDerSz = sizeof_client_key_der_1024;
  47589. #else
  47590. const unsigned char* privDer = client_key_der_2048;
  47591. size_t privDerSz = sizeof_client_key_der_2048;
  47592. #endif
  47593. const unsigned char* der;
  47594. der = privDer;
  47595. rsa = NULL;
  47596. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47597. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  47598. /* Does nothing so all parameters are valid. */
  47599. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  47600. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  47601. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  47602. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  47603. wolfSSL_BN_CTX_free(bnCtx);
  47604. RSA_free(rsa);
  47605. res = TEST_RES_CHECK(1);
  47606. #endif
  47607. return res;
  47608. }
  47609. static int test_wolfSSL_RSA_ex_data(void)
  47610. {
  47611. int res = TEST_SKIPPED;
  47612. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  47613. RSA* rsa;
  47614. unsigned char data[1];
  47615. rsa = RSA_new();
  47616. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  47617. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47618. #ifdef MAX_EX_DATA
  47619. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  47620. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  47621. #endif
  47622. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  47623. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  47624. #ifdef HAVE_EX_DATA
  47625. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  47626. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  47627. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  47628. #else
  47629. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  47630. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  47631. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47632. #endif
  47633. RSA_free(rsa);
  47634. res = TEST_RES_CHECK(1);
  47635. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47636. return res;
  47637. }
  47638. static int test_wolfSSL_RSA_LoadDer(void)
  47639. {
  47640. int res = TEST_SKIPPED;
  47641. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  47642. defined(OPENSSL_EXTRA_X509_SMALL))
  47643. RSA *rsa;
  47644. #ifdef USE_CERT_BUFFERS_1024
  47645. const unsigned char* privDer = client_key_der_1024;
  47646. size_t privDerSz = sizeof_client_key_der_1024;
  47647. #else
  47648. const unsigned char* privDer = client_key_der_2048;
  47649. size_t privDerSz = sizeof_client_key_der_2048;
  47650. #endif
  47651. AssertNotNull(rsa = RSA_new());
  47652. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  47653. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  47654. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  47655. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  47656. RSA_free(rsa);
  47657. res = TEST_RES_CHECK(1);
  47658. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47659. return res;
  47660. }
  47661. /* Local API. */
  47662. static int test_wolfSSL_RSA_To_Der(void)
  47663. {
  47664. int res = TEST_SKIPPED;
  47665. #ifdef WOLFSSL_TEST_STATIC_BUILD
  47666. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  47667. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47668. RSA* rsa;
  47669. #ifdef USE_CERT_BUFFERS_1024
  47670. const unsigned char* privDer = client_key_der_1024;
  47671. size_t privDerSz = sizeof_client_key_der_1024;
  47672. const unsigned char* pubDer = client_keypub_der_1024;
  47673. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47674. unsigned char out[sizeof(client_key_der_1024)];
  47675. #else
  47676. const unsigned char* privDer = client_key_der_2048;
  47677. size_t privDerSz = sizeof_client_key_der_2048;
  47678. const unsigned char* pubDer = client_keypub_der_2048;
  47679. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47680. unsigned char out[sizeof(client_key_der_2048)];
  47681. #endif
  47682. const unsigned char* der;
  47683. unsigned char* outDer = NULL;
  47684. der = privDer;
  47685. rsa = NULL;
  47686. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47687. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47688. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  47689. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  47690. outDer = out;
  47691. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47692. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  47693. outDer = NULL;
  47694. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47695. AssertNotNull(outDer);
  47696. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  47697. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47698. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  47699. outDer = out;
  47700. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  47701. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  47702. RSA_free(rsa);
  47703. AssertNotNull(rsa = RSA_new());
  47704. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47705. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  47706. RSA_free(rsa);
  47707. der = pubDer;
  47708. rsa = NULL;
  47709. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47710. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47711. RSA_free(rsa);
  47712. res = TEST_RES_CHECK(1);
  47713. #endif
  47714. #endif
  47715. return res;
  47716. }
  47717. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47718. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  47719. {
  47720. int res = TEST_SKIPPED;
  47721. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  47722. XFILE file;
  47723. const char* fname = "./certs/server-keyPub.pem";
  47724. RSA *rsa;
  47725. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  47726. file = XFOPEN(fname, "rb");
  47727. AssertTrue((file != XBADFILE));
  47728. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  47729. AssertIntEQ(RSA_size(rsa), 256);
  47730. RSA_free(rsa);
  47731. XFCLOSE(file);
  47732. res = TEST_RES_CHECK(1);
  47733. #endif
  47734. return res;
  47735. }
  47736. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47737. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  47738. {
  47739. int res = TEST_SKIPPED;
  47740. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  47741. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  47742. RSA* rsa = NULL;
  47743. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  47744. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  47745. /* Valid but stub so returns 0. */
  47746. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  47747. res = TEST_RES_CHECK(1);
  47748. #endif
  47749. return res;
  47750. }
  47751. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  47752. {
  47753. int res = TEST_SKIPPED;
  47754. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47755. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47756. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  47757. RSA* rsa;
  47758. #ifdef USE_CERT_BUFFERS_1024
  47759. const unsigned char* privDer = client_key_der_1024;
  47760. size_t privDerSz = sizeof_client_key_der_1024;
  47761. #else
  47762. const unsigned char* privDer = client_key_der_2048;
  47763. size_t privDerSz = sizeof_client_key_der_2048;
  47764. #endif
  47765. const unsigned char* der;
  47766. #ifndef NO_AES
  47767. unsigned char passwd[] = "password";
  47768. #endif
  47769. AssertNotNull(rsa = RSA_new());
  47770. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47771. NULL, NULL), 0);
  47772. RSA_free(rsa);
  47773. der = privDer;
  47774. rsa = NULL;
  47775. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47776. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  47777. NULL, NULL), 0);
  47778. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  47779. NULL, NULL), 0);
  47780. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47781. NULL, NULL), 1);
  47782. #ifndef NO_AES
  47783. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  47784. NULL, 0, NULL, NULL), 1);
  47785. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  47786. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  47787. #endif
  47788. RSA_free(rsa);
  47789. res = TEST_RES_CHECK(1);
  47790. #endif
  47791. return res;
  47792. }
  47793. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  47794. {
  47795. int res = TEST_SKIPPED;
  47796. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47797. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47798. defined(WOLFSSL_DER_TO_PEM))
  47799. RSA* rsa;
  47800. #ifdef USE_CERT_BUFFERS_1024
  47801. const unsigned char* privDer = client_key_der_1024;
  47802. size_t privDerSz = sizeof_client_key_der_1024;
  47803. #else
  47804. const unsigned char* privDer = client_key_der_2048;
  47805. size_t privDerSz = sizeof_client_key_der_2048;
  47806. #endif
  47807. const unsigned char* der;
  47808. #ifndef NO_AES
  47809. unsigned char passwd[] = "password";
  47810. #endif
  47811. unsigned char* pem;
  47812. int plen;
  47813. AssertNotNull(rsa = RSA_new());
  47814. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47815. &plen), 0);
  47816. RSA_free(rsa);
  47817. der = privDer;
  47818. rsa = NULL;
  47819. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47820. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  47821. &plen), 0);
  47822. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  47823. &plen), 0);
  47824. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47825. NULL), 0);
  47826. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47827. &plen), 1);
  47828. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47829. #ifndef NO_AES
  47830. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47831. NULL, 0, &pem, &plen), 1);
  47832. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47833. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47834. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  47835. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47836. #endif
  47837. RSA_free(rsa);
  47838. res = TEST_RES_CHECK(1);
  47839. #endif
  47840. return res;
  47841. }
  47842. static int test_wolfSSL_DH(void)
  47843. {
  47844. int res = TEST_SKIPPED;
  47845. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47846. DH *dh = NULL;
  47847. BIGNUM* p;
  47848. BIGNUM* q;
  47849. BIGNUM* g;
  47850. BIGNUM* pub;
  47851. BIGNUM* priv;
  47852. #if defined(OPENSSL_ALL)
  47853. #if !defined(HAVE_FIPS) || \
  47854. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  47855. FILE* f = NULL;
  47856. unsigned char buf[268];
  47857. const unsigned char* pt = buf;
  47858. long len = 0;
  47859. dh = NULL;
  47860. XMEMSET(buf, 0, sizeof(buf));
  47861. /* Test 2048 bit parameters */
  47862. f = XFOPEN("./certs/dh2048.der", "rb");
  47863. AssertTrue(f != XBADFILE);
  47864. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47865. XFCLOSE(f);
  47866. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  47867. AssertNotNull(dh->p);
  47868. AssertNotNull(dh->g);
  47869. AssertTrue(pt == buf);
  47870. AssertIntEQ(DH_generate_key(dh), 1);
  47871. AssertIntEQ(DH_generate_key(dh), 1);
  47872. AssertIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  47873. AssertNotNull(pub = BN_new());
  47874. AssertIntEQ(BN_set_word(pub, 1), 1);
  47875. AssertIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  47876. AssertIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  47877. AssertIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  47878. AssertIntEQ(DH_compute_key(buf, pub, NULL), -1);
  47879. AssertIntEQ(DH_compute_key(buf, NULL, dh), -1);
  47880. AssertIntEQ(DH_compute_key(NULL, pub, dh), -1);
  47881. AssertIntEQ(DH_compute_key(buf, pub, dh), -1);
  47882. BN_free(pub);
  47883. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47884. (const BIGNUM**)&q,
  47885. (const BIGNUM**)&g);
  47886. AssertPtrEq(p, dh->p);
  47887. AssertPtrEq(q, dh->q);
  47888. AssertPtrEq(g, dh->g);
  47889. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47890. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47891. AssertPtrEq(pub, dh->pub_key);
  47892. AssertPtrEq(priv, dh->priv_key);
  47893. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  47894. AssertPtrEq(pub, dh->pub_key);
  47895. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  47896. AssertPtrEq(priv, dh->priv_key);
  47897. AssertNotNull(pub = BN_new());
  47898. AssertNotNull(priv = BN_new());
  47899. AssertIntEQ(DH_set0_key(NULL, pub, priv), 0);
  47900. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47901. AssertNotNull(pub = BN_new());
  47902. AssertIntEQ(DH_set0_key(dh, pub, NULL), 1);
  47903. AssertNotNull(priv = BN_new());
  47904. AssertIntEQ(DH_set0_key(dh, NULL, priv), 1);
  47905. AssertPtrEq(pub, dh->pub_key);
  47906. AssertPtrEq(priv, dh->priv_key);
  47907. DH_free(dh);
  47908. AssertNotNull(dh = DH_new());
  47909. AssertNotNull(p = BN_new());
  47910. AssertIntEQ(BN_set_word(p, 1), 1);
  47911. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  47912. AssertNotNull(pub = BN_new());
  47913. AssertNotNull(priv = BN_new());
  47914. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47915. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  47916. BN_free(p);
  47917. DH_free(dh);
  47918. #ifdef WOLFSSL_KEY_GEN
  47919. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  47920. AssertIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  47921. DH_free(dh);
  47922. #endif
  47923. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  47924. #endif /* OPENSSL_ALL */
  47925. (void)dh;
  47926. (void)p;
  47927. (void)q;
  47928. (void)g;
  47929. (void)pub;
  47930. (void)priv;
  47931. dh = wolfSSL_DH_new();
  47932. AssertNotNull(dh);
  47933. /* invalid parameters test */
  47934. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  47935. (const BIGNUM**)&q,
  47936. (const BIGNUM**)&g);
  47937. DH_get0_pqg(dh, NULL,
  47938. (const BIGNUM**)&q,
  47939. (const BIGNUM**)&g);
  47940. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  47941. DH_get0_pqg(dh, NULL, NULL, NULL);
  47942. AssertTrue(1);
  47943. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47944. (const BIGNUM**)&q,
  47945. (const BIGNUM**)&g);
  47946. AssertPtrEq(p, NULL);
  47947. AssertPtrEq(q, NULL);
  47948. AssertPtrEq(g, NULL);
  47949. DH_free(dh);
  47950. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  47951. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  47952. #if defined(OPENSSL_ALL) || \
  47953. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47954. dh = wolfSSL_DH_new();
  47955. AssertNotNull(dh);
  47956. p = wolfSSL_BN_new();
  47957. AssertNotNull(p);
  47958. AssertIntEQ(BN_set_word(p, 11), 1);
  47959. g = wolfSSL_BN_new();
  47960. AssertNotNull(g);
  47961. AssertIntEQ(BN_set_word(g, 2), 1);
  47962. q = wolfSSL_BN_new();
  47963. AssertNotNull(q);
  47964. AssertIntEQ(BN_set_word(q, 5), 1);
  47965. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  47966. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  47967. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  47968. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  47969. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  47970. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  47971. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  47972. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  47973. /* Don't need q. */
  47974. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47975. /* Setting again will free the p and g. */
  47976. wolfSSL_BN_free(q);
  47977. DH_free(dh);
  47978. dh = wolfSSL_DH_new();
  47979. AssertNotNull(dh);
  47980. p = wolfSSL_BN_new();
  47981. AssertNotNull(p);
  47982. AssertIntEQ(BN_set_word(p, 11), 1);
  47983. g = wolfSSL_BN_new();
  47984. AssertNotNull(g);
  47985. AssertIntEQ(BN_set_word(g, 2), 1);
  47986. q = wolfSSL_BN_new();
  47987. AssertNotNull(q);
  47988. AssertIntEQ(BN_set_word(q, 5), 1);
  47989. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  47990. /* p, q and g are now owned by dh - don't free. */
  47991. p = wolfSSL_BN_new();
  47992. AssertNotNull(p);
  47993. AssertIntEQ(BN_set_word(p, 11), 1);
  47994. g = wolfSSL_BN_new();
  47995. AssertNotNull(g);
  47996. AssertIntEQ(BN_set_word(g, 2), 1);
  47997. q = wolfSSL_BN_new();
  47998. AssertNotNull(q);
  47999. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  48000. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  48001. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  48002. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  48003. /* p, q and g are now owned by dh - don't free. */
  48004. DH_free(dh);
  48005. AssertIntEQ(DH_generate_key(NULL), 0);
  48006. AssertNotNull(dh = DH_new());
  48007. AssertIntEQ(DH_generate_key(dh), 0);
  48008. p = wolfSSL_BN_new();
  48009. AssertNotNull(p);
  48010. AssertIntEQ(BN_set_word(p, 0), 1);
  48011. g = wolfSSL_BN_new();
  48012. AssertNotNull(g);
  48013. AssertIntEQ(BN_set_word(g, 2), 1);
  48014. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  48015. AssertIntEQ(DH_generate_key(dh), 0);
  48016. DH_free(dh);
  48017. #endif
  48018. #endif
  48019. /* Test DH_up_ref() */
  48020. dh = wolfSSL_DH_new();
  48021. AssertNotNull(dh);
  48022. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  48023. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  48024. DH_free(dh); /* decrease ref count */
  48025. DH_free(dh); /* free WOLFSSL_DH */
  48026. AssertNull((dh = DH_new_by_nid(NID_sha1)));
  48027. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  48028. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  48029. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  48030. #ifdef HAVE_FFDHE_2048
  48031. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  48032. DH_free(dh);
  48033. #endif
  48034. #ifdef HAVE_FFDHE_3072
  48035. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  48036. DH_free(dh);
  48037. #endif
  48038. #ifdef HAVE_FFDHE_4096
  48039. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  48040. DH_free(dh);
  48041. #endif
  48042. #else
  48043. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  48044. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  48045. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  48046. AssertIntEQ(wolfSSL_DH_size(NULL), -1);
  48047. res = TEST_RES_CHECK(1);
  48048. #endif /* OPENSSL_EXTRA && !NO_DH */
  48049. return res;
  48050. }
  48051. static int test_wolfSSL_DH_dup(void)
  48052. {
  48053. int res = TEST_SKIPPED;
  48054. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  48055. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  48056. defined(OPENSSL_EXTRA)
  48057. DH *dh;
  48058. DH *dhDup;
  48059. WOLFSSL_BIGNUM* p;
  48060. WOLFSSL_BIGNUM* g;
  48061. AssertNotNull(p = wolfSSL_BN_new());
  48062. AssertNotNull(g = wolfSSL_BN_new());
  48063. AssertIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  48064. AssertIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  48065. dhDup = wolfSSL_DH_dup(NULL);
  48066. AssertNull(dhDup);
  48067. dh = wolfSSL_DH_new();
  48068. AssertNotNull(dh);
  48069. dhDup = wolfSSL_DH_dup(dh);
  48070. AssertNull(dhDup);
  48071. #if defined(OPENSSL_ALL) || \
  48072. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  48073. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  48074. dhDup = wolfSSL_DH_dup(dh);
  48075. AssertNotNull(dhDup);
  48076. wolfSSL_DH_free(dhDup);
  48077. #else
  48078. wolfSSL_BN_free(p);
  48079. wolfSSL_BN_free(g);
  48080. #endif
  48081. wolfSSL_DH_free(dh);
  48082. res = TEST_RES_CHECK(1);
  48083. #endif
  48084. #endif
  48085. return res;
  48086. }
  48087. static int test_wolfSSL_DH_check(void)
  48088. {
  48089. int res = TEST_SKIPPED;
  48090. #ifdef OPENSSL_ALL
  48091. #ifndef NO_DH
  48092. #ifndef NO_BIO
  48093. #ifndef NO_DSA
  48094. byte buf[6000];
  48095. char file[] = "./certs/dsaparams.pem";
  48096. XFILE f;
  48097. int bytes;
  48098. BIO* bio;
  48099. DSA* dsa;
  48100. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  48101. static const byte dh2048[] = {
  48102. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  48103. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  48104. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  48105. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  48106. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  48107. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  48108. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  48109. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  48110. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  48111. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  48112. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  48113. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  48114. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  48115. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  48116. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  48117. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  48118. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  48119. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  48120. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  48121. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  48122. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  48123. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  48124. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  48125. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  48126. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  48127. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  48128. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  48129. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  48130. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  48131. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  48132. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  48133. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  48134. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  48135. 0x93, 0x02, 0x01, 0x02
  48136. };
  48137. const byte* params;
  48138. #endif
  48139. DH* dh = NULL;
  48140. WOLFSSL_BIGNUM* p;
  48141. WOLFSSL_BIGNUM* g;
  48142. WOLFSSL_BIGNUM* pTmp = NULL;
  48143. WOLFSSL_BIGNUM* gTmp = NULL;
  48144. int codes = -1;
  48145. #ifndef NO_DSA
  48146. /* Initialize DH */
  48147. f = XFOPEN(file, "rb");
  48148. AssertTrue((f != XBADFILE));
  48149. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  48150. XFCLOSE(f);
  48151. bio = BIO_new_mem_buf((void*)buf, bytes);
  48152. AssertNotNull(bio);
  48153. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  48154. AssertNotNull(dsa);
  48155. dh = wolfSSL_DSA_dup_DH(dsa);
  48156. AssertNotNull(dh);
  48157. BIO_free(bio);
  48158. DSA_free(dsa);
  48159. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  48160. params = dh2048;
  48161. dh = wolfSSL_d2i_DHparams(NULL, &params, (long)sizeof(dh2048));
  48162. AssertNotNull(dh);
  48163. #else
  48164. dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048);
  48165. AssertNotNull(dh);
  48166. #endif
  48167. /* Test assumed to be valid dh.
  48168. * Should return WOLFSSL_SUCCESS
  48169. * codes should be 0
  48170. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  48171. */
  48172. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48173. AssertIntEQ(codes, 0);
  48174. /* Test NULL dh: expected BAD_FUNC_ARG */
  48175. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  48176. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  48177. pTmp = dh->p;
  48178. dh->p = NULL;
  48179. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48180. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48181. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  48182. /* set dh->p back to normal so it wont fail on next tests */
  48183. dh->p = pTmp;
  48184. pTmp = NULL;
  48185. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  48186. gTmp = dh->g;
  48187. dh->g = NULL;
  48188. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48189. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48190. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  48191. dh->g = gTmp;
  48192. gTmp = NULL;
  48193. /* Cleanup */
  48194. DH_free(dh);
  48195. dh = DH_new();
  48196. AssertNotNull(dh);
  48197. /* Check empty DH. */
  48198. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48199. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48200. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  48201. /* Check non-prime valued p. */
  48202. AssertNotNull(p = BN_new());
  48203. AssertIntEQ(BN_set_word(p, 4), 1);
  48204. AssertNotNull(g = BN_new());
  48205. AssertIntEQ(BN_set_word(g, 2), 1);
  48206. AssertIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  48207. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48208. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48209. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  48210. DH_free(dh);
  48211. res = TEST_RES_CHECK(1);
  48212. #endif
  48213. #endif /* !NO_DH && !NO_DSA */
  48214. #endif
  48215. return res;
  48216. }
  48217. static int test_wolfSSL_DH_prime(void)
  48218. {
  48219. int res = TEST_SKIPPED;
  48220. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48221. WOLFSSL_BIGNUM* bn;
  48222. #if WOLFSSL_MAX_BN_BITS >= 768
  48223. WOLFSSL_BIGNUM* bn2;
  48224. #endif
  48225. bn = wolfSSL_DH_768_prime(NULL);
  48226. #if WOLFSSL_MAX_BN_BITS >= 768
  48227. AssertNotNull(bn);
  48228. bn2 = wolfSSL_DH_768_prime(bn);
  48229. AssertNotNull(bn2);
  48230. AssertTrue(bn == bn2);
  48231. wolfSSL_BN_free(bn);
  48232. #else
  48233. AssertNull(bn);
  48234. #endif
  48235. bn = wolfSSL_DH_1024_prime(NULL);
  48236. #if WOLFSSL_MAX_BN_BITS >= 1024
  48237. AssertNotNull(bn);
  48238. wolfSSL_BN_free(bn);
  48239. #else
  48240. AssertNull(bn);
  48241. #endif
  48242. bn = wolfSSL_DH_2048_prime(NULL);
  48243. #if WOLFSSL_MAX_BN_BITS >= 2048
  48244. AssertNotNull(bn);
  48245. wolfSSL_BN_free(bn);
  48246. #else
  48247. AssertNull(bn);
  48248. #endif
  48249. bn = wolfSSL_DH_3072_prime(NULL);
  48250. #if WOLFSSL_MAX_BN_BITS >= 3072
  48251. AssertNotNull(bn);
  48252. wolfSSL_BN_free(bn);
  48253. #else
  48254. AssertNull(bn);
  48255. #endif
  48256. bn = wolfSSL_DH_4096_prime(NULL);
  48257. #if WOLFSSL_MAX_BN_BITS >= 4096
  48258. AssertNotNull(bn);
  48259. wolfSSL_BN_free(bn);
  48260. #else
  48261. AssertNull(bn);
  48262. #endif
  48263. bn = wolfSSL_DH_6144_prime(NULL);
  48264. #if WOLFSSL_MAX_BN_BITS >= 6144
  48265. AssertNotNull(bn);
  48266. wolfSSL_BN_free(bn);
  48267. #else
  48268. AssertNull(bn);
  48269. #endif
  48270. bn = wolfSSL_DH_8192_prime(NULL);
  48271. #if WOLFSSL_MAX_BN_BITS >= 8192
  48272. AssertNotNull(bn);
  48273. wolfSSL_BN_free(bn);
  48274. #else
  48275. AssertNull(bn);
  48276. #endif
  48277. res = TEST_RES_CHECK(1);
  48278. #endif
  48279. return res;
  48280. }
  48281. static int test_wolfSSL_DH_1536_prime(void)
  48282. {
  48283. int res = TEST_SKIPPED;
  48284. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48285. BIGNUM* bn;
  48286. unsigned char bits[200];
  48287. int sz = 192; /* known binary size */
  48288. const byte expected[] = {
  48289. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48290. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  48291. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  48292. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  48293. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  48294. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  48295. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  48296. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  48297. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  48298. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  48299. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  48300. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  48301. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  48302. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  48303. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  48304. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  48305. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  48306. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  48307. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  48308. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  48309. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  48310. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  48311. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  48312. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48313. };
  48314. bn = get_rfc3526_prime_1536(NULL);
  48315. AssertNotNull(bn);
  48316. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  48317. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  48318. BN_free(bn);
  48319. res = TEST_RES_CHECK(1);
  48320. #endif
  48321. return res;
  48322. }
  48323. static int test_wolfSSL_DH_get_2048_256(void)
  48324. {
  48325. int res = TEST_SKIPPED;
  48326. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48327. WOLFSSL_DH* dh;
  48328. const WOLFSSL_BIGNUM* pBn;
  48329. const WOLFSSL_BIGNUM* gBn;
  48330. const WOLFSSL_BIGNUM* qBn;
  48331. const byte pExpected[] = {
  48332. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  48333. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  48334. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  48335. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  48336. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  48337. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  48338. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  48339. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  48340. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  48341. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  48342. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  48343. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  48344. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  48345. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  48346. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  48347. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  48348. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  48349. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  48350. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  48351. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  48352. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  48353. 0x1E, 0x1A, 0x15, 0x97
  48354. };
  48355. const byte gExpected[] = {
  48356. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  48357. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  48358. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  48359. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  48360. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  48361. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  48362. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  48363. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  48364. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  48365. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  48366. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  48367. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  48368. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  48369. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  48370. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  48371. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  48372. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  48373. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  48374. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  48375. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  48376. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  48377. 0x6C, 0xC4, 0x16, 0x59
  48378. };
  48379. const byte qExpected[] = {
  48380. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  48381. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  48382. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  48383. };
  48384. int pSz;
  48385. int qSz;
  48386. int gSz;
  48387. byte* pReturned;
  48388. byte* qReturned;
  48389. byte* gReturned;
  48390. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  48391. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  48392. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  48393. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  48394. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  48395. AssertIntEQ(pSz, sizeof(pExpected));
  48396. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  48397. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  48398. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  48399. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  48400. AssertIntEQ(qSz, sizeof(qExpected));
  48401. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  48402. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  48403. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  48404. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  48405. AssertIntEQ(gSz, sizeof(gExpected));
  48406. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  48407. wolfSSL_DH_free(dh);
  48408. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48409. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48410. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48411. res = TEST_RES_CHECK(1);
  48412. #endif
  48413. return res;
  48414. }
  48415. static int test_wolfSSL_PEM_write_DHparams(void)
  48416. {
  48417. int res = TEST_SKIPPED;
  48418. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  48419. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  48420. DH* dh;
  48421. BIO* bio;
  48422. XFILE fp;
  48423. byte pem[2048];
  48424. int pemSz;
  48425. const char expected[] =
  48426. "-----BEGIN DH PARAMETERS-----\n"
  48427. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  48428. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  48429. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  48430. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  48431. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  48432. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  48433. "-----END DH PARAMETERS-----\n";
  48434. const char badPem[] =
  48435. "-----BEGIN DH PARAMETERS-----\n"
  48436. "-----END DH PARAMETERS-----\n";
  48437. const char emptySeqPem[] =
  48438. "-----BEGIN DH PARAMETERS-----\n"
  48439. "MAA=\n"
  48440. "-----END DH PARAMETERS-----\n";
  48441. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  48442. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  48443. XFCLOSE(fp);
  48444. AssertNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  48445. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  48446. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48447. AssertIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  48448. (int)sizeof(badPem));
  48449. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48450. BIO_free(bio);
  48451. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  48452. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48453. AssertIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  48454. (int)sizeof(emptySeqPem));
  48455. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48456. BIO_free(bio);
  48457. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  48458. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  48459. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48460. BIO_free(bio);
  48461. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  48462. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  48463. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  48464. DH_free(dh);
  48465. dh = wolfSSL_DH_new();
  48466. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  48467. XFCLOSE(fp);
  48468. wolfSSL_DH_free(dh);
  48469. /* check results */
  48470. XMEMSET(pem, 0, sizeof(pem));
  48471. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  48472. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  48473. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  48474. XFCLOSE(fp);
  48475. res = TEST_RES_CHECK(1);
  48476. #endif
  48477. return res;
  48478. }
  48479. static int test_wolfSSL_d2i_DHparams(void)
  48480. {
  48481. int res = TEST_SKIPPED;
  48482. #ifdef OPENSSL_ALL
  48483. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  48484. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48485. FILE* f = NULL;
  48486. unsigned char buf[4096];
  48487. const unsigned char* pt = buf;
  48488. #ifdef HAVE_FFDHE_2048
  48489. const char* params1 = "./certs/dh2048.der";
  48490. #endif
  48491. #ifdef HAVE_FFDHE_3072
  48492. const char* params2 = "./certs/dh3072.der";
  48493. #endif
  48494. long len = 0;
  48495. WOLFSSL_DH* dh = NULL;
  48496. XMEMSET(buf, 0, sizeof(buf));
  48497. /* Test 2048 bit parameters */
  48498. #ifdef HAVE_FFDHE_2048
  48499. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  48500. f = XFOPEN(params1, "rb");
  48501. AssertTrue(f != XBADFILE);
  48502. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48503. XFCLOSE(f);
  48504. /* Valid case */
  48505. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48506. AssertNotNull(dh->p);
  48507. AssertNotNull(dh->g);
  48508. AssertTrue(pt == buf);
  48509. AssertIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  48510. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  48511. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  48512. /* Invalid cases */
  48513. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  48514. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  48515. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  48516. DH_free(dh);
  48517. *buf = 0;
  48518. pt = buf;
  48519. res = TEST_RES_CHECK(1);
  48520. }
  48521. #endif /* HAVE_FFDHE_2048 */
  48522. /* Test 3072 bit parameters */
  48523. #ifdef HAVE_FFDHE_3072
  48524. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  48525. f = XFOPEN(params2, "rb");
  48526. AssertTrue(f != XBADFILE);
  48527. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48528. XFCLOSE(f);
  48529. /* Valid case */
  48530. AssertNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  48531. AssertNotNull(dh->p);
  48532. AssertNotNull(dh->g);
  48533. AssertTrue(pt != buf);
  48534. AssertIntEQ(DH_generate_key(dh), 1);
  48535. /* Invalid cases */
  48536. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  48537. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  48538. DH_free(dh);
  48539. res = TEST_RES_CHECK(1);
  48540. }
  48541. #endif /* HAVE_FFDHE_3072 */
  48542. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48543. #endif /* !NO_DH */
  48544. #endif
  48545. return res;
  48546. }
  48547. static int test_wolfSSL_DH_LoadDer(void)
  48548. {
  48549. int res = TEST_SKIPPED;
  48550. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  48551. defined(OPENSSL_EXTRA)
  48552. static const byte dh2048[] = {
  48553. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  48554. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  48555. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  48556. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  48557. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  48558. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  48559. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  48560. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  48561. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  48562. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  48563. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  48564. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  48565. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  48566. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  48567. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  48568. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  48569. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  48570. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  48571. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  48572. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  48573. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  48574. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  48575. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  48576. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  48577. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  48578. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  48579. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  48580. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  48581. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  48582. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  48583. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  48584. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  48585. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  48586. 0x93, 0x02, 0x01, 0x02
  48587. };
  48588. WOLFSSL_DH* dh;
  48589. dh = wolfSSL_DH_new();
  48590. AssertNotNull(dh);
  48591. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  48592. AssertIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  48593. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  48594. AssertIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  48595. wolfSSL_DH_free(dh);
  48596. res = TEST_RES_CHECK(1);
  48597. #endif
  48598. return res;
  48599. }
  48600. static int test_wolfSSL_i2d_DHparams(void)
  48601. {
  48602. int res = TEST_SKIPPED;
  48603. #ifdef OPENSSL_ALL
  48604. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  48605. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48606. FILE* f;
  48607. unsigned char buf[4096];
  48608. const unsigned char* pt;
  48609. unsigned char* pt2;
  48610. #ifdef HAVE_FFDHE_2048
  48611. const char* params1 = "./certs/dh2048.der";
  48612. #endif
  48613. #ifdef HAVE_FFDHE_3072
  48614. const char* params2 = "./certs/dh3072.der";
  48615. #endif
  48616. long len;
  48617. WOLFSSL_DH* dh;
  48618. /* Test 2048 bit parameters */
  48619. #ifdef HAVE_FFDHE_2048
  48620. pt = buf;
  48621. pt2 = buf;
  48622. f = XFOPEN(params1, "rb");
  48623. AssertTrue(f != XBADFILE);
  48624. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48625. XFCLOSE(f);
  48626. /* Valid case */
  48627. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48628. AssertTrue(pt == buf);
  48629. AssertIntEQ(DH_generate_key(dh), 1);
  48630. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  48631. /* Invalid case */
  48632. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48633. /* Return length only */
  48634. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  48635. DH_free(dh);
  48636. *buf = 0;
  48637. #endif
  48638. /* Test 3072 bit parameters */
  48639. #ifdef HAVE_FFDHE_3072
  48640. pt = buf;
  48641. pt2 = buf;
  48642. f = XFOPEN(params2, "rb");
  48643. AssertTrue(f != XBADFILE);
  48644. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48645. XFCLOSE(f);
  48646. /* Valid case */
  48647. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48648. AssertTrue(pt == buf);
  48649. AssertIntEQ(DH_generate_key(dh), 1);
  48650. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  48651. /* Invalid case */
  48652. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48653. /* Return length only */
  48654. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  48655. DH_free(dh);
  48656. #endif
  48657. dh = DH_new();
  48658. AssertNotNull(dh);
  48659. pt2 = buf;
  48660. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  48661. DH_free(dh);
  48662. res = TEST_RES_CHECK(1);
  48663. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48664. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  48665. #endif
  48666. return res;
  48667. }
  48668. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  48669. /*----------------------------------------------------------------------------*
  48670. | EC
  48671. *----------------------------------------------------------------------------*/
  48672. static int test_wolfSSL_EC_GROUP(void)
  48673. {
  48674. int res = TEST_SKIPPED;
  48675. #ifdef OPENSSL_EXTRA
  48676. EC_GROUP *group;
  48677. EC_GROUP *group2;
  48678. EC_GROUP *group3;
  48679. #ifndef HAVE_ECC_BRAINPOOL
  48680. EC_GROUP *group4;
  48681. #endif
  48682. WOLFSSL_BIGNUM* order;
  48683. int group_bits;
  48684. int i;
  48685. static const int knownEccNids[] = {
  48686. NID_X9_62_prime192v1,
  48687. NID_X9_62_prime192v2,
  48688. NID_X9_62_prime192v3,
  48689. NID_X9_62_prime239v1,
  48690. NID_X9_62_prime239v2,
  48691. NID_X9_62_prime239v3,
  48692. NID_X9_62_prime256v1,
  48693. NID_secp112r1,
  48694. NID_secp112r2,
  48695. NID_secp128r1,
  48696. NID_secp128r2,
  48697. NID_secp160r1,
  48698. NID_secp160r2,
  48699. NID_secp224r1,
  48700. NID_secp384r1,
  48701. NID_secp521r1,
  48702. NID_secp160k1,
  48703. NID_secp192k1,
  48704. NID_secp224k1,
  48705. NID_secp256k1,
  48706. NID_brainpoolP160r1,
  48707. NID_brainpoolP192r1,
  48708. NID_brainpoolP224r1,
  48709. NID_brainpoolP256r1,
  48710. NID_brainpoolP320r1,
  48711. NID_brainpoolP384r1,
  48712. NID_brainpoolP512r1,
  48713. };
  48714. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  48715. static const int knownEccEnums[] = {
  48716. ECC_SECP192R1,
  48717. ECC_PRIME192V2,
  48718. ECC_PRIME192V3,
  48719. ECC_PRIME239V1,
  48720. ECC_PRIME239V2,
  48721. ECC_PRIME239V3,
  48722. ECC_SECP256R1,
  48723. ECC_SECP112R1,
  48724. ECC_SECP112R2,
  48725. ECC_SECP128R1,
  48726. ECC_SECP128R2,
  48727. ECC_SECP160R1,
  48728. ECC_SECP160R2,
  48729. ECC_SECP224R1,
  48730. ECC_SECP384R1,
  48731. ECC_SECP521R1,
  48732. ECC_SECP160K1,
  48733. ECC_SECP192K1,
  48734. ECC_SECP224K1,
  48735. ECC_SECP256K1,
  48736. ECC_BRAINPOOLP160R1,
  48737. ECC_BRAINPOOLP192R1,
  48738. ECC_BRAINPOOLP224R1,
  48739. ECC_BRAINPOOLP256R1,
  48740. ECC_BRAINPOOLP320R1,
  48741. ECC_BRAINPOOLP384R1,
  48742. ECC_BRAINPOOLP512R1,
  48743. };
  48744. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  48745. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  48746. AssertNotNull(group2 = EC_GROUP_dup(group));
  48747. AssertNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  48748. #ifndef HAVE_ECC_BRAINPOOL
  48749. AssertNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  48750. NID_brainpoolP256r1));
  48751. #endif
  48752. AssertNull(EC_GROUP_dup(NULL));
  48753. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  48754. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  48755. AssertIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  48756. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  48757. #ifndef HAVE_ECC_BRAINPOOL
  48758. AssertIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  48759. #endif
  48760. AssertIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  48761. AssertIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  48762. AssertNotNull(order = BN_new());
  48763. AssertIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  48764. AssertIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  48765. AssertIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  48766. AssertIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  48767. wolfSSL_BN_free(order);
  48768. AssertNotNull(EC_GROUP_method_of(group));
  48769. AssertIntEQ(EC_METHOD_get_field_type(NULL), 0);
  48770. AssertIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  48771. NID_X9_62_prime_field);
  48772. AssertIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  48773. AssertIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  48774. AssertIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  48775. AssertIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  48776. #ifndef NO_WOLFSSL_STUB
  48777. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  48778. #endif
  48779. #ifndef HAVE_ECC_BRAINPOOL
  48780. EC_GROUP_free(group4);
  48781. #endif
  48782. EC_GROUP_free(group3);
  48783. EC_GROUP_free(group2);
  48784. EC_GROUP_free(group);
  48785. for (i = 0; i < knowEccNidsLen; i++) {
  48786. AssertNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  48787. AssertIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  48788. EC_GROUP_free(group);
  48789. }
  48790. for (i = 0; i < knowEccEnumsLen; i++) {
  48791. AssertNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  48792. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  48793. EC_GROUP_free(group);
  48794. }
  48795. res = TEST_RES_CHECK(1);
  48796. #endif
  48797. return res;
  48798. }
  48799. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  48800. {
  48801. int res = TEST_SKIPPED;
  48802. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  48803. EC_GROUP *group;
  48804. BIO* bio;
  48805. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  48806. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  48807. EC_GROUP *ret;
  48808. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  48809. "BgUrgQQAIg==\n"
  48810. "-----END EC PARAMETERS-----";
  48811. #endif
  48812. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  48813. "MAA=\n"
  48814. "-----END EC PARAMETERS-----";
  48815. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  48816. "BgA=\n"
  48817. "-----END EC PARAMETERS-----";
  48818. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  48819. "BgE=\n"
  48820. "-----END EC PARAMETERS-----";
  48821. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  48822. "BgE*\n"
  48823. "-----END EC PARAMETERS-----";
  48824. /* Test that first parameter, bio, being NULL fails. */
  48825. AssertNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  48826. /* Test that reading named parameters works. */
  48827. AssertNotNull(bio = BIO_new(BIO_s_file()));
  48828. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  48829. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  48830. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  48831. BIO_free(bio);
  48832. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  48833. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  48834. /* Test that reusing group works. */
  48835. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  48836. sizeof(ec_nc_p384)));
  48837. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  48838. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  48839. BIO_free(bio);
  48840. EC_GROUP_free(group);
  48841. group = NULL;
  48842. /* Test that returning through group works. */
  48843. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  48844. sizeof(ec_nc_p384)));
  48845. AssertNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  48846. AssertIntEQ(group == ret, 1);
  48847. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  48848. BIO_free(bio);
  48849. #endif
  48850. EC_GROUP_free(group);
  48851. /* Test 0x30, 0x00 (not and object id) fails. */
  48852. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  48853. sizeof(ec_nc_bad_1)));
  48854. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  48855. BIO_free(bio);
  48856. /* Test 0x06, 0x00 (empty object id) fails. */
  48857. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  48858. sizeof(ec_nc_bad_2)));
  48859. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  48860. BIO_free(bio);
  48861. /* Test 0x06, 0x01 (badly formed object id) fails. */
  48862. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  48863. sizeof(ec_nc_bad_3)));
  48864. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  48865. BIO_free(bio);
  48866. /* Test invalid PEM encoding - invalid character. */
  48867. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  48868. sizeof(ec_nc_bad_4)));
  48869. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  48870. BIO_free(bio);
  48871. res = TEST_RES_CHECK(1);
  48872. #endif
  48873. return res;
  48874. }
  48875. static int test_wolfSSL_EC_POINT(void)
  48876. {
  48877. int res = TEST_SKIPPED;
  48878. #if !defined(WOLFSSL_SP_MATH) && \
  48879. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  48880. #ifdef OPENSSL_EXTRA
  48881. BN_CTX* ctx;
  48882. EC_GROUP* group;
  48883. #ifndef HAVE_ECC_BRAINPOOL
  48884. EC_GROUP* group2;
  48885. #endif
  48886. EC_POINT* Gxy;
  48887. EC_POINT* new_point;
  48888. EC_POINT* set_point;
  48889. EC_POINT* infinity;
  48890. BIGNUM* k = NULL;
  48891. BIGNUM* Gx = NULL;
  48892. BIGNUM* Gy = NULL;
  48893. BIGNUM* Gz = NULL;
  48894. BIGNUM* X;
  48895. BIGNUM* Y;
  48896. BIGNUM* set_point_bn;
  48897. char* hexStr;
  48898. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  48899. "17AF381913FF7A96314EA47055EA0FD0";
  48900. /* NISTP256R1 Gx/Gy */
  48901. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  48902. "77037D812DEB33A0F4A13945D898C296";
  48903. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  48904. "2BCE33576B315ECECBB6406837BF51F5";
  48905. #ifndef HAVE_SELFTEST
  48906. EC_POINT *tmp;
  48907. size_t bin_len;
  48908. unsigned int blen;
  48909. unsigned char* buf = NULL;
  48910. unsigned char bufInf[1] = { 0x00 };
  48911. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  48912. "77037D812DEB33A0F4A13945D898C296"
  48913. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  48914. "2BCE33576B315ECECBB6406837BF51F5";
  48915. const unsigned char binUncompG[] = {
  48916. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  48917. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  48918. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  48919. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  48920. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  48921. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  48922. };
  48923. const unsigned char binUncompGBad[] = {
  48924. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  48925. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  48926. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  48927. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  48928. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  48929. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  48930. };
  48931. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  48932. "77037D812DEB33A0F4A13945D898C296";
  48933. #ifdef HAVE_COMP_KEY
  48934. const unsigned char binCompG[] = {
  48935. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  48936. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  48937. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  48938. };
  48939. #endif
  48940. #endif
  48941. AssertNotNull(ctx = BN_CTX_new());
  48942. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  48943. #ifndef HAVE_ECC_BRAINPOOL
  48944. /* Used to make groups curve_idx == -1. */
  48945. AssertNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  48946. #endif
  48947. AssertNull(EC_POINT_new(NULL));
  48948. AssertNotNull(Gxy = EC_POINT_new(group));
  48949. AssertNotNull(new_point = EC_POINT_new(group));
  48950. AssertNotNull(set_point = EC_POINT_new(group));
  48951. AssertNotNull(X = BN_new());
  48952. AssertNotNull(Y = BN_new());
  48953. AssertNotNull(set_point_bn = BN_new());
  48954. AssertNotNull(infinity = EC_POINT_new(group));
  48955. /* load test values */
  48956. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  48957. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  48958. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  48959. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  48960. /* populate coordinates for input point */
  48961. Gxy->X = Gx;
  48962. Gxy->Y = Gy;
  48963. Gxy->Z = Gz;
  48964. /* Test handling of NULL point. */
  48965. EC_POINT_clear_free(NULL);
  48966. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  48967. NULL, NULL, ctx), 0);
  48968. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  48969. NULL, NULL, ctx), 0);
  48970. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  48971. NULL, NULL, ctx), 0);
  48972. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  48973. X, NULL, ctx), 0);
  48974. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  48975. NULL, Y, ctx), 0);
  48976. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  48977. X, Y, ctx), 0);
  48978. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  48979. X, Y, ctx), 0);
  48980. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  48981. NULL, Y, ctx), 0);
  48982. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  48983. X, NULL, ctx), 0);
  48984. /* Getting point at infinity returns an error. */
  48985. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  48986. X, Y, ctx), 0);
  48987. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  48988. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  48989. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  48990. AssertIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  48991. AssertIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  48992. AssertIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  48993. AssertIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  48994. AssertIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  48995. AssertIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  48996. AssertIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  48997. AssertIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  48998. AssertIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  48999. AssertIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  49000. AssertIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  49001. AssertIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  49002. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  49003. /* perform point multiplication */
  49004. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  49005. AssertIntEQ(BN_is_zero(new_point->X), 0);
  49006. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  49007. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  49008. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  49009. AssertIntEQ(BN_is_zero(new_point->X), 0);
  49010. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  49011. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  49012. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  49013. AssertIntEQ(BN_is_zero(new_point->X), 0);
  49014. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  49015. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  49016. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  49017. AssertIntEQ(BN_is_zero(new_point->X), 1);
  49018. AssertIntEQ(BN_is_zero(new_point->Y), 1);
  49019. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  49020. /* Set point to something. */
  49021. AssertIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  49022. #else
  49023. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  49024. ctx), 1);
  49025. AssertIntEQ(BN_is_zero(new_point->X), 0);
  49026. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  49027. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  49028. #endif
  49029. /* check if point X coordinate is zero */
  49030. AssertIntEQ(BN_is_zero(new_point->X), 0);
  49031. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  49032. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  49033. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  49034. #endif
  49035. /* extract the coordinates from point */
  49036. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  49037. ctx), WOLFSSL_SUCCESS);
  49038. /* check if point X coordinate is zero */
  49039. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  49040. /* set the same X and Y points in another object */
  49041. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  49042. ctx), WOLFSSL_SUCCESS);
  49043. /* compare points as they should be the same */
  49044. AssertIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  49045. AssertIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  49046. AssertIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  49047. AssertIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  49048. AssertIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  49049. AssertIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  49050. AssertIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  49051. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  49052. /* Test copying */
  49053. AssertIntEQ(EC_POINT_copy(NULL, NULL), 0);
  49054. AssertIntEQ(EC_POINT_copy(NULL, set_point), 0);
  49055. AssertIntEQ(EC_POINT_copy(new_point, NULL), 0);
  49056. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  49057. /* Test inverting */
  49058. AssertIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  49059. AssertIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  49060. AssertIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  49061. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  49062. /* Test getting affine converts from projective. */
  49063. AssertIntEQ(EC_POINT_copy(set_point, new_point), 1);
  49064. /* Force non-affine coordinates */
  49065. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  49066. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  49067. new_point->inSet = 0;
  49068. /* extract the coordinates from point */
  49069. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  49070. ctx), WOLFSSL_SUCCESS);
  49071. /* check if point ordinates have changed. */
  49072. AssertIntNE(BN_cmp(X, set_point->X), 0);
  49073. AssertIntNE(BN_cmp(Y, set_point->Y), 0);
  49074. /* Test check for infinity */
  49075. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  49076. AssertIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  49077. AssertIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  49078. AssertIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  49079. AssertIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  49080. AssertIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  49081. #else
  49082. AssertIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  49083. #endif
  49084. AssertPtrEq(EC_POINT_point2bn(group, set_point,
  49085. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  49086. /* check bn2hex */
  49087. hexStr = BN_bn2hex(k);
  49088. AssertStrEQ(hexStr, kTest);
  49089. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49090. BN_print_fp(stderr, k);
  49091. fprintf(stderr, "\n");
  49092. #endif
  49093. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49094. hexStr = BN_bn2hex(Gx);
  49095. AssertStrEQ(hexStr, kGx);
  49096. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49097. BN_print_fp(stderr, Gx);
  49098. fprintf(stderr, "\n");
  49099. #endif
  49100. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49101. hexStr = BN_bn2hex(Gy);
  49102. AssertStrEQ(hexStr, kGy);
  49103. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49104. BN_print_fp(stderr, Gy);
  49105. fprintf(stderr, "\n");
  49106. #endif
  49107. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49108. #ifndef HAVE_SELFTEST
  49109. /* Test point to hex */
  49110. AssertNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49111. ctx));
  49112. AssertNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49113. ctx));
  49114. AssertNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49115. ctx));
  49116. #ifndef HAVE_ECC_BRAINPOOL
  49117. /* Group not supported in wolfCrypt. */
  49118. AssertNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49119. ctx));
  49120. #endif
  49121. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  49122. AssertStrEQ(hexStr, uncompG);
  49123. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49124. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  49125. AssertStrEQ(hexStr, compG);
  49126. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49127. /* Test point to oct */
  49128. AssertIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49129. NULL, 0, ctx), 0);
  49130. AssertIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49131. NULL, 0, ctx), 0);
  49132. AssertIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49133. NULL, 0, ctx), 0);
  49134. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49135. NULL, 0, ctx);
  49136. AssertIntEQ(bin_len, sizeof(binUncompG));
  49137. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  49138. DYNAMIC_TYPE_ECC));
  49139. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49140. buf, bin_len, ctx), bin_len);
  49141. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  49142. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49143. /* Infinity (x=0, y=0) encodes as '0x00'. */
  49144. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  49145. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  49146. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  49147. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  49148. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  49149. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  49150. AssertIntEQ(bufInf[0], 0);
  49151. wolfSSL_EC_POINT_dump(NULL, NULL);
  49152. /* Test point i2d */
  49153. AssertIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  49154. AssertIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  49155. AssertIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  49156. AssertIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  49157. AssertIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  49158. AssertIntEQ(blen, sizeof(binUncompG));
  49159. AssertNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  49160. blen -= 1;
  49161. AssertIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  49162. blen += 1;
  49163. AssertIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  49164. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  49165. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49166. #ifdef HAVE_COMP_KEY
  49167. /* Test point to oct compressed */
  49168. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  49169. 0, ctx);
  49170. AssertIntEQ(bin_len, sizeof(binCompG));
  49171. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  49172. DYNAMIC_TYPE_ECC));
  49173. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  49174. bin_len, ctx), bin_len);
  49175. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  49176. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49177. #endif
  49178. /* Test point BN */
  49179. AssertNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  49180. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49181. AssertNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  49182. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49183. AssertNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  49184. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49185. AssertNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  49186. /* Test oct to point */
  49187. AssertNotNull(tmp = EC_POINT_new(group));
  49188. AssertIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  49189. ctx), 0);
  49190. AssertIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  49191. ctx), 0);
  49192. AssertIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  49193. ctx), 0);
  49194. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  49195. sizeof(binUncompGBad), ctx), 0);
  49196. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  49197. ctx), 1);
  49198. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49199. EC_POINT_free(tmp);
  49200. /* Test setting BN ordinates. */
  49201. AssertNotNull(tmp = EC_POINT_new(group));
  49202. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  49203. NULL, ctx), 0);
  49204. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  49205. NULL, ctx), 0);
  49206. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  49207. NULL, ctx), 0);
  49208. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  49209. NULL, ctx), 0);
  49210. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  49211. Gy, ctx), 0);
  49212. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  49213. ctx), 0);
  49214. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  49215. ctx), 0);
  49216. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  49217. Gy, ctx), 0);
  49218. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  49219. NULL, ctx), 0);
  49220. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  49221. ctx), 1);
  49222. EC_POINT_free(tmp);
  49223. /* Test point d2i */
  49224. AssertNotNull(tmp = EC_POINT_new(group));
  49225. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  49226. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  49227. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  49228. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  49229. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  49230. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  49231. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  49232. AssertIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  49233. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  49234. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49235. EC_POINT_free(tmp);
  49236. #ifdef HAVE_COMP_KEY
  49237. /* Test oct compressed to point */
  49238. AssertNotNull(tmp = EC_POINT_new(group));
  49239. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  49240. 1);
  49241. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49242. EC_POINT_free(tmp);
  49243. /* Test point d2i - compressed */
  49244. AssertNotNull(tmp = EC_POINT_new(group));
  49245. AssertIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  49246. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49247. EC_POINT_free(tmp);
  49248. #endif
  49249. #endif
  49250. /* test BN_mod_add */
  49251. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  49252. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  49253. 1);
  49254. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  49255. /* cleanup */
  49256. BN_free(X);
  49257. BN_free(Y);
  49258. BN_free(k);
  49259. BN_free(set_point_bn);
  49260. EC_POINT_free(infinity);
  49261. EC_POINT_free(new_point);
  49262. EC_POINT_free(set_point);
  49263. EC_POINT_clear_free(Gxy);
  49264. #ifndef HAVE_ECC_BRAINPOOL
  49265. EC_GROUP_free(group2);
  49266. #endif
  49267. EC_GROUP_free(group);
  49268. BN_CTX_free(ctx);
  49269. res = TEST_RES_CHECK(1);
  49270. #endif
  49271. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  49272. return res;
  49273. }
  49274. static int test_wolfSSL_EC_KEY_generate(void)
  49275. {
  49276. int res = TEST_SKIPPED;
  49277. #ifdef OPENSSL_EXTRA
  49278. WOLFSSL_EC_KEY* key;
  49279. #ifndef HAVE_ECC_BRAINPOOL
  49280. WOLFSSL_EC_GROUP* group;
  49281. #endif
  49282. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49283. AssertIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  49284. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  49285. wolfSSL_EC_KEY_free(key);
  49286. #ifndef HAVE_ECC_BRAINPOOL
  49287. AssertNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  49288. NID_brainpoolP256r1));
  49289. AssertNotNull(key = wolfSSL_EC_KEY_new());
  49290. AssertIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  49291. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  49292. wolfSSL_EC_KEY_free(key);
  49293. wolfSSL_EC_GROUP_free(group);
  49294. #endif
  49295. res = TEST_RES_CHECK(1);
  49296. #endif
  49297. return res;
  49298. }
  49299. static int test_EC_i2d(void)
  49300. {
  49301. int res = TEST_SKIPPED;
  49302. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  49303. EC_KEY *key;
  49304. EC_KEY *copy = NULL;
  49305. int len;
  49306. unsigned char *buf = NULL;
  49307. unsigned char *p;
  49308. const unsigned char *tmp = NULL;
  49309. const unsigned char octBad[] = {
  49310. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49311. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49312. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49313. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  49314. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  49315. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  49316. };
  49317. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49318. AssertIntEQ(EC_KEY_generate_key(key), 1);
  49319. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  49320. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49321. DYNAMIC_TYPE_TMP_BUFFER));
  49322. p = buf;
  49323. AssertIntEQ(i2d_EC_PUBKEY(key, &p), len);
  49324. AssertNull(o2i_ECPublicKey(NULL, NULL, -1));
  49325. AssertNull(o2i_ECPublicKey(&copy, NULL, -1));
  49326. AssertNull(o2i_ECPublicKey(&key, NULL, -1));
  49327. AssertNull(o2i_ECPublicKey(NULL, &tmp, -1));
  49328. AssertNull(o2i_ECPublicKey(NULL, NULL, 0));
  49329. AssertNull(o2i_ECPublicKey(&key, NULL, 0));
  49330. AssertNull(o2i_ECPublicKey(&key, &tmp, 0));
  49331. tmp = buf;
  49332. AssertNull(o2i_ECPublicKey(NULL, &tmp, 0));
  49333. AssertNull(o2i_ECPublicKey(&copy, &tmp, 0));
  49334. AssertNull(o2i_ECPublicKey(NULL, &tmp, -1));
  49335. AssertNull(o2i_ECPublicKey(&key, &tmp, -1));
  49336. AssertIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  49337. AssertIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  49338. tmp = buf;
  49339. AssertNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  49340. AssertNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  49341. AssertNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  49342. AssertNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  49343. AssertNull(d2i_ECPrivateKey(&key, &tmp, 0));
  49344. AssertIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  49345. AssertIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  49346. AssertIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  49347. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  49348. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  49349. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  49350. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  49351. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  49352. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49353. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  49354. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49355. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  49356. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49357. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  49358. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49359. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  49360. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  49361. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  49362. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  49363. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  49364. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  49365. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49366. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49367. buf = NULL;
  49368. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  49369. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49370. DYNAMIC_TYPE_TMP_BUFFER));
  49371. p = buf;
  49372. AssertIntEQ(i2d_ECPrivateKey(key, &p), len);
  49373. p = NULL;
  49374. AssertIntEQ(i2d_ECPrivateKey(key, &p), len);
  49375. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49376. /* Bad point is also an invalid private key. */
  49377. tmp = octBad;
  49378. AssertNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  49379. tmp = buf;
  49380. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  49381. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49382. buf = NULL;
  49383. AssertIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  49384. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49385. DYNAMIC_TYPE_TMP_BUFFER));
  49386. p = buf;
  49387. AssertIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  49388. p = NULL;
  49389. AssertIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  49390. tmp = buf;
  49391. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  49392. tmp = octBad;
  49393. AssertNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  49394. AssertIntEQ(EC_KEY_check_key(NULL), 0);
  49395. AssertIntEQ(EC_KEY_check_key(key), 1);
  49396. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  49397. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  49398. EC_KEY_free(key);
  49399. EC_KEY_free(copy);
  49400. res = TEST_RES_CHECK(1);
  49401. #endif
  49402. return res;
  49403. }
  49404. static int test_wolfSSL_EC_curve(void)
  49405. {
  49406. int res = TEST_SKIPPED;
  49407. #if defined(OPENSSL_EXTRA)
  49408. int nid = NID_secp160k1;
  49409. const char* nid_name;
  49410. AssertNull(EC_curve_nid2nist(NID_sha256));
  49411. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  49412. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  49413. AssertIntEQ(EC_curve_nist2nid("INVALID"), 0);
  49414. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  49415. res = TEST_RES_CHECK(1);
  49416. #endif
  49417. return res;
  49418. }
  49419. static int test_wolfSSL_EC_KEY_dup(void)
  49420. {
  49421. int res = TEST_SKIPPED;
  49422. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  49423. WOLFSSL_EC_KEY* ecKey;
  49424. WOLFSSL_EC_KEY* dupKey;
  49425. ecc_key* srcKey;
  49426. ecc_key* destKey;
  49427. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  49428. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49429. /* Valid cases */
  49430. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49431. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  49432. /* Compare pubkey */
  49433. srcKey = (ecc_key*)ecKey->internal;
  49434. destKey = (ecc_key*)dupKey->internal;
  49435. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  49436. /* compare EC_GROUP */
  49437. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  49438. /* compare EC_POINT */
  49439. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  49440. dupKey->pub_key, NULL), MP_EQ);
  49441. /* compare BIGNUM */
  49442. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  49443. wolfSSL_EC_KEY_free(dupKey);
  49444. /* Invalid cases */
  49445. /* NULL key */
  49446. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  49447. /* NULL ecc_key */
  49448. wc_ecc_free((ecc_key*)ecKey->internal);
  49449. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  49450. ecKey->internal = NULL; /* Set ecc_key to NULL */
  49451. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49452. wolfSSL_EC_KEY_free(ecKey);
  49453. wolfSSL_EC_KEY_free(dupKey);
  49454. /* NULL Group */
  49455. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  49456. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49457. wolfSSL_EC_GROUP_free(ecKey->group);
  49458. ecKey->group = NULL; /* Set group to NULL */
  49459. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49460. wolfSSL_EC_KEY_free(ecKey);
  49461. wolfSSL_EC_KEY_free(dupKey);
  49462. /* NULL public key */
  49463. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  49464. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49465. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  49466. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  49467. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49468. wolfSSL_EC_POINT_free(ecKey->pub_key);
  49469. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  49470. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49471. wolfSSL_EC_KEY_free(ecKey);
  49472. wolfSSL_EC_KEY_free(dupKey);
  49473. /* NULL private key */
  49474. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  49475. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49476. wolfSSL_BN_free(ecKey->priv_key);
  49477. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  49478. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49479. wolfSSL_EC_KEY_free(ecKey);
  49480. wolfSSL_EC_KEY_free(dupKey);
  49481. /* Test EC_KEY_up_ref */
  49482. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  49483. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  49484. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  49485. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  49486. /* reference count doesn't follow duplicate */
  49487. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49488. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  49489. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  49490. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  49491. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  49492. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  49493. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  49494. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  49495. res = TEST_RES_CHECK(1);
  49496. #endif
  49497. return res;
  49498. }
  49499. static int test_wolfSSL_EC_KEY_set_group(void)
  49500. {
  49501. int res = TEST_SKIPPED;
  49502. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  49503. defined(OPENSSL_EXTRA)
  49504. EC_KEY *key = NULL;
  49505. EC_GROUP *group = NULL;
  49506. const EC_GROUP *group2 = NULL;
  49507. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  49508. AssertNotNull(key = EC_KEY_new());
  49509. AssertNull(EC_KEY_get0_group(NULL));
  49510. AssertIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  49511. AssertIntEQ(EC_KEY_set_group(key, NULL), 0);
  49512. AssertIntEQ(EC_KEY_set_group(NULL, group), 0);
  49513. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  49514. AssertNotNull(group2 = EC_KEY_get0_group(key));
  49515. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  49516. EC_GROUP_free(group);
  49517. EC_KEY_free(key);
  49518. res = TEST_RES_CHECK(1);
  49519. #endif
  49520. return res;
  49521. }
  49522. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  49523. {
  49524. int res = TEST_SKIPPED;
  49525. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49526. BIO* bio;
  49527. EC_KEY* key;
  49528. /* Error condition: NULL key. */
  49529. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  49530. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  49531. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  49532. /* Conversion form defaults to uncompressed. */
  49533. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  49534. #ifdef HAVE_COMP_KEY
  49535. /* Explicitly set to compressed. */
  49536. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  49537. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  49538. #else
  49539. /* Will still work just won't change anything. */
  49540. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  49541. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  49542. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  49543. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  49544. #endif
  49545. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  49546. BIO_free(bio);
  49547. EC_KEY_free(key);
  49548. res = TEST_RES_CHECK(1);
  49549. #endif
  49550. return res;
  49551. }
  49552. static int test_wolfSSL_EC_KEY_private_key(void)
  49553. {
  49554. int res = TEST_SKIPPED;
  49555. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49556. WOLFSSL_EC_KEY* key;
  49557. WOLFSSL_BIGNUM* priv = NULL;
  49558. WOLFSSL_BIGNUM* priv2 = NULL;
  49559. WOLFSSL_BIGNUM* bn;
  49560. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49561. AssertNotNull(priv = wolfSSL_BN_new());
  49562. AssertNotNull(priv2 = wolfSSL_BN_new());
  49563. AssertIntNE(BN_set_word(priv, 1), 0);
  49564. AssertIntNE(BN_set_word(priv2, 1), 0);
  49565. AssertNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  49566. /* No private key set. */
  49567. AssertNull(wolfSSL_EC_KEY_get0_private_key(key));
  49568. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  49569. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  49570. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  49571. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  49572. AssertNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  49573. AssertPtrNE(bn, priv);
  49574. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  49575. AssertNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  49576. AssertPtrNE(bn, priv2);
  49577. wolfSSL_BN_free(priv2);
  49578. wolfSSL_BN_free(priv);
  49579. wolfSSL_EC_KEY_free(key);
  49580. res = TEST_RES_CHECK(1);
  49581. #endif
  49582. return res;
  49583. }
  49584. static int test_wolfSSL_EC_KEY_public_key(void)
  49585. {
  49586. int res = TEST_SKIPPED;
  49587. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49588. WOLFSSL_EC_KEY* key;
  49589. WOLFSSL_EC_POINT* pub;
  49590. WOLFSSL_EC_POINT* point;
  49591. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49592. AssertNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  49593. AssertNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  49594. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  49595. AssertNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  49596. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  49597. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  49598. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  49599. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  49600. AssertNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  49601. AssertPtrEq(point, pub);
  49602. wolfSSL_EC_KEY_free(key);
  49603. res = TEST_RES_CHECK(1);
  49604. #endif
  49605. return res;
  49606. }
  49607. static int test_wolfSSL_EC_KEY_print_fp(void)
  49608. {
  49609. int res = TEST_SKIPPED;
  49610. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  49611. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  49612. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  49613. !defined(NO_STDIO_FILESYSTEM)
  49614. EC_KEY* key = NULL;
  49615. /* Bad file pointer. */
  49616. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  49617. /* NULL key. */
  49618. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  49619. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  49620. /* Negative indent. */
  49621. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  49622. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  49623. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  49624. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  49625. wolfSSL_EC_KEY_free(key);
  49626. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  49627. NID_X9_62_prime256v1)));
  49628. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  49629. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  49630. wolfSSL_EC_KEY_free(key);
  49631. res = TEST_RES_CHECK(1);
  49632. #endif
  49633. return res;
  49634. }
  49635. static int test_wolfSSL_EC_get_builtin_curves(void)
  49636. {
  49637. int res = TEST_SKIPPED;
  49638. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  49639. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49640. EC_builtin_curve* curves = NULL;
  49641. size_t crv_len = 0;
  49642. size_t i = 0;
  49643. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  49644. AssertNotNull(curves = (EC_builtin_curve*)XMALLOC(
  49645. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  49646. AssertIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  49647. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  49648. for (i = 0; i < crv_len; i++) {
  49649. if (curves[i].comment != NULL) {
  49650. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  49651. }
  49652. }
  49653. if (crv_len > 1) {
  49654. AssertIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  49655. }
  49656. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49657. res = TEST_RES_CHECK(1);
  49658. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  49659. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  49660. return res;
  49661. }
  49662. static int test_wolfSSL_ECDSA_SIG(void)
  49663. {
  49664. int res = TEST_SKIPPED;
  49665. #ifdef OPENSSL_EXTRA
  49666. WOLFSSL_ECDSA_SIG* sig = NULL;
  49667. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  49668. WOLFSSL_BIGNUM* r;
  49669. WOLFSSL_BIGNUM* s;
  49670. const WOLFSSL_BIGNUM* r2;
  49671. const WOLFSSL_BIGNUM* s2;
  49672. const unsigned char* cp;
  49673. unsigned char* p;
  49674. unsigned char outSig[8];
  49675. unsigned char sigData[8] =
  49676. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  49677. unsigned char sigDataBad[8] =
  49678. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  49679. wolfSSL_ECDSA_SIG_free(NULL);
  49680. AssertNotNull(sig = wolfSSL_ECDSA_SIG_new());
  49681. AssertNotNull(r = wolfSSL_BN_new());
  49682. AssertNotNull(s = wolfSSL_BN_new());
  49683. AssertIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  49684. AssertIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  49685. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  49686. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  49687. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  49688. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  49689. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  49690. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  49691. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  49692. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  49693. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  49694. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  49695. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  49696. r2 = NULL;
  49697. s2 = NULL;
  49698. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  49699. AssertNull(r2);
  49700. AssertNull(s2);
  49701. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  49702. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  49703. AssertPtrEq(r2, r);
  49704. AssertPtrEq(s2, s);
  49705. r2 = NULL;
  49706. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  49707. AssertPtrEq(r2, r);
  49708. s2 = NULL;
  49709. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  49710. AssertPtrEq(s2, s);
  49711. /* r and s are freed when sig is freed. */
  49712. wolfSSL_ECDSA_SIG_free(sig);
  49713. AssertNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  49714. cp = sigDataBad;
  49715. AssertNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  49716. cp = sigData;
  49717. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  49718. AssertIntEQ((cp == sigData + 8), 1);
  49719. cp = sigData;
  49720. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  49721. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  49722. AssertIntEQ((sig == sig2), 1);
  49723. cp = outSig;
  49724. p = outSig;
  49725. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  49726. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  49727. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  49728. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  49729. AssertIntEQ((p == outSig + 8), 1);
  49730. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  49731. wolfSSL_ECDSA_SIG_free(sig);
  49732. res = TEST_RES_CHECK(1);
  49733. #endif
  49734. return res;
  49735. }
  49736. static int test_ECDSA_size_sign(void)
  49737. {
  49738. int res = TEST_SKIPPED;
  49739. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  49740. EC_KEY* key;
  49741. ECDSA_SIG* ecdsaSig;
  49742. int id;
  49743. byte hash[WC_MAX_DIGEST_SIZE];
  49744. byte hash2[WC_MAX_DIGEST_SIZE];
  49745. byte sig[ECC_MAX_SIG_SIZE];
  49746. unsigned int sigSz = sizeof(sig);
  49747. XMEMSET(hash, 123, sizeof(hash));
  49748. XMEMSET(hash2, 234, sizeof(hash2));
  49749. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  49750. AssertIntEQ(id, ECC_SECP256R1);
  49751. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49752. AssertIntEQ(EC_KEY_generate_key(key), 1);
  49753. AssertIntGE(ECDSA_size(NULL), 0);
  49754. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  49755. AssertIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  49756. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  49757. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  49758. AssertIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  49759. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  49760. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  49761. AssertIntGE(ECDSA_size(key), sigSz);
  49762. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  49763. AssertIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  49764. AssertNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  49765. AssertNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  49766. AssertNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  49767. AssertNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  49768. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  49769. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  49770. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  49771. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  49772. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  49773. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  49774. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  49775. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  49776. AssertIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  49777. ECDSA_SIG_free(ecdsaSig);
  49778. EC_KEY_free(key);
  49779. res = TEST_RES_CHECK(1);
  49780. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  49781. return res;
  49782. }
  49783. static int test_ECDH_compute_key(void)
  49784. {
  49785. int res = TEST_SKIPPED;
  49786. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  49787. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49788. EC_KEY* key1;
  49789. EC_KEY* key2;
  49790. EC_POINT* pub1;
  49791. EC_POINT* pub2;
  49792. byte secret1[32];
  49793. byte secret2[32];
  49794. int i;
  49795. AssertNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49796. AssertIntEQ(EC_KEY_generate_key(key1), 1);
  49797. AssertNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  49798. AssertNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49799. AssertIntEQ(EC_KEY_generate_key(key2), 1);
  49800. AssertNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  49801. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  49802. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  49803. 0);
  49804. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  49805. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  49806. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  49807. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  49808. 0);
  49809. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  49810. 0);
  49811. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  49812. NULL), 0);
  49813. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  49814. sizeof(secret1));
  49815. AssertIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  49816. sizeof(secret2));
  49817. for (i = 0; i < (int)sizeof(secret1); i++) {
  49818. AssertIntEQ(secret1[i], secret2[i]);
  49819. }
  49820. EC_KEY_free(key2);
  49821. EC_KEY_free(key1);
  49822. res = TEST_RES_CHECK(1);
  49823. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  49824. * !WOLF_CRYPTO_CB_ONLY_ECC */
  49825. return res;
  49826. }
  49827. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  49828. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  49829. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  49830. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  49831. {
  49832. X509* x509 = NULL;
  49833. BIGNUM* serial_number = NULL;
  49834. X509_NAME* name = NULL;
  49835. time_t epoch_off = 0;
  49836. ASN1_INTEGER* asn1_serial_number;
  49837. long not_before, not_after;
  49838. AssertNotNull(x509 = X509_new());
  49839. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  49840. AssertNotNull(serial_number = BN_new());
  49841. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  49842. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  49843. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  49844. /* version 3 */
  49845. AssertIntNE(X509_set_version(x509, 2L), 0);
  49846. AssertNotNull(name = X509_NAME_new());
  49847. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  49848. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  49849. AssertIntNE(X509_set_subject_name(x509, name), 0);
  49850. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  49851. not_before = (long)wc_Time(NULL);
  49852. not_after = not_before + (365 * 24 * 60 * 60);
  49853. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  49854. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  49855. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  49856. BN_free(serial_number);
  49857. X509_NAME_free(name);
  49858. X509_free(x509);
  49859. return 0;
  49860. }
  49861. #endif
  49862. static int test_openssl_generate_key_and_cert(void)
  49863. {
  49864. int res = TEST_SKIPPED;
  49865. #if defined(OPENSSL_EXTRA)
  49866. #if !defined(NO_RSA)
  49867. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  49868. EVP_PKEY* pkey = EVP_PKEY_new();
  49869. int key_length = 2048;
  49870. BIGNUM* exponent = BN_new();
  49871. RSA* rsa = RSA_new();
  49872. AssertNotNull(pkey);
  49873. AssertNotNull(exponent);
  49874. AssertNotNull(rsa);
  49875. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  49876. #ifndef WOLFSSL_KEY_GEN
  49877. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  49878. #if defined(USE_CERT_BUFFERS_1024)
  49879. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  49880. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  49881. key_length = 1024;
  49882. #elif defined(USE_CERT_BUFFERS_2048)
  49883. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  49884. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  49885. #else
  49886. RSA_free(rsa);
  49887. rsa = NULL;
  49888. #endif
  49889. #else
  49890. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  49891. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  49892. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  49893. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  49894. #endif
  49895. if (rsa) {
  49896. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  49897. BN_free(exponent);
  49898. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  49899. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  49900. test_openssl_make_self_signed_certificate(pkey);
  49901. #endif
  49902. }
  49903. EVP_PKEY_free(pkey);
  49904. res = TEST_RES_CHECK(1);
  49905. }
  49906. #endif /* !NO_RSA */
  49907. #ifdef HAVE_ECC
  49908. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  49909. EVP_PKEY* pkey = EVP_PKEY_new();
  49910. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  49911. AssertNotNull(pkey);
  49912. AssertNotNull(ec_key);
  49913. #ifndef NO_WOLFSSL_STUB
  49914. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  49915. #endif
  49916. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  49917. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  49918. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  49919. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  49920. test_openssl_make_self_signed_certificate(pkey);
  49921. #endif
  49922. EVP_PKEY_free(pkey);
  49923. res = TEST_RES_CHECK(1);
  49924. }
  49925. #endif /* HAVE_ECC */
  49926. #endif /* OPENSSL_EXTRA */
  49927. return res;
  49928. }
  49929. static int test_stubs_are_stubs(void)
  49930. {
  49931. int res = TEST_SKIPPED;
  49932. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  49933. WOLFSSL_CTX* ctx = NULL;
  49934. WOLFSSL_CTX* ctxN = NULL;
  49935. #ifndef NO_WOLFSSL_CLIENT
  49936. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  49937. AssertNotNull(ctx);
  49938. #elif !defined(NO_WOLFSSL_SERVER)
  49939. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  49940. AssertNotNull(ctx);
  49941. #else
  49942. return res;
  49943. #endif
  49944. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  49945. AssertIntEQ((int) x(z), 0)
  49946. /* test logic, all stubs return same result regardless of ctx being NULL
  49947. * as there are no sanity checks, it's just a stub! If at some
  49948. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  49949. * if invalid inputs are supplied. Test calling both
  49950. * with and without valid inputs, if a stub functionality remains unchanged.
  49951. */
  49952. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  49953. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  49954. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  49955. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  49956. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  49957. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  49958. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  49959. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  49960. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  49961. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  49962. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  49963. wolfSSL_CTX_free(ctx);
  49964. ctx = NULL;
  49965. res = TEST_RES_CHECK(1);
  49966. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  49967. return res;
  49968. }
  49969. static int test_CONF_modules_xxx(void)
  49970. {
  49971. int res = TEST_SKIPPED;
  49972. #if defined(OPENSSL_EXTRA)
  49973. CONF_modules_free();
  49974. AssertTrue(1); /* to confirm previous call gives no harm */
  49975. CONF_modules_unload(0);
  49976. AssertTrue(1);
  49977. CONF_modules_unload(1);
  49978. AssertTrue(1);
  49979. CONF_modules_unload(-1);
  49980. AssertTrue(1);
  49981. res = TEST_RES_CHECK(1);
  49982. #endif /* OPENSSL_EXTRA */
  49983. return res;
  49984. }
  49985. static int test_CRYPTO_set_dynlock_xxx(void)
  49986. {
  49987. int res = TEST_SKIPPED;
  49988. #if defined(OPENSSL_EXTRA)
  49989. CRYPTO_set_dynlock_create_callback(
  49990. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  49991. CRYPTO_set_dynlock_create_callback(
  49992. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  49993. CRYPTO_set_dynlock_destroy_callback(
  49994. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  49995. CRYPTO_set_dynlock_destroy_callback(
  49996. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  49997. CRYPTO_set_dynlock_lock_callback(
  49998. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  49999. CRYPTO_set_dynlock_lock_callback(
  50000. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  50001. AssertTrue(1); /* to confirm previous call gives no harm */
  50002. res = TEST_RES_CHECK(1);
  50003. #endif /* OPENSSL_EXTRA */
  50004. return res;
  50005. }
  50006. static int test_CRYPTO_THREADID_xxx(void)
  50007. {
  50008. int res = TEST_SKIPPED;
  50009. #if defined(OPENSSL_EXTRA)
  50010. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  50011. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  50012. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  50013. res = TEST_RES_CHECK(1);
  50014. #endif /* OPENSSL_EXTRA */
  50015. return res;
  50016. }
  50017. static int test_ENGINE_cleanup(void)
  50018. {
  50019. int res = TEST_SKIPPED;
  50020. #if defined(OPENSSL_EXTRA)
  50021. ENGINE_cleanup();
  50022. AssertTrue(1); /* to confirm previous call gives no harm */
  50023. res = TEST_RES_CHECK(1);
  50024. #endif /* OPENSSL_EXTRA */
  50025. return res;
  50026. }
  50027. static int test_wolfSSL_CTX_LoadCRL(void)
  50028. {
  50029. int res = TEST_SKIPPED;
  50030. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  50031. WOLFSSL_CTX* ctx = NULL;
  50032. WOLFSSL* ssl = NULL;
  50033. const char* badPath = "dummypath";
  50034. const char* validPath = "./certs/crl";
  50035. const char* validFilePath = "./certs/crl/cliCrl.pem";
  50036. const char* issuerCert = "./certs/client-cert.pem";
  50037. int derType = WOLFSSL_FILETYPE_ASN1;
  50038. int pemType = WOLFSSL_FILETYPE_PEM;
  50039. int monitor = WOLFSSL_CRL_MONITOR;
  50040. WOLFSSL_CERT_MANAGER* cm = NULL;
  50041. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  50042. BAD_FUNC_ARG)
  50043. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  50044. WOLFSSL_SUCCESS)
  50045. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  50046. #ifndef NO_WOLFSSL_CLIENT
  50047. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50048. #elif !defined(NO_WOLFSSL_SERVER)
  50049. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50050. #else
  50051. return;
  50052. #endif
  50053. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  50054. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  50055. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  50056. wolfSSL_CTX_free(ctx);
  50057. #ifndef NO_WOLFSSL_CLIENT
  50058. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50059. #elif !defined(NO_WOLFSSL_SERVER)
  50060. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50061. #else
  50062. return;
  50063. #endif
  50064. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  50065. WOLFSSL_SUCCESS);
  50066. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  50067. wolfSSL_CTX_free(ctx);
  50068. #ifndef NO_WOLFSSL_CLIENT
  50069. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50070. #elif !defined(NO_WOLFSSL_SERVER)
  50071. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50072. #else
  50073. return;
  50074. #endif
  50075. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  50076. WOLFSSL_SUCCESS);
  50077. AssertNotNull(ssl = wolfSSL_new(ctx));
  50078. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  50079. wolfSSL_free(ssl);
  50080. wolfSSL_CTX_free(ctx);
  50081. AssertNotNull(cm = wolfSSL_CertManagerNew());
  50082. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  50083. WOLFSSL_SUCCESS);
  50084. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  50085. wolfSSL_CertManagerFree(cm);
  50086. res = TEST_RES_CHECK(1);
  50087. #endif
  50088. return res;
  50089. }
  50090. static int test_SetTmpEC_DHE_Sz(void)
  50091. {
  50092. int res = TEST_SKIPPED;
  50093. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  50094. WOLFSSL_CTX *ctx;
  50095. WOLFSSL *ssl;
  50096. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  50097. AssertNotNull(ctx);
  50098. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  50099. ssl = wolfSSL_new(ctx);
  50100. AssertNotNull(ssl);
  50101. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  50102. wolfSSL_free(ssl);
  50103. wolfSSL_CTX_free(ctx);
  50104. res = TEST_RES_CHECK(1);
  50105. #endif
  50106. return res;
  50107. }
  50108. static int test_wolfSSL_CTX_get0_privatekey(void)
  50109. {
  50110. int res = TEST_SKIPPED;
  50111. #ifdef OPENSSL_ALL
  50112. WOLFSSL_CTX* ctx = NULL;
  50113. #ifndef NO_RSA
  50114. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  50115. AssertNull(SSL_CTX_get0_privatekey(ctx));
  50116. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  50117. WOLFSSL_FILETYPE_PEM));
  50118. AssertNull(SSL_CTX_get0_privatekey(ctx));
  50119. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  50120. WOLFSSL_FILETYPE_PEM));
  50121. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  50122. wolfSSL_CTX_free(ctx);
  50123. #endif
  50124. #ifdef HAVE_ECC
  50125. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  50126. AssertNull(SSL_CTX_get0_privatekey(ctx));
  50127. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  50128. WOLFSSL_FILETYPE_PEM));
  50129. AssertNull(SSL_CTX_get0_privatekey(ctx));
  50130. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  50131. WOLFSSL_FILETYPE_PEM));
  50132. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  50133. wolfSSL_CTX_free(ctx);
  50134. #endif
  50135. res = TEST_RES_CHECK(1);
  50136. #endif
  50137. return res;
  50138. }
  50139. static int test_wolfSSL_dtls_set_mtu(void)
  50140. {
  50141. int res = TEST_SKIPPED;
  50142. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  50143. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  50144. WOLFSSL_CTX* ctx = NULL;
  50145. WOLFSSL* ssl = NULL;
  50146. const char* testCertFile;
  50147. const char* testKeyFile;
  50148. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  50149. #ifndef NO_RSA
  50150. testCertFile = svrCertFile;
  50151. testKeyFile = svrKeyFile;
  50152. #elif defined(HAVE_ECC)
  50153. testCertFile = eccCertFile;
  50154. testKeyFile = eccKeyFile;
  50155. #endif
  50156. if (testCertFile != NULL && testKeyFile != NULL) {
  50157. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  50158. WOLFSSL_FILETYPE_PEM));
  50159. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  50160. WOLFSSL_FILETYPE_PEM));
  50161. }
  50162. AssertNotNull(ssl = wolfSSL_new(ctx));
  50163. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  50164. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  50165. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  50166. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  50167. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  50168. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  50169. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  50170. wolfSSL_free(ssl);
  50171. wolfSSL_CTX_free(ctx);
  50172. res = TEST_RES_CHECK(1);
  50173. #endif
  50174. return res;
  50175. }
  50176. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  50177. defined(WOLFSSL_DTLS)
  50178. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  50179. enum HandShakeType hsType, word16 length)
  50180. {
  50181. size_t idx = 0;
  50182. byte* l;
  50183. /* record layer */
  50184. /* handshake type */
  50185. out[idx++] = handshake;
  50186. /* protocol version */
  50187. out[idx++] = 0xfe;
  50188. out[idx++] = 0xfd; /* DTLS 1.2 */
  50189. /* epoch 0 */
  50190. XMEMSET(out + idx, 0, 2);
  50191. idx += 2;
  50192. /* sequence number */
  50193. XMEMSET(out + idx, 0, 6);
  50194. c32toa(seq, out + idx + 2);
  50195. idx += 6;
  50196. /* length in BE */
  50197. if (length)
  50198. c16toa(length, out + idx);
  50199. else
  50200. c16toa(outSz - idx - 2, out + idx);
  50201. idx += 2;
  50202. /* handshake layer */
  50203. /* handshake type */
  50204. out[idx++] = (byte)hsType;
  50205. /* length */
  50206. l = out + idx;
  50207. idx += 3;
  50208. /* message seq */
  50209. c16toa(0, out + idx);
  50210. idx += 2;
  50211. /* frag offset */
  50212. c32to24(0, out + idx);
  50213. idx += 3;
  50214. /* frag length */
  50215. c32to24((word32)outSz - (word32)idx - 3, l);
  50216. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  50217. idx += 3;
  50218. XMEMSET(out + idx, 0, outSz - idx);
  50219. }
  50220. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  50221. {
  50222. byte msg[] = "This is a msg for the client";
  50223. byte reply[40];
  50224. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  50225. reply[sizeof(reply) - 1] = '\0';
  50226. fprintf(stderr, "Client message: %s\n", reply);
  50227. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  50228. }
  50229. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  50230. {
  50231. byte ch[50];
  50232. int fd = wolfSSL_get_fd(ssl);
  50233. byte msg[] = "This is a msg for the server";
  50234. byte reply[40];
  50235. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  50236. /* Server should ignore this datagram */
  50237. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  50238. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  50239. /* Server should ignore this datagram */
  50240. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  50241. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  50242. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  50243. reply[sizeof(reply) - 1] = '\0';
  50244. fprintf(stderr, "Server response: %s\n", reply);
  50245. }
  50246. static int test_wolfSSL_dtls_plaintext(void)
  50247. {
  50248. callback_functions func_cb_client;
  50249. callback_functions func_cb_server;
  50250. size_t i;
  50251. struct test_params {
  50252. method_provider client_meth;
  50253. method_provider server_meth;
  50254. ssl_callback on_result_server;
  50255. ssl_callback on_result_client;
  50256. } params[] = {
  50257. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  50258. test_wolfSSL_dtls_plaintext_server,
  50259. test_wolfSSL_dtls_plaintext_client},
  50260. };
  50261. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  50262. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  50263. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  50264. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  50265. func_cb_server.method = params[i].server_meth;
  50266. func_cb_client.method = params[i].client_meth;
  50267. func_cb_client.on_result = params[i].on_result_client;
  50268. func_cb_server.on_result = params[i].on_result_server;
  50269. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  50270. if (!func_cb_client.return_code)
  50271. return TEST_FAIL;
  50272. if (!func_cb_server.return_code)
  50273. return TEST_FAIL;
  50274. }
  50275. return TEST_RES_CHECK(1);
  50276. }
  50277. #else
  50278. static int test_wolfSSL_dtls_plaintext(void) {
  50279. return TEST_SKIPPED;
  50280. }
  50281. #endif
  50282. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  50283. defined(WOLFSSL_DTLS)
  50284. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  50285. {
  50286. byte b[1100]; /* buffer for the messages to send */
  50287. size_t idx = 0;
  50288. size_t seq_offset = 0;
  50289. size_t msg_offset = 0;
  50290. int i;
  50291. int fd = wolfSSL_get_fd(ssl);
  50292. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  50293. word32 seq_number = 100; /* start high so server definitely reads this */
  50294. word16 msg_number = 50; /* start high so server has to buffer this */
  50295. AssertIntEQ(ret, 1);
  50296. /* Now let's start spamming the peer with fragments it needs to store */
  50297. XMEMSET(b, -1, sizeof(b));
  50298. /* record layer */
  50299. /* handshake type */
  50300. b[idx++] = 22;
  50301. /* protocol version */
  50302. b[idx++] = 0xfe;
  50303. b[idx++] = 0xfd; /* DTLS 1.2 */
  50304. /* epoch 0 */
  50305. XMEMSET(b + idx, 0, 2);
  50306. idx += 2;
  50307. /* sequence number */
  50308. XMEMSET(b + idx, 0, 6);
  50309. seq_offset = idx + 2; /* increment only the low 32 bits */
  50310. idx += 6;
  50311. /* static length in BE */
  50312. c16toa(42, b + idx);
  50313. idx += 2;
  50314. /* handshake layer */
  50315. /* cert type */
  50316. b[idx++] = 11;
  50317. /* length */
  50318. c32to24(1000, b + idx);
  50319. idx += 3;
  50320. /* message seq */
  50321. c16toa(0, b + idx);
  50322. msg_offset = idx;
  50323. idx += 2;
  50324. /* frag offset */
  50325. c32to24(500, b + idx);
  50326. idx += 3;
  50327. /* frag length */
  50328. c32to24(30, b + idx);
  50329. idx += 3;
  50330. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  50331. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  50332. seq_number++, msg_number++, i++) {
  50333. struct timespec delay;
  50334. XMEMSET(&delay, 0, sizeof(delay));
  50335. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  50336. c32toa(seq_number, b + seq_offset);
  50337. c16toa(msg_number, b + msg_offset);
  50338. ret = (int)send(fd, b, 55, 0);
  50339. nanosleep(&delay, NULL);
  50340. }
  50341. }
  50342. #ifdef WOLFSSL_DTLS13
  50343. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  50344. {
  50345. byte b[150]; /* buffer for the messages to send */
  50346. size_t idx = 0;
  50347. size_t msg_offset = 0;
  50348. int fd = wolfSSL_get_fd(ssl);
  50349. word16 msg_number = 10; /* start high so server has to buffer this */
  50350. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  50351. AssertIntEQ(ret, 1);
  50352. /* Now let's start spamming the peer with fragments it needs to store */
  50353. XMEMSET(b, -1, sizeof(b));
  50354. /* handshake type */
  50355. b[idx++] = 11;
  50356. /* length */
  50357. c32to24(10000, b + idx);
  50358. idx += 3;
  50359. /* message_seq */
  50360. msg_offset = idx;
  50361. idx += 2;
  50362. /* fragment_offset */
  50363. c32to24(5000, b + idx);
  50364. idx += 3;
  50365. /* fragment_length */
  50366. c32to24(100, b + idx);
  50367. idx += 3;
  50368. /* fragment contents */
  50369. idx += 100;
  50370. for (; ret > 0; msg_number++) {
  50371. byte sendBuf[150];
  50372. int sendSz = sizeof(sendBuf);
  50373. struct timespec delay;
  50374. XMEMSET(&delay, 0, sizeof(delay));
  50375. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  50376. c16toa(msg_number, b + msg_offset);
  50377. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  50378. (int)idx, handshake, 0, 0, 0);
  50379. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  50380. nanosleep(&delay, NULL);
  50381. }
  50382. }
  50383. #endif
  50384. static int test_wolfSSL_dtls_fragments(void)
  50385. {
  50386. callback_functions func_cb_client;
  50387. callback_functions func_cb_server;
  50388. size_t i;
  50389. struct test_params {
  50390. method_provider client_meth;
  50391. method_provider server_meth;
  50392. ssl_callback spammer;
  50393. } params[] = {
  50394. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  50395. test_wolfSSL_dtls12_fragments_spammer},
  50396. #ifdef WOLFSSL_DTLS13
  50397. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  50398. test_wolfSSL_dtls13_fragments_spammer},
  50399. #endif
  50400. };
  50401. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  50402. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  50403. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  50404. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  50405. func_cb_server.method = params[i].server_meth;
  50406. func_cb_client.method = params[i].client_meth;
  50407. func_cb_client.ssl_ready = params[i].spammer;
  50408. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  50409. AssertFalse(func_cb_client.return_code);
  50410. AssertFalse(func_cb_server.return_code);
  50411. /* The socket should be closed by the server resulting in a
  50412. * socket error or reading a close notify alert */
  50413. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  50414. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN) {
  50415. AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  50416. }
  50417. /* Check the server returned an error indicating the msg buffer
  50418. * was full */
  50419. AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  50420. }
  50421. return TEST_RES_CHECK(1);
  50422. }
  50423. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  50424. {
  50425. int fd, ret;
  50426. byte alert_msg[] = {
  50427. 0x15, /* alert type */
  50428. 0xfe, 0xfd, /* version */
  50429. 0x00, 0x00, /* epoch */
  50430. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  50431. 0x00, 0x02, /* length */
  50432. 0x02, /* level: fatal */
  50433. 0x46 /* protocol version */
  50434. };
  50435. fd = wolfSSL_get_fd(ssl);
  50436. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  50437. AssertIntGT(ret, 0);
  50438. }
  50439. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  50440. {
  50441. callback_functions client_cbs, server_cbs;
  50442. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  50443. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  50444. client_cbs.doUdp = server_cbs.doUdp = 1;
  50445. if (version12) {
  50446. client_cbs.method = wolfDTLSv1_2_client_method;
  50447. server_cbs.method = wolfDTLSv1_2_server_method;
  50448. }
  50449. else {
  50450. #ifdef WOLFSSL_DTLS13
  50451. client_cbs.method = wolfDTLSv1_3_client_method;
  50452. server_cbs.method = wolfDTLSv1_3_server_method;
  50453. #else
  50454. return TEST_SKIPPED;
  50455. #endif /* WOLFSSL_DTLS13 */
  50456. }
  50457. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  50458. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  50459. if (!client_cbs.return_code)
  50460. return TEST_FAIL;
  50461. if (!server_cbs.return_code)
  50462. return TEST_FAIL;
  50463. return TEST_SUCCESS;
  50464. }
  50465. static int test_wolfSSL_ignore_alert_before_cookie(void)
  50466. {
  50467. int ret;
  50468. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  50469. if (ret != 0)
  50470. return ret;
  50471. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  50472. if (ret != 0)
  50473. return ret;
  50474. return 0;
  50475. }
  50476. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  50477. {
  50478. int ret;
  50479. int fd;
  50480. byte bad_msg[] = {
  50481. 0x17, /* app data */
  50482. 0xaa, 0xfd, /* bad version */
  50483. 0x00, 0x01, /* epoch 1 */
  50484. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  50485. 0x00, 0x26, /* length: 38 bytes */
  50486. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  50487. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  50488. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  50489. 0x03, 0x18, 0x72
  50490. };
  50491. fd = wolfSSL_get_fd(ssl);
  50492. AssertIntGE(fd, 0);
  50493. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  50494. AssertIntEQ(ret, sizeof(bad_msg));
  50495. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  50496. AssertIntEQ(ret, sizeof("badrecordtest"));
  50497. }
  50498. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  50499. {
  50500. byte buf[100];
  50501. int ret;
  50502. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  50503. AssertIntGT(ret, 0);
  50504. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  50505. }
  50506. static int _test_wolfSSL_dtls_bad_record(
  50507. method_provider client_method, method_provider server_method)
  50508. {
  50509. callback_functions client_cbs, server_cbs;
  50510. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  50511. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  50512. client_cbs.doUdp = server_cbs.doUdp = 1;
  50513. client_cbs.method = client_method;
  50514. server_cbs.method = server_method;
  50515. client_cbs.on_result = test_wolfSSL_send_bad_record;
  50516. server_cbs.on_result = test_wolfSSL_read_string;
  50517. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  50518. if (!client_cbs.return_code)
  50519. return TEST_FAIL;
  50520. if (!server_cbs.return_code)
  50521. return TEST_FAIL;
  50522. return TEST_SUCCESS;
  50523. }
  50524. static int test_wolfSSL_dtls_bad_record(void)
  50525. {
  50526. int ret;
  50527. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  50528. wolfDTLSv1_2_server_method);
  50529. #ifdef WOLFSSL_DTLS13
  50530. if (ret != TEST_SUCCESS)
  50531. return ret;
  50532. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  50533. wolfDTLSv1_3_server_method);
  50534. #else
  50535. return ret;
  50536. #endif /* WOLFSSL_DTLS13 */
  50537. }
  50538. #else
  50539. static int test_wolfSSL_dtls_fragments(void) {
  50540. return TEST_SKIPPED;
  50541. }
  50542. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  50543. return TEST_SKIPPED;
  50544. }
  50545. static int test_wolfSSL_dtls_bad_record(void) {
  50546. return TEST_SKIPPED;
  50547. }
  50548. #endif
  50549. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  50550. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  50551. defined(HAVE_IO_TESTS_DEPENDENCIES)
  50552. static byte test_AEAD_fail_decryption = 0;
  50553. static byte test_AEAD_seq_num = 0;
  50554. static byte test_AEAD_done = 0;
  50555. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  50556. {
  50557. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  50558. if (ret > 0) {
  50559. if (test_AEAD_fail_decryption) {
  50560. /* Modify the packet to trigger a decryption failure */
  50561. buf[ret/2] ^= 0xFF;
  50562. if (test_AEAD_fail_decryption == 1)
  50563. test_AEAD_fail_decryption = 0;
  50564. }
  50565. }
  50566. (void)ctx;
  50567. return ret;
  50568. }
  50569. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  50570. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  50571. {
  50572. if (sendLimit)
  50573. w64Zero(sendLimit);
  50574. switch (ssl->specs.bulk_cipher_algorithm) {
  50575. case wolfssl_aes_gcm:
  50576. if (sendLimit)
  50577. *sendLimit = AEAD_AES_LIMIT;
  50578. FALL_THROUGH;
  50579. case wolfssl_chacha:
  50580. if (hardLimit)
  50581. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  50582. if (keyUpdateLimit)
  50583. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  50584. break;
  50585. case wolfssl_aes_ccm:
  50586. if (sendLimit)
  50587. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  50588. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  50589. if (hardLimit)
  50590. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  50591. if (keyUpdateLimit)
  50592. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  50593. }
  50594. else {
  50595. if (hardLimit)
  50596. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  50597. if (keyUpdateLimit)
  50598. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  50599. }
  50600. break;
  50601. default:
  50602. fprintf(stderr, "Unrecognized bulk cipher");
  50603. AssertFalse(1);
  50604. break;
  50605. }
  50606. }
  50607. static void test_AEAD_limit_client(WOLFSSL* ssl)
  50608. {
  50609. int ret;
  50610. int i;
  50611. int didReKey = 0;
  50612. char msgBuf[20];
  50613. w64wrapper hardLimit;
  50614. w64wrapper keyUpdateLimit;
  50615. w64wrapper counter;
  50616. w64wrapper sendLimit;
  50617. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  50618. w64Zero(&counter);
  50619. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  50620. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  50621. for (i = 0; i < 10; i++) {
  50622. /* Test some failed decryptions */
  50623. test_AEAD_fail_decryption = 1;
  50624. w64Increment(&counter);
  50625. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  50626. /* Should succeed since decryption failures are dropped */
  50627. AssertIntGT(ret, 0);
  50628. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  50629. }
  50630. test_AEAD_fail_decryption = 1;
  50631. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  50632. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  50633. /* 100 read calls should be enough to complete the key update */
  50634. w64Zero(&counter);
  50635. for (i = 0; i < 100; i++) {
  50636. /* Key update should be sent and negotiated */
  50637. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  50638. AssertIntGT(ret, 0);
  50639. /* Epoch after one key update is 4 */
  50640. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  50641. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  50642. didReKey = 1;
  50643. break;
  50644. }
  50645. }
  50646. AssertTrue(didReKey);
  50647. if (!w64IsZero(sendLimit)) {
  50648. /* Test the sending limit for AEAD ciphers */
  50649. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  50650. test_AEAD_seq_num = 1;
  50651. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  50652. AssertIntGT(ret, 0);
  50653. didReKey = 0;
  50654. w64Zero(&counter);
  50655. /* 100 read calls should be enough to complete the key update */
  50656. for (i = 0; i < 100; i++) {
  50657. /* Key update should be sent and negotiated */
  50658. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  50659. AssertIntGT(ret, 0);
  50660. /* Epoch after another key update is 5 */
  50661. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  50662. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  50663. didReKey = 1;
  50664. break;
  50665. }
  50666. }
  50667. AssertTrue(didReKey);
  50668. }
  50669. test_AEAD_fail_decryption = 2;
  50670. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  50671. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  50672. /* Connection should fail with a DECRYPT_ERROR */
  50673. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  50674. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  50675. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  50676. test_AEAD_done = 1;
  50677. }
  50678. int counter = 0;
  50679. static void test_AEAD_limit_server(WOLFSSL* ssl)
  50680. {
  50681. char msgBuf[] = "Sending data";
  50682. int ret = WOLFSSL_SUCCESS;
  50683. w64wrapper sendLimit;
  50684. SOCKET_T fd = wolfSSL_get_fd(ssl);
  50685. struct timespec delay;
  50686. XMEMSET(&delay, 0, sizeof(delay));
  50687. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  50688. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  50689. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  50690. while (!test_AEAD_done && ret > 0) {
  50691. counter++;
  50692. if (test_AEAD_seq_num) {
  50693. /* We need to update the seq number so that we can understand the
  50694. * peer. Otherwise we will incorrectly interpret the seq number. */
  50695. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  50696. AssertNotNull(e);
  50697. e->nextPeerSeqNumber = sendLimit;
  50698. test_AEAD_seq_num = 0;
  50699. }
  50700. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  50701. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  50702. nanosleep(&delay, NULL);
  50703. }
  50704. }
  50705. static int test_wolfSSL_dtls_AEAD_limit(void)
  50706. {
  50707. callback_functions func_cb_client;
  50708. callback_functions func_cb_server;
  50709. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  50710. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  50711. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  50712. func_cb_server.method = wolfDTLSv1_3_server_method;
  50713. func_cb_client.method = wolfDTLSv1_3_client_method;
  50714. func_cb_server.on_result = test_AEAD_limit_server;
  50715. func_cb_client.on_result = test_AEAD_limit_client;
  50716. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  50717. if (!func_cb_client.return_code)
  50718. return TEST_FAIL;
  50719. if (!func_cb_server.return_code)
  50720. return TEST_FAIL;
  50721. return TEST_SUCCESS;
  50722. }
  50723. #else
  50724. static int test_wolfSSL_dtls_AEAD_limit(void)
  50725. {
  50726. return TEST_SKIPPED;
  50727. }
  50728. #endif
  50729. #if defined(WOLFSSL_DTLS) && \
  50730. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  50731. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  50732. {
  50733. int fd, ret;
  50734. byte ch_msg[] = {
  50735. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  50736. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  50737. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  50738. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  50739. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  50740. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  50741. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  50742. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  50743. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  50744. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  50745. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  50746. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  50747. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  50748. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  50749. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  50750. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  50751. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  50752. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  50753. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  50754. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  50755. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  50756. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  50757. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  50758. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  50759. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  50760. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  50761. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  50762. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  50763. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  50764. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  50765. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  50766. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  50767. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  50768. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  50769. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  50770. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  50771. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  50772. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  50773. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  50774. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  50775. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  50776. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  50777. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  50778. 0x31, 0x68, 0x4c
  50779. };
  50780. fd = wolfSSL_get_fd(ssl);
  50781. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  50782. AssertIntGT(ret, 0);
  50783. /* consume the HRR otherwise handshake will fail */
  50784. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  50785. AssertIntGT(ret, 0);
  50786. }
  50787. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  50788. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  50789. {
  50790. int ret;
  50791. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  50792. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  50793. }
  50794. #endif
  50795. static int test_wolfSSL_dtls_stateless(void)
  50796. {
  50797. callback_functions client_cbs, server_cbs;
  50798. size_t i;
  50799. struct {
  50800. method_provider client_meth;
  50801. method_provider server_meth;
  50802. ssl_callback client_ssl_ready;
  50803. ssl_callback server_ssl_ready;
  50804. } test_params[] = {
  50805. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  50806. test_wolfSSL_dtls_send_ch, NULL},
  50807. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  50808. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  50809. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  50810. #endif
  50811. };
  50812. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  50813. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  50814. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  50815. client_cbs.doUdp = server_cbs.doUdp = 1;
  50816. client_cbs.method = test_params[i].client_meth;
  50817. server_cbs.method = test_params[i].server_meth;
  50818. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  50819. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  50820. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  50821. if (!client_cbs.return_code)
  50822. return TEST_FAIL;
  50823. if (!server_cbs.return_code)
  50824. return TEST_FAIL;
  50825. }
  50826. return TEST_SUCCESS;
  50827. }
  50828. #else
  50829. static int test_wolfSSL_dtls_stateless(void)
  50830. {
  50831. return TEST_SKIPPED;
  50832. }
  50833. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  50834. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  50835. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  50836. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  50837. !defined(WOLFSSL_NO_CLIENT_AUTH))
  50838. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  50839. {
  50840. int ret;
  50841. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  50842. fprintf(stderr, "loading cert %s failed\n", certA);
  50843. fprintf(stderr, "Error: (%d): %s\n", ret,
  50844. wolfSSL_ERR_reason_error_string(ret));
  50845. return -1;
  50846. }
  50847. return 0;
  50848. }
  50849. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  50850. {
  50851. int ret;
  50852. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  50853. != WOLFSSL_SUCCESS) {
  50854. fprintf(stderr, "could not verify the cert: %s\n", certA);
  50855. fprintf(stderr, "Error: (%d): %s\n", ret,
  50856. wolfSSL_ERR_reason_error_string(ret));
  50857. return -1;
  50858. }
  50859. else {
  50860. fprintf(stderr, "successfully verified: %s\n", certA);
  50861. }
  50862. return 0;
  50863. }
  50864. #define LOAD_ONE_CA(a, b, c, d) \
  50865. do { \
  50866. (a) = load_ca_into_cm(c, d); \
  50867. if ((a) != 0) \
  50868. return (b); \
  50869. else \
  50870. (b)--; \
  50871. } while(0)
  50872. #define VERIFY_ONE_CERT(a, b, c, d) \
  50873. do { \
  50874. (a) = verify_cert_with_cm(c, d); \
  50875. if ((a) != 0) \
  50876. return (b); \
  50877. else \
  50878. (b)--; \
  50879. } while(0)
  50880. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  50881. {
  50882. int ret;
  50883. int i = -1;
  50884. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  50885. char chainGArr[9][50] = {"certs/ca-cert.pem",
  50886. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  50887. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  50888. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  50889. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  50890. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  50891. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  50892. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  50893. "certs/test-pathlen/chainG-entity.pem"};
  50894. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  50895. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  50896. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  50897. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  50898. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  50899. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  50900. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  50901. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  50902. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  50903. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  50904. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  50905. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  50906. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  50907. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  50908. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  50909. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  50910. /* test validating the entity twice, should have no effect on pathLen since
  50911. * entity/leaf cert */
  50912. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  50913. return ret;
  50914. }
  50915. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  50916. {
  50917. int ret;
  50918. int i = -1;
  50919. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  50920. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  50921. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  50922. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  50923. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  50924. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  50925. */
  50926. char chainHArr[6][50] = {"certs/ca-cert.pem",
  50927. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  50928. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  50929. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  50930. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  50931. "certs/test-pathlen/chainH-entity.pem"};
  50932. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  50933. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  50934. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  50935. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  50936. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  50937. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  50938. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  50939. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  50940. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  50941. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  50942. return ret;
  50943. }
  50944. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  50945. {
  50946. int ret;
  50947. int i = -1;
  50948. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  50949. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  50950. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  50951. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  50952. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  50953. */
  50954. char chainIArr[5][50] = {"certs/ca-cert.pem",
  50955. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  50956. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  50957. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  50958. "certs/test-pathlen/chainI-entity.pem"};
  50959. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  50960. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  50961. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  50962. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  50963. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  50964. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  50965. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  50966. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  50967. return ret;
  50968. }
  50969. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  50970. {
  50971. int ret;
  50972. int i = -1;
  50973. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  50974. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  50975. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  50976. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  50977. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  50978. */
  50979. char chainJArr[6][50] = {"certs/ca-cert.pem",
  50980. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  50981. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  50982. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  50983. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  50984. "certs/test-pathlen/chainJ-entity.pem"};
  50985. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  50986. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  50987. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  50988. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  50989. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  50990. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  50991. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  50992. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  50993. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  50994. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  50995. return ret;
  50996. }
  50997. static int test_various_pathlen_chains(void)
  50998. {
  50999. int ret;
  51000. WOLFSSL_CERT_MANAGER* cm;
  51001. /* Test chain G (large chain with varying pathLens) */
  51002. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  51003. fprintf(stderr, "cert manager new failed\n");
  51004. return -1;
  51005. }
  51006. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  51007. AssertIntEQ(test_chainG(cm), -1);
  51008. #else
  51009. AssertIntEQ(test_chainG(cm), 0);
  51010. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  51011. ret = wolfSSL_CertManagerUnloadCAs(cm);
  51012. if (ret != WOLFSSL_SUCCESS)
  51013. return -1;
  51014. wolfSSL_CertManagerFree(cm);
  51015. /* end test chain G */
  51016. /* Test chain H (5 chain with same pathLens) */
  51017. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  51018. fprintf(stderr, "cert manager new failed\n");
  51019. return -1;
  51020. }
  51021. AssertIntLT(test_chainH(cm), 0);
  51022. wolfSSL_CertManagerUnloadCAs(cm);
  51023. wolfSSL_CertManagerFree(cm);
  51024. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  51025. fprintf(stderr, "cert manager new failed\n");
  51026. return -1;
  51027. }
  51028. ret = wolfSSL_CertManagerUnloadCAs(cm);
  51029. if (ret != WOLFSSL_SUCCESS)
  51030. return -1;
  51031. wolfSSL_CertManagerFree(cm);
  51032. /* end test chain H */
  51033. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  51034. * followed by 2 ICA's, should pass) */
  51035. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  51036. fprintf(stderr, "cert manager new failed\n");
  51037. return -1;
  51038. }
  51039. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  51040. AssertIntEQ(test_chainI(cm), -1);
  51041. #else
  51042. AssertIntEQ(test_chainI(cm), 0);
  51043. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  51044. wolfSSL_CertManagerUnloadCAs(cm);
  51045. wolfSSL_CertManagerFree(cm);
  51046. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  51047. fprintf(stderr, "cert manager new failed\n");
  51048. return -1;
  51049. }
  51050. ret = wolfSSL_CertManagerUnloadCAs(cm);
  51051. if (ret != WOLFSSL_SUCCESS)
  51052. return -1;
  51053. wolfSSL_CertManagerFree(cm);
  51054. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  51055. * this time followed by 3 ICA's, should fail */
  51056. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  51057. fprintf(stderr, "cert manager new failed\n");
  51058. return -1;
  51059. }
  51060. AssertIntLT(test_chainJ(cm), 0);
  51061. wolfSSL_CertManagerUnloadCAs(cm);
  51062. wolfSSL_CertManagerFree(cm);
  51063. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  51064. fprintf(stderr, "cert manager new failed\n");
  51065. return -1;
  51066. }
  51067. ret = wolfSSL_CertManagerUnloadCAs(cm);
  51068. wolfSSL_CertManagerFree(cm);
  51069. return TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  51070. }
  51071. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  51072. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  51073. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  51074. {
  51075. byte ekm[100] = {0};
  51076. (void)ctx;
  51077. /* Succes Cases */
  51078. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51079. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  51080. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51081. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  51082. /* Use some random context */
  51083. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51084. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  51085. /* Failure cases */
  51086. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51087. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  51088. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51089. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  51090. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51091. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  51092. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51093. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  51094. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51095. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  51096. return TEST_RES_CHECK(1);
  51097. }
  51098. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  51099. {
  51100. wolfSSL_KeepArrays(ssl);
  51101. }
  51102. static int test_export_keying_material(void)
  51103. {
  51104. int res = TEST_SKIPPED;
  51105. #ifndef SINGLE_THREADED
  51106. tcp_ready ready;
  51107. callback_functions clientCb;
  51108. func_args client_args;
  51109. func_args server_args;
  51110. THREAD_TYPE serverThread;
  51111. XMEMSET(&client_args, 0, sizeof(func_args));
  51112. XMEMSET(&server_args, 0, sizeof(func_args));
  51113. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  51114. #ifdef WOLFSSL_TIRTOS
  51115. fdOpenSession(Task_self());
  51116. #endif
  51117. StartTCP();
  51118. InitTcpReady(&ready);
  51119. #if defined(USE_WINDOWS_API)
  51120. /* use RNG to get random port if using windows */
  51121. ready.port = GetRandomPort();
  51122. #endif
  51123. server_args.signal = &ready;
  51124. client_args.signal = &ready;
  51125. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  51126. client_args.callbacks = &clientCb;
  51127. start_thread(test_server_nofail, &server_args, &serverThread);
  51128. wait_tcp_ready(&server_args);
  51129. test_client_nofail(&client_args, test_export_keying_material_cb);
  51130. join_thread(serverThread);
  51131. AssertTrue(client_args.return_code);
  51132. AssertTrue(server_args.return_code);
  51133. FreeTcpReady(&ready);
  51134. #ifdef WOLFSSL_TIRTOS
  51135. fdOpenSession(Task_self());
  51136. #endif
  51137. res = TEST_RES_CHECK(1);
  51138. #endif /* !SINGLE_THREADED */
  51139. return res;
  51140. }
  51141. #endif /* HAVE_KEYING_MATERIAL */
  51142. static int test_wolfSSL_THREADID_hash(void)
  51143. {
  51144. int result = TEST_SKIPPED;
  51145. #if defined(OPENSSL_EXTRA)
  51146. unsigned long res;
  51147. CRYPTO_THREADID id;
  51148. CRYPTO_THREADID_current(NULL);
  51149. AssertTrue(1);
  51150. res = CRYPTO_THREADID_hash(NULL);
  51151. AssertTrue( res == 0UL);
  51152. XMEMSET(&id, 0, sizeof(id));
  51153. res = CRYPTO_THREADID_hash(&id);
  51154. AssertTrue( res == 0UL);
  51155. result = TEST_RES_CHECK(1);
  51156. #endif /* OPENSSL_EXTRA */
  51157. return result;
  51158. }
  51159. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  51160. {
  51161. int res = TEST_SKIPPED;
  51162. #if defined(OPENSSL_EXTRA)
  51163. WOLFSSL_CTX* ctx = NULL;
  51164. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  51165. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  51166. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  51167. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  51168. res = TEST_RES_CHECK(1);
  51169. #endif /* OPENSSL_EXTRA */
  51170. return res;
  51171. }
  51172. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  51173. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  51174. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  51175. {
  51176. callback_functions* callbacks = NULL;
  51177. WOLFSSL_CTX* ctx = NULL;
  51178. WOLFSSL* ssl = NULL;
  51179. SOCKET_T sfd = 0;
  51180. SOCKET_T cfd = 0;
  51181. word16 port;
  51182. char msg[] = "I hear you fa shizzle!";
  51183. int len = (int) XSTRLEN(msg);
  51184. char input[1024];
  51185. int ret, err;
  51186. if (!args)
  51187. return 0;
  51188. ((func_args*)args)->return_code = TEST_FAIL;
  51189. callbacks = ((func_args*)args)->callbacks;
  51190. ctx = wolfSSL_CTX_new(callbacks->method());
  51191. #if defined(USE_WINDOWS_API)
  51192. port = ((func_args*)args)->signal->port;
  51193. #else
  51194. /* Let tcp_listen assign port */
  51195. port = 0;
  51196. #endif
  51197. #ifdef WOLFSSL_TIRTOS
  51198. fdOpenSession(Task_self());
  51199. #endif
  51200. AssertIntEQ(WOLFSSL_SUCCESS,
  51201. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  51202. AssertIntEQ(WOLFSSL_SUCCESS,
  51203. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  51204. WOLFSSL_FILETYPE_PEM));
  51205. AssertIntEQ(WOLFSSL_SUCCESS,
  51206. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  51207. WOLFSSL_FILETYPE_PEM));
  51208. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  51209. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  51210. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  51211. #elif !defined(NO_DH)
  51212. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  51213. #endif
  51214. if (callbacks->ctx_ready)
  51215. callbacks->ctx_ready(ctx);
  51216. ssl = wolfSSL_new(ctx);
  51217. AssertNotNull(ssl);
  51218. /* listen and accept */
  51219. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  51220. CloseSocket(sfd);
  51221. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  51222. if (callbacks->ssl_ready)
  51223. callbacks->ssl_ready(ssl);
  51224. do {
  51225. err = 0; /* Reset error */
  51226. ret = wolfSSL_accept(ssl);
  51227. if (ret != WOLFSSL_SUCCESS) {
  51228. err = wolfSSL_get_error(ssl, 0);
  51229. }
  51230. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  51231. if (ret != WOLFSSL_SUCCESS) {
  51232. wolfSSL_free(ssl);
  51233. wolfSSL_CTX_free(ctx);
  51234. CloseSocket(cfd);
  51235. ((func_args*)args)->return_code = TEST_FAIL;
  51236. return 0;
  51237. }
  51238. /* read and write data */
  51239. XMEMSET( input, 0, sizeof(input));
  51240. while (1) {
  51241. ret = wolfSSL_read(ssl, input, sizeof(input));
  51242. if (ret > 0) {
  51243. break;
  51244. }
  51245. else {
  51246. err = wolfSSL_get_error(ssl,ret);
  51247. if (err == WOLFSSL_ERROR_WANT_READ) {
  51248. continue;
  51249. }
  51250. break;
  51251. }
  51252. }
  51253. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  51254. do {
  51255. ret = wolfSSL_write(ssl, msg, len);
  51256. if (ret > 0) {
  51257. break;
  51258. }
  51259. } while (ret < 0);
  51260. }
  51261. /* bidirectional shutdown */
  51262. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  51263. continue;
  51264. }
  51265. /* wait for the peer to disconnect the tcp connection */
  51266. do {
  51267. ret = wolfSSL_read(ssl, input, sizeof(input));
  51268. err = wolfSSL_get_error(ssl, ret);
  51269. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  51270. /* detect TCP disconnect */
  51271. AssertIntLE(ret,WOLFSSL_FAILURE);
  51272. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  51273. ((func_args*)args)->return_code = TEST_SUCCESS;
  51274. wolfSSL_free(ssl);
  51275. wolfSSL_CTX_free(ctx);
  51276. CloseSocket(cfd);
  51277. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  51278. wc_ecc_fp_free(); /* free per thread cache */
  51279. #endif
  51280. return 0;
  51281. }
  51282. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  51283. {
  51284. callback_functions* callbacks = NULL;
  51285. WOLFSSL_CTX* ctx = NULL;
  51286. WOLFSSL* ssl = NULL;
  51287. SOCKET_T sfd = 0;
  51288. char msg[] = "hello wolfssl server!";
  51289. int len = (int) XSTRLEN(msg);
  51290. char input[1024];
  51291. int idx;
  51292. int ret, err;
  51293. if (!args)
  51294. return 0;
  51295. ((func_args*)args)->return_code = TEST_FAIL;
  51296. callbacks = ((func_args*)args)->callbacks;
  51297. ctx = wolfSSL_CTX_new(callbacks->method());
  51298. #ifdef WOLFSSL_TIRTOS
  51299. fdOpenSession(Task_self());
  51300. #endif
  51301. AssertIntEQ(WOLFSSL_SUCCESS,
  51302. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  51303. AssertIntEQ(WOLFSSL_SUCCESS,
  51304. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  51305. WOLFSSL_FILETYPE_PEM));
  51306. AssertIntEQ(WOLFSSL_SUCCESS,
  51307. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  51308. WOLFSSL_FILETYPE_PEM));
  51309. AssertNotNull((ssl = wolfSSL_new(ctx)));
  51310. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  51311. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  51312. do {
  51313. err = 0; /* Reset error */
  51314. ret = wolfSSL_connect(ssl);
  51315. if (ret != WOLFSSL_SUCCESS) {
  51316. err = wolfSSL_get_error(ssl, 0);
  51317. }
  51318. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  51319. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  51320. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  51321. input[idx] = 0;
  51322. }
  51323. ret = wolfSSL_shutdown(ssl);
  51324. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  51325. ret = wolfSSL_shutdown(ssl);
  51326. }
  51327. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  51328. ((func_args*)args)->return_code = TEST_SUCCESS;
  51329. wolfSSL_free(ssl);
  51330. wolfSSL_CTX_free(ctx);
  51331. CloseSocket(sfd);
  51332. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  51333. wc_ecc_fp_free(); /* free per thread cache */
  51334. #endif
  51335. return 0;
  51336. }
  51337. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  51338. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  51339. /* This test is to check wolfSSL_read behaves as same as
  51340. * openSSL when it is called after SSL_shutdown completes.
  51341. */
  51342. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  51343. {
  51344. int res = TEST_SKIPPED;
  51345. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  51346. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  51347. tcp_ready ready;
  51348. func_args client_args;
  51349. func_args server_args;
  51350. THREAD_TYPE serverThread;
  51351. THREAD_TYPE clientThread;
  51352. callback_functions server_cbf;
  51353. callback_functions client_cbf;
  51354. #ifdef WOLFSSL_TIRTOS
  51355. fdOpenSession(Task_self());
  51356. #endif
  51357. StartTCP();
  51358. InitTcpReady(&ready);
  51359. #if defined(USE_WINDOWS_API)
  51360. /* use RNG to get random port if using windows */
  51361. ready.port = GetRandomPort();
  51362. #endif
  51363. XMEMSET(&client_args, 0, sizeof(func_args));
  51364. XMEMSET(&server_args, 0, sizeof(func_args));
  51365. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  51366. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  51367. server_cbf.method = wolfTLSv1_2_server_method;
  51368. client_cbf.method = wolfTLSv1_2_client_method;
  51369. server_args.callbacks = &server_cbf;
  51370. client_args.callbacks = &client_cbf;
  51371. server_args.signal = &ready;
  51372. client_args.signal = &ready;
  51373. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  51374. wait_tcp_ready(&server_args);
  51375. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  51376. join_thread(clientThread);
  51377. join_thread(serverThread);
  51378. AssertTrue(client_args.return_code);
  51379. AssertTrue(server_args.return_code);
  51380. FreeTcpReady(&ready);
  51381. res = TEST_RES_CHECK(1);
  51382. #endif
  51383. return res;
  51384. }
  51385. static int test_wolfSSL_CTX_get_min_proto_version(void)
  51386. {
  51387. int res = TEST_SKIPPED;
  51388. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  51389. WOLFSSL_CTX *ctx;
  51390. (void)ctx;
  51391. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  51392. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  51393. #ifdef WOLFSSL_ALLOW_SSLV3
  51394. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  51395. #else
  51396. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  51397. #endif
  51398. wolfSSL_CTX_free(ctx);
  51399. #ifdef WOLFSSL_ALLOW_TLSV10
  51400. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  51401. #else
  51402. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  51403. #endif
  51404. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  51405. #ifdef WOLFSSL_ALLOW_TLSV10
  51406. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  51407. #else
  51408. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  51409. #endif
  51410. wolfSSL_CTX_free(ctx);
  51411. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  51412. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  51413. #ifndef NO_OLD_TLS
  51414. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  51415. #else
  51416. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  51417. #endif
  51418. wolfSSL_CTX_free(ctx);
  51419. #ifndef WOLFSSL_NO_TLS12
  51420. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  51421. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  51422. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  51423. wolfSSL_CTX_free(ctx);
  51424. #endif
  51425. #ifdef WOLFSSL_TLS13
  51426. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  51427. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  51428. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  51429. wolfSSL_CTX_free(ctx);
  51430. #endif
  51431. res = TEST_RES_CHECK(1);
  51432. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  51433. return res;
  51434. }
  51435. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  51436. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  51437. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  51438. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  51439. static int test_wolfSSL_set_SSL_CTX(void)
  51440. {
  51441. int res = TEST_SKIPPED;
  51442. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  51443. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  51444. !defined(NO_RSA)
  51445. WOLFSSL_CTX *ctx1, *ctx2;
  51446. WOLFSSL *ssl;
  51447. const byte *session_id1 = (const byte *)"CTX1";
  51448. const byte *session_id2 = (const byte *)"CTX2";
  51449. AssertNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  51450. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  51451. WOLFSSL_FILETYPE_PEM));
  51452. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  51453. WOLFSSL_FILETYPE_PEM));
  51454. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  51455. WOLFSSL_SUCCESS);
  51456. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  51457. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  51458. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  51459. WOLFSSL_SUCCESS);
  51460. AssertNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  51461. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  51462. WOLFSSL_FILETYPE_PEM));
  51463. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  51464. WOLFSSL_FILETYPE_PEM));
  51465. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  51466. WOLFSSL_SUCCESS);
  51467. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  51468. WOLFSSL_SUCCESS);
  51469. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  51470. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  51471. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  51472. WOLFSSL_SUCCESS);
  51473. #ifdef HAVE_SESSION_TICKET
  51474. AssertIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  51475. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  51476. AssertIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  51477. #endif
  51478. AssertNotNull(ssl = wolfSSL_new(ctx2));
  51479. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  51480. #ifdef WOLFSSL_INT_H
  51481. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  51482. AssertTrue(ssl->buffers.certificate == ctx2->certificate);
  51483. AssertTrue(ssl->buffers.certChain == ctx2->certChain);
  51484. #endif
  51485. #ifdef HAVE_SESSION_TICKET
  51486. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  51487. #endif
  51488. /* Set the ctx1 that has TLSv1.3 as max proto version */
  51489. AssertNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  51490. /* MUST not change proto versions of ssl */
  51491. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  51492. #ifdef HAVE_SESSION_TICKET
  51493. /* MUST not change */
  51494. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  51495. #endif
  51496. /* MUST change */
  51497. #ifdef WOLFSSL_INT_H
  51498. AssertTrue(ssl->buffers.certificate == ctx1->certificate);
  51499. AssertTrue(ssl->buffers.certChain == ctx1->certChain);
  51500. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  51501. #endif
  51502. wolfSSL_free(ssl);
  51503. wolfSSL_CTX_free(ctx1);
  51504. wolfSSL_CTX_free(ctx2);
  51505. res = TEST_RES_CHECK(1);
  51506. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  51507. return res;
  51508. }
  51509. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  51510. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  51511. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  51512. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  51513. static int test_wolfSSL_security_level(void)
  51514. {
  51515. int res = TEST_SKIPPED;
  51516. #if defined(OPENSSL_EXTRA)
  51517. SSL_CTX *ctx;
  51518. #ifdef WOLFSSL_TLS13
  51519. #ifdef NO_WOLFSSL_SERVER
  51520. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  51521. #else
  51522. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  51523. #endif
  51524. SSL_CTX_set_security_level(ctx, 1);
  51525. AssertTrue(1);
  51526. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  51527. SSL_CTX_free(ctx);
  51528. #else
  51529. (void)ctx;
  51530. #endif
  51531. res = TEST_RES_CHECK(1);
  51532. #endif
  51533. return res;
  51534. }
  51535. static int test_wolfSSL_SSL_in_init(void)
  51536. {
  51537. int res = TEST_SKIPPED;
  51538. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  51539. SSL_CTX* ctx;
  51540. SSL* ssl;
  51541. const char* testCertFile;
  51542. const char* testKeyFile;
  51543. #ifdef WOLFSSL_TLS13
  51544. #ifdef NO_WOLFSSL_SERVER
  51545. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  51546. #else
  51547. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  51548. #endif
  51549. #ifndef NO_RSA
  51550. testCertFile = svrCertFile;
  51551. testKeyFile = svrKeyFile;
  51552. #elif defined(HAVE_ECC)
  51553. testCertFile = eccCertFile;
  51554. testKeyFile = eccKeyFile;
  51555. #else
  51556. testCertFile = NULL;
  51557. testKeyFile = NULL;
  51558. #endif
  51559. if (testCertFile != NULL && testKeyFile != NULL) {
  51560. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  51561. SSL_FILETYPE_PEM));
  51562. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  51563. SSL_FILETYPE_PEM));
  51564. }
  51565. ssl = SSL_new(ctx);
  51566. AssertNotNull(ssl);
  51567. AssertIntEQ(SSL_in_init(ssl), 1);
  51568. SSL_CTX_free(ctx);
  51569. SSL_free(ssl);
  51570. #else
  51571. (void)ctx;
  51572. (void)ssl;
  51573. (void)testCertFile;
  51574. (void)testKeyFile;
  51575. #endif
  51576. res = TEST_RES_CHECK(1);
  51577. #endif
  51578. return res;
  51579. }
  51580. static int test_wolfSSL_CTX_set_timeout(void)
  51581. {
  51582. int res = TEST_SKIPPED;
  51583. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  51584. int timeout;
  51585. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  51586. (void)timeout;
  51587. AssertNotNull(ctx);
  51588. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  51589. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  51590. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  51591. */
  51592. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  51593. /* giving 0 as timeout value sets default timeout */
  51594. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  51595. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  51596. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  51597. #else
  51598. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  51599. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  51600. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  51601. #endif
  51602. wolfSSL_CTX_free(ctx);
  51603. res = TEST_RES_CHECK(1);
  51604. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  51605. return res;
  51606. }
  51607. static int test_wolfSSL_OpenSSL_version(void)
  51608. {
  51609. int res = TEST_SKIPPED;
  51610. #if defined(OPENSSL_EXTRA)
  51611. const char* ver;
  51612. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  51613. AssertNotNull(ver = OpenSSL_version(0));
  51614. #else
  51615. AssertNotNull(ver = OpenSSL_version());
  51616. #endif
  51617. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  51618. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  51619. res = TEST_RES_CHECK(1);
  51620. #endif
  51621. return res;
  51622. }
  51623. static int test_CONF_CTX_CMDLINE(void)
  51624. {
  51625. int res = TEST_SKIPPED;
  51626. #if defined(OPENSSL_ALL)
  51627. SSL_CTX* ctx = NULL;
  51628. SSL_CONF_CTX* cctx = NULL;
  51629. AssertNotNull(cctx = SSL_CONF_CTX_new());
  51630. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  51631. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  51632. AssertTrue(1);
  51633. /* set flags */
  51634. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  51635. WOLFSSL_CONF_FLAG_CMDLINE);
  51636. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  51637. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  51638. /* cmd invalid command */
  51639. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  51640. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  51641. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  51642. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  51643. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  51644. /* cmd Certificate and Private Key*/
  51645. {
  51646. #if !defined(NO_CERTS) && !defined(NO_RSA)
  51647. const char* ourCert = svrCertFile;
  51648. const char* ourKey = svrKeyFile;
  51649. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  51650. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  51651. WOLFSSL_SUCCESS);
  51652. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  51653. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  51654. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  51655. #endif
  51656. }
  51657. /* cmd curves */
  51658. {
  51659. #if defined(HAVE_ECC)
  51660. const char* curve = "secp256r1";
  51661. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  51662. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  51663. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  51664. #endif
  51665. }
  51666. /* cmd CipherString */
  51667. {
  51668. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  51669. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  51670. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  51671. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  51672. }
  51673. /* cmd DH parameter */
  51674. {
  51675. #if !defined(NO_DH) && !defined(NO_BIO)
  51676. const char* ourdhcert = "./certs/dh2048.pem";
  51677. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  51678. -3);
  51679. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  51680. WOLFSSL_SUCCESS);
  51681. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  51682. #endif
  51683. }
  51684. SSL_CTX_free(ctx);
  51685. SSL_CONF_CTX_free(cctx);
  51686. res = TEST_RES_CHECK(1);
  51687. #endif /* OPENSSL_EXTRA */
  51688. return res;
  51689. }
  51690. static int test_CONF_CTX_FILE(void)
  51691. {
  51692. int res = TEST_SKIPPED;
  51693. #if defined(OPENSSL_ALL)
  51694. SSL_CTX* ctx = NULL;
  51695. SSL_CONF_CTX* cctx = NULL;
  51696. AssertNotNull(cctx = SSL_CONF_CTX_new());
  51697. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  51698. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  51699. AssertTrue(1);
  51700. /* set flags */
  51701. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  51702. WOLFSSL_CONF_FLAG_FILE);
  51703. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  51704. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  51705. /* sanity check */
  51706. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  51707. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  51708. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  51709. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  51710. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  51711. /* cmd Certificate and Private Key*/
  51712. {
  51713. #if !defined(NO_CERTS) && !defined(NO_RSA)
  51714. const char* ourCert = svrCertFile;
  51715. const char* ourKey = svrKeyFile;
  51716. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  51717. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  51718. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  51719. WOLFSSL_SUCCESS);
  51720. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  51721. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  51722. #endif
  51723. }
  51724. /* cmd curves */
  51725. {
  51726. #if defined(HAVE_ECC)
  51727. const char* curve = "secp256r1";
  51728. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  51729. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  51730. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  51731. #endif
  51732. }
  51733. /* cmd CipherString */
  51734. {
  51735. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  51736. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  51737. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  51738. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  51739. }
  51740. /* cmd DH parameter */
  51741. {
  51742. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  51743. const char* ourdhcert = "./certs/dh3072.pem";
  51744. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  51745. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  51746. WOLFSSL_SUCCESS);
  51747. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  51748. #endif
  51749. }
  51750. SSL_CTX_free(ctx);
  51751. SSL_CONF_CTX_free(cctx);
  51752. res = TEST_RES_CHECK(1);
  51753. #endif /* OPENSSL_EXTRA */
  51754. return res;
  51755. }
  51756. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  51757. {
  51758. int res = TEST_SKIPPED;
  51759. #ifdef HAVE_EX_DATA
  51760. int idx1, idx2;
  51761. /* test for unsupported class index */
  51762. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  51763. 0,NULL, NULL, NULL, NULL ), -1);
  51764. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  51765. 0,NULL, NULL, NULL, NULL ), -1);
  51766. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  51767. 0,NULL, NULL, NULL, NULL ), -1);
  51768. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  51769. 0,NULL, NULL, NULL, NULL ), -1);
  51770. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  51771. 0,NULL, NULL, NULL, NULL ), -1);
  51772. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  51773. 0,NULL, NULL, NULL, NULL ), -1);
  51774. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  51775. 0,NULL, NULL, NULL, NULL ), -1);
  51776. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  51777. 0,NULL, NULL, NULL, NULL ), -1);
  51778. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  51779. 0,NULL, NULL, NULL, NULL ), -1);
  51780. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  51781. 0,NULL, NULL, NULL, NULL ), -1);
  51782. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  51783. 0,NULL, NULL, NULL, NULL ), -1);
  51784. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  51785. 0,NULL, NULL, NULL, NULL ), -1);
  51786. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  51787. /* test for supported class index */
  51788. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  51789. 0,NULL, NULL, NULL, NULL );
  51790. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  51791. 0,NULL, NULL, NULL, NULL );
  51792. AssertIntNE(idx1, -1);
  51793. AssertIntNE(idx2, -1);
  51794. AssertIntNE(idx1, idx2);
  51795. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  51796. 0,NULL, NULL, NULL, NULL );
  51797. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  51798. 0,NULL, NULL, NULL, NULL );
  51799. AssertIntNE(idx1, -1);
  51800. AssertIntNE(idx2, -1);
  51801. AssertIntNE(idx1, idx2);
  51802. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  51803. 0,NULL, NULL, NULL, NULL );
  51804. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  51805. 0,NULL, NULL, NULL, NULL );
  51806. AssertIntNE(idx1, -1);
  51807. AssertIntNE(idx2, -1);
  51808. AssertIntNE(idx1, idx2);
  51809. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  51810. 0,NULL, NULL, NULL, NULL );
  51811. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  51812. 0,NULL, NULL, NULL, NULL );
  51813. AssertIntNE(idx1, -1);
  51814. AssertIntNE(idx2, -1);
  51815. AssertIntNE(idx1, idx2);
  51816. res = TEST_RES_CHECK(1);
  51817. #endif /* HAVE_EX_DATA */
  51818. return res;
  51819. }
  51820. #if defined(HAVE_EX_DATA) && \
  51821. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  51822. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  51823. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  51824. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  51825. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  51826. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  51827. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  51828. #define SESSION_NEW_IDX_PTR "Testing"
  51829. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  51830. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  51831. {
  51832. AssertNotNull(p);
  51833. AssertNull(ptr);
  51834. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  51835. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  51836. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  51837. }
  51838. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  51839. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  51840. void* arg)
  51841. {
  51842. AssertNotNull(out);
  51843. AssertNotNull(in);
  51844. AssertPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  51845. AssertPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  51846. AssertPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  51847. AssertIntEQ(argV, SESSION_NEW_IDX_LONG);
  51848. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  51849. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  51850. return SSL_SUCCESS;
  51851. }
  51852. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  51853. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  51854. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  51855. {
  51856. AssertNotNull(p);
  51857. AssertNull(ptr);
  51858. AssertPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  51859. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  51860. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  51861. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  51862. }
  51863. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  51864. {
  51865. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  51866. (void*)SESSION_NEW_IDX_PTR,
  51867. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  51868. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  51869. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  51870. SSL_SESSION* s = SSL_SESSION_new();
  51871. SSL_SESSION* d = NULL;
  51872. AssertNotNull(s);
  51873. AssertPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  51874. AssertNotNull(d = SSL_SESSION_dup(s));
  51875. AssertPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  51876. SSL_SESSION_free(s);
  51877. AssertIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  51878. SSL_SESSION_free(d);
  51879. AssertIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  51880. return TEST_RES_CHECK(1);
  51881. }
  51882. #else
  51883. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  51884. {
  51885. return TEST_SKIPPED;
  51886. }
  51887. #endif
  51888. static int test_wolfSSL_set_psk_use_session_callback(void)
  51889. {
  51890. int res = TEST_SKIPPED;
  51891. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  51892. SSL_CTX* ctx;
  51893. SSL* ssl;
  51894. const char* testCertFile;
  51895. const char* testKeyFile;
  51896. #ifdef WOLFSSL_TLS13
  51897. #ifdef NO_WOLFSSL_SERVER
  51898. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  51899. #else
  51900. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  51901. #endif
  51902. #ifndef NO_RSA
  51903. testCertFile = svrCertFile;
  51904. testKeyFile = svrKeyFile;
  51905. #elif defined(HAVE_ECC)
  51906. testCertFile = eccCertFile;
  51907. testKeyFile = eccKeyFile;
  51908. #else
  51909. testCertFile = NULL;
  51910. testKeyFile = NULL;
  51911. #endif
  51912. if (testCertFile != NULL && testKeyFile != NULL) {
  51913. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  51914. SSL_FILETYPE_PEM));
  51915. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  51916. SSL_FILETYPE_PEM));
  51917. }
  51918. ssl = SSL_new(ctx);
  51919. AssertNotNull(ssl);
  51920. SSL_set_psk_use_session_callback(ssl,
  51921. my_psk_use_session_cb);
  51922. AssertTrue(1);
  51923. SSL_CTX_free(ctx);
  51924. SSL_free(ssl);
  51925. #else
  51926. (void)ctx;
  51927. (void)ssl;
  51928. (void)testCertFile;
  51929. (void)testKeyFile;
  51930. #endif
  51931. res = TEST_RES_CHECK(1);
  51932. #endif
  51933. return res;
  51934. }
  51935. static int test_wolfSSL_ERR_strings(void)
  51936. {
  51937. int res = TEST_SKIPPED;
  51938. #if !defined(NO_ERROR_STRINGS)
  51939. const char* err1 = "unsupported cipher suite";
  51940. const char* err2 = "wolfSSL PEM routines";
  51941. const char* err = NULL;
  51942. (void)err;
  51943. (void)err1;
  51944. (void)err2;
  51945. #if defined(OPENSSL_EXTRA)
  51946. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  51947. AssertTrue(err != NULL);
  51948. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  51949. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  51950. AssertTrue(err != NULL);
  51951. AssertIntEQ((*err == '\0'), 1);
  51952. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  51953. AssertTrue(err != NULL);
  51954. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  51955. #else
  51956. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  51957. AssertTrue(err != NULL);
  51958. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  51959. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  51960. AssertTrue(err != NULL);
  51961. AssertIntEQ((*err == '\0'), 1);
  51962. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  51963. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  51964. AssertTrue(err != NULL);
  51965. AssertIntEQ((*err == '\0'), 1);
  51966. #endif
  51967. res = TEST_RES_CHECK(1);
  51968. #endif
  51969. return res;
  51970. }
  51971. static int test_wolfSSL_EVP_shake128(void)
  51972. {
  51973. int res = TEST_SKIPPED;
  51974. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  51975. defined(WOLFSSL_SHAKE128)
  51976. const EVP_MD* md = NULL;
  51977. md = EVP_shake128();
  51978. AssertTrue(md != NULL);
  51979. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  51980. res = TEST_RES_CHECK(1);
  51981. #endif
  51982. return res;
  51983. }
  51984. static int test_wolfSSL_EVP_shake256(void)
  51985. {
  51986. int res = TEST_SKIPPED;
  51987. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  51988. defined(WOLFSSL_SHAKE256)
  51989. const EVP_MD* md = NULL;
  51990. md = EVP_shake256();
  51991. AssertTrue(md != NULL);
  51992. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  51993. res = TEST_RES_CHECK(1);
  51994. #endif
  51995. return res;
  51996. }
  51997. static int test_EVP_blake2(void)
  51998. {
  51999. int res = TEST_SKIPPED;
  52000. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  52001. const EVP_MD* md = NULL;
  52002. (void)md;
  52003. #if defined(HAVE_BLAKE2)
  52004. md = EVP_blake2b512();
  52005. AssertTrue(md != NULL);
  52006. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  52007. #endif
  52008. #if defined(HAVE_BLAKE2S)
  52009. md = EVP_blake2s256();
  52010. AssertTrue(md != NULL);
  52011. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  52012. #endif
  52013. res = TEST_RES_CHECK(1);
  52014. #endif
  52015. return res;
  52016. }
  52017. #if defined(OPENSSL_EXTRA)
  52018. static void list_md_fn(const EVP_MD* m, const char* from,
  52019. const char* to, void* arg)
  52020. {
  52021. const char* mn;
  52022. BIO *bio;
  52023. (void) from;
  52024. (void) to;
  52025. (void) arg;
  52026. (void) mn;
  52027. (void) bio;
  52028. if (!m) {
  52029. /* alias */
  52030. AssertNull(m);
  52031. AssertNotNull(to);
  52032. }
  52033. else {
  52034. AssertNotNull(m);
  52035. AssertNull(to);
  52036. }
  52037. AssertNotNull(from);
  52038. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  52039. mn = EVP_get_digestbyname(from);
  52040. /* print to stderr */
  52041. AssertNotNull(arg);
  52042. bio = BIO_new(BIO_s_file());
  52043. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  52044. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  52045. BIO_free(bio);
  52046. #endif
  52047. }
  52048. #endif
  52049. static int test_EVP_MD_do_all(void)
  52050. {
  52051. int res = TEST_SKIPPED;
  52052. #if defined(OPENSSL_EXTRA)
  52053. EVP_MD_do_all(NULL, stderr);
  52054. /* to confirm previous call gives no harm */
  52055. AssertTrue(1);
  52056. EVP_MD_do_all(list_md_fn, stderr);
  52057. /* to confirm previous call gives no harm */
  52058. AssertTrue(1);
  52059. res = TEST_RES_CHECK(1);
  52060. #endif
  52061. return res;
  52062. }
  52063. #if defined(OPENSSL_EXTRA)
  52064. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  52065. {
  52066. (void)arg;
  52067. (void)nm;
  52068. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  52069. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  52070. /* print to stderr */
  52071. AssertNotNull(arg);
  52072. bio = BIO_new(BIO_s_file());
  52073. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  52074. BIO_printf(bio, "%s\n", mn);
  52075. BIO_free(bio);
  52076. #endif
  52077. }
  52078. #endif
  52079. static int test_OBJ_NAME_do_all(void)
  52080. {
  52081. int res = TEST_SKIPPED;
  52082. #if defined(OPENSSL_EXTRA)
  52083. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  52084. /* to confirm previous call gives no harm */
  52085. AssertTrue(1);
  52086. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  52087. /* to confirm previous call gives no harm */
  52088. AssertTrue(1);
  52089. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  52090. AssertTrue(1);
  52091. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  52092. AssertTrue(1);
  52093. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  52094. AssertTrue(1);
  52095. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  52096. AssertTrue(1);
  52097. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  52098. AssertTrue(1);
  52099. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  52100. AssertTrue(1);
  52101. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  52102. AssertTrue(1);
  52103. res = TEST_RES_CHECK(1);
  52104. #endif
  52105. return res;
  52106. }
  52107. static int test_SSL_CIPHER_get_xxx(void)
  52108. {
  52109. int res = TEST_SKIPPED;
  52110. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  52111. !defined(NO_FILESYSTEM)
  52112. const SSL_CIPHER* cipher = NULL;
  52113. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  52114. int i, numCiphers = 0;
  52115. SSL_CTX* ctx = NULL;
  52116. SSL* ssl = NULL;
  52117. const char* testCertFile;
  52118. const char* testKeyFile;
  52119. char buf[256] = {0};
  52120. const char* cipher_id = NULL;
  52121. int expect_nid1 = NID_undef;
  52122. int expect_nid2 = NID_undef;
  52123. int expect_nid3 = NID_undef;
  52124. int expect_nid4 = NID_undef;
  52125. int expect_nid5 = 0;
  52126. const char* cipher_id2 = NULL;
  52127. int expect_nid21 = NID_undef;
  52128. int expect_nid22 = NID_undef;
  52129. int expect_nid23 = NID_undef;
  52130. int expect_nid24 = NID_undef;
  52131. int expect_nid25 = 0;
  52132. (void)cipher;
  52133. (void)supportedCiphers;
  52134. (void)i;
  52135. (void)numCiphers;
  52136. (void)ctx;
  52137. (void)ssl;
  52138. (void)testCertFile;
  52139. (void)testKeyFile;
  52140. #if defined(WOLFSSL_TLS13)
  52141. cipher_id = "TLS13-AES128-GCM-SHA256";
  52142. expect_nid1 = NID_auth_rsa;
  52143. expect_nid2 = NID_aes_128_gcm;
  52144. expect_nid3 = NID_sha256;
  52145. expect_nid4 = NID_kx_any;
  52146. expect_nid5 = 1;
  52147. #if !defined(WOLFSSL_NO_TLS12)
  52148. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  52149. expect_nid21 = NID_auth_rsa;
  52150. expect_nid22 = NID_aes_256_gcm;
  52151. expect_nid23 = NID_sha384;
  52152. expect_nid24 = NID_kx_ecdhe;
  52153. expect_nid25 = 1;
  52154. #endif
  52155. #endif
  52156. #ifdef NO_WOLFSSL_SERVER
  52157. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52158. #else
  52159. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52160. #endif
  52161. if (cipher_id) {
  52162. #ifndef NO_RSA
  52163. testCertFile = svrCertFile;
  52164. testKeyFile = svrKeyFile;
  52165. #elif defined(HAVE_ECC)
  52166. testCertFile = eccCertFile;
  52167. testKeyFile = eccKeyFile;
  52168. #else
  52169. testCertFile = NULL;
  52170. testKeyFile = NULL;
  52171. #endif
  52172. if (testCertFile != NULL && testKeyFile != NULL) {
  52173. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  52174. SSL_FILETYPE_PEM));
  52175. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52176. SSL_FILETYPE_PEM));
  52177. }
  52178. ssl = SSL_new(ctx);
  52179. AssertNotNull(ssl);
  52180. AssertIntEQ(SSL_in_init(ssl), 1);
  52181. supportedCiphers = SSL_get_ciphers(ssl);
  52182. numCiphers = sk_num(supportedCiphers);
  52183. for (i = 0; i < numCiphers; ++i) {
  52184. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  52185. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  52186. }
  52187. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  52188. break;
  52189. }
  52190. }
  52191. /* test case for */
  52192. if (i != numCiphers) {
  52193. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  52194. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  52195. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  52196. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  52197. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  52198. }
  52199. if (cipher_id2) {
  52200. for (i = 0; i < numCiphers; ++i) {
  52201. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  52202. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  52203. }
  52204. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  52205. break;
  52206. }
  52207. }
  52208. /* test case for */
  52209. if (i != numCiphers) {
  52210. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  52211. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  52212. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  52213. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  52214. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  52215. }
  52216. }
  52217. }
  52218. if (ctx)
  52219. SSL_CTX_free(ctx);
  52220. if (ssl)
  52221. SSL_free(ssl);
  52222. res = TEST_RES_CHECK(1);
  52223. #endif
  52224. return res;
  52225. }
  52226. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  52227. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  52228. int* keyFormat)
  52229. {
  52230. int ret;
  52231. byte* key_buf = NULL;
  52232. size_t key_sz = 0;
  52233. EncryptedInfo encInfo;
  52234. XMEMSET(&encInfo, 0, sizeof(encInfo));
  52235. ret = load_file(privKeyFile, &key_buf, &key_sz);
  52236. if (ret == 0) {
  52237. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  52238. NULL, &encInfo, keyFormat);
  52239. }
  52240. if (key_buf != NULL) {
  52241. free(key_buf); key_buf = NULL;
  52242. }
  52243. (void)encInfo; /* not used in this test */
  52244. #ifdef DEBUG_WOLFSSL
  52245. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  52246. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  52247. (*pDer)->length);
  52248. #endif
  52249. return ret;
  52250. }
  52251. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  52252. {
  52253. int ret = CRYPTOCB_UNAVAILABLE;
  52254. const char* privKeyFile = (const char*)ctx;
  52255. DerBuffer* pDer = NULL;
  52256. int keyFormat = 0;
  52257. if (info->algo_type == WC_ALGO_TYPE_PK) {
  52258. #ifdef DEBUG_WOLFSSL
  52259. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  52260. #endif
  52261. #ifndef NO_RSA
  52262. if (info->pk.type == WC_PK_TYPE_RSA) {
  52263. switch (info->pk.rsa.type) {
  52264. case RSA_PUBLIC_ENCRYPT:
  52265. case RSA_PUBLIC_DECRYPT:
  52266. /* perform software based RSA public op */
  52267. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  52268. break;
  52269. case RSA_PRIVATE_ENCRYPT:
  52270. case RSA_PRIVATE_DECRYPT:
  52271. {
  52272. RsaKey key;
  52273. /* perform software based RSA private op */
  52274. #ifdef DEBUG_WOLFSSL
  52275. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  52276. #endif
  52277. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  52278. &keyFormat);
  52279. if (ret != 0) {
  52280. return ret;
  52281. }
  52282. ret = wc_InitRsaKey(&key, HEAP_HINT);
  52283. if (ret == 0) {
  52284. word32 keyIdx = 0;
  52285. /* load RSA private key and perform private transform */
  52286. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  52287. &key, pDer->length);
  52288. if (ret == 0) {
  52289. ret = wc_RsaFunction(
  52290. info->pk.rsa.in, info->pk.rsa.inLen,
  52291. info->pk.rsa.out, info->pk.rsa.outLen,
  52292. info->pk.rsa.type, &key, info->pk.rsa.rng);
  52293. }
  52294. else {
  52295. /* if decode fails, then fall-back to software based crypto */
  52296. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  52297. "key decode failed %d, falling back to "
  52298. "software\n", ret);
  52299. ret = CRYPTOCB_UNAVAILABLE;
  52300. }
  52301. wc_FreeRsaKey(&key);
  52302. }
  52303. wc_FreeDer(&pDer); pDer = NULL;
  52304. break;
  52305. }
  52306. }
  52307. #ifdef DEBUG_WOLFSSL
  52308. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  52309. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  52310. #endif
  52311. }
  52312. #endif /* !NO_RSA */
  52313. #ifdef HAVE_ECC
  52314. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  52315. /* mark this key as ephemeral */
  52316. if (info->pk.eckg.key != NULL) {
  52317. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  52318. sizeof(info->pk.eckg.key->label));
  52319. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  52320. }
  52321. }
  52322. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  52323. ecc_key key;
  52324. /* perform software based ECC sign */
  52325. #ifdef DEBUG_WOLFSSL
  52326. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  52327. #endif
  52328. if (info->pk.eccsign.key != NULL &&
  52329. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  52330. /* this is an empheral key */
  52331. #ifdef DEBUG_WOLFSSL
  52332. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  52333. "ephemeral key\n");
  52334. #endif
  52335. return CRYPTOCB_UNAVAILABLE;
  52336. }
  52337. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  52338. if (ret != 0) {
  52339. return ret;
  52340. }
  52341. ret = wc_ecc_init(&key);
  52342. if (ret == 0) {
  52343. word32 keyIdx = 0;
  52344. /* load ECC private key and perform private transform */
  52345. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  52346. &key, pDer->length);
  52347. if (ret == 0) {
  52348. ret = wc_ecc_sign_hash(
  52349. info->pk.eccsign.in, info->pk.eccsign.inlen,
  52350. info->pk.eccsign.out, info->pk.eccsign.outlen,
  52351. info->pk.eccsign.rng, &key);
  52352. }
  52353. else {
  52354. /* if decode fails, then fall-back to software based crypto */
  52355. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  52356. "decode failed %d, falling back to software\n", ret);
  52357. ret = CRYPTOCB_UNAVAILABLE;
  52358. }
  52359. wc_ecc_free(&key);
  52360. }
  52361. wc_FreeDer(&pDer); pDer = NULL;
  52362. #ifdef DEBUG_WOLFSSL
  52363. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  52364. ret, *info->pk.eccsign.outlen);
  52365. #endif
  52366. }
  52367. #endif /* HAVE_ECC */
  52368. #ifdef HAVE_ED25519
  52369. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  52370. ed25519_key key;
  52371. /* perform software based ED25519 sign */
  52372. #ifdef DEBUG_WOLFSSL
  52373. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  52374. #endif
  52375. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  52376. if (ret != 0) {
  52377. return ret;
  52378. }
  52379. ret = wc_ed25519_init(&key);
  52380. if (ret == 0) {
  52381. word32 keyIdx = 0;
  52382. /* load ED25519 private key and perform private transform */
  52383. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  52384. &key, pDer->length);
  52385. if (ret == 0) {
  52386. /* calculate public key */
  52387. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  52388. if (ret == 0) {
  52389. key.pubKeySet = 1;
  52390. ret = wc_ed25519_sign_msg_ex(
  52391. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  52392. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  52393. &key, info->pk.ed25519sign.type,
  52394. info->pk.ed25519sign.context,
  52395. info->pk.ed25519sign.contextLen);
  52396. }
  52397. }
  52398. else {
  52399. /* if decode fails, then fall-back to software based crypto */
  52400. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  52401. "decode failed %d, falling back to software\n", ret);
  52402. ret = CRYPTOCB_UNAVAILABLE;
  52403. }
  52404. wc_ed25519_free(&key);
  52405. }
  52406. wc_FreeDer(&pDer); pDer = NULL;
  52407. #ifdef DEBUG_WOLFSSL
  52408. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  52409. ret, *info->pk.ed25519sign.outLen);
  52410. #endif
  52411. }
  52412. #endif /* HAVE_ED25519 */
  52413. }
  52414. (void)thisDevId;
  52415. (void)keyFormat;
  52416. return ret;
  52417. }
  52418. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  52419. static void test_wc_CryptoCb_TLS(int tlsVer,
  52420. const char* cliCaPemFile, const char* cliCertPemFile,
  52421. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  52422. const char* svrCaPemFile, const char* svrCertPemFile,
  52423. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  52424. {
  52425. callback_functions client_cbf;
  52426. callback_functions server_cbf;
  52427. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  52428. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  52429. if (tlsVer == WOLFSSL_TLSV1_3) {
  52430. #ifdef WOLFSSL_TLS13
  52431. server_cbf.method = wolfTLSv1_3_server_method;
  52432. client_cbf.method = wolfTLSv1_3_client_method;
  52433. #endif
  52434. }
  52435. else if (tlsVer == WOLFSSL_TLSV1_2) {
  52436. #ifndef WOLFSSL_NO_TLS12
  52437. server_cbf.method = wolfTLSv1_2_server_method;
  52438. client_cbf.method = wolfTLSv1_2_client_method;
  52439. #endif
  52440. }
  52441. else if (tlsVer == WOLFSSL_TLSV1_1) {
  52442. #ifndef NO_OLD_TLS
  52443. server_cbf.method = wolfTLSv1_1_server_method;
  52444. client_cbf.method = wolfTLSv1_1_client_method;
  52445. #endif
  52446. }
  52447. else if (tlsVer == WOLFSSL_TLSV1) {
  52448. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  52449. server_cbf.method = wolfTLSv1_server_method;
  52450. client_cbf.method = wolfTLSv1_client_method;
  52451. #endif
  52452. }
  52453. else if (tlsVer == WOLFSSL_SSLV3) {
  52454. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  52455. defined(WOLFSSL_STATIC_RSA)
  52456. server_cbf.method = wolfSSLv3_server_method;
  52457. client_cbf.method = wolfSSLv3_client_method;
  52458. #endif
  52459. }
  52460. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  52461. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  52462. server_cbf.method = wolfDTLSv1_2_server_method;
  52463. client_cbf.method = wolfDTLSv1_2_client_method;
  52464. #endif
  52465. }
  52466. else if (tlsVer == WOLFSSL_DTLSV1) {
  52467. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  52468. server_cbf.method = wolfDTLSv1_server_method;
  52469. client_cbf.method = wolfDTLSv1_client_method;
  52470. #endif
  52471. }
  52472. if (server_cbf.method == NULL) {
  52473. /* not enabled */
  52474. return;
  52475. }
  52476. /* Setup the keys for the TLS test */
  52477. client_cbf.certPemFile = cliCertPemFile;
  52478. client_cbf.keyPemFile = cliPubKeyPemFile;
  52479. client_cbf.caPemFile = cliCaPemFile;
  52480. server_cbf.certPemFile = svrCertPemFile;
  52481. server_cbf.keyPemFile = svrPubKeyPemFile;
  52482. server_cbf.caPemFile = svrCaPemFile;
  52483. /* Setup a crypto callback with pointer to private key file for testing */
  52484. client_cbf.devId = 1;
  52485. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  52486. (void*)cliPrivKeyPemFile);
  52487. server_cbf.devId = 2;
  52488. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  52489. (void*)svrPrivKeyPemFile);
  52490. /* Perform TLS server and client test */
  52491. /* First test is at WOLFSSL_CTX level */
  52492. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  52493. /* Check for success */
  52494. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  52495. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  52496. /* Second test is a WOLFSSL object level */
  52497. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  52498. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  52499. /* Check for success */
  52500. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  52501. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  52502. /* Un register the devId's */
  52503. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  52504. client_cbf.devId = INVALID_DEVID;
  52505. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  52506. server_cbf.devId = INVALID_DEVID;
  52507. }
  52508. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  52509. static int test_wc_CryptoCb(void)
  52510. {
  52511. int res = TEST_SKIPPED;
  52512. #ifdef WOLF_CRYPTO_CB
  52513. /* TODO: Add crypto callback API tests */
  52514. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  52515. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  52516. int tlsVer;
  52517. #endif
  52518. #ifndef NO_RSA
  52519. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  52520. test_wc_CryptoCb_TLS(tlsVer,
  52521. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  52522. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  52523. }
  52524. #endif
  52525. #ifdef HAVE_ECC
  52526. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  52527. test_wc_CryptoCb_TLS(tlsVer,
  52528. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  52529. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  52530. }
  52531. #endif
  52532. #ifdef HAVE_ED25519
  52533. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  52534. if (tlsVer == WOLFSSL_DTLSV1) continue;
  52535. test_wc_CryptoCb_TLS(tlsVer,
  52536. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  52537. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  52538. }
  52539. #endif
  52540. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  52541. res = TEST_RES_CHECK(1);
  52542. #endif /* WOLF_CRYPTO_CB */
  52543. return res;
  52544. }
  52545. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  52546. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  52547. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  52548. const char* cliCaPemFile, const char* cliCertPemFile,
  52549. const char* cliPrivKeyPemFile,
  52550. const char* svrCaPemFile, const char* svrCertPemFile,
  52551. const char* svrPrivKeyPemFile,
  52552. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  52553. {
  52554. callback_functions client_cbf;
  52555. callback_functions server_cbf;
  52556. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  52557. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  52558. if (tlsVer == WOLFSSL_TLSV1_3) {
  52559. #ifdef WOLFSSL_TLS13
  52560. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  52561. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  52562. #endif
  52563. }
  52564. else if (tlsVer == WOLFSSL_TLSV1_2) {
  52565. #ifndef WOLFSSL_NO_TLS12
  52566. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  52567. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  52568. #endif
  52569. }
  52570. else if (tlsVer == WOLFSSL_TLSV1_1) {
  52571. #ifndef NO_OLD_TLS
  52572. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  52573. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  52574. #endif
  52575. }
  52576. else if (tlsVer == WOLFSSL_TLSV1) {
  52577. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  52578. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  52579. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  52580. #endif
  52581. }
  52582. else if (tlsVer == WOLFSSL_SSLV3) {
  52583. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  52584. defined(WOLFSSL_STATIC_RSA)
  52585. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  52586. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  52587. #endif
  52588. }
  52589. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  52590. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  52591. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  52592. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  52593. #endif
  52594. }
  52595. else if (tlsVer == WOLFSSL_DTLSV1) {
  52596. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  52597. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  52598. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  52599. #endif
  52600. }
  52601. if (server_cbf.method_ex == NULL) {
  52602. /* not enabled */
  52603. return;
  52604. }
  52605. /* Setup the keys for the TLS test */
  52606. client_cbf.certPemFile = cliCertPemFile;
  52607. client_cbf.keyPemFile = cliPrivKeyPemFile;
  52608. client_cbf.caPemFile = cliCaPemFile;
  52609. server_cbf.certPemFile = svrCertPemFile;
  52610. server_cbf.keyPemFile = svrPrivKeyPemFile;
  52611. server_cbf.caPemFile = svrCaPemFile;
  52612. client_cbf.mem = cliMem;
  52613. client_cbf.memSz = cliMemSz;
  52614. server_cbf.mem = svrMem;
  52615. server_cbf.memSz = svrMemSz;
  52616. client_cbf.devId = INVALID_DEVID;
  52617. server_cbf.devId = INVALID_DEVID;
  52618. /* Perform TLS server and client test */
  52619. /* First test is at WOLFSSL_CTX level */
  52620. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  52621. /* Check for success */
  52622. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  52623. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  52624. /* Second test is a WOLFSSL object level */
  52625. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  52626. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  52627. /* Check for success */
  52628. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  52629. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  52630. }
  52631. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  52632. #ifdef WOLFSSL_STATIC_MEMORY
  52633. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  52634. defined(SESSION_CERTS)
  52635. #ifdef OPENSSL_EXTRA
  52636. #define TEST_TLS_STATIC_MEMSZ (400000)
  52637. #else
  52638. #define TEST_TLS_STATIC_MEMSZ (320000)
  52639. #endif
  52640. #else
  52641. #define TEST_TLS_STATIC_MEMSZ (80000)
  52642. #endif
  52643. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  52644. {
  52645. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  52646. WOLFSSL_MEM_STATS mem_stats;
  52647. WOLFSSL_MEM_CONN_STATS ssl_stats;
  52648. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  52649. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  52650. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  52651. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  52652. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  52653. #endif
  52654. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  52655. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  52656. /* this should fail because kMaxCtxClients == 2 */
  52657. AssertNull((ssl3 = wolfSSL_new(ctx)));
  52658. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  52659. #ifdef DEBUG_WOLFSSL
  52660. wolfSSL_PrintStatsConn(&ssl_stats);
  52661. #endif
  52662. (void)ssl_stats;
  52663. }
  52664. /* display collected statistics */
  52665. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  52666. #ifdef DEBUG_WOLFSSL
  52667. wolfSSL_PrintStats(&mem_stats);
  52668. #endif
  52669. (void)mem_stats;
  52670. }
  52671. wolfSSL_free(ssl1);
  52672. wolfSSL_free(ssl2);
  52673. return TEST_RES_CHECK(1);
  52674. }
  52675. #endif /* WOLFSSL_STATIC_MEMORY */
  52676. static int test_wolfSSL_CTX_StaticMemory(void)
  52677. {
  52678. int res = TEST_SKIPPED;
  52679. #ifdef WOLFSSL_STATIC_MEMORY
  52680. wolfSSL_method_func method_func;
  52681. WOLFSSL_CTX* ctx;
  52682. const int kMaxCtxClients = 2;
  52683. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  52684. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  52685. int tlsVer;
  52686. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  52687. #endif
  52688. #endif
  52689. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  52690. #ifndef NO_WOLFSSL_SERVER
  52691. #ifndef WOLFSSL_NO_TLS12
  52692. method_func = wolfTLSv1_2_server_method_ex;
  52693. #else
  52694. method_func = wolfTLSv1_3_server_method_ex;
  52695. #endif
  52696. #else
  52697. #ifndef WOLFSSL_NO_TLS12
  52698. method_func = wolfTLSv1_2_client_method_ex;
  52699. #else
  52700. method_func = wolfTLSv1_3_client_method_ex;
  52701. #endif
  52702. #endif
  52703. /* Test creating CTX directly from static memory pool */
  52704. ctx = NULL;
  52705. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  52706. &ctx, method_func, svrMem, sizeof(svrMem),
  52707. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  52708. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  52709. wolfSSL_CTX_free(ctx);
  52710. ctx = NULL;
  52711. /* Test for heap allocated CTX, then assigning static pool to it */
  52712. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  52713. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  52714. NULL, svrMem, sizeof(svrMem),
  52715. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  52716. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  52717. wolfSSL_CTX_free(ctx);
  52718. /* TLS Level Tests using static memory */
  52719. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  52720. #ifndef NO_RSA
  52721. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  52722. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  52723. svrCertFile, cliCertFile, cliKeyFile,
  52724. cliCertFile, svrCertFile, svrKeyFile,
  52725. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  52726. }
  52727. #endif
  52728. #ifdef HAVE_ECC
  52729. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  52730. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  52731. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  52732. cliEccCertFile, eccCertFile, eccKeyFile,
  52733. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  52734. }
  52735. #endif
  52736. #ifdef HAVE_ED25519
  52737. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  52738. if (tlsVer == WOLFSSL_DTLSV1) continue;
  52739. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  52740. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  52741. cliEdCertFile, edCertFile, edKeyFile,
  52742. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  52743. }
  52744. #endif
  52745. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  52746. res = TEST_RES_CHECK(1);
  52747. #endif
  52748. return res;
  52749. }
  52750. static int test_openssl_FIPS_drbg(void)
  52751. {
  52752. int res = TEST_SKIPPED;
  52753. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  52754. DRBG_CTX* dctx;
  52755. byte data1[32], data2[32], zeroData[32];
  52756. byte testSeed[16];
  52757. size_t dlen = sizeof(data1);
  52758. int i;
  52759. XMEMSET(data1, 0, dlen);
  52760. XMEMSET(data2, 0, dlen);
  52761. XMEMSET(zeroData, 0, sizeof(zeroData));
  52762. for (i=0; i<(int)sizeof(testSeed); i++) {
  52763. testSeed[i] = (byte)i;
  52764. }
  52765. AssertNotNull(dctx = FIPS_get_default_drbg());
  52766. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  52767. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  52768. WOLFSSL_SUCCESS);
  52769. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  52770. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  52771. WOLFSSL_SUCCESS);
  52772. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  52773. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  52774. WOLFSSL_SUCCESS);
  52775. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  52776. WOLFSSL_SUCCESS);
  52777. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  52778. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  52779. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  52780. res = TEST_RES_CHECK(1);
  52781. #endif
  52782. return res;
  52783. }
  52784. static int test_wolfSSL_FIPS_mode(void)
  52785. {
  52786. int res = TEST_SKIPPED;
  52787. #if defined(OPENSSL_ALL)
  52788. #ifdef HAVE_FIPS
  52789. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  52790. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  52791. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  52792. #else
  52793. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  52794. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  52795. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  52796. #endif
  52797. res = TEST_RES_CHECK(1);
  52798. #endif
  52799. return res;
  52800. }
  52801. #ifdef WOLFSSL_DTLS
  52802. /* Prints out the current window */
  52803. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  52804. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  52805. int i;
  52806. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  52807. word32 b = w[i];
  52808. int j;
  52809. /* Prints out a 32 bit binary number in big endian order */
  52810. for (j = 0; j < 32; j++, b <<= 1) {
  52811. if (b & (((word32)1) << 31))
  52812. fprintf(stderr, "1");
  52813. else
  52814. fprintf(stderr, "0");
  52815. }
  52816. fprintf(stderr, " ");
  52817. }
  52818. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  52819. #else
  52820. (void)h;
  52821. (void)l;
  52822. (void)w;
  52823. #endif
  52824. }
  52825. /* a - cur_hi
  52826. * b - cur_lo
  52827. * c - next_hi
  52828. * d - next_lo
  52829. * e - window
  52830. * f - expected next_hi
  52831. * g - expected next_lo
  52832. * h - expected window[1]
  52833. * i - expected window[0]
  52834. */
  52835. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  52836. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  52837. DUW_TEST_print_window_binary((a), (b), (e)); \
  52838. AssertIntEQ((c), (f)); \
  52839. AssertIntEQ((d), (g)); \
  52840. AssertIntEQ((e)[1], (h)); \
  52841. AssertIntEQ((e)[0], (i)); \
  52842. } while (0)
  52843. static int test_wolfSSL_DtlsUpdateWindow(void)
  52844. {
  52845. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  52846. word32 next_lo = 0;
  52847. word16 next_hi = 0;
  52848. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  52849. fprintf(stderr, "\n");
  52850. #endif
  52851. XMEMSET(window, 0, sizeof window);
  52852. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  52853. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  52854. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  52855. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  52856. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  52857. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  52858. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  52859. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  52860. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  52861. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  52862. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  52863. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  52864. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  52865. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  52866. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  52867. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  52868. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  52869. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  52870. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  52871. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  52872. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  52873. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  52874. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  52875. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  52876. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  52877. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  52878. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  52879. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  52880. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  52881. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  52882. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  52883. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  52884. return TEST_RES_CHECK(1);
  52885. }
  52886. #endif /* WOLFSSL_DTLS */
  52887. #ifdef WOLFSSL_DTLS
  52888. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  52889. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  52890. {
  52891. DtlsMsg* cur;
  52892. static byte msg[100];
  52893. static byte msgInit = 0;
  52894. if (!msgInit) {
  52895. int i;
  52896. for (i = 0; i < 100; i++)
  52897. msg[i] = i + 1;
  52898. msgInit = 1;
  52899. }
  52900. /* Sanitize test parameters */
  52901. if (len > sizeof(msg))
  52902. return -1;
  52903. if (f_offset + f_len > sizeof(msg))
  52904. return -1;
  52905. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  52906. if (ssl->dtls_rx_msg_list == NULL)
  52907. return -100;
  52908. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  52909. return -200;
  52910. if (cur->fragBucketListCount != f_count)
  52911. return -300;
  52912. if (cur->ready != ready)
  52913. return -400;
  52914. if (cur->bytesReceived != bytesReceived)
  52915. return -500;
  52916. if (ready) {
  52917. if (cur->fragBucketList != NULL)
  52918. return -600;
  52919. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  52920. return -700;
  52921. }
  52922. else {
  52923. DtlsFragBucket* fb;
  52924. if (cur->fragBucketList == NULL)
  52925. return -800;
  52926. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  52927. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  52928. return -900;
  52929. }
  52930. }
  52931. return 0;
  52932. }
  52933. static void DFB_TEST_RESET(WOLFSSL* ssl)
  52934. {
  52935. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  52936. ssl->dtls_rx_msg_list = NULL;
  52937. ssl->dtls_rx_msg_list_sz = 0;
  52938. }
  52939. static int test_wolfSSL_DTLS_fragment_buckets(void)
  52940. {
  52941. WOLFSSL ssl[1];
  52942. XMEMSET(ssl, 0, sizeof(*ssl));
  52943. AssertIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  52944. AssertIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  52945. AssertIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  52946. AssertIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  52947. AssertIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  52948. AssertIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  52949. /* Test all permutations of 3 regions */
  52950. /* 1 2 3 */
  52951. AssertIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  52952. AssertIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  52953. AssertIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  52954. /* 1 3 2 */
  52955. AssertIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  52956. AssertIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  52957. AssertIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  52958. /* 2 1 3 */
  52959. AssertIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  52960. AssertIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  52961. AssertIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  52962. /* 2 3 1 */
  52963. AssertIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  52964. AssertIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  52965. AssertIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  52966. /* 3 1 2 */
  52967. AssertIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  52968. AssertIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  52969. AssertIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  52970. /* 3 2 1 */
  52971. AssertIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  52972. AssertIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  52973. AssertIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  52974. /* Test overlapping regions */
  52975. AssertIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  52976. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  52977. AssertIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  52978. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  52979. AssertIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  52980. /* Test overlapping multiple regions */
  52981. AssertIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  52982. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  52983. AssertIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  52984. AssertIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  52985. AssertIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  52986. AssertIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  52987. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  52988. AssertIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  52989. AssertIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  52990. AssertIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  52991. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  52992. AssertIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  52993. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  52994. AssertIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  52995. DFB_TEST_RESET(ssl);
  52996. return TEST_RES_CHECK(1);
  52997. }
  52998. #endif
  52999. #if !defined(NO_FILESYSTEM) && \
  53000. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53001. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  53002. static int test_wolfSSL_dtls_stateless2(void)
  53003. {
  53004. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  53005. struct test_memio_ctx test_ctx;
  53006. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  53007. int ret;
  53008. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53009. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53010. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  53011. if (ret != 0)
  53012. return -1;
  53013. ssl_c2 = wolfSSL_new(ctx_c);
  53014. if (ssl_c2 == NULL)
  53015. return -2;
  53016. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53017. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53018. /* send CH */
  53019. ret = wolfSSL_connect(ssl_c2);
  53020. if (ret == 0 || ssl_c2->error != WANT_READ)
  53021. return -3;
  53022. ret = wolfSSL_accept(ssl_s);
  53023. if (ret == 0 || ssl_s->error != WANT_READ)
  53024. return -4;
  53025. if (test_ctx.c_len == 0)
  53026. return -5;
  53027. /* consume HRR */
  53028. test_ctx.c_len = 0;
  53029. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  53030. if (ret != 0)
  53031. return -6;
  53032. wolfSSL_free(ssl_c2);
  53033. wolfSSL_free(ssl_c);
  53034. wolfSSL_free(ssl_s);
  53035. wolfSSL_CTX_free(ctx_c);
  53036. wolfSSL_CTX_free(ctx_s);
  53037. return TEST_SUCCESS;
  53038. }
  53039. #ifdef HAVE_MAX_FRAGMENT
  53040. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  53041. {
  53042. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  53043. struct test_memio_ctx test_ctx;
  53044. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  53045. word16 max_fragment;
  53046. int ret;
  53047. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53048. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53049. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  53050. if (ret != 0)
  53051. return -1;
  53052. ssl_c2 = wolfSSL_new(ctx_c);
  53053. if (ssl_c2 == NULL)
  53054. return -2;
  53055. ret = wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8);
  53056. if (ret != WOLFSSL_SUCCESS)
  53057. return -3;
  53058. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53059. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53060. max_fragment = ssl_s->max_fragment;
  53061. /* send CH */
  53062. ret = wolfSSL_connect(ssl_c2);
  53063. if (ret == 0 || ssl_c2->error != WANT_READ)
  53064. return -4;
  53065. ret = wolfSSL_accept(ssl_s);
  53066. if (ret == 0 || ssl_s->error != WANT_READ)
  53067. return -5;
  53068. /* CH without cookie shouldn't change state */
  53069. if (ssl_s->max_fragment != max_fragment)
  53070. return -6;
  53071. if (test_ctx.c_len == 0)
  53072. return -7;
  53073. /* consume HRR from buffer */
  53074. test_ctx.c_len = 0;
  53075. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  53076. if (ret != 0)
  53077. return -8;
  53078. wolfSSL_free(ssl_c2);
  53079. wolfSSL_free(ssl_c);
  53080. wolfSSL_free(ssl_s);
  53081. wolfSSL_CTX_free(ctx_c);
  53082. wolfSSL_CTX_free(ctx_s);
  53083. return TEST_SUCCESS;
  53084. }
  53085. #endif /* HAVE_MAX_FRAGMENT */
  53086. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  53087. #define ROUNDS_WITH_HVR 4
  53088. #define ROUNDS_WITHOUT_HVR 2
  53089. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  53090. static int buf_is_hvr(const byte *data, int len)
  53091. {
  53092. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  53093. return 0;
  53094. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  53095. }
  53096. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  53097. {
  53098. struct test_memio_ctx test_ctx;
  53099. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  53100. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  53101. WOLFSSL_SESSION *sess;
  53102. int ret, round_trips;
  53103. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53104. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53105. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  53106. if (ret != 0)
  53107. return -1;
  53108. #ifdef HAVE_SESSION_TICKET
  53109. if (useticket) {
  53110. ret = wolfSSL_UseSessionTicket(ssl_c);
  53111. if (ret != WOLFSSL_SUCCESS)
  53112. return -2;
  53113. }
  53114. #endif
  53115. round_trips = ROUNDS_WITH_HVR;
  53116. ret = test_memio_do_handshake(ssl_c, ssl_s, round_trips, &round_trips);
  53117. if (ret != 0)
  53118. return -3;
  53119. if (round_trips != ROUNDS_WITH_HVR)
  53120. return -4;
  53121. sess = wolfSSL_get1_session(ssl_c);
  53122. if (sess == NULL)
  53123. return -5;
  53124. wolfSSL_shutdown(ssl_c);
  53125. wolfSSL_shutdown(ssl_s);
  53126. wolfSSL_free(ssl_c);
  53127. wolfSSL_free(ssl_s);
  53128. test_ctx.c_len = test_ctx.s_len = 0;
  53129. /* make resumption invalid */
  53130. if (bad) {
  53131. if (useticket) {
  53132. #ifdef HAVE_SESSION_TICKET
  53133. sess->ticket[0] = !sess->ticket[0];
  53134. #endif /* HAVE_SESSION_TICKET */
  53135. }
  53136. else {
  53137. sess->sessionID[0] = !sess->sessionID[0];
  53138. }
  53139. }
  53140. ssl_c = wolfSSL_new(ctx_c);
  53141. ssl_s = wolfSSL_new(ctx_s);
  53142. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  53143. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  53144. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  53145. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  53146. ret = wolfSSL_set_session(ssl_c, sess);
  53147. if (ret != WOLFSSL_SUCCESS)
  53148. return -6;
  53149. ret = wolfSSL_connect(ssl_c);
  53150. if (ret == WOLFSSL_SUCCESS || ssl_c->error != WANT_READ)
  53151. return -7;
  53152. ret = wolfSSL_accept(ssl_s);
  53153. if (ret == WOLFSSL_SUCCESS || ssl_s->error != WANT_READ)
  53154. return -8;
  53155. if (bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  53156. return -9;
  53157. if (!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  53158. return -10;
  53159. if (!useticket) {
  53160. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips);
  53161. if (ret != 0)
  53162. return -11;
  53163. if (bad && round_trips != ROUNDS_WITH_HVR - 1)
  53164. return -12;
  53165. if (!bad && round_trips != ROUNDS_WITHOUT_HVR - 1)
  53166. return -13;
  53167. }
  53168. wolfSSL_SESSION_free(sess);
  53169. wolfSSL_free(ssl_c);
  53170. wolfSSL_free(ssl_s);
  53171. wolfSSL_CTX_free(ctx_c);
  53172. wolfSSL_CTX_free(ctx_s);
  53173. return 0;
  53174. }
  53175. static int test_wolfSSL_dtls_stateless_resume(void)
  53176. {
  53177. int ret;
  53178. #ifdef HAVE_SESSION_TICKET
  53179. ret = _test_wolfSSL_dtls_stateless_resume(1, 0);
  53180. if (ret != 0)
  53181. return TEST_RES_CHECK(ret);
  53182. ret = _test_wolfSSL_dtls_stateless_resume(1, 1);
  53183. if (ret != 0)
  53184. return TEST_RES_CHECK(ret - 100);
  53185. #endif /* HAVE_SESION_TICKET */
  53186. ret = _test_wolfSSL_dtls_stateless_resume(0, 0);
  53187. if (ret != 0)
  53188. return TEST_RES_CHECK(ret - 200);
  53189. ret = _test_wolfSSL_dtls_stateless_resume(0, 1);
  53190. if (ret != 0)
  53191. return TEST_RES_CHECK(ret - 300);
  53192. return TEST_RES_CHECK(TEST_SUCCESS);
  53193. }
  53194. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  53195. #if !defined(NO_OLD_TLS)
  53196. static int test_wolfSSL_dtls_stateless_downgrade(void)
  53197. {
  53198. WOLFSSL_CTX *ctx_c = NULL, *ctx_c2 = NULL, *ctx_s = NULL;
  53199. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  53200. struct test_memio_ctx test_ctx;
  53201. int ret;
  53202. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53203. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53204. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  53205. if (ret != 0)
  53206. return -1;
  53207. ret = wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1);
  53208. if (ret != WOLFSSL_SUCCESS)
  53209. return -2;
  53210. ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method());
  53211. if (ctx_c2 == NULL)
  53212. return -3;
  53213. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  53214. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  53215. ssl_c2 = wolfSSL_new(ctx_c2);
  53216. if (ssl_c2 == NULL)
  53217. return -4;
  53218. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53219. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53220. /* send CH */
  53221. ret = wolfSSL_connect(ssl_c2);
  53222. if (ret == 0 || ssl_c2->error != WANT_READ)
  53223. return -5;
  53224. ret = wolfSSL_accept(ssl_s);
  53225. if (ret == 0 || ssl_s->error != WANT_READ)
  53226. return -6;
  53227. if (test_ctx.c_len == 0)
  53228. return -7;
  53229. /* consume HRR */
  53230. test_ctx.c_len = 0;
  53231. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  53232. if (ret != 0)
  53233. return -8;
  53234. wolfSSL_free(ssl_c2);
  53235. wolfSSL_free(ssl_c);
  53236. wolfSSL_free(ssl_s);
  53237. wolfSSL_CTX_free(ctx_c);
  53238. wolfSSL_CTX_free(ctx_c2);
  53239. wolfSSL_CTX_free(ctx_s);
  53240. return TEST_SUCCESS;
  53241. }
  53242. #endif /* !defined(NO_OLD_TLS) */
  53243. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53244. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  53245. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53246. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  53247. !defined(NO_OLD_TLS)
  53248. static int test_WOLFSSL_dtls_version_alert(void)
  53249. {
  53250. struct test_memio_ctx test_ctx;
  53251. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  53252. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  53253. int ret;
  53254. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53255. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53256. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method);
  53257. if (ret != 0)
  53258. return -1;
  53259. /* client hello */
  53260. ret = wolfSSL_connect(ssl_c);
  53261. if (ret == 0 || ssl_c->error != WANT_READ )
  53262. return -2;
  53263. /* hrr */
  53264. ret = wolfSSL_accept(ssl_s);
  53265. if (ret == 0 || ssl_s->error != WANT_READ )
  53266. return -3;
  53267. /* client hello 1 */
  53268. ret = wolfSSL_connect(ssl_c);
  53269. if (ret == 0 || ssl_c->error != WANT_READ )
  53270. return -4;
  53271. /* server hello */
  53272. ret = wolfSSL_accept(ssl_s);
  53273. if (ret == 0 || ssl_s->error != WANT_READ )
  53274. return -5;
  53275. /* should fail */
  53276. ret = wolfSSL_connect(ssl_c);
  53277. if (ret == 0 || ssl_c->error != VERSION_ERROR)
  53278. return -6;
  53279. /* shuould fail */
  53280. ret = wolfSSL_accept(ssl_s);
  53281. if (ret == 0 ||
  53282. (ssl_s->error != VERSION_ERROR && ssl_s->error != FATAL_ERROR))
  53283. return -7;
  53284. wolfSSL_free(ssl_c);
  53285. wolfSSL_free(ssl_s);
  53286. wolfSSL_CTX_free(ctx_c);
  53287. wolfSSL_CTX_free(ctx_s);
  53288. return TEST_RES_CHECK(1);
  53289. }
  53290. #else
  53291. static int test_WOLFSSL_dtls_version_alert(void)
  53292. {
  53293. return TEST_SKIPPED;
  53294. }
  53295. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  53296. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  53297. * !defined(NO_OLD_TLS)
  53298. */
  53299. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  53300. && defined(WOLFSSL_TLS13) && \
  53301. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  53302. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  53303. {
  53304. struct test_memio_ctx *test_ctx;
  53305. byte buf[2048];
  53306. int idx, sz;
  53307. word32 tmp;
  53308. int ret;
  53309. idx = 5; /* space for record header */
  53310. buf[idx] = session_ticket; /* type */
  53311. idx++;
  53312. tmp = OPAQUE32_LEN +
  53313. OPAQUE32_LEN +
  53314. OPAQUE8_LEN + nonceLength +
  53315. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  53316. c32to24(tmp, buf + idx);
  53317. idx += OPAQUE24_LEN;
  53318. c32toa((word32)12345, buf+idx); /* lifetime */
  53319. idx += OPAQUE32_LEN;
  53320. c32toa((word32)12345, buf+idx); /* add */
  53321. idx += OPAQUE32_LEN;
  53322. buf[idx] = nonceLength; /* nonce length */
  53323. idx++;
  53324. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  53325. idx += nonceLength;
  53326. tmp = 1; /* ticket len */
  53327. c16toa((word16)tmp, buf+idx);
  53328. idx += 2;
  53329. buf[idx] = 0xFF; /* ticket */
  53330. idx++;
  53331. tmp = 0; /* ext len */
  53332. c16toa((word16)tmp, buf+idx);
  53333. idx += 2;
  53334. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  53335. handshake, 0, 0, 0);
  53336. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  53337. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  53338. return !(ret == sz);
  53339. }
  53340. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  53341. {
  53342. int i;
  53343. if (sess == NULL)
  53344. return -1;
  53345. if (sess->ticketNonce.len != len)
  53346. return -1;
  53347. for (i = 0; i < len; i++)
  53348. if (sess->ticketNonce.data[i] != len)
  53349. return -1;
  53350. return 0;
  53351. }
  53352. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  53353. {
  53354. char *buf[1024];
  53355. int ret;
  53356. ret = send_new_session_ticket(ssl_s, len, len);
  53357. if (ret != 0)
  53358. return -1;
  53359. ret = wolfSSL_recv(ssl_c, buf, 1024, 0);
  53360. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  53361. return -1;
  53362. return test_ticket_nonce_check(ssl_c->session, len);
  53363. }
  53364. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  53365. {
  53366. WOLFSSL_SESSION *sess, *cached;
  53367. WOLFSSL_CTX *ctx;
  53368. int ret;
  53369. ctx = ssl_c->ctx;
  53370. ret = test_ticket_nonce_malloc_do(ssl_s, ssl_c, len);
  53371. if (ret != 0)
  53372. return -1;
  53373. sess = wolfSSL_get1_session(ssl_c);
  53374. if (sess == NULL)
  53375. return -1;
  53376. ret = AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  53377. NULL, ssl_c->options.side, 1,NULL);
  53378. if (ret != 0)
  53379. return -1;
  53380. cached = wolfSSL_SESSION_new();
  53381. if (cached == NULL)
  53382. return -1;
  53383. ret = wolfSSL_GetSessionFromCache(ssl_c, cached);
  53384. if (ret != WOLFSSL_SUCCESS)
  53385. return -1;
  53386. ret = test_ticket_nonce_check(cached, len);
  53387. if (ret != 0)
  53388. return -1;
  53389. wolfSSL_SESSION_free(cached);
  53390. wolfSSL_SESSION_free(sess);
  53391. return 0;
  53392. }
  53393. static int test_ticket_nonce_malloc(void)
  53394. {
  53395. struct test_memio_ctx test_ctx;
  53396. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  53397. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  53398. byte small, medium, big;
  53399. int ret;
  53400. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53401. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53402. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method);
  53403. if (ret != 0)
  53404. return -1;
  53405. /* will send ticket manually */
  53406. wolfSSL_no_ticket_TLSv13(ssl_s);
  53407. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  53408. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  53409. while (!ssl_c->options.handShakeDone && !ssl_s->options.handShakeDone) {
  53410. ret = wolfSSL_connect(ssl_c);
  53411. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  53412. return -2;
  53413. ret = wolfSSL_accept(ssl_s);
  53414. if (ret != WOLFSSL_SUCCESS && ssl_s->error != WANT_READ)
  53415. return -3;
  53416. }
  53417. small = TLS13_TICKET_NONCE_STATIC_SZ;
  53418. medium = small + 20 <= 255 ? small + 20 : 255;
  53419. big = medium + 20 <= 255 ? small + 20 : 255;
  53420. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  53421. return -1;
  53422. if (ssl_c->session->ticketNonce.data !=
  53423. ssl_c->session->ticketNonce.dataStatic)
  53424. return -1;
  53425. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  53426. return -1;
  53427. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, big))
  53428. return -1;
  53429. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  53430. return -5;
  53431. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  53432. return -6;
  53433. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  53434. return -1;
  53435. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  53436. return -1;
  53437. if (test_ticket_nonce_cache(ssl_s, ssl_c, big))
  53438. return -1;
  53439. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  53440. return -1;
  53441. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  53442. return -1;
  53443. wolfSSL_free(ssl_c);
  53444. wolfSSL_free(ssl_s);
  53445. wolfSSL_CTX_free(ctx_c);
  53446. wolfSSL_CTX_free(ctx_s);
  53447. return 0;
  53448. }
  53449. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  53450. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  53451. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  53452. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  53453. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  53454. static int test_ticket_ret_create(void)
  53455. {
  53456. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  53457. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  53458. byte ticket[SESSION_TICKET_LEN];
  53459. struct test_memio_ctx test_ctx;
  53460. WOLFSSL_SESSION *sess = NULL;
  53461. word16 ticketLen;
  53462. int ret;
  53463. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53464. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53465. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  53466. if (ret != 0)
  53467. return TEST_FAIL;
  53468. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  53469. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  53470. wolfSSL_CTX_UseSessionTicket(ctx_c);
  53471. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  53472. if (ret != 0)
  53473. return TEST_FAIL;
  53474. sess = wolfSSL_get1_session(ssl_c);
  53475. if (sess->ticketLen > SESSION_TICKET_LEN)
  53476. return TEST_FAIL;
  53477. ticketLen = sess->ticketLen;
  53478. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  53479. wolfSSL_free(ssl_c);
  53480. wolfSSL_free(ssl_s);
  53481. ssl_s = wolfSSL_new(ctx_s);
  53482. if (ssl_s == NULL)
  53483. return TEST_FAIL;
  53484. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  53485. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  53486. ssl_c = wolfSSL_new(ctx_c);
  53487. if (ssl_c == NULL)
  53488. return TEST_FAIL;
  53489. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  53490. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  53491. wolfSSL_set_session(ssl_c, sess);
  53492. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  53493. if (ret != 0)
  53494. return TEST_FAIL;
  53495. if (ssl_c->session->ticketLen > SESSION_TICKET_LEN)
  53496. return TEST_FAIL;
  53497. if (ssl_c->session->ticketLen != ticketLen)
  53498. return TEST_FAIL;
  53499. if (XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) == 0)
  53500. return TEST_FAIL;
  53501. wolfSSL_SESSION_free(sess);
  53502. wolfSSL_free(ssl_c);
  53503. wolfSSL_free(ssl_s);
  53504. wolfSSL_CTX_free(ctx_c);
  53505. wolfSSL_CTX_free(ctx_s);
  53506. return TEST_SUCCESS;
  53507. }
  53508. #else
  53509. static int test_ticket_ret_create(void)
  53510. {
  53511. return TEST_SKIPPED;
  53512. }
  53513. #endif
  53514. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  53515. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  53516. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  53517. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  53518. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  53519. {
  53520. int ret;
  53521. WOLFSSL_SESSION* session = NULL;
  53522. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  53523. if (!wolfSSL_is_server(ssl)) {
  53524. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  53525. AssertNotNull(session);
  53526. }
  53527. do {
  53528. ret = wolfSSL_shutdown(ssl);
  53529. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  53530. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  53531. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  53532. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  53533. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  53534. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  53535. #endif
  53536. if (!wolfSSL_is_server(ssl)) {
  53537. /* client */
  53538. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  53539. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  53540. wolfSSL_set_session(ssl, session);
  53541. wolfSSL_SESSION_free(session);
  53542. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  53543. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  53544. }
  53545. else {
  53546. /* server */
  53547. /* Different ciphersuite so that the ticket will be invalidated based on
  53548. * the ciphersuite */
  53549. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  53550. WOLFSSL_SUCCESS);
  53551. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  53552. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  53553. }
  53554. }
  53555. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  53556. {
  53557. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  53558. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  53559. WOLFSSL_SUCCESS);
  53560. }
  53561. static int test_ticket_and_psk_mixing(void)
  53562. {
  53563. /* Test mixing tickets and regular PSK */
  53564. callback_functions client_cbs, server_cbs;
  53565. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53566. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53567. client_cbs.method = wolfTLSv1_3_client_method;
  53568. server_cbs.method = wolfTLSv1_3_server_method;
  53569. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  53570. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  53571. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  53572. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53573. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  53574. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  53575. return TEST_RES_CHECK(1);
  53576. }
  53577. #else
  53578. static int test_ticket_and_psk_mixing(void)
  53579. {
  53580. return TEST_SKIPPED;
  53581. }
  53582. #endif
  53583. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  53584. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  53585. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  53586. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  53587. static int test_prioritize_psk_cb_called = FALSE;
  53588. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  53589. const char* identity, unsigned char* key, unsigned int key_max_len,
  53590. const char** ciphersuite)
  53591. {
  53592. test_prioritize_psk_cb_called = TRUE;
  53593. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  53594. }
  53595. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  53596. {
  53597. int ret;
  53598. WOLFSSL_SESSION* session = NULL;
  53599. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  53600. if (!wolfSSL_is_server(ssl)) {
  53601. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  53602. AssertNotNull(session);
  53603. }
  53604. do {
  53605. ret = wolfSSL_shutdown(ssl);
  53606. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  53607. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  53608. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  53609. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  53610. * important. */
  53611. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  53612. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  53613. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  53614. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  53615. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  53616. #endif
  53617. if (!wolfSSL_is_server(ssl)) {
  53618. /* client */
  53619. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  53620. wolfSSL_set_session(ssl, session);
  53621. wolfSSL_SESSION_free(session);
  53622. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  53623. }
  53624. else {
  53625. /* server */
  53626. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  53627. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  53628. #ifdef WOLFSSL_PRIORITIZE_PSK
  53629. /* The ticket should be first tried with all ciphersuites and chosen */
  53630. AssertFalse(test_prioritize_psk_cb_called);
  53631. #else
  53632. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  53633. * callback that sets this var. */
  53634. AssertTrue(test_prioritize_psk_cb_called);
  53635. #endif
  53636. }
  53637. }
  53638. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  53639. {
  53640. if (!wolfSSL_is_server(ssl))
  53641. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  53642. WOLFSSL_SUCCESS);
  53643. else
  53644. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  53645. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  53646. }
  53647. static int test_prioritize_psk(void)
  53648. {
  53649. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  53650. * of the PSK's will be followed instead of the ciphersuite. */
  53651. callback_functions client_cbs, server_cbs;
  53652. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53653. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53654. client_cbs.method = wolfTLSv1_3_client_method;
  53655. server_cbs.method = wolfTLSv1_3_server_method;
  53656. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  53657. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  53658. client_cbs.on_result = test_prioritize_psk_on_result;
  53659. server_cbs.on_result = test_prioritize_psk_on_result;
  53660. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53661. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  53662. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  53663. return TEST_RES_CHECK(1);
  53664. }
  53665. #else
  53666. static int test_prioritize_psk(void)
  53667. {
  53668. return TEST_SKIPPED;
  53669. }
  53670. #endif
  53671. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  53672. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  53673. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  53674. !defined(WOLFSSL_NO_TLS12)
  53675. static void test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  53676. {
  53677. AssertTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  53678. /* Set TLS 1.3 specific suite */
  53679. AssertTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  53680. }
  53681. static int test_wolfSSL_CTX_set_ciphersuites(void)
  53682. {
  53683. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  53684. * do a 1.2 connection. */
  53685. callback_functions client_cbs, server_cbs;
  53686. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53687. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53688. client_cbs.method = wolfTLSv1_2_client_method;
  53689. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  53690. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  53691. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53692. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  53693. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  53694. return TEST_RES_CHECK(1);
  53695. }
  53696. #else
  53697. static int test_wolfSSL_CTX_set_ciphersuites(void)
  53698. {
  53699. return TEST_SKIPPED;
  53700. }
  53701. #endif
  53702. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  53703. defined(HAVE_IO_TESTS_DEPENDENCIES)
  53704. static void test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  53705. {
  53706. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  53707. }
  53708. static void test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  53709. {
  53710. WOLFSSL_ALERT_HISTORY h;
  53711. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  53712. AssertIntEQ(h.last_rx.level, alert_fatal);
  53713. AssertIntEQ(h.last_rx.code, certificate_revoked);
  53714. }
  53715. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  53716. {
  53717. callback_functions client_cbs, server_cbs;
  53718. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53719. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53720. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  53721. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  53722. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  53723. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  53724. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  53725. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53726. AssertIntEQ(client_cbs.return_code, TEST_FAIL);
  53727. AssertIntEQ(server_cbs.return_code, TEST_FAIL);
  53728. return TEST_RES_CHECK(1);
  53729. }
  53730. #else
  53731. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  53732. {
  53733. return TEST_SKIPPED;
  53734. }
  53735. #endif
  53736. /*----------------------------------------------------------------------------*
  53737. | Main
  53738. *----------------------------------------------------------------------------*/
  53739. typedef int (*TEST_FUNC)(void);
  53740. typedef struct {
  53741. const char *name;
  53742. TEST_FUNC func;
  53743. byte run:1;
  53744. } TEST_CASE;
  53745. #define TEST_DECL(func) { #func, func, 0 }
  53746. int testAll = 1;
  53747. TEST_CASE testCases[] = {
  53748. TEST_DECL(test_fileAccess),
  53749. TEST_DECL(test_wolfSSL_Init),
  53750. TEST_DECL(test_wolfSSL_Method_Allocators),
  53751. #ifndef NO_WOLFSSL_SERVER
  53752. TEST_DECL(test_wolfSSL_CTX_new),
  53753. #endif
  53754. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  53755. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  53756. TEST_DECL(test_for_double_Free),
  53757. #endif
  53758. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  53759. TEST_DECL(test_wolfSSL_get_finished),
  53760. TEST_DECL(test_wolfSSL_CTX_add_session),
  53761. #endif
  53762. TEST_DECL(test_SSL_CIPHER_get_xxx),
  53763. TEST_DECL(test_wolfSSL_ERR_strings),
  53764. TEST_DECL(test_wolfSSL_EVP_shake128),
  53765. TEST_DECL(test_wolfSSL_EVP_shake256),
  53766. TEST_DECL(test_EVP_blake2),
  53767. TEST_DECL(test_EVP_MD_do_all),
  53768. TEST_DECL(test_OBJ_NAME_do_all),
  53769. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  53770. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  53771. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  53772. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  53773. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  53774. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  53775. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  53776. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  53777. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  53778. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  53779. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  53780. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  53781. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  53782. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  53783. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  53784. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  53785. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  53786. TEST_DECL(test_wolfSSL_FPKI),
  53787. TEST_DECL(test_wolfSSL_OtherName),
  53788. TEST_DECL(test_wolfSSL_CertRsaPss),
  53789. TEST_DECL(test_wolfSSL_CertManagerCRL),
  53790. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  53791. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  53792. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  53793. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  53794. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  53795. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  53796. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  53797. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  53798. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  53799. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  53800. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  53801. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  53802. TEST_DECL(test_server_wolfSSL_new),
  53803. TEST_DECL(test_client_wolfSSL_new),
  53804. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  53805. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  53806. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  53807. TEST_DECL(test_SetTmpEC_DHE_Sz),
  53808. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  53809. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  53810. TEST_DECL(test_wolfSSL_dtls_plaintext),
  53811. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  53812. defined(HAVE_IO_TESTS_DEPENDENCIES)
  53813. TEST_DECL(test_wolfSSL_read_write),
  53814. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  53815. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
  53816. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  53817. TEST_DECL(test_wolfSSL_dtls_export),
  53818. TEST_DECL(test_wolfSSL_tls_export),
  53819. #endif
  53820. TEST_DECL(test_wolfSSL_SetMinVersion),
  53821. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  53822. /* TLS extensions tests */
  53823. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  53824. #ifdef HAVE_SNI
  53825. TEST_DECL(test_wolfSSL_UseSNI_params),
  53826. TEST_DECL(test_wolfSSL_UseSNI_connection),
  53827. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  53828. #endif /* HAVE_SNI */
  53829. #endif
  53830. TEST_DECL(test_wolfSSL_UseTrustedCA),
  53831. TEST_DECL(test_wolfSSL_UseMaxFragment),
  53832. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  53833. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  53834. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  53835. TEST_DECL(test_wolfSSL_UseALPN_connection),
  53836. TEST_DECL(test_wolfSSL_UseALPN_params),
  53837. #endif
  53838. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  53839. TEST_DECL(test_wolfSSL_set_alpn_protos),
  53840. #endif
  53841. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  53842. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  53843. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  53844. TEST_DECL(test_tls_ext_duplicate),
  53845. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  53846. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  53847. TEST_DECL(test_wolfSSL_Tls13_ECH),
  53848. #endif
  53849. /* X509 tests */
  53850. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  53851. TEST_DECL(test_wolfSSL_PKCS12),
  53852. TEST_DECL(test_wolfSSL_no_password_cb),
  53853. TEST_DECL(test_wolfSSL_PKCS8),
  53854. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  53855. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  53856. TEST_DECL(test_wolfSSL_PKCS5),
  53857. TEST_DECL(test_wolfSSL_URI),
  53858. TEST_DECL(test_wolfSSL_TBS),
  53859. TEST_DECL(test_wolfSSL_X509_verify),
  53860. TEST_DECL(test_wolfSSL_X509_TLS_version),
  53861. TEST_DECL(test_wc_PemToDer),
  53862. TEST_DECL(test_wc_AllocDer),
  53863. TEST_DECL(test_wc_CertPemToDer),
  53864. TEST_DECL(test_wc_PubKeyPemToDer),
  53865. TEST_DECL(test_wc_PemPubKeyToDer),
  53866. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  53867. TEST_DECL(test_wc_CheckCertSigPubKey),
  53868. /* OCSP Stapling */
  53869. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  53870. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  53871. /* Multicast */
  53872. TEST_DECL(test_wolfSSL_mcast),
  53873. /* compatibility tests */
  53874. TEST_DECL(test_wolfSSL_lhash),
  53875. TEST_DECL(test_wolfSSL_X509_NAME),
  53876. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  53877. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  53878. #ifndef NO_BIO
  53879. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  53880. TEST_DECL(test_wolfSSL_X509_INFO),
  53881. #endif
  53882. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  53883. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  53884. TEST_DECL(test_wolfSSL_X509_check_host),
  53885. TEST_DECL(test_wolfSSL_X509_check_email),
  53886. TEST_DECL(test_wolfSSL_DES),
  53887. TEST_DECL(test_wolfSSL_certs),
  53888. TEST_DECL(test_wolfSSL_X509_check_private_key),
  53889. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  53890. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  53891. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  53892. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  53893. TEST_DECL(test_wolfSSL_private_keys),
  53894. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  53895. #ifndef NO_BIO
  53896. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  53897. #endif
  53898. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  53899. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  53900. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  53901. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  53902. #ifndef NO_BIO
  53903. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  53904. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  53905. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  53906. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  53907. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  53908. #endif
  53909. TEST_DECL(test_DSA_do_sign_verify),
  53910. TEST_DECL(test_wolfSSL_tmp_dh),
  53911. TEST_DECL(test_wolfSSL_ctrl),
  53912. TEST_DECL(test_wolfSSL_EVP_MD_size),
  53913. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  53914. TEST_DECL(test_wolfSSL_EVP_Digest),
  53915. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  53916. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  53917. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  53918. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  53919. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  53920. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  53921. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  53922. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  53923. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  53924. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  53925. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  53926. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  53927. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  53928. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  53929. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  53930. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  53931. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  53932. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  53933. #endif
  53934. #ifndef NO_BIO
  53935. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  53936. TEST_DECL(test_wolfSSL_GetLoggingCb),
  53937. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  53938. TEST_DECL(test_wc_ERR_remove_state),
  53939. TEST_DECL(test_wc_ERR_print_errors_fp),
  53940. #endif
  53941. TEST_DECL(test_wolfSSL_set_options),
  53942. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  53943. TEST_DECL(test_wolfSSL_set1_curves_list),
  53944. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  53945. TEST_DECL(test_wolfSSL_PKCS7_certs),
  53946. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  53947. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  53948. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  53949. TEST_DECL(test_wolfSSL_msgCb),
  53950. TEST_DECL(test_wolfSSL_either_side),
  53951. TEST_DECL(test_wolfSSL_DTLS_either_side),
  53952. TEST_DECL(test_wolfSSL_dtls_fragments),
  53953. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  53954. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  53955. TEST_DECL(test_wolfSSL_dtls_bad_record),
  53956. TEST_DECL(test_wolfSSL_dtls_stateless),
  53957. TEST_DECL(test_generate_cookie),
  53958. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  53959. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  53960. TEST_DECL(test_wolfSSL_X509_Name_canon),
  53961. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  53962. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  53963. TEST_DECL(test_wolfSSL_X509_NID),
  53964. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  53965. TEST_DECL(test_wolfSSL_get0_param),
  53966. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  53967. TEST_DECL(test_wolfSSL_set1_host),
  53968. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  53969. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  53970. TEST_DECL(test_wolfSSL_X509_STORE),
  53971. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  53972. TEST_DECL(test_X509_STORE_get0_objects),
  53973. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  53974. TEST_DECL(test_wolfSSL_BN),
  53975. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  53976. #ifndef NO_BIO
  53977. TEST_DECL(test_wolfSSL_PEM_read_bio),
  53978. TEST_DECL(test_wolfSSL_BIO),
  53979. #endif
  53980. TEST_DECL(test_wolfSSL_ASN1_STRING),
  53981. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  53982. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  53983. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  53984. TEST_DECL(test_wolfSSL_X509),
  53985. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  53986. TEST_DECL(test_wolfSSL_X509_sign),
  53987. TEST_DECL(test_wolfSSL_X509_sign2),
  53988. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  53989. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  53990. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  53991. TEST_DECL(test_wolfSSL_check_domain),
  53992. #endif
  53993. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  53994. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  53995. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  53996. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  53997. TEST_DECL(test_wolfSSL_RAND),
  53998. TEST_DECL(test_wolfSSL_BUF),
  53999. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  54000. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  54001. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  54002. TEST_DECL(test_wolfSSL_X509_cmp_time),
  54003. TEST_DECL(test_wolfSSL_X509_time_adj),
  54004. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  54005. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  54006. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  54007. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  54008. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  54009. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  54010. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  54011. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  54012. TEST_DECL(test_wolfSSL_Tls13_postauth),
  54013. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  54014. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  54015. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  54016. TEST_DECL(test_wolfSSL_THREADID_hash),
  54017. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  54018. TEST_DECL(test_wolfSSL_RAND_bytes),
  54019. TEST_DECL(test_wolfSSL_BN_rand),
  54020. TEST_DECL(test_wolfSSL_pseudo_rand),
  54021. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  54022. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  54023. TEST_DECL(test_error_queue_per_thread),
  54024. TEST_DECL(test_wolfSSL_ERR_put_error),
  54025. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  54026. #ifndef NO_BIO
  54027. TEST_DECL(test_wolfSSL_ERR_print_errors),
  54028. #endif
  54029. TEST_DECL(test_wolfSSL_HMAC),
  54030. TEST_DECL(test_wolfSSL_CMAC),
  54031. TEST_DECL(test_wolfSSL_OBJ),
  54032. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  54033. TEST_DECL(test_wolfSSL_OBJ_cmp),
  54034. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  54035. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  54036. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  54037. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  54038. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  54039. TEST_DECL(test_wolfSSL_X509_set_name),
  54040. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  54041. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  54042. TEST_DECL(test_wolfSSL_X509_set_version),
  54043. #ifndef NO_BIO
  54044. TEST_DECL(test_wolfSSL_BIO_gets),
  54045. TEST_DECL(test_wolfSSL_BIO_puts),
  54046. TEST_DECL(test_wolfSSL_BIO_dump),
  54047. TEST_DECL(test_wolfSSL_BIO_should_retry),
  54048. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  54049. TEST_DECL(test_wolfSSL_BIO_write),
  54050. TEST_DECL(test_wolfSSL_BIO_connect),
  54051. TEST_DECL(test_wolfSSL_BIO_accept),
  54052. TEST_DECL(test_wolfSSL_BIO_printf),
  54053. TEST_DECL(test_wolfSSL_BIO_f_md),
  54054. TEST_DECL(test_wolfSSL_BIO_up_ref),
  54055. TEST_DECL(test_wolfSSL_BIO_tls),
  54056. TEST_DECL(test_wolfSSL_BIO_reset),
  54057. #endif
  54058. TEST_DECL(test_wolfSSL_cert_cb),
  54059. TEST_DECL(test_wolfSSL_SESSION),
  54060. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  54061. TEST_DECL(test_wolfSSL_ticket_keys),
  54062. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  54063. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  54064. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  54065. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  54066. TEST_DECL(test_wolfSSL_MD4),
  54067. TEST_DECL(test_wolfSSL_verify_mode),
  54068. TEST_DECL(test_wolfSSL_verify_depth),
  54069. TEST_DECL(test_wolfSSL_HMAC_CTX),
  54070. TEST_DECL(test_wolfSSL_msg_callback),
  54071. TEST_DECL(test_wolfSSL_SHA),
  54072. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  54073. TEST_DECL(test_wolfSSL_MD5),
  54074. TEST_DECL(test_wolfSSL_MD5_Transform),
  54075. TEST_DECL(test_wolfSSL_SHA_Transform),
  54076. TEST_DECL(test_wolfSSL_SHA256),
  54077. TEST_DECL(test_wolfSSL_SHA256_Transform),
  54078. TEST_DECL(test_wolfSSL_SHA224),
  54079. TEST_DECL(test_wolfSSL_SHA512_Transform),
  54080. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  54081. TEST_DECL(test_wolfSSL_X509_CRL),
  54082. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  54083. TEST_DECL(test_wolfSSL_PEM_read_X509),
  54084. TEST_DECL(test_wolfSSL_PEM_read),
  54085. #ifndef NO_BIO
  54086. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  54087. #endif
  54088. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  54089. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  54090. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  54091. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  54092. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  54093. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  54094. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  54095. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  54096. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  54097. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  54098. TEST_DECL(test_wolfSSL_X509_check_ca),
  54099. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  54100. TEST_DECL(test_wolfSSL_make_cert),
  54101. TEST_DECL(test_wolfSSL_DES_ncbc),
  54102. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  54103. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  54104. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  54105. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  54106. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  54107. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  54108. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  54109. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  54110. TEST_DECL(test_wolfssl_EVP_chacha20),
  54111. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  54112. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  54113. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  54114. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  54115. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  54116. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  54117. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  54118. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  54119. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  54120. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  54121. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  54122. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  54123. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  54124. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  54125. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  54126. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  54127. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  54128. TEST_DECL(test_CONF_modules_xxx),
  54129. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  54130. TEST_DECL(test_CRYPTO_THREADID_xxx),
  54131. TEST_DECL(test_ENGINE_cleanup),
  54132. #ifdef OPENSSL_ALL
  54133. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  54134. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  54135. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  54136. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  54137. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  54138. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  54139. TEST_DECL(test_wolfSSL_DSA_SIG),
  54140. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  54141. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  54142. TEST_DECL(test_wolfSSL_CTX_ctrl),
  54143. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  54144. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  54145. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  54146. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  54147. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  54148. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  54149. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  54150. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  54151. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  54152. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  54153. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  54154. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  54155. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  54156. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  54157. TEST_DECL(test_wolfSSL_EVP_rc4),
  54158. TEST_DECL(test_wolfSSL_EVP_enc_null),
  54159. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  54160. TEST_DECL(test_wolfSSL_EVP_mdc2),
  54161. TEST_DECL(test_wolfSSL_EVP_md4),
  54162. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  54163. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  54164. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  54165. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  54166. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  54167. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  54168. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  54169. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  54170. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  54171. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  54172. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  54173. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  54174. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  54175. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  54176. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  54177. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  54178. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  54179. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  54180. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  54181. TEST_DECL(test_evp_cipher_aes_gcm),
  54182. TEST_DECL(test_wolfSSL_OBJ_ln),
  54183. TEST_DECL(test_wolfSSL_OBJ_sn),
  54184. TEST_DECL(test_wolfSSL_TXT_DB),
  54185. TEST_DECL(test_wolfSSL_NCONF),
  54186. #endif /* OPENSSL_ALL */
  54187. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  54188. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  54189. #ifndef NO_BIO
  54190. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  54191. #endif /* !NO_BIO */
  54192. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  54193. TEST_DECL(test_wolfSSL_X509_CA_num),
  54194. TEST_DECL(test_wolfSSL_X509_get_version),
  54195. #ifndef NO_BIO
  54196. TEST_DECL(test_wolfSSL_X509_print),
  54197. TEST_DECL(test_wolfSSL_X509_CRL_print),
  54198. TEST_DECL(test_wolfSSL_BIO_get_len),
  54199. #endif
  54200. TEST_DECL(test_wolfSSL_RSA),
  54201. TEST_DECL(test_wolfSSL_RSA_DER),
  54202. TEST_DECL(test_wolfSSL_RSA_print),
  54203. #ifndef NO_RSA
  54204. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  54205. #endif
  54206. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  54207. TEST_DECL(test_wolfSSL_RSA_get0_key),
  54208. TEST_DECL(test_wolfSSL_RSA_meth),
  54209. TEST_DECL(test_wolfSSL_RSA_verify),
  54210. TEST_DECL(test_wolfSSL_RSA_sign),
  54211. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  54212. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  54213. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  54214. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  54215. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  54216. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  54217. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  54218. TEST_DECL(test_wolfSSL_RSA_ex_data),
  54219. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  54220. TEST_DECL(test_wolfSSL_RSA_To_Der),
  54221. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  54222. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  54223. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  54224. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  54225. TEST_DECL(test_wolfSSL_DH),
  54226. TEST_DECL(test_wolfSSL_DH_dup),
  54227. TEST_DECL(test_wolfSSL_DH_check),
  54228. TEST_DECL(test_wolfSSL_DH_prime),
  54229. TEST_DECL(test_wolfSSL_DH_1536_prime),
  54230. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  54231. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  54232. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  54233. TEST_DECL(test_wolfSSL_d2i_DHparams),
  54234. TEST_DECL(test_wolfSSL_DH_LoadDer),
  54235. TEST_DECL(test_wolfSSL_i2d_DHparams),
  54236. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  54237. TEST_DECL(test_wolfSSL_EC_GROUP),
  54238. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  54239. TEST_DECL(test_wolfSSL_EC_POINT),
  54240. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  54241. TEST_DECL(test_EC_i2d),
  54242. TEST_DECL(test_wolfSSL_EC_curve),
  54243. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  54244. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  54245. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  54246. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  54247. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  54248. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  54249. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  54250. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  54251. TEST_DECL(test_ECDSA_size_sign),
  54252. TEST_DECL(test_ECDH_compute_key),
  54253. #endif
  54254. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  54255. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  54256. TEST_DECL(test_wolfSSL_X509V3_EXT),
  54257. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  54258. TEST_DECL(test_wolfSSL_X509_get_ext),
  54259. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  54260. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  54261. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  54262. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  54263. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  54264. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  54265. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  54266. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  54267. TEST_DECL(test_wolfSSL_X509_cmp),
  54268. #ifndef NO_BIO
  54269. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  54270. #endif
  54271. TEST_DECL(test_wolfSSL_ASN1_get_object),
  54272. TEST_DECL(test_openssl_generate_key_and_cert),
  54273. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  54274. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  54275. /* test the no op functions for compatibility */
  54276. TEST_DECL(test_no_op_functions),
  54277. /* OpenSSL EVP_PKEY API tests */
  54278. TEST_DECL(test_EVP_PKEY_rsa),
  54279. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  54280. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  54281. TEST_DECL(test_EVP_PKEY_ec),
  54282. TEST_DECL(test_EVP_PKEY_cmp),
  54283. /* OpenSSL error API tests */
  54284. TEST_DECL(test_ERR_load_crypto_strings),
  54285. /* OpenSSL sk_X509 API test */
  54286. TEST_DECL(test_sk_X509),
  54287. /* OpenSSL sk_X509_CRL API test */
  54288. TEST_DECL(test_sk_X509_CRL),
  54289. /* OpenSSL X509 API test */
  54290. TEST_DECL(test_X509_get_signature_nid),
  54291. /* OpenSSL X509 REQ API test */
  54292. TEST_DECL(test_X509_REQ),
  54293. /* OpenSSL PKCS7 API test */
  54294. TEST_DECL(test_wolfssl_PKCS7),
  54295. TEST_DECL(test_wolfSSL_PKCS7_sign),
  54296. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  54297. #ifndef NO_BIO
  54298. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  54299. #ifdef HAVE_SMIME
  54300. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  54301. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  54302. #endif /* HAVE_SMIME */
  54303. #endif /* !NO_BIO */
  54304. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  54305. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  54306. TEST_DECL(test_X509_LOOKUP_add_dir),
  54307. /* wolfCrypt ASN tests */
  54308. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  54309. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  54310. TEST_DECL(test_wc_SetSubjectRaw),
  54311. TEST_DECL(test_wc_GetSubjectRaw),
  54312. TEST_DECL(test_wc_SetIssuerRaw),
  54313. TEST_DECL(test_wc_SetIssueBuffer),
  54314. TEST_DECL(test_wc_SetSubjectKeyId),
  54315. TEST_DECL(test_wc_SetSubject),
  54316. TEST_DECL(test_CheckCertSignature),
  54317. TEST_DECL(test_wc_ParseCert),
  54318. TEST_DECL(test_wc_ParseCert_Error),
  54319. TEST_DECL(test_MakeCertWithPathLen),
  54320. /* wolfCrypt ECC tests */
  54321. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  54322. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  54323. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  54324. #ifdef WOLFSSL_TLS13
  54325. /* TLS v1.3 API tests */
  54326. TEST_DECL(test_tls13_apis),
  54327. TEST_DECL(test_tls13_cipher_suites),
  54328. #endif
  54329. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  54330. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  54331. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  54332. /* Bad certificate signature tests */
  54333. TEST_DECL(test_EccSigFailure_cm),
  54334. TEST_DECL(test_RsaSigFailure_cm),
  54335. #endif /* NO_CERTS */
  54336. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  54337. !defined(NO_OLD_TLS))
  54338. TEST_DECL(test_DhCallbacks),
  54339. #endif
  54340. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  54341. TEST_DECL(test_export_keying_material),
  54342. #endif
  54343. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  54344. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54345. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54346. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54347. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  54348. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  54349. #endif
  54350. TEST_DECL(test_wolfSSL_security_level),
  54351. TEST_DECL(test_wolfSSL_SSL_in_init),
  54352. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  54353. TEST_DECL(test_wolfSSL_OpenSSL_version),
  54354. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  54355. TEST_DECL(test_ticket_and_psk_mixing),
  54356. TEST_DECL(test_prioritize_psk),
  54357. TEST_DECL(test_CONF_CTX_FILE),
  54358. TEST_DECL(test_CONF_CTX_CMDLINE),
  54359. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  54360. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  54361. /* wolfcrypt */
  54362. TEST_DECL(test_wolfCrypt_Init),
  54363. TEST_DECL(test_wc_InitMd5),
  54364. TEST_DECL(test_wc_Md5Update),
  54365. TEST_DECL(test_wc_Md5Final),
  54366. TEST_DECL(test_wc_InitSha),
  54367. TEST_DECL(test_wc_ShaUpdate),
  54368. TEST_DECL(test_wc_ShaFinal),
  54369. TEST_DECL(test_wc_InitSha256),
  54370. TEST_DECL(test_wc_Sha256Update),
  54371. TEST_DECL(test_wc_Sha256Final),
  54372. TEST_DECL(test_wc_Sha256FinalRaw),
  54373. TEST_DECL(test_wc_Sha256GetFlags),
  54374. TEST_DECL(test_wc_Sha256Free),
  54375. TEST_DECL(test_wc_Sha256GetHash),
  54376. TEST_DECL(test_wc_Sha256Copy),
  54377. TEST_DECL(test_wc_InitSha512),
  54378. TEST_DECL(test_wc_Sha512Update),
  54379. TEST_DECL(test_wc_Sha512Final),
  54380. TEST_DECL(test_wc_Sha512GetFlags),
  54381. TEST_DECL(test_wc_Sha512FinalRaw),
  54382. TEST_DECL(test_wc_Sha512Free),
  54383. TEST_DECL(test_wc_Sha512GetHash),
  54384. TEST_DECL(test_wc_Sha512Copy),
  54385. TEST_DECL(test_wc_InitSha512_224),
  54386. TEST_DECL(test_wc_Sha512_224Update),
  54387. TEST_DECL(test_wc_Sha512_224Final),
  54388. TEST_DECL(test_wc_Sha512_224GetFlags),
  54389. TEST_DECL(test_wc_Sha512_224FinalRaw),
  54390. TEST_DECL(test_wc_Sha512_224Free),
  54391. TEST_DECL(test_wc_Sha512_224GetHash),
  54392. TEST_DECL(test_wc_Sha512_224Copy),
  54393. TEST_DECL(test_wc_InitSha512_256),
  54394. TEST_DECL(test_wc_Sha512_256Update),
  54395. TEST_DECL(test_wc_Sha512_256Final),
  54396. TEST_DECL(test_wc_Sha512_256GetFlags),
  54397. TEST_DECL(test_wc_Sha512_256FinalRaw),
  54398. TEST_DECL(test_wc_Sha512_256Free),
  54399. TEST_DECL(test_wc_Sha512_256GetHash),
  54400. TEST_DECL(test_wc_Sha512_256Copy),
  54401. TEST_DECL(test_wc_InitSha384),
  54402. TEST_DECL(test_wc_Sha384Update),
  54403. TEST_DECL(test_wc_Sha384Final),
  54404. TEST_DECL(test_wc_Sha384GetFlags),
  54405. TEST_DECL(test_wc_Sha384FinalRaw),
  54406. TEST_DECL(test_wc_Sha384Free),
  54407. TEST_DECL(test_wc_Sha384GetHash),
  54408. TEST_DECL(test_wc_Sha384Copy),
  54409. TEST_DECL(test_wc_InitSha224),
  54410. TEST_DECL(test_wc_Sha224Update),
  54411. TEST_DECL(test_wc_Sha224Final),
  54412. TEST_DECL(test_wc_Sha224SetFlags),
  54413. TEST_DECL(test_wc_Sha224GetFlags),
  54414. TEST_DECL(test_wc_Sha224Free),
  54415. TEST_DECL(test_wc_Sha224GetHash),
  54416. TEST_DECL(test_wc_Sha224Copy),
  54417. TEST_DECL(test_wc_InitBlake2b),
  54418. TEST_DECL(test_wc_InitBlake2b_WithKey),
  54419. TEST_DECL(test_wc_InitBlake2s_WithKey),
  54420. TEST_DECL(test_wc_InitRipeMd),
  54421. TEST_DECL(test_wc_RipeMdUpdate),
  54422. TEST_DECL(test_wc_RipeMdFinal),
  54423. TEST_DECL(test_wc_InitSha3),
  54424. TEST_DECL(testing_wc_Sha3_Update),
  54425. TEST_DECL(test_wc_Sha3_224_Final),
  54426. TEST_DECL(test_wc_Sha3_256_Final),
  54427. TEST_DECL(test_wc_Sha3_384_Final),
  54428. TEST_DECL(test_wc_Sha3_512_Final),
  54429. TEST_DECL(test_wc_Sha3_224_Copy),
  54430. TEST_DECL(test_wc_Sha3_256_Copy),
  54431. TEST_DECL(test_wc_Sha3_384_Copy),
  54432. TEST_DECL(test_wc_Sha3_512_Copy),
  54433. TEST_DECL(test_wc_Sha3_GetFlags),
  54434. TEST_DECL(test_wc_InitShake256),
  54435. TEST_DECL(testing_wc_Shake256_Update),
  54436. TEST_DECL(test_wc_Shake256_Final),
  54437. TEST_DECL(test_wc_Shake256_Copy),
  54438. TEST_DECL(test_wc_Shake256Hash),
  54439. TEST_DECL(test_wc_Md5HmacSetKey),
  54440. TEST_DECL(test_wc_Md5HmacUpdate),
  54441. TEST_DECL(test_wc_Md5HmacFinal),
  54442. TEST_DECL(test_wc_ShaHmacSetKey),
  54443. TEST_DECL(test_wc_ShaHmacUpdate),
  54444. TEST_DECL(test_wc_ShaHmacFinal),
  54445. TEST_DECL(test_wc_Sha224HmacSetKey),
  54446. TEST_DECL(test_wc_Sha224HmacUpdate),
  54447. TEST_DECL(test_wc_Sha224HmacFinal),
  54448. TEST_DECL(test_wc_Sha256HmacSetKey),
  54449. TEST_DECL(test_wc_Sha256HmacUpdate),
  54450. TEST_DECL(test_wc_Sha256HmacFinal),
  54451. TEST_DECL(test_wc_Sha384HmacSetKey),
  54452. TEST_DECL(test_wc_Sha384HmacUpdate),
  54453. TEST_DECL(test_wc_Sha384HmacFinal),
  54454. TEST_DECL(test_wc_HashInit),
  54455. TEST_DECL(test_wc_HashSetFlags),
  54456. TEST_DECL(test_wc_HashGetFlags),
  54457. TEST_DECL(test_wc_InitCmac),
  54458. TEST_DECL(test_wc_CmacUpdate),
  54459. TEST_DECL(test_wc_CmacFinal),
  54460. TEST_DECL(test_wc_AesCmacGenerate),
  54461. TEST_DECL(test_wc_AesGcmStream),
  54462. TEST_DECL(test_wc_Des3_SetIV),
  54463. TEST_DECL(test_wc_Des3_SetKey),
  54464. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  54465. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  54466. TEST_DECL(test_wc_Des3_EcbEncrypt),
  54467. TEST_DECL(test_wc_Chacha_SetKey),
  54468. TEST_DECL(test_wc_Chacha_Process),
  54469. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  54470. TEST_DECL(test_wc_Poly1305SetKey),
  54471. TEST_DECL(test_wc_CamelliaSetKey),
  54472. TEST_DECL(test_wc_CamelliaSetIV),
  54473. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  54474. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  54475. TEST_DECL(test_wc_Arc4SetKey),
  54476. TEST_DECL(test_wc_Arc4Process),
  54477. TEST_DECL(test_wc_Rc2SetKey),
  54478. TEST_DECL(test_wc_Rc2SetIV),
  54479. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  54480. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  54481. TEST_DECL(test_wc_AesSetKey),
  54482. TEST_DECL(test_wc_AesSetIV),
  54483. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  54484. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  54485. TEST_DECL(test_wc_AesGcmSetKey),
  54486. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  54487. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  54488. TEST_DECL(test_wc_GmacSetKey),
  54489. TEST_DECL(test_wc_GmacUpdate),
  54490. TEST_DECL(test_wc_InitRsaKey),
  54491. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  54492. TEST_DECL(test_wc_RsaPublicKeyDecode),
  54493. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  54494. TEST_DECL(test_wc_MakeRsaKey),
  54495. TEST_DECL(test_wc_SetKeyUsage),
  54496. TEST_DECL(test_wc_CheckProbablePrime),
  54497. TEST_DECL(test_wc_RsaPSS_Verify),
  54498. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  54499. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  54500. TEST_DECL(test_wc_SetMutexCb),
  54501. TEST_DECL(test_wc_LockMutex_ex),
  54502. TEST_DECL(test_wc_RsaKeyToDer),
  54503. TEST_DECL(test_wc_RsaKeyToPublicDer),
  54504. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  54505. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  54506. TEST_DECL(test_wc_RsaEncryptSize),
  54507. TEST_DECL(test_wc_RsaSSL_SignVerify),
  54508. TEST_DECL(test_wc_RsaFlattenPublicKey),
  54509. TEST_DECL(test_RsaDecryptBoundsCheck),
  54510. TEST_DECL(test_wc_AesCcmSetKey),
  54511. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  54512. TEST_DECL(test_wc_InitDsaKey),
  54513. TEST_DECL(test_wc_DsaSignVerify),
  54514. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  54515. TEST_DECL(test_wc_MakeDsaKey),
  54516. TEST_DECL(test_wc_DsaKeyToDer),
  54517. TEST_DECL(test_wc_DsaKeyToPublicDer),
  54518. TEST_DECL(test_wc_DsaImportParamsRaw),
  54519. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  54520. TEST_DECL(test_wc_DsaExportParamsRaw),
  54521. TEST_DECL(test_wc_DsaExportKeyRaw),
  54522. TEST_DECL(test_wc_SignatureGetSize_ecc),
  54523. TEST_DECL(test_wc_SignatureGetSize_rsa),
  54524. /*
  54525. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  54526. * avoid memory leaks.
  54527. */
  54528. TEST_DECL(test_wolfCrypt_Cleanup),
  54529. #ifdef OPENSSL_EXTRA
  54530. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  54531. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  54532. TEST_DECL(test_ED25519),
  54533. TEST_DECL(test_ED448),
  54534. #endif
  54535. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  54536. !defined(HAVE_SELFTEST) && \
  54537. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  54538. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  54539. #endif
  54540. #ifdef HAVE_HASHDRBG
  54541. #ifdef TEST_RESEED_INTERVAL
  54542. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  54543. #endif
  54544. TEST_DECL(test_wc_RNG_GenerateBlock),
  54545. #endif
  54546. TEST_DECL(test_get_rand_digit),
  54547. TEST_DECL(test_get_digit_count),
  54548. TEST_DECL(test_mp_cond_copy),
  54549. TEST_DECL(test_mp_rand),
  54550. TEST_DECL(test_get_digit),
  54551. TEST_DECL(test_wc_export_int),
  54552. TEST_DECL(test_wc_InitRngNonce),
  54553. TEST_DECL(test_wc_InitRngNonce_ex),
  54554. TEST_DECL(test_wc_ed25519_make_key),
  54555. TEST_DECL(test_wc_ed25519_init),
  54556. TEST_DECL(test_wc_ed25519_sign_msg),
  54557. TEST_DECL(test_wc_ed25519_import_public),
  54558. TEST_DECL(test_wc_ed25519_import_private_key),
  54559. TEST_DECL(test_wc_ed25519_export),
  54560. TEST_DECL(test_wc_ed25519_size),
  54561. TEST_DECL(test_wc_ed25519_exportKey),
  54562. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  54563. TEST_DECL(test_wc_curve25519_init),
  54564. TEST_DECL(test_wc_curve25519_size),
  54565. TEST_DECL(test_wc_curve25519_export_key_raw),
  54566. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  54567. TEST_DECL(test_wc_curve25519_make_key),
  54568. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  54569. TEST_DECL(test_wc_curve25519_make_pub),
  54570. TEST_DECL(test_wc_curve25519_export_public_ex),
  54571. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  54572. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  54573. TEST_DECL(test_wc_curve25519_import_private),
  54574. TEST_DECL(test_wc_ed448_make_key),
  54575. TEST_DECL(test_wc_ed448_init),
  54576. TEST_DECL(test_wc_ed448_sign_msg),
  54577. TEST_DECL(test_wc_ed448_import_public),
  54578. TEST_DECL(test_wc_ed448_import_private_key),
  54579. TEST_DECL(test_wc_ed448_export),
  54580. TEST_DECL(test_wc_ed448_size),
  54581. TEST_DECL(test_wc_ed448_exportKey),
  54582. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  54583. TEST_DECL(test_wc_curve448_make_key),
  54584. TEST_DECL(test_wc_curve448_shared_secret_ex),
  54585. TEST_DECL(test_wc_curve448_export_public_ex),
  54586. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  54587. TEST_DECL(test_wc_curve448_export_key_raw),
  54588. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  54589. TEST_DECL(test_wc_curve448_import_private),
  54590. TEST_DECL(test_wc_curve448_init),
  54591. TEST_DECL(test_wc_curve448_size),
  54592. TEST_DECL(test_wc_ecc_make_key),
  54593. TEST_DECL(test_wc_ecc_init),
  54594. TEST_DECL(test_wc_ecc_check_key),
  54595. TEST_DECL(test_wc_ecc_get_generator),
  54596. TEST_DECL(test_wc_ecc_size),
  54597. TEST_DECL(test_wc_ecc_params),
  54598. TEST_DECL(test_wc_ecc_signVerify_hash),
  54599. TEST_DECL(test_wc_ecc_shared_secret),
  54600. TEST_DECL(test_wc_ecc_export_x963),
  54601. TEST_DECL(test_wc_ecc_export_x963_ex),
  54602. TEST_DECL(test_wc_ecc_import_x963),
  54603. TEST_DECL(ecc_import_private_key),
  54604. TEST_DECL(test_wc_ecc_export_private_only),
  54605. TEST_DECL(test_wc_ecc_rs_to_sig),
  54606. TEST_DECL(test_wc_ecc_import_raw),
  54607. TEST_DECL(test_wc_ecc_import_unsigned),
  54608. TEST_DECL(test_wc_ecc_sig_size),
  54609. TEST_DECL(test_wc_ecc_ctx_new),
  54610. TEST_DECL(test_wc_ecc_ctx_reset),
  54611. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  54612. TEST_DECL(test_wc_ecc_ctx_set_info),
  54613. TEST_DECL(test_wc_ecc_encryptDecrypt),
  54614. TEST_DECL(test_wc_ecc_del_point),
  54615. TEST_DECL(test_wc_ecc_pointFns),
  54616. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  54617. TEST_DECL(test_wc_ecc_verify_hash_ex),
  54618. TEST_DECL(test_wc_ecc_mulmod),
  54619. TEST_DECL(test_wc_ecc_is_valid_idx),
  54620. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  54621. TEST_DECL(test_wc_ecc_sig_size_calc),
  54622. TEST_DECL(test_ToTraditional),
  54623. TEST_DECL(test_wc_EccPrivateKeyToDer),
  54624. TEST_DECL(test_wc_DhPublicKeyDecode),
  54625. TEST_DECL(test_wc_Ed25519KeyToDer),
  54626. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  54627. TEST_DECL(test_wc_Ed448KeyToDer),
  54628. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  54629. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  54630. TEST_DECL(test_wc_SetSubjectBuffer),
  54631. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  54632. TEST_DECL(test_wc_PKCS7_New),
  54633. TEST_DECL(test_wc_PKCS7_Init),
  54634. TEST_DECL(test_wc_PKCS7_InitWithCert),
  54635. TEST_DECL(test_wc_PKCS7_EncodeData),
  54636. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  54637. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  54638. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  54639. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  54640. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  54641. TEST_DECL(test_wc_PKCS7_Degenerate),
  54642. TEST_DECL(test_wc_PKCS7_BER),
  54643. TEST_DECL(test_PKCS7_signed_enveloped),
  54644. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  54645. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  54646. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  54647. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  54648. TEST_DECL(test_wc_i2d_PKCS12),
  54649. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  54650. TEST_DECL(test_openssl_FIPS_drbg),
  54651. TEST_DECL(test_wc_CryptoCb),
  54652. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  54653. TEST_DECL(test_wolfSSL_FIPS_mode),
  54654. #ifdef WOLFSSL_DTLS
  54655. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  54656. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  54657. #endif
  54658. #if !defined(NO_FILESYSTEM) && \
  54659. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  54660. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  54661. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  54662. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  54663. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  54664. #ifdef HAVE_MAX_FRAGMENT
  54665. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  54666. #endif /* HAVE_MAX_FRAGMENT */
  54667. TEST_DECL(test_wolfSSL_dtls_stateless2),
  54668. #if !defined(NO_OLD_TLS)
  54669. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  54670. #endif /* !defined(NO_OLD_TLS) */
  54671. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  54672. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  54673. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  54674. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  54675. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  54676. TEST_DECL(test_ForceZero),
  54677. TEST_DECL(test_wolfSSL_Cleanup),
  54678. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  54679. && defined(WOLFSSL_TLS13) && \
  54680. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  54681. TEST_DECL(test_ticket_nonce_malloc),
  54682. #endif
  54683. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  54684. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  54685. !defined(WOLFSSL_NO_CLIENT_AUTH))
  54686. TEST_DECL(test_various_pathlen_chains),
  54687. #endif
  54688. TEST_DECL(test_ticket_ret_create),
  54689. /* If at some point a stub get implemented this test should fail indicating
  54690. * a need to implement a new test case
  54691. */
  54692. TEST_DECL(test_stubs_are_stubs)
  54693. };
  54694. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  54695. static void TestSetup(void)
  54696. {
  54697. /* Stub, for now. Add common test setup code here. */
  54698. }
  54699. static void TestCleanup(void)
  54700. {
  54701. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  54702. /* Clear any errors added to the error queue during the test run. */
  54703. wolfSSL_ERR_clear_error();
  54704. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  54705. }
  54706. /* Print out all API test cases with numeric identifier.
  54707. */
  54708. void ApiTest_PrintTestCases(void)
  54709. {
  54710. int i;
  54711. printf("All Test Cases:\n");
  54712. for (i = 0; i < TEST_CASE_CNT; i++) {
  54713. printf("%3d: %s\n", i + 1, testCases[i].name);
  54714. }
  54715. }
  54716. /* Add test case with index to the list to run.
  54717. *
  54718. * @param [in] idx Index of test case to run starting at 1.
  54719. * @return 0 on success.
  54720. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  54721. */
  54722. int ApiTest_RunIdx(int idx)
  54723. {
  54724. if (idx < 1 || idx > TEST_CASE_CNT) {
  54725. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  54726. return BAD_FUNC_ARG;
  54727. }
  54728. testAll = 0;
  54729. testCases[idx-1].run = 1;
  54730. return 0;
  54731. }
  54732. /* Add test case with name to the list to run.
  54733. *
  54734. * @param [in] name Name of test case to run.
  54735. * @return 0 on success.
  54736. * @return BAD_FUNC_ARG when name is not a known test case name.
  54737. */
  54738. int ApiTest_RunName(char* name)
  54739. {
  54740. int i;
  54741. for (i = 0; i < TEST_CASE_CNT; i++) {
  54742. if (XSTRCMP(testCases[i].name, name) == 0) {
  54743. testAll = 0;
  54744. testCases[i].run = 1;
  54745. return 0;
  54746. }
  54747. }
  54748. printf("Test case name not found: %s\n", name);
  54749. printf("Use --list to see all test case names.\n");
  54750. return BAD_FUNC_ARG;
  54751. }
  54752. /* Converts the result code to a string.
  54753. *
  54754. * @param [in] res Test result code.
  54755. * @return String describing test result.
  54756. */
  54757. static const char* apitest_res_string(int res)
  54758. {
  54759. const char* str = "invalid result";
  54760. switch (res) {
  54761. case TEST_SUCCESS:
  54762. str = "passed";
  54763. break;
  54764. case TEST_FAIL:
  54765. str = "failed";
  54766. break;
  54767. case TEST_SKIPPED:
  54768. str = "skipped";
  54769. break;
  54770. }
  54771. return str;
  54772. }
  54773. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  54774. static double gettime_secs(void)
  54775. {
  54776. struct timeval tv;
  54777. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  54778. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  54779. }
  54780. #endif
  54781. void ApiTest(void)
  54782. {
  54783. int i;
  54784. int ret;
  54785. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  54786. double timeDiff;
  54787. #endif
  54788. printf(" Begin API Tests\n");
  54789. fflush(stdout);
  54790. /* we must perform init and cleanup if not all tests are running */
  54791. if (!testAll) {
  54792. #ifdef WOLFCRYPT_ONLY
  54793. wolfCrypt_Init();
  54794. #else
  54795. wolfSSL_Init();
  54796. #endif
  54797. }
  54798. for (i = 0; i < TEST_CASE_CNT; ++i) {
  54799. /* When not testing all cases then skip if not marked for running. */
  54800. if (!testAll && !testCases[i].run) {
  54801. continue;
  54802. }
  54803. TestSetup();
  54804. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  54805. fflush(stdout);
  54806. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  54807. timeDiff = gettime_secs();
  54808. #endif
  54809. ret = testCases[i].func();
  54810. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  54811. timeDiff = gettime_secs() - timeDiff;
  54812. #endif
  54813. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  54814. if (ret != TEST_SKIPPED) {
  54815. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  54816. }
  54817. else
  54818. #endif
  54819. {
  54820. printf(" %s\n", apitest_res_string(ret));
  54821. }
  54822. fflush(stdout);
  54823. /* if return code is < 0 and not skipped then assert error */
  54824. Assert((ret > 0 || ret == TEST_SKIPPED),
  54825. ("Test failed\n"),
  54826. ("ret %d", ret));
  54827. TestCleanup();
  54828. }
  54829. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  54830. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  54831. wc_ecc_fp_free(); /* free per thread cache */
  54832. #endif
  54833. if (!testAll) {
  54834. #ifdef WOLFCRYPT_ONLY
  54835. wolfCrypt_Cleanup();
  54836. #else
  54837. wolfSSL_Cleanup();
  54838. #endif
  54839. }
  54840. (void)testDevId;
  54841. printf(" End API Tests\n");
  54842. fflush(stdout);
  54843. }